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#include <cmath>
13
14namespace ttk {
15
16 namespace Geometry {
17
23 template <typename T>
24 T angle(const T *vA0, const T *vA1, const T *vB0, const T *vB1);
25
26 template <typename T>
27 T angle2D(const T *vA0, const T *vA1, const T *vB0, const T *vB1);
28
29 // Computes the BAC angle, in the interval [0, 2pi]
30 template <typename T>
31 inline double angle2DUndirected(const T *vA, const T *vB, const T *vC) {
32 double angle = angle2D<T>(vA, vB, vA, vC);
33 if(angle < 0) {
34 angle += 2 * M_PI;
35 }
36 return angle;
37 }
38
49 template <typename T>
50 bool areVectorsColinear(const T *vA0,
51 const T *vA1,
52 const T *vB0,
53 const T *vB1,
54 std::array<T, 3> *coefficients = nullptr,
55 const T *tolerance = NULL);
56 template <typename T>
57 bool isTriangleColinear2D(const T *pptA,
58 const T *pptB,
59 const T *pptC,
60 const T tolerance);
61
72 template <typename T>
73 int computeBarycentricCoordinates(const T *p0,
74 const T *p1,
75 const T *p,
76 std::array<T, 2> &baryCentrics,
77 const int &dimension = 3);
78
88 template <typename T>
89 int computeBarycentricCoordinates(const T *p0,
90 const T *p1,
91 const T *p2,
92 const T *p,
93 std::array<T, 3> &baryCentrics);
94
107 template <typename T>
108 bool computeSegmentIntersection(const T &xA,
109 const T &yA,
110 const T &xB,
111 const T &yB,
112 const T &xC,
113 const T &yC,
114 const T &xD,
115 const T &yD,
116 T &x,
117 T &y);
118
124 template <typename T>
125 int computeTriangleAngles(const T *p0,
126 const T *p1,
127 const T *p2,
128 std::array<T, 3> &angles);
129
130 // Get the angle opposite to edge s2 using cosine law
136 template <typename T>
137 int computeTriangleAngleFromSides(const T s0,
138 const T s1,
139 const T s2,
140 T &angle);
141
148 template <typename T>
149 int computeTriangleArea(const T *p0, const T *p1, const T *p2, T &area);
150
157 template <typename T>
158 int
159 computeTriangleAreaFromSides(const T s0, const T s1, const T s2, T &area);
160
168 template <typename T>
169 int crossProduct(const T *vA0,
170 const T *vA1,
171 const T *vB0,
172 const T *vB1,
173 std::array<T, 3> &crossProduct);
174
180 template <typename T>
181 int crossProduct(const T *vA, const T *vB, T *crossProduct);
182
188 template <typename T>
189 T distance(const T *p0, const T *p1, const int &dimension = 3);
190
194 template <typename T>
195 T distance(const std::vector<T> &p0, const std::vector<T> &p1);
196
197 template <typename T>
198 inline T distance2D(const T *p0, const T *p1) {
199 T distance = 0;
200 T di0 = (p0[0] - p1[0]);
201 T di1 = (p0[1] - p1[1]);
202 if(std::is_same<T, float>::value) { // TODO constexpr when c++17
203 distance = fmaf(di0, di0, distance);
204 distance = fmaf(di1, di1, distance);
205 } else {
206 distance = fma(di0, di0, distance);
207 distance = fma(di1, di1, distance);
208 }
209
210 return sqrt(distance);
211 }
212
217 template <typename T>
218 T distanceFlatten(const std::vector<std::vector<T>> &p0,
219 const std::vector<std::vector<T>> &p1);
220
225 template <typename T>
226 T distanceFlatten(const std::vector<std::vector<T>> &p0,
227 const std::vector<std::vector<T>> &p1);
228
235 template <typename T>
236 T dotProduct(const T *vA0, const T *vA1, const T *vB0, const T *vB1);
237
244 template <typename T>
245 T dotProduct(const T *vA, const T *vB, const int &dimension = 3);
246
251 template <typename T>
252 T dotProduct(const std::vector<T> &vA, const std::vector<T> &vB);
253
258 template <typename T>
259 T dotProductFlatten(const std::vector<std::vector<T>> &vA,
260 const std::vector<std::vector<T>> &vB);
261
270 template <typename T, typename Container, size_t dim>
271 int getBoundingBox(const Container &points,
272 std::array<std::pair<T, T>, dim> &bBox) {
273 if(points.empty()) {
274 return -1;
275 }
276
277 for(size_t i = 0; i < points.size(); i++) {
278
279 if(i == 0) {
280 for(size_t j = 0; j < dim; j++) {
281 bBox[j].first = points[i][j];
282 bBox[j].second = points[i][j];
283 }
284 } else {
285 for(size_t j = 0; j < dim; j++) {
286 if(points[i][j] < bBox[j].first) {
287 bBox[j].first = points[i][j];
288 }
289 if(points[i][j] > bBox[j].second) {
290 bBox[j].second = points[i][j];
291 }
292 }
293 }
294 }
295
296 return 0;
297 }
298
305 template <typename T>
306 bool isPointInTriangle(const T *p0, const T *p1, const T *p2, const T *p);
307
317 template <typename T>
318 bool isPointOnSegment(const T &x,
319 const T &y,
320 const T &xA,
321 const T &yA,
322 const T &xB,
323 const T &yB);
324
333 template <typename T>
334 bool isPointOnSegment(const T *p,
335 const T *pA,
336 const T *pB,
337 const int &dimension = 3);
338
345 template <typename T>
346 bool isTriangleColinear(const T *p0,
347 const T *p1,
348 const T *p2,
349 const T *tolerance = nullptr);
350
356 template <typename T>
357 T magnitude(const T *v, const int &dimension = 3);
358
362 template <typename T>
363 T magnitude(const std::vector<T> &v);
364
369 template <typename T>
370 T magnitudeFlatten(const std::vector<std::vector<T>> &v);
371
375 template <typename T>
376 T magnitude(const T *o, const T *d);
377
380 template <typename T>
381 inline T powInt(const T val, const int n) {
382 if(n < 0) {
383 return 1.0 / powInt(val, -n);
384 } else if(n == 0) {
385 return 1;
386 } else if(n == 1) {
387 return val;
388 } else if(n == 2) {
389 return val * val;
390 } else if(n == 3) {
391 return val * val * val;
392 } else {
393 T ret = val;
394 for(int i = 0; i < n - 1; ++i) {
395 ret *= val;
396 }
397 return ret;
398 }
399 }
400
402 template <typename T = double>
403 inline T powIntTen(const int n) {
404 return powInt(static_cast<T>(10), n);
405 }
406
428#define TTK_POW_LAMBDA(CALLEXPR, TYPE, EXPN, ...) \
429 { \
430 const auto one = [](const TYPE ttkNotUsed(a)) { return TYPE{1}; }; \
431 const auto id = [](const TYPE a) { return a; }; \
432 const auto square = [](const TYPE a) { return a * a; }; \
433 const auto cube = [](const TYPE a) { return a * a * a; }; \
434 const auto powInt \
435 = [EXPN](const TYPE a) { return Geometry::powInt(a, EXPN); }; \
436 \
437 if(EXPN == 0) { \
438 CALLEXPR(__VA_ARGS__, one); \
439 } else if(EXPN == 1) { \
440 CALLEXPR(__VA_ARGS__, id); \
441 } else if(EXPN == 2) { \
442 CALLEXPR(__VA_ARGS__, square); \
443 } else if(EXPN == 3) { \
444 CALLEXPR(__VA_ARGS__, cube); \
445 } else { \
446 CALLEXPR(__VA_ARGS__, powInt); \
447 } \
448 }
449
453 template <typename T1, typename T2>
454 inline T1 pow(const T1 val, const T2 n) {
455 static_assert(
456 std::is_arithmetic<T1>::value && std::is_arithmetic<T2>::value,
457 "pow can only be applied on arithmetic values");
458
459 if(std::is_integral<T2>::value) {
460 return powInt(val, n);
461 } else if(std::is_floating_point<T2>::value) {
462 return std::pow(val, n);
463 }
464 // this return statement should be unreachable thanks to the
465 // static_assert
466 return T1{};
467 }
468
476 template <typename T>
477 int
478 subtractVectors(const T *a, const T *b, T *out, const int &dimension = 3);
479
485 template <typename T>
486 int subtractVectors(const std::vector<T> &a,
487 const std::vector<T> &b,
488 std::vector<T> &out);
489
497 template <typename T>
498 int addVectors(const T *a, const T *b, T *out, const int &dimension = 3);
499
505 template <typename T>
506 int addVectors(const std::vector<T> &a,
507 const std::vector<T> &b,
508 std::vector<T> &out);
509
514 template <typename T>
515 int multiAddVectors(const std::vector<std::vector<T>> &a,
516 const std::vector<std::vector<T>> &b,
517 std::vector<std::vector<T>> &out);
518
524 template <typename T>
525 int
526 multiAddVectorsFlatten(const std::vector<std::vector<std::vector<T>>> &a,
527 const std::vector<std::vector<std::vector<T>>> &b,
528 std::vector<std::vector<T>> &out);
529
537 template <typename T>
538 int
539 scaleVector(const T *a, const T factor, T *out, const int &dimension = 3);
540
546 template <typename T>
547 int
548 scaleVector(const std::vector<T> &a, const T factor, std::vector<T> &out);
549
557 template <typename T>
558 int vectorProjection(const T *a,
559 const T *b,
560 T *out,
561 const int &dimension = 3);
562
568 template <typename T>
569 int vectorProjection(const std::vector<T> &a,
570 const std::vector<T> &b,
571 std::vector<T> &out);
572
578 template <typename T>
579 void addVectorsProjection(const std::vector<T> &a,
580 const std::vector<T> &b,
581 std::vector<T> &a_out,
582 std::vector<T> &b_out);
583
589 template <typename T>
590 void gramSchmidt(const std::vector<std::vector<T>> &a,
591 std::vector<std::vector<T>> &out);
592
596 template <typename T>
597 bool isVectorUniform(const std::vector<T> &a);
598
602 template <typename T>
603 bool isVectorNull(const std::vector<T> &a);
604
609 template <typename T>
610 bool isVectorNullFlatten(const std::vector<std::vector<T>> &a);
611
617 template <typename T>
618 int flattenMultiDimensionalVector(const std::vector<std::vector<T>> &a,
619 std::vector<T> &out);
620
625 template <typename T>
627 const std::vector<std::vector<std::vector<T>>> &a,
628 std::vector<std::vector<T>> &out);
629
637 template <typename T>
638 int unflattenMultiDimensionalVector(const std::vector<T> &a,
639 std::vector<std::vector<T>> &out,
640 const int &no_columns = 2);
641
646 template <typename T>
647 void matrixMultiplication(const std::vector<std::vector<T>> &a,
648 const std::vector<std::vector<T>> &b,
649 std::vector<std::vector<T>> &out);
650
656 template <typename T>
657 void subtractMatrices(const std::vector<std::vector<T>> &a,
658 const std::vector<std::vector<T>> &b,
659 std::vector<std::vector<T>> &out);
660
665 template <typename T>
666 void addMatrices(const std::vector<std::vector<T>> &a,
667 const std::vector<std::vector<T>> &b,
668 std::vector<std::vector<T>> &out);
669
674 template <typename T>
675 void scaleMatrix(const std::vector<std::vector<T>> &a,
676 const T factor,
677 std::vector<std::vector<T>> &out);
678
682 template <typename T>
683 void transposeMatrix(const std::vector<std::vector<T>> &a,
684 std::vector<std::vector<T>> &out);
685
686 } // namespace Geometry
687} // namespace ttk
#define M_PI
Definition Os.h:50
int scaleVector(const T *a, const T factor, T *out, const int &dimension=3)
Definition Geometry.cpp:593
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:27
int computeTriangleArea(const T *p0, const T *p1, const T *p2, T &area)
Definition Geometry.cpp:281
double angle2DUndirected(const T *vA, const T *vB, const T *vC)
Definition Geometry.h:31
int addVectors(const T *a, const T *b, T *out, const int &dimension=3)
Definition Geometry.cpp:556
T dotProductFlatten(const std::vector< std::vector< T > > &vA, const std::vector< std::vector< T > > &vB)
Definition Geometry.cpp:412
int multiFlattenMultiDimensionalVector(const std::vector< std::vector< std::vector< T > > > &a, std::vector< std::vector< T > > &out)
Definition Geometry.cpp:698
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:734
bool isTriangleColinear2D(const T *pptA, const T *pptB, const T *pptC, const T tolerance)
Definition Geometry.cpp:130
T dotProduct(const T *vA0, const T *vA1, const T *vB0, const T *vB1)
Definition Geometry.cpp:388
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:723
bool isVectorNullFlatten(const std::vector< std::vector< T > > &a)
Definition Geometry.cpp:681
bool isPointOnSegment(const T &x, const T &y, const T &xA, const T &yA, const T &xB, const T &yB)
Definition Geometry.cpp:443
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:248
int computeBarycentricCoordinates(const T *p0, const T *p1, const T *p, std::array< T, 2 > &baryCentrics, const int &dimension=3)
Definition Geometry.cpp:142
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:744
void gramSchmidt(const std::vector< std::vector< T > > &a, std::vector< std::vector< T > > &out)
Definition Geometry.cpp:647
void scaleMatrix(const std::vector< std::vector< T > > &a, const T factor, std::vector< std::vector< T > > &out)
Definition Geometry.cpp:754
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:571
int flattenMultiDimensionalVector(const std::vector< std::vector< T > > &a, std::vector< T > &out)
Definition Geometry.cpp:688
bool isVectorUniform(const std::vector< T > &a)
Definition Geometry.cpp:665
int subtractVectors(const T *a, const T *b, T *out, const int &dimension=3)
Definition Geometry.cpp:538
int computeTriangleAreaFromSides(const T s0, const T s1, const T s2, T &area)
Definition Geometry.cpp:296
T magnitudeFlatten(const std::vector< std::vector< T > > &v)
Definition Geometry.cpp:519
bool isVectorNull(const std::vector< T > &a)
Definition Geometry.cpp:673
T1 pow(const T1 val, const T2 n)
Definition Geometry.h:454
T distance2D(const T *p0, const T *p1)
Definition Geometry.h:198
bool isTriangleColinear(const T *p0, const T *p1, const T *p2, const T *tolerance=nullptr)
Definition Geometry.cpp:466
int getBoundingBox(const Container &points, std::array< std::pair< T, T >, dim > &bBox)
Definition Geometry.h:271
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:636
T powIntTen(const int n)
Compute the nth power of ten.
Definition Geometry.h:403
T powInt(const T val, const int n)
Definition Geometry.h:381
int computeTriangleAngles(const T *p0, const T *p1, const T *p2, std::array< T, 3 > &angles)
Definition Geometry.cpp:308
int computeTriangleAngleFromSides(const T s0, const T s1, const T s2, T &angle)
Definition Geometry.cpp:321
int vectorProjection(const T *a, const T *b, T *out, const int &dimension=3)
Definition Geometry.cpp:611
int crossProduct(const T *vA0, const T *vA1, const T *vB0, const T *vB1, std::array< T, 3 > &crossProduct)
Definition Geometry.cpp:332
void transposeMatrix(const std::vector< std::vector< T > > &a, std::vector< std::vector< T > > &out)
Definition Geometry.cpp:764
int unflattenMultiDimensionalVector(const std::vector< T > &a, std::vector< std::vector< T > > &out, const int &no_columns=2)
Definition Geometry.cpp:708
T magnitude(const T *v, const int &dimension=3)
Definition Geometry.cpp:509
T distance(const T *p0, const T *p1, const int &dimension=3)
Definition Geometry.cpp:362
T angle2D(const T *vA0, const T *vA1, const T *vB0, const T *vB1)
Definition Geometry.cpp:20
T distanceFlatten(const std::vector< std::vector< T > > &p0, const std::vector< std::vector< T > > &p1)
Definition Geometry.cpp:379
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:581
bool isPointInTriangle(const T *p0, const T *p1, const T *p2, const T *p)
Definition Geometry.cpp:421
The Topology ToolKit.