/
Face.cpp
144 lines (112 loc) · 3.49 KB
/
Face.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "Face.h"
#include "HalfEdge.h"
#include "Vertex.h"
#include "BoundingBox.h"
#define EPSILON 1e-6
bool Face::isBoundary() const
{
return he->onBoundary;
}
double Face::area() const
{
if (isBoundary()) {
return 0;
}
return 0.5 * normal().norm();
}
Eigen::Vector3d Face::normal() const
{
const Eigen::Vector3d& a(he->vertex->position);
const Eigen::Vector3d& b(he->next->vertex->position);
const Eigen::Vector3d& c(he->next->next->vertex->position);
Eigen::Vector3d v1 = b - a;
Eigen::Vector3d v2 = c - a;
return v1.cross(v2);
}
BoundingBox Face::boundingBox() const
{
if (isBoundary()) {
return BoundingBox(he->vertex->position, he->next->vertex->position);
}
const Eigen::Vector3d& p1(he->vertex->position);
const Eigen::Vector3d& p2(he->next->vertex->position);
const Eigen::Vector3d& p3(he->next->next->vertex->position);
Eigen::Vector3d min = p1;
Eigen::Vector3d max = p1;
if (p2.x() < min.x()) min.x() = p2.x();
if (p3.x() < min.x()) min.x() = p3.x();
if (p2.y() < min.y()) min.y() = p2.y();
if (p3.y() < min.y()) min.y() = p3.y();
if (p2.z() < min.z()) min.z() = p2.z();
if (p3.z() < min.z()) min.z() = p3.z();
if (p2.x() > max.x()) max.x() = p2.x();
if (p3.x() > max.x()) max.x() = p3.x();
if (p2.y() > max.y()) max.y() = p2.y();
if (p3.y() > max.y()) max.y() = p3.y();
if (p2.z() > max.z()) max.z() = p2.z();
if (p3.z() > max.z()) max.z() = p3.z();
return BoundingBox(min, max);
}
Eigen::Vector3d Face::centroid() const
{
Eigen::Vector3d centroid;
if (isBoundary()) {
centroid = (he->vertex->position +
he->next->vertex->position) / 2.0;
} else {
centroid = (he->vertex->position +
he->next->vertex->position +
he->next->next->vertex->position) / 3.0;
}
return centroid;
}
bool sameDirection(const Eigen::Vector3d& v1, const Eigen::Vector3d& v2)
{
double c = v1.dot(v2);
if (c < -1.0) c = -1.0;
else if (c > 1.0) c = 1.0;
double angle = acos(c) * 180.0 / M_PI;
if (angle <= 90.0) {
return true;
}
return false;
}
double Face::distance(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction) const
{
// check for false intersection with the outside of the mesh
if (!sameDirection(direction, normal().normalized())) {
return INFINITY;
}
// Möller–Trumbore intersection algorithm
const Eigen::Vector3d& p1(he->vertex->position);
const Eigen::Vector3d& p2(he->next->vertex->position);
const Eigen::Vector3d& p3(he->next->next->vertex->position);
Eigen::Vector3d e1 = p2 - p1;
Eigen::Vector3d e2 = p3 - p1;
Eigen::Vector3d n = direction.cross(e2);
double det = e1.dot(n);
// ray does not lie in the plane
if (det > -EPSILON && det < EPSILON) {
return INFINITY;
}
double invDet = 1.0 / det;
Eigen::Vector3d t = origin - p1;
double u = t.dot(n) * invDet;
// ray lies outside triangle
if (u < 0.0 || u > 1.0) {
return INFINITY;
}
Eigen::Vector3d q = t.cross(e1);
double v = direction.dot(q) * invDet;
// ray lies outside the triangle
if (v < 0.0 || v + u > 1.0) {
return INFINITY;
}
double s = e2.dot(q) * invDet;
// intersection
if (s > EPSILON) {
return s;
}
// no hit
return INFINITY;
}