CGVIS Utils
Loading...
Searching...
No Matches
Grid2D.h
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <string>
5#include <vector>
6
7#include "Vec2.h"
8#include "Vec3.h"
9#include "Image.h"
10#include "GLTexture2D.h"
11
27class Grid2D {
28public:
34 Grid2D(size_t width, size_t height);
35
43 Grid2D(size_t width, size_t height, const std::vector<float> data);
44
49 Grid2D(const Grid2D& other);
50
56 Grid2D(const Image& image);
57
62 Grid2D(std::istream &is);
63
68 void save(std::ostream &os) const;
69
72
73 size_t getWidth() const;
75 size_t getHeight() const;
77
82 std::string toString() const;
83
88 std::vector<uint8_t> toByteArray() const;
89
96 Grid2D toSignedDistance(float threshold) const;
97
102 GLTexture2D toTexture() const;
103
110 void setValue(size_t x, size_t y, float value);
111
118 float getValueNormalized(float x, float y) const;
119
126 float getValue(size_t x, size_t y) const;
127
134 float sample(float x, float y) const ;
135
140 float sample(const Vec2& pos) const ;
141
147 Vec3 normal(float x, float y) const;
148
153 Vec3 normal(const Vec2& pos) const;
154
161 static Grid2D genRandom(size_t x, size_t y, uint32_t seed);
162
168 static Grid2D genRandom(size_t x, size_t y);
169
170 // ===== Scalar arithmetic =====
172 Grid2D operator*(const float& value) const;
174 Grid2D operator/(const float& value) const;
176 Grid2D operator+(const float& value) const;
178 Grid2D operator-(const float& value) const;
179
180 // ===== Grid‑wise arithmetic (size reconciliation by resampling) =====
182 Grid2D operator+(const Grid2D& other) const;
184 Grid2D operator/(const Grid2D& other) const;
186 Grid2D operator*(const Grid2D& other) const;
188 Grid2D operator-(const Grid2D& other) const;
189
194 void normalize(const float maxVal = 1);
195
200 Vec2t<size_t> maxValue() const;
201
206 Vec2t<size_t> minValue() const;
207
212 void fill(float value);
213
217 friend std::ostream& operator<<(std::ostream &os, const Grid2D& v);
218
224 static Grid2D fromBMP(const std::string& filename);
225
226private:
227 size_t width;
228 size_t height;
229 std::vector<float> data{};
230
237 size_t index(size_t x, size_t y) const;
238
244 std::pair<size_t,size_t> findMaxSize(const Grid2D& other) const;
245};
RAII wrapper for an OpenGL 2D texture with byte/half/float upload helpers.
Lightweight image container with basic processing utilities.
2D vector template with arithmetic, length utilities, and helpers.
Vec2t< float > Vec2
Float vector (x,y).
Definition Vec2.h:258
3D vector template with arithmetic, geometry helpers, and RNG utilities.
Vec3t< float > Vec3
3D float vector (x,y,z).
Definition Vec3.h:424
Definition GLTexture2D.h:19
Grid2D operator/(const float &value) const
Per‑element divide by scalar.
Definition Grid2D.cpp:183
Grid2D operator-(const float &value) const
Per‑element subtract scalar.
Definition Grid2D.cpp:175
void normalize(const float maxVal=1)
Normalize values to [0,maxVal] by affine remapping.
Definition Grid2D.cpp:384
std::string toString() const
String dump of values in row‑major order.
Definition Grid2D.cpp:52
static Grid2D fromBMP(const std::string &filename)
Create a grid from the luminance of a BMP file.
Definition Grid2D.cpp:432
void fill(float value)
Fill all elements with a constant.
Definition Grid2D.cpp:448
friend std::ostream & operator<<(std::ostream &os, const Grid2D &v)
Stream insertion (delegates to toString()).
Definition Grid2D.cpp:428
Grid2D operator+(const float &value) const
Per‑element add scalar.
Definition Grid2D.cpp:167
Grid2D toSignedDistance(float threshold) const
Compute a signed distance field from a binary threshold.
Definition Grid2D.cpp:479
Vec2t< size_t > maxValue() const
Location of the maximum value.
Definition Grid2D.cpp:400
Vec3 normal(float x, float y) const
Estimate a normal from local height differences at normalized coords.
Definition Grid2D.cpp:119
float getValue(size_t x, size_t y) const
Fetch value by integer coordinates.
Definition Grid2D.cpp:83
Vec2t< size_t > minValue() const
Location of the minimum value.
Definition Grid2D.cpp:412
GLTexture2D toTexture() const
Upload to an OpenGL texture (1 channel).
Definition Grid2D.cpp:558
Grid2D operator*(const float &value) const
Per‑element multiply by scalar.
Definition Grid2D.cpp:159
Grid2D(size_t width, size_t height)
Construct an empty grid initialized to zeros.
Definition Grid2D.cpp:11
size_t getWidth() const
Grid width in elements.
Definition Grid2D.cpp:44
static Grid2D genRandom(size_t x, size_t y, uint32_t seed)
Generate a grid of random values in [0,1) using a fixed seed.
Definition Grid2D.cpp:142
std::vector< uint8_t > toByteArray() const
Convert to an 8‑bit RGB byte array (grayscale replicated to RGB).
Definition Grid2D.cpp:64
void setValue(size_t x, size_t y, float value)
Set one value.
Definition Grid2D.cpp:74
float getValueNormalized(float x, float y) const
Fetch value using normalized [0,1] coordinates (nearest‑neighbor via index mapping).
Definition Grid2D.cpp:79
float sample(float x, float y) const
Bilinear sample at normalized coordinates in [0,1].
Definition Grid2D.cpp:92
size_t getHeight() const
Grid height in elements.
Definition Grid2D.cpp:48
void save(std::ostream &os) const
Write grid to a binary stream.
Definition Grid2D.cpp:461
Definition Image.h:22
Definition Vec2.h:27