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// --- For unordered_map
28
29// for glm::ivec2 map
31{
32 size_t operator()(const glm::ivec2 &v) const noexcept
33 {
34 return std::hash<int>()(v.x) ^ (std::hash<int>()(v.y) << 1);
35 }
36};
37
38struct IVec2Eq
39{
40 bool operator()(const glm::ivec2 &a, const glm::ivec2 &b) const noexcept
41 {
42 return a == b;
43 }
44};
45
46// for glm::ivec4 map
48{
49 std::size_t operator()(const glm::ivec4 &v) const noexcept
50 {
51 std::size_t h = 0;
52 h ^= std::hash<int>{}(v.x) + 0x9e3779b9 + (h << 6) + (h >> 2);
53 h ^= std::hash<int>{}(v.y) + 0x9e3779b9 + (h << 6) + (h >> 2);
54 h ^= std::hash<int>{}(v.z) + 0x9e3779b9 + (h << 6) + (h >> 2);
55 h ^= std::hash<int>{}(v.w) + 0x9e3779b9 + (h << 6) + (h >> 2);
56 return h;
57 }
58};
59
60struct IVec4Eq
61{
62 bool operator()(const glm::ivec4 &a, const glm::ivec4 &b) const noexcept
63 {
64 return a == b;
65 }
66};
67
68inline glm::vec4 adjust(const glm::vec4 &v,
69 float dx,
70 float dy,
71 float dz,
72 float dw)
73{
74 return glm::vec4{v.x + dx, v.y + dy, v.z + dz, v.w + dw};
75}
76
77inline glm::vec4 adjust(const glm::vec4 &v, float dr)
78{
79 return glm::vec4{v.x - dr, v.y + dr, v.z - dr, v.w + dr};
80}
81
91template <typename T> struct Mat
92{
93 std::vector<T> vector;
95 glm::ivec2 shape;
98 Mat() = default;
99
110 Mat(glm::ivec2 shape) : shape(shape)
111 {
112 this->vector.resize(shape.x * shape.y);
113 }
114
126 Mat(glm::ivec2 shape, T value) : shape(shape)
127 {
128 this->vector.resize(shape.x * shape.y);
129 std::fill(this->vector.begin(), this->vector.end(), value);
130 }
131
142 T &operator()(int i, int j)
143 {
144 return this->vector[j * this->shape.x + i];
145 }
146
158 const T &operator()(int i, int j) const
159 {
160 return this->vector[j * this->shape.x + i];
161 }
162
163 T &operator()(glm::ivec2 ij)
164 {
165 return this->vector[ij.y * this->shape.x + ij.x];
166 }
167
168 const T &operator()(glm::ivec2 ij) const
169 {
170 return this->vector[ij.y * this->shape.x + ij.x];
171 }
172};
173
174} // namespace hmap
Definition algebra.hpp:23
glm::vec4 adjust(const glm::vec4 &v, float dx, float dy, float dz, float dw)
Definition algebra.hpp:68
void to_csv(const std::vector< glm::vec3 > &xyz, const std::string &fname)
Definition algebra.cpp:13
Definition algebra.hpp:39
bool operator()(const glm::ivec2 &a, const glm::ivec2 &b) const noexcept
Definition algebra.hpp:40
Definition algebra.hpp:31
size_t operator()(const glm::ivec2 &v) const noexcept
Definition algebra.hpp:32
Definition algebra.hpp:61
bool operator()(const glm::ivec4 &a, const glm::ivec4 &b) const noexcept
Definition algebra.hpp:62
Definition algebra.hpp:48
std::size_t operator()(const glm::ivec4 &v) const noexcept
Definition algebra.hpp:49
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:92
glm::ivec2 shape
Dimensions of the matrix (rows x columns).
Definition algebra.hpp:95
T & operator()(int i, int j)
Access operator to get a reference to the element at (i, j).
Definition algebra.hpp:142
std::vector< T > vector
1D vector storing matrix elements in row-major order.
Definition algebra.hpp:93
const T & operator()(int i, int j) const
Const access operator to get the value of the element at (i, j).
Definition algebra.hpp:158
T & operator()(glm::ivec2 ij)
Definition algebra.hpp:163
Mat(glm::ivec2 shape)
Constructor to initialize a matrix with a given shape.
Definition algebra.hpp:110
const T & operator()(glm::ivec2 ij) const
Definition algebra.hpp:168
Mat(glm::ivec2 shape, T value)
Constructor to initialize a matrix with a given shape and value.
Definition algebra.hpp:126
Mat()=default