TTK
Loading...
Searching...
No Matches
Geometry.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <Debug.h>
11#include <array>
12
13namespace ttk {
14
15 namespace Geometry {
16
22 template <typename T>
23 T angle(const T *vA0, const T *vA1, const T *vB0, const T *vB1);
24
35 template <typename T>
36 bool areVectorsColinear(const T *vA0,
37 const T *vA1,
38 const T *vB0,
39 const T *vB1,
40 std::array<T, 3> *coefficients = nullptr,
41 const T *tolerance = NULL);
42
53 template <typename T>
54 int computeBarycentricCoordinates(const T *p0,
55 const T *p1,
56 const T *p,
57 std::array<T, 2> &baryCentrics,
58 const int &dimension = 3);
59
69 template <typename T>
70 int computeBarycentricCoordinates(const T *p0,
71 const T *p1,
72 const T *p2,
73 const T *p,
74 std::array<T, 3> &baryCentrics);
75
88 template <typename T>
89 bool computeSegmentIntersection(const T &xA,
90 const T &yA,
91 const T &xB,
92 const T &yB,
93 const T &xC,
94 const T &yC,
95 const T &xD,
96 const T &yD,
97 T &x,
98 T &y);
99
105 template <typename T>
106 int computeTriangleAngles(const T *p0,
107 const T *p1,
108 const T *p2,
109 std::array<T, 3> &angles);
110
111 // Get the angle opposite to edge s2 using cosine law
117 template <typename T>
118 int computeTriangleAngleFromSides(const T s0,
119 const T s1,
120 const T s2,
121 T &angle);
122
129 template <typename T>
130 int computeTriangleArea(const T *p0, const T *p1, const T *p2, T &area);
131
138 template <typename T>
139 int
140 computeTriangleAreaFromSides(const T s0, const T s1, const T s2, T &area);
141
149 template <typename T>
150 int crossProduct(const T *vA0,
151 const T *vA1,
152 const T *vB0,
153 const T *vB1,
154 std::array<T, 3> &crossProduct);
155
161 template <typename T>
162 int crossProduct(const T *vA, const T *vB, T *crossProduct);
163
169 template <typename T>
170 T distance(const T *p0, const T *p1, const int &dimension = 3);
171
175 template <typename T>
176 T distance(const std::vector<T> &p0, const std::vector<T> &p1);
177
182 template <typename T>
183 T distanceFlatten(const std::vector<std::vector<T>> &p0,
184 const std::vector<std::vector<T>> &p1);
185
192 template <typename T>
193 T dotProduct(const T *vA0, const T *vA1, const T *vB0, const T *vB1);
194
201 template <typename T>
202 T dotProduct(const T *vA, const T *vB, const int &dimension = 3);
203
208 template <typename T>
209 T dotProduct(const std::vector<T> &vA, const std::vector<T> &vB);
210
215 template <typename T>
216 T dotProductFlatten(const std::vector<std::vector<T>> &vA,
217 const std::vector<std::vector<T>> &vB);
218
227 template <typename T, typename Container, size_t dim>
228 int getBoundingBox(const Container &points,
229 std::array<std::pair<T, T>, dim> &bBox) {
230 if(points.empty()) {
231 return -1;
232 }
233
234 for(size_t i = 0; i < points.size(); i++) {
235
236 if(i == 0) {
237 for(size_t j = 0; j < dim; j++) {
238 bBox[j].first = points[i][j];
239 bBox[j].second = points[i][j];
240 }
241 } else {
242 for(size_t j = 0; j < dim; j++) {
243 if(points[i][j] < bBox[j].first) {
244 bBox[j].first = points[i][j];
245 }
246 if(points[i][j] > bBox[j].second) {
247 bBox[j].second = points[i][j];
248 }
249 }
250 }
251 }
252
253 return 0;
254 }
255
262 template <typename T>
263 bool isPointInTriangle(const T *p0, const T *p1, const T *p2, const T *p);
264
274 template <typename T>
275 bool isPointOnSegment(const T &x,
276 const T &y,
277 const T &xA,
278 const T &yA,
279 const T &xB,
280 const T &yB);
281
290 template <typename T>
291 bool isPointOnSegment(const T *p,
292 const T *pA,
293 const T *pB,
294 const int &dimension = 3);
295
302 template <typename T>
303 bool isTriangleColinear(const T *p0,
304 const T *p1,
305 const T *p2,
306 const T *tolerance = nullptr);
307
313 template <typename T>
314 T magnitude(const T *v, const int &dimension = 3);
315
319 template <typename T>
320 T magnitude(const std::vector<T> &v);
321
326 template <typename T>
327 T magnitudeFlatten(const std::vector<std::vector<T>> &v);
328
332 template <typename T>
333 T magnitude(const T *o, const T *d);
334
337 template <typename T>
338 inline T powInt(const T val, const int n) {
339 if(n < 0) {
340 return 1.0 / powInt(val, -n);
341 } else if(n == 0) {
342 return 1;
343 } else if(n == 1) {
344 return val;
345 } else if(n == 2) {
346 return val * val;
347 } else if(n == 3) {
348 return val * val * val;
349 } else {
350 T ret = val;
351 for(int i = 0; i < n - 1; ++i) {
352 ret *= val;
353 }
354 return ret;
355 }
356 }
357
359 template <typename T = double>
360 inline T powIntTen(const int n) {
361 return powInt(static_cast<T>(10), n);
362 }
363
385#define TTK_POW_LAMBDA(CALLEXPR, TYPE, EXPN, ...) \
386 { \
387 const auto one = [](const TYPE ttkNotUsed(a)) { return TYPE{1}; }; \
388 const auto id = [](const TYPE a) { return a; }; \
389 const auto square = [](const TYPE a) { return a * a; }; \
390 const auto cube = [](const TYPE a) { return a * a * a; }; \
391 const auto powInt \
392 = [EXPN](const TYPE a) { return Geometry::powInt(a, EXPN); }; \
393 \
394 if(EXPN == 0) { \
395 CALLEXPR(__VA_ARGS__, one); \
396 } else if(EXPN == 1) { \
397 CALLEXPR(__VA_ARGS__, id); \
398 } else if(EXPN == 2) { \
399 CALLEXPR(__VA_ARGS__, square); \
400 } else if(EXPN == 3) { \
401 CALLEXPR(__VA_ARGS__, cube); \
402 } else { \
403 CALLEXPR(__VA_ARGS__, powInt); \
404 } \
405 }
406
410 template <typename T1, typename T2>
411 inline T1 pow(const T1 val, const T2 n) {
412 static_assert(
413 std::is_arithmetic<T1>::value && std::is_arithmetic<T2>::value,
414 "pow can only be applied on arithmetic values");
415
416 if(std::is_integral<T2>::value) {
417 return powInt(val, n);
418 } else if(std::is_floating_point<T2>::value) {
419 return std::pow(val, n);
420 }
421 // this return statement should be unreachable thanks to the
422 // static_assert
423 return T1{};
424 }
425
433 template <typename T>
434 int
435 subtractVectors(const T *a, const T *b, T *out, const int &dimension = 3);
436
442 template <typename T>
443 int subtractVectors(const std::vector<T> &a,
444 const std::vector<T> &b,
445 std::vector<T> &out);
446
454 template <typename T>
455 int addVectors(const T *a, const T *b, T *out, const int &dimension = 3);
456
462 template <typename T>
463 int addVectors(const std::vector<T> &a,
464 const std::vector<T> &b,
465 std::vector<T> &out);
466
471 template <typename T>
472 int multiAddVectors(const std::vector<std::vector<T>> &a,
473 const std::vector<std::vector<T>> &b,
474 std::vector<std::vector<T>> &out);
475
481 template <typename T>
482 int
483 multiAddVectorsFlatten(const std::vector<std::vector<std::vector<T>>> &a,
484 const std::vector<std::vector<std::vector<T>>> &b,
485 std::vector<std::vector<T>> &out);
486
494 template <typename T>
495 int
496 scaleVector(const T *a, const T factor, T *out, const int &dimension = 3);
497
503 template <typename T>
504 int
505 scaleVector(const std::vector<T> &a, const T factor, std::vector<T> &out);
506
514 template <typename T>
515 int vectorProjection(const T *a,
516 const T *b,
517 T *out,
518 const int &dimension = 3);
519
525 template <typename T>
526 int vectorProjection(const std::vector<T> &a,
527 const std::vector<T> &b,
528 std::vector<T> &out);
529
535 template <typename T>
536 void addVectorsProjection(const std::vector<T> &a,
537 const std::vector<T> &b,
538 std::vector<T> &a_out,
539 std::vector<T> &b_out);
540
546 template <typename T>
547 void gramSchmidt(const std::vector<std::vector<T>> &a,
548 std::vector<std::vector<T>> &out);
549
553 template <typename T>
554 bool isVectorUniform(const std::vector<T> &a);
555
559 template <typename T>
560 bool isVectorNull(const std::vector<T> &a);
561
566 template <typename T>
567 bool isVectorNullFlatten(const std::vector<std::vector<T>> &a);
568
574 template <typename T>
575 int flattenMultiDimensionalVector(const std::vector<std::vector<T>> &a,
576 std::vector<T> &out);
577
582 template <typename T>
584 const std::vector<std::vector<std::vector<T>>> &a,
585 std::vector<std::vector<T>> &out);
586
594 template <typename T>
595 int unflattenMultiDimensionalVector(const std::vector<T> &a,
596 std::vector<std::vector<T>> &out,
597 const int &no_columns = 2);
598
603 template <typename T>
604 void matrixMultiplication(const std::vector<std::vector<T>> &a,
605 const std::vector<std::vector<T>> &b,
606 std::vector<std::vector<T>> &out);
607
613 template <typename T>
614 void subtractMatrices(const std::vector<std::vector<T>> &a,
615 const std::vector<std::vector<T>> &b,
616 std::vector<std::vector<T>> &out);
617
622 template <typename T>
623 void addMatrices(const std::vector<std::vector<T>> &a,
624 const std::vector<std::vector<T>> &b,
625 std::vector<std::vector<T>> &out);
626
631 template <typename T>
632 void scaleMatrix(const std::vector<std::vector<T>> &a,
633 const T factor,
634 std::vector<std::vector<T>> &out);
635
639 template <typename T>
640 void transposeMatrix(const std::vector<std::vector<T>> &a,
641 std::vector<std::vector<T>> &out);
642
643 } // namespace Geometry
644} // namespace ttk
int scaleVector(const T *a, const T factor, T *out, const int &dimension=3)
Definition: Geometry.cpp:575
bool areVectorsColinear(const T *vA0, const T *vA1, const T *vB0, const T *vB1, std::array< T, 3 > *coefficients=nullptr, const T *tolerance=NULL)
Definition: Geometry.cpp:20
int computeTriangleArea(const T *p0, const T *p1, const T *p2, T &area)
Definition: Geometry.cpp:263
int addVectors(const T *a, const T *b, T *out, const int &dimension=3)
Definition: Geometry.cpp:538
T dotProductFlatten(const std::vector< std::vector< T > > &vA, const std::vector< std::vector< T > > &vB)
Definition: Geometry.cpp:394
int multiFlattenMultiDimensionalVector(const std::vector< std::vector< std::vector< T > > > &a, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:680
void subtractMatrices(const std::vector< std::vector< T > > &a, const std::vector< std::vector< T > > &b, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:716
T dotProduct(const T *vA0, const T *vA1, const T *vB0, const T *vB1)
Definition: Geometry.cpp:370
void matrixMultiplication(const std::vector< std::vector< T > > &a, const std::vector< std::vector< T > > &b, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:705
bool isVectorNullFlatten(const std::vector< std::vector< T > > &a)
Definition: Geometry.cpp:663
bool isPointOnSegment(const T &x, const T &y, const T &xA, const T &yA, const T &xB, const T &yB)
Definition: Geometry.cpp:425
bool computeSegmentIntersection(const T &xA, const T &yA, const T &xB, const T &yB, const T &xC, const T &yC, const T &xD, const T &yD, T &x, T &y)
Definition: Geometry.cpp:230
int computeBarycentricCoordinates(const T *p0, const T *p1, const T *p, std::array< T, 2 > &baryCentrics, const int &dimension=3)
Definition: Geometry.cpp:124
void addMatrices(const std::vector< std::vector< T > > &a, const std::vector< std::vector< T > > &b, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:726
void gramSchmidt(const std::vector< std::vector< T > > &a, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:629
void scaleMatrix(const std::vector< std::vector< T > > &a, const T factor, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:736
int multiAddVectors(const std::vector< std::vector< T > > &a, const std::vector< std::vector< T > > &b, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:553
int flattenMultiDimensionalVector(const std::vector< std::vector< T > > &a, std::vector< T > &out)
Definition: Geometry.cpp:670
bool isVectorUniform(const std::vector< T > &a)
Definition: Geometry.cpp:647
int subtractVectors(const T *a, const T *b, T *out, const int &dimension=3)
Definition: Geometry.cpp:520
int computeTriangleAreaFromSides(const T s0, const T s1, const T s2, T &area)
Definition: Geometry.cpp:278
T magnitudeFlatten(const std::vector< std::vector< T > > &v)
Definition: Geometry.cpp:501
bool isVectorNull(const std::vector< T > &a)
Definition: Geometry.cpp:655
T1 pow(const T1 val, const T2 n)
Definition: Geometry.h:411
bool isTriangleColinear(const T *p0, const T *p1, const T *p2, const T *tolerance=nullptr)
Definition: Geometry.cpp:448
int getBoundingBox(const Container &points, std::array< std::pair< T, T >, dim > &bBox)
Definition: Geometry.h:228
T angle(const T *vA0, const T *vA1, const T *vB0, const T *vB1)
Definition: Geometry.cpp:13
void addVectorsProjection(const std::vector< T > &a, const std::vector< T > &b, std::vector< T > &a_out, std::vector< T > &b_out)
Definition: Geometry.cpp:618
T powIntTen(const int n)
Compute the nth power of ten.
Definition: Geometry.h:360
T powInt(const T val, const int n)
Definition: Geometry.h:338
int computeTriangleAngles(const T *p0, const T *p1, const T *p2, std::array< T, 3 > &angles)
Definition: Geometry.cpp:290
int computeTriangleAngleFromSides(const T s0, const T s1, const T s2, T &angle)
Definition: Geometry.cpp:303
int vectorProjection(const T *a, const T *b, T *out, const int &dimension=3)
Definition: Geometry.cpp:593
int crossProduct(const T *vA0, const T *vA1, const T *vB0, const T *vB1, std::array< T, 3 > &crossProduct)
Definition: Geometry.cpp:314
void transposeMatrix(const std::vector< std::vector< T > > &a, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:746
int unflattenMultiDimensionalVector(const std::vector< T > &a, std::vector< std::vector< T > > &out, const int &no_columns=2)
Definition: Geometry.cpp:690
T magnitude(const T *v, const int &dimension=3)
Definition: Geometry.cpp:491
T distance(const T *p0, const T *p1, const int &dimension=3)
Definition: Geometry.cpp:344
T distanceFlatten(const std::vector< std::vector< T > > &p0, const std::vector< std::vector< T > > &p1)
Definition: Geometry.cpp:361
int multiAddVectorsFlatten(const std::vector< std::vector< std::vector< T > > > &a, const std::vector< std::vector< std::vector< T > > > &b, std::vector< std::vector< T > > &out)
Definition: Geometry.cpp:563
bool isPointInTriangle(const T *p0, const T *p1, const T *p2, const T *p)
Definition: Geometry.cpp:403
The Topology ToolKit.