HighMap library (C++)
Loading...
Searching...
No Matches
algebra.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
14#pragma once
15#include <cmath>
16#include <stdexcept>
17#include <vector>
18
19#include <glm/glm.hpp>
20
21namespace hmap
22{
23
24// --- Backward compatiblity with former custom classes (deprecated)
25
26template <typename T> struct Vec2; // forward decl only
27
28template <>
29struct [[deprecated("Replaced by glm::vec2")]] Vec2<float> : public glm::vec2
30{
31 using glm::vec2::vec2;
32 using glm::vec2::x;
33 using glm::vec2::y;
34};
35
36template <>
37struct [[deprecated("Replaced by glm::ivec2")]] Vec2<int> : public glm::ivec2
38{
39 using glm::ivec2::ivec2;
40 using glm::ivec2::x;
41 using glm::ivec2::y;
42};
43
44template <typename T> struct Vec3; // forward decl only
45
46template <>
47struct [[deprecated("Replaced by glm::vec3")]] Vec3<float> : public glm::vec3
48{
49 using glm::vec3::vec3;
50 using glm::vec3::x;
51 using glm::vec3::y;
52 using glm::vec3::z;
53};
54
55template <>
56struct [[deprecated("Replaced by glm::ivec3")]] Vec3<int> : public glm::ivec3
57{
58 using glm::ivec3::ivec3;
59 using glm::ivec3::x;
60 using glm::ivec3::y;
61 using glm::ivec3::z;
62};
63
64template <typename T> struct Vec4; // forward decl only
65
66template <>
67struct [[deprecated("Replaced by glm::vec4")]] Vec4<float> : public glm::vec4
68{
69 using glm::vec4::vec4;
70
71 // expose glm names
72 using glm::vec4::w;
73 using glm::vec4::x;
74 using glm::vec4::y;
75 using glm::vec4::z;
76
77 // retro-compat aliases
78 float &a = x;
79 float &b = y;
80 float &c = z;
81 float &d = w;
82
83 const float &a_const = x;
84 const float &b_const = y;
85 const float &c_const = z;
86 const float &d_const = w;
87};
88
89template <>
90struct [[deprecated("Replaced by glm::ivec4")]] Vec4<int> : public glm::ivec4
91{
92 using glm::ivec4::ivec4;
93
94 // expose glm names
95 using glm::ivec4::w;
96 using glm::ivec4::x;
97 using glm::ivec4::y;
98 using glm::ivec4::z;
99
100 // retro-compat aliases
101 int &a = x;
102 int &b = y;
103 int &c = z;
104 int &d = w;
105
106 const int &a_const = x;
107 const int &b_const = y;
108 const int &c_const = z;
109 const int &d_const = w;
110};
111
112// --- For unordered_map
113
114// for glm::ivec4 map
116{
117 std::size_t operator()(const glm::ivec4 &v) const noexcept
118 {
119 std::size_t h = 0;
120 h ^= std::hash<int>{}(v.x) + 0x9e3779b9 + (h << 6) + (h >> 2);
121 h ^= std::hash<int>{}(v.y) + 0x9e3779b9 + (h << 6) + (h >> 2);
122 h ^= std::hash<int>{}(v.z) + 0x9e3779b9 + (h << 6) + (h >> 2);
123 h ^= std::hash<int>{}(v.w) + 0x9e3779b9 + (h << 6) + (h >> 2);
124 return h;
125 }
126};
127
129{
130 bool operator()(const glm::ivec4 &a, const glm::ivec4 &b) const noexcept
131 {
132 return a == b;
133 }
134};
135
136inline glm::vec4 adjust(const glm::vec4 &v,
137 float dx,
138 float dy,
139 float dz,
140 float dw)
141{
142 return glm::vec4{v.x + dx, v.y + dy, v.z + dz, v.w + dw};
143}
144
154template <typename T> struct Mat
155{
156 std::vector<T> vector;
158 glm::ivec2 shape;
171 Mat(glm::ivec2 shape) : shape(shape)
172 {
173 this->vector.resize(shape.x * shape.y);
174 }
175
187 Mat(glm::ivec2 shape, T value) : shape(shape)
188 {
189 this->vector.resize(shape.x * shape.y);
190 std::fill(this->vector.begin(), this->vector.end(), value);
191 }
192
203 T &operator()(int i, int j)
204 {
205 return this->vector[j * this->shape.x + i];
206 }
207
219 const T &operator()(int i, int j) const
220 {
221 return this->vector[j * this->shape.x + i];
222 }
223
224 T &operator()(glm::ivec2 ij)
225 {
226 return this->vector[ij.y * this->shape.x + ij.x];
227 }
228
229 const T &operator()(glm::ivec2 ij) const
230 {
231 return this->vector[ij.y * this->shape.x + ij.x];
232 }
233};
234
235} // namespace hmap
Definition algebra.hpp:22
glm::vec4 adjust(const glm::vec4 &v, float dx, float dy, float dz, float dw)
Definition algebra.hpp:136
Definition algebra.hpp:129
bool operator()(const glm::ivec4 &a, const glm::ivec4 &b) const noexcept
Definition algebra.hpp:130
Definition algebra.hpp:116
std::size_t operator()(const glm::ivec4 &v) const noexcept
Definition algebra.hpp:117
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:155
glm::ivec2 shape
Dimensions of the matrix (rows x columns).
Definition algebra.hpp:158
T & operator()(int i, int j)
Access operator to get a reference to the element at (i, j).
Definition algebra.hpp:203
std::vector< T > vector
1D vector storing matrix elements in row-major order.
Definition algebra.hpp:156
const T & operator()(int i, int j) const
Const access operator to get the value of the element at (i, j).
Definition algebra.hpp:219
T & operator()(glm::ivec2 ij)
Definition algebra.hpp:224
Mat(glm::ivec2 shape)
Constructor to initialize a matrix with a given shape.
Definition algebra.hpp:171
const T & operator()(glm::ivec2 ij) const
Definition algebra.hpp:229
Mat(glm::ivec2 shape, T value)
Constructor to initialize a matrix with a given shape and value.
Definition algebra.hpp:187
Definition algebra.hpp:26
Definition algebra.hpp:44
Definition algebra.hpp:64