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 <string>
18#include <vector>
19
20#include <glm/glm.hpp>
21
22namespace hmap
23{
24
25void to_csv(const std::vector<glm::vec3> &xyz, const std::string &fname);
26
27// --- Backward compatiblity with former custom classes (deprecated)
28
29template <typename T> struct Vec2; // forward decl only
30
31template <>
32struct [[deprecated("Replaced by glm::vec2")]] Vec2<float> : public glm::vec2
33{
34 using glm::vec2::vec2;
35 using glm::vec2::x;
36 using glm::vec2::y;
37};
38
39template <>
40struct [[deprecated("Replaced by glm::ivec2")]] Vec2<int> : public glm::ivec2
41{
42 using glm::ivec2::ivec2;
43 using glm::ivec2::x;
44 using glm::ivec2::y;
45};
46
47template <typename T> struct Vec3; // forward decl only
48
49template <>
50struct [[deprecated("Replaced by glm::vec3")]] Vec3<float> : public glm::vec3
51{
52 using glm::vec3::vec3;
53 using glm::vec3::x;
54 using glm::vec3::y;
55 using glm::vec3::z;
56};
57
58template <>
59struct [[deprecated("Replaced by glm::ivec3")]] Vec3<int> : public glm::ivec3
60{
61 using glm::ivec3::ivec3;
62 using glm::ivec3::x;
63 using glm::ivec3::y;
64 using glm::ivec3::z;
65};
66
67template <typename T> struct Vec4; // forward decl only
68
69template <>
70struct [[deprecated("Replaced by glm::vec4")]] Vec4<float> : public glm::vec4
71{
72 using glm::vec4::vec4;
73
74 // expose glm names
75 using glm::vec4::w;
76 using glm::vec4::x;
77 using glm::vec4::y;
78 using glm::vec4::z;
79
80 // retro-compat aliases
81 float &a = x;
82 float &b = y;
83 float &c = z;
84 float &d = w;
85
86 const float &a_const = x;
87 const float &b_const = y;
88 const float &c_const = z;
89 const float &d_const = w;
90};
91
92template <>
93struct [[deprecated("Replaced by glm::ivec4")]] Vec4<int> : public glm::ivec4
94{
95 using glm::ivec4::ivec4;
96
97 // expose glm names
98 using glm::ivec4::w;
99 using glm::ivec4::x;
100 using glm::ivec4::y;
101 using glm::ivec4::z;
102
103 // retro-compat aliases
104 int &a = x;
105 int &b = y;
106 int &c = z;
107 int &d = w;
108
109 const int &a_const = x;
110 const int &b_const = y;
111 const int &c_const = z;
112 const int &d_const = w;
113};
114
115// --- For unordered_map
116
117// for glm::ivec2 map
119{
120 size_t operator()(const glm::ivec2 &v) const noexcept
121 {
122 return std::hash<int>()(v.x) ^ (std::hash<int>()(v.y) << 1);
123 }
124};
125
127{
128 bool operator()(const glm::ivec2 &a, const glm::ivec2 &b) const noexcept
129 {
130 return a == b;
131 }
132};
133
134// for glm::ivec4 map
136{
137 std::size_t operator()(const glm::ivec4 &v) const noexcept
138 {
139 std::size_t h = 0;
140 h ^= std::hash<int>{}(v.x) + 0x9e3779b9 + (h << 6) + (h >> 2);
141 h ^= std::hash<int>{}(v.y) + 0x9e3779b9 + (h << 6) + (h >> 2);
142 h ^= std::hash<int>{}(v.z) + 0x9e3779b9 + (h << 6) + (h >> 2);
143 h ^= std::hash<int>{}(v.w) + 0x9e3779b9 + (h << 6) + (h >> 2);
144 return h;
145 }
146};
147
149{
150 bool operator()(const glm::ivec4 &a, const glm::ivec4 &b) const noexcept
151 {
152 return a == b;
153 }
154};
155
156inline glm::vec4 adjust(const glm::vec4 &v,
157 float dx,
158 float dy,
159 float dz,
160 float dw)
161{
162 return glm::vec4{v.x + dx, v.y + dy, v.z + dz, v.w + dw};
163}
164
165inline glm::vec4 adjust(const glm::vec4 &v, float dr)
166{
167 return glm::vec4{v.x - dr, v.y + dr, v.z - dr, v.w + dr};
168}
169
179template <typename T> struct Mat
180{
181 std::vector<T> vector;
183 glm::ivec2 shape;
186 Mat() = default;
187
198 Mat(glm::ivec2 shape) : shape(shape)
199 {
200 this->vector.resize(shape.x * shape.y);
201 }
202
214 Mat(glm::ivec2 shape, T value) : shape(shape)
215 {
216 this->vector.resize(shape.x * shape.y);
217 std::fill(this->vector.begin(), this->vector.end(), value);
218 }
219
230 T &operator()(int i, int j)
231 {
232 return this->vector[j * this->shape.x + i];
233 }
234
246 const T &operator()(int i, int j) const
247 {
248 return this->vector[j * this->shape.x + i];
249 }
250
251 T &operator()(glm::ivec2 ij)
252 {
253 return this->vector[ij.y * this->shape.x + ij.x];
254 }
255
256 const T &operator()(glm::ivec2 ij) const
257 {
258 return this->vector[ij.y * this->shape.x + ij.x];
259 }
260};
261
262} // namespace hmap
Definition algebra.hpp:23
glm::vec4 adjust(const glm::vec4 &v, float dx, float dy, float dz, float dw)
Definition algebra.hpp:156
void to_csv(const std::vector< glm::vec3 > &xyz, const std::string &fname)
Definition algebra.cpp:12
Definition algebra.hpp:127
bool operator()(const glm::ivec2 &a, const glm::ivec2 &b) const noexcept
Definition algebra.hpp:128
Definition algebra.hpp:119
size_t operator()(const glm::ivec2 &v) const noexcept
Definition algebra.hpp:120
Definition algebra.hpp:149
bool operator()(const glm::ivec4 &a, const glm::ivec4 &b) const noexcept
Definition algebra.hpp:150
Definition algebra.hpp:136
std::size_t operator()(const glm::ivec4 &v) const noexcept
Definition algebra.hpp:137
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:180
glm::ivec2 shape
Dimensions of the matrix (rows x columns).
Definition algebra.hpp:183
T & operator()(int i, int j)
Access operator to get a reference to the element at (i, j).
Definition algebra.hpp:230
std::vector< T > vector
1D vector storing matrix elements in row-major order.
Definition algebra.hpp:181
const T & operator()(int i, int j) const
Const access operator to get the value of the element at (i, j).
Definition algebra.hpp:246
T & operator()(glm::ivec2 ij)
Definition algebra.hpp:251
Mat(glm::ivec2 shape)
Constructor to initialize a matrix with a given shape.
Definition algebra.hpp:198
const T & operator()(glm::ivec2 ij) const
Definition algebra.hpp:256
Mat(glm::ivec2 shape, T value)
Constructor to initialize a matrix with a given shape and value.
Definition algebra.hpp:214
Mat()=default
Definition algebra.hpp:29
Definition algebra.hpp:47
Definition algebra.hpp:67