CellModules
geometry.hpp
Go to the documentation of this file.
1#ifndef MECACELL_GEOMETRY_H
2#define MECACELL_GEOMETRY_H
3#include <vector>
4#include "../utilities/utils.hpp"
5namespace MecaCell {
6
19std::pair<bool, Vec> inline rayInTriangle(const Vec &v0, const Vec &v1, const Vec &v2,
20 const Vec &o, const Vec &r,
21 const double tolerance = 0) {
22 Vec u = v1 - v0;
23 Vec v = v2 - v0;
24 Vec n = u.cross(v);
25 double l = Vec::rayCast(v0, n, o, r);
26 if (l > 0) {
27 Vec p = o + l * r;
28 Vec w = p - v0;
29 double nsq = n.sqlength();
30 l = u.cross(w).dot(n) / nsq;
31 double b = w.cross(v).dot(n) / nsq;
32 double a = 1.0 - l - b;
33 return {0 - tolerance <= a && a <= 1.0 + tolerance && 0 - tolerance <= b &&
34 b <= 1.0 + tolerance && 0 - tolerance <= l && l <= 1.0 + tolerance,
35 a * v0 + b * v1 + l * v2};
36 }
37 return {false, o};
38}
39
53std::pair<bool, Vec> inline projectionIntriangle(const Vec &v0, const Vec &v1,
54 const Vec &v2, const Vec &p,
55 const double tolerance = 0) {
56 Vec u = v1 - v0;
57 Vec v = v2 - v0;
58 Vec n = u.cross(v);
59 Vec w = p - v0;
60 double nsq = n.sqlength();
61 double l = u.cross(w).dot(n) / nsq;
62 double b = w.cross(v).dot(n) / nsq;
63 double a = 1.0 - l - b;
64 return {0 - tolerance <= a && a <= 1.0 + tolerance && 0 - tolerance <= b &&
65 b <= 1.0 + tolerance && 0 - tolerance <= l && l <= 1.0 + tolerance,
66 a * v0 + b * v1 + l * v2};
67}
68
79double inline closestDistToTriangleEdge(const Vec &v0, const Vec &v1, const Vec &v2,
80 const Vec &p) {
81 Vec a = v1 - v0;
82 Vec b = v2 - v0;
83 Vec c = v2 - v1;
84 // on teste si la projection de p sur a est entre v1 et v0;
85 Vec v0p = p - v0;
86 Vec v1p = p - v1;
87 Vec v2p = p - v2;
88 double sqV0pa = v0p.dot(a);
89 double sqV0pb = v0p.dot(b);
90 double sqV1pc = v1p.dot(c);
91 double adist, bdist, cdist;
92 double v0dist, v1dist, v2dist;
93 v0dist = v0p.sqlength();
94 v1dist = v1p.sqlength();
95 v2dist = v2p.sqlength();
96 if (sqV0pa >= 0 && sqV0pa <= a.sqlength()) {
97 adist = ((v0 + (sqV0pa / a.sqlength()) * a) - p).sqlength();
98 } else if (sqV0pa < 0) { // v0 is the closest
99 adist = v0dist;
100 } else { // v1 is the closest
101 adist = v1dist;
102 }
103 if (sqV0pb >= 0 && sqV0pb <= b.sqlength()) {
104 bdist = ((v0 + (sqV0pb / b.sqlength()) * b) - p).sqlength();
105 } else if (sqV0pb < 0) { // v0 is the closest
106 bdist = v0dist;
107 } else { // v2 is the closest
108 bdist = v2dist;
109 }
110 if (sqV1pc >= 0 && sqV1pc <= c.sqlength()) {
111 cdist = ((v1 + (sqV1pc / c.sqlength()) * c) - p).sqlength();
112 } else if (sqV1pc < 0) { // v1 is the closest
113 cdist = v1dist;
114 } else { // v2 is the closest
115 cdist = v2dist;
116 }
117 return sqrt(min(adist, min(bdist, cdist)));
118}
119
120
121
122} // namespace MecaCell
123#endif
general purpose 3D vector/point class.
Definition: vector3D.h:20
double sqlength() const
compute the square length of the current vector (faster than length)
Definition: vector3D.h:265
static double rayCast(const Vector3D &o, const Vector3D &n, const Vector3D &p, const Vector3D &r)
simple raycasting on a plane
Definition: vector3D.h:346
const Vector3D cross(const Vector3D &v) const
cross product calculation
Definition: vector3D.h:65
double dot(const Vector3D &v) const
dot product calculation
Definition: vector3D.h:54
this file contains various miscellanious utility functions & helpers *
std::pair< bool, Vec > rayInTriangle(const Vec &v0, const Vec &v1, const Vec &v2, const Vec &o, const Vec &r, const double tolerance=0)
test if a ray hits a triangle
Definition: geometry.hpp:19
std::pair< bool, Vec > projectionIntriangle(const Vec &v0, const Vec &v1, const Vec &v2, const Vec &p, const double tolerance=0)
tests if the projection of a point along the normal of a triangle is inside said triangle
Definition: geometry.hpp:53
double closestDistToTriangleEdge(const Vec &v0, const Vec &v1, const Vec &v2, const Vec &p)
computes the smallest distance to a triangle edge from a given point
Definition: geometry.hpp:79
double sqrt(double x)
Computes the square root of a number.
Definition: std.hpp:32