Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_MATH_UTILS_HPP_
2#define IV_SRC_MATH_UTILS_HPP_
3
4#include "core/utils.hpp"
5#include "math/structs.hpp"
6
7#include <cmath>
8#include <vector>
9
10namespace iv::math
11{
12
13namespace utils
14{
15
16bool isSameSign(double value1, double value2, double precision);
17double valueInterpolation(double valueNormalized, double valueMin, double valueMax);
18int64_t mmToDp(int64_t sizeMm);
21
23
24template<typename T1, typename T2 = double>
25bool isZero(T1 value, T2 tolerance = iv::constants::maths::Precision)
26 requires std::is_arithmetic_v<T1> && std::is_arithmetic_v<T2>
27{
28 return iv::math::utils::isEqual(value, 0, tolerance);
29}
30
39template<typename T>
40inline bool isInRange(T value, T min, T max)
41 requires std::is_arithmetic_v<T>
42{
43 if (min > max)
44 {
45 throw std::invalid_argument("min must be less than or equal to max");
46 }
47
48 return min <= value and value <= max;
49}
50
51double stepsIncrement(iv::types::Range<double> range, uint64_t steps);
52double roundToNearestStep(double value, double step, iv::types::Range<double> range);
53
54}// namespace utils
55
56namespace geometry::utils
57{
58
59inline iv::math::geometry::Point2d getCentroid(const std::vector<iv::math::geometry::Point2d> &points)
60{
61 if (points.empty())
62 {
63 return {0, 0};
64 }
65
66 iv::types::coord x = 0;
67 iv::types::coord y = 0;
68
69 for (const auto &point: points)
70 {
71 x += point.x;
72 y += point.y;
73 }
74
75 x /= points.size();
76 y /= points.size();
77
78 return {x, y};
79}
80
81inline iv::types::height getHeight(const std::vector<iv::math::geometry::Point2d> &points)
82{
83 if (points.empty())
84 {
85 return 0;
86 }
87
88 iv::types::coord minY = points.front().y;
89 iv::types::coord maxY = points.front().y;
90
91 for (const auto &point: points)
92 {
93 minY = std::min(minY, point.y);
94 maxY = std::max(maxY, point.y);
95 }
96
97 return maxY - minY;
98}
99
100inline iv::types::width getWidth(const std::vector<iv::math::geometry::Point2d> &points)
101{
102 if (points.empty())
103 {
104 return 0;
105 }
106
107 iv::types::coord minX = points.front().x;
108 iv::types::coord maxX = points.front().x;
109
110 for (const auto &point: points)
111 {
112 minX = std::min(minX, point.x);
113 maxX = std::max(maxX, point.x);
114 }
115
116 return maxX - minX;
117}
118
120{
121 iv::types::coord sizeX = std::abs(secondPoint.x - firstPoint.x);
122 iv::types::coord sizeY = std::abs(secondPoint.y - firstPoint.y);
123
124 if (sizeX > sizeY)
125 {
126 secondPoint.y = firstPoint.y;
127 }
128 else
129 {
130 secondPoint.x = firstPoint.x;
131 }
132}
133
135 iv::math::geometry::Point2d &secondPoint)
136{
137 iv::types::coord dx = secondPoint.x - firstPoint.x;
138 iv::types::coord dy = secondPoint.y - firstPoint.y;
139
140 iv::types::coord absDx = std::abs(dx);
141 iv::types::coord absDy = std::abs(dy);
142
143 iv::types::coord distH = absDy;
144 iv::types::coord distV = absDx;
145 iv::types::coord distD = std::abs(absDx - absDy);
146
147 if (distH <= distV && distH <= distD)
148 {
149 secondPoint.y = firstPoint.y;
150 }
151 else if (distV <= distD)
152 {
153 secondPoint.x = firstPoint.x;
154 }
155 else
156 {
157 if (absDx > absDy)
158 {
159 secondPoint.x = firstPoint.x + (dx > 0 ? absDy : -absDy);
160 secondPoint.y = firstPoint.y + (dy > 0 ? absDy : -absDy);
161 }
162 else
163 {
164 secondPoint.x = firstPoint.x + (dx > 0 ? absDx : -absDx);
165 secondPoint.y = firstPoint.y + (dy > 0 ? absDx : -absDx);
166 }
167 }
168}
169
170inline bool isSamePoint2D(const iv::math::geometry::Point2d &firstPoint, const iv::math::geometry::Point2d &secondPoint,
171 iv::types::coord precision)
172{
173 return iv::math::utils::isEqual(firstPoint.x, secondPoint.x, precision) &&
174 iv::math::utils::isEqual(firstPoint.y, secondPoint.y, precision);
175}
176
177template<typename T>
178 requires std::is_same_v<T, iv::math::geometry::Point2d> || std::is_same_v<T, iv::math::geometry::Point3d>
179void close(std::vector<T> &v)
180{
181 if (v.empty() || v.front() == v.back())
182 {
183 return;
184 }
185
186 v.push_back(v.front());
187}
188
189template<typename T>
190 requires std::is_same_v<T, iv::math::geometry::Point2d> || std::is_same_v<T, iv::math::geometry::Point3d>
191bool isClosed(const std::vector<T> &v)
192{
193 if (v.empty())
194 {
195 return false;
196 }
197
198 return v.front() == v.back();
199}
200
201template<typename T>
202 requires std::is_same_v<T, iv::math::geometry::Point2d>
203double calculateArea(const std::vector<T> &v)
204{
205 double sum {0};
206
207 for (uint64_t i = 0; i < v.size() - 1; i++)
208 {
209 T point1 = v[i];
210 T point2 = v[i + 1];
211
212 sum += (point2.x - point1.x) * (point2.y + point1.y);
213 }
214
215 return std::abs(sum / 2.);
216}
217
218template<typename T>
219 requires std::is_same_v<T, iv::math::geometry::Point2d>
220float *getArrayFloatC(std::vector<T> points)
221{
222 auto *array = static_cast<float *>(calloc(points.size() * 2, sizeof(float)));
223
224 for (uint64_t itr = 0; itr < points.size(); itr++)
225 {
226 const uint64_t ind = itr * 2;
227
228 array[ind] = static_cast<float>(points.at(itr).x);
229 array[ind + 1] = static_cast<float>(points.at(itr).y);
230 }
231
232 return array;
233}
234
235template<typename T>
236 requires std::is_same_v<T, iv::math::geometry::Point2d> || std::is_same_v<T, iv::math::geometry::Point3d>
237float *getArray3dc(const std::vector<T> &v, const double z)
238{
239 float *array;
240 if (not v.empty())
241 {
242 array = static_cast<float *>(calloc(3 * v.size(), sizeof(float)));
243
244 for (uint64_t itr = 0; itr < v.size(); itr++)
245 {
246 const uint64_t ind = itr * 3;
247
248 array[ind] = static_cast<float>(v.at(itr).x);
249 array[ind + 1] = static_cast<float>(v.at(itr).y);
250 array[ind + 2] = static_cast<float>(z);
251 }
252 }
253 else
254 {
255 array = nullptr;
256 }
257
258 return array;
259}
260
261float *getArrayFloatC3d(std::vector<iv::math::geometry::Point3d> points);
262
263//JSA he traido esto aquĆ­ porque trabaja con puntos 3d
264//TODO: Eliminar esto lo antes posible
265float *getArrayRgbac_old(const std::vector<iv::math::geometry::Point3d> &v, float alpha);
266
267float *getArrayFloatCAppendingZ(const std::vector<iv::math::geometry::Point2d> &v, float z);
268
269double module(Point2d begining, Point2d end);
270
271}// namespace geometry::utils
272
273}// namespace iv::math
274
275#endif//IV_SRC_MATH_UTILS_HPP_
constexpr double Precision
Definition defines.hpp:130
double calculateArea(const std::vector< T > &v)
Definition utils.hpp:203
iv::math::geometry::Point2d getCentroid(const std::vector< iv::math::geometry::Point2d > &points)
Definition utils.hpp:59
bool isClosed(const std::vector< T > &v)
Definition utils.hpp:191
void alignSecondPointWithDiagonal(const iv::math::geometry::Point2d &firstPoint, iv::math::geometry::Point2d &secondPoint)
Definition utils.hpp:134
float * getArrayRgbac_old(const std::vector< iv::math::geometry::Point3d > &v, float alpha)
Definition utils.cpp:105
float * getArrayFloatC3d(std::vector< iv::math::geometry::Point3d > points)
Definition utils.cpp:87
double module(Point2d begining, Point2d end)
Definition utils.cpp:205
iv::types::height getHeight(const std::vector< iv::math::geometry::Point2d > &points)
Definition utils.hpp:81
void alignSecondPoint(const iv::math::geometry::Point2d &firstPoint, iv::math::geometry::Point2d &secondPoint)
Definition utils.hpp:119
bool isSamePoint2D(const iv::math::geometry::Point2d &firstPoint, const iv::math::geometry::Point2d &secondPoint, iv::types::coord precision)
Definition utils.hpp:170
float * getArray3dc(const std::vector< T > &v, const double z)
Definition utils.hpp:237
iv::types::width getWidth(const std::vector< iv::math::geometry::Point2d > &points)
Definition utils.hpp:100
void close(std::vector< T > &v)
Definition utils.hpp:179
float * getArrayFloatC(const std::vector< iv::math::geometry::Point2d > &v)
Definition utils.cpp:138
float * getArrayFloatCAppendingZ(const std::vector< iv::math::geometry::Point2d > &v, float z)
Definition utils.cpp:171
iv::types::radian degreeToRadian(iv::types::degree degree)
Definition utils.cpp:44
bool isSameSign(const double value1, const double value2, const double precision)
Definition utils.cpp:14
bool isEqual(T1 d1, T2 d2, T3 tolerance=iv::constants::maths::Precision)
Definition utils.hpp:270
iv::math::geometry::Point2d pointOnCircle(iv::types::radius radius, iv::types::degree angle)
Definition utils.cpp:54
double valueInterpolation(double valueNormalized, double valueMin, double valueMax)
Definition utils.cpp:21
bool isInRange(T value, T min, T max)
Checks if a value is in a range [min, max] (inclusive).
Definition utils.hpp:40
double stepsIncrement(const iv::types::Range< double > range, const uint64_t steps)
Definition utils.cpp:64
bool isZero(T1 value, T2 tolerance=iv::constants::maths::Precision)
Definition utils.hpp:25
double roundToNearestStep(double value, const double step, iv::types::Range< double > range)
Definition utils.cpp:72
iv::types::degree radianToDegree(iv::types::radian radian)
Definition utils.cpp:49
int64_t mmToDp(int64_t sizeMm)
Definition utils.cpp:33
Definition utils.hpp:267
double radian
Definition types.hpp:19
double height
Definition types.hpp:14
double radius
Definition types.hpp:17
double degree
Definition types.hpp:18
double width
Definition types.hpp:23
double coord
Definition types.hpp:13
Definition structs.hpp:16
iv::types::coord y
Definition structs.hpp:52
iv::types::coord x
Definition structs.hpp:51
Definition types.hpp:30