-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh.cc
More file actions
148 lines (116 loc) · 5.32 KB
/
mesh.cc
File metadata and controls
148 lines (116 loc) · 5.32 KB
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
145
146
147
148
#include "mesh.h"
namespace Engine {
float_max_t Mesh::distanceRayRay (
const Spatial::Vec<3> &ray_1_start,
const Spatial::Vec<3> &ray_1_end,
const Spatial::Vec<3> &ray_2_start,
const Spatial::Vec<3> &ray_2_end,
Spatial::Vec<3> &ray_start,
Spatial::Vec<3> &ray_end
) {
const Spatial::Vec<3>
ray_1_delta = ray_1_end - ray_1_start,
ray_2_delta = ray_2_end - ray_2_start,
rays_delta = ray_1_start - ray_2_start;
const float_max_t
ray_1_size2 = ray_1_delta.length2(),
ray_2_size2 = ray_2_delta.length2();
float_max_t mua = 0.0, mub = 0.0;
if (ray_1_size2 <= Spatial::EPSILON) {
if (ray_2_size2 > Spatial::EPSILON) {
mub = Spatial::clamp(ray_2_delta.dot(rays_delta) / ray_2_size2, 0.0, 1.0);
}
} else {
const float_max_t c = ray_1_delta.dot(rays_delta);
if (ray_2_size2 <= Spatial::EPSILON) {
mua = Spatial::clamp(-c / ray_1_size2, 0.0, 1.0);
} else {
const float_max_t
b = ray_1_delta.dot(ray_2_delta),
denom = ray_1_size2 * ray_2_size2 - b * b,
f = ray_2_delta.dot(rays_delta);
if (denom != 0.0) {
mua = Spatial::clamp((b * f - c * ray_2_size2) / denom, 0.0, 1.0);
}
const float_max_t numer = b * mua + f;
if (numer <= 0.0) {
mua = Spatial::clamp(-c / ray_1_size2, 0.0, 1.0);
} else if (numer >= ray_2_size2) {
mub = 1.0;
mua = Spatial::clamp((b - c) / ray_1_size2, 0.0, 1.0);
} else {
mub = numer / ray_2_size2;
}
}
}
ray_start = ray_1_start + ray_1_delta * mua;
ray_end = ray_2_start + ray_2_delta * mub;
return ray_start.distance(ray_end);
}
bool Mesh::intersectionRectangleRectangle (
const Spatial::Vec<3> &rect_1_top_left,
const Spatial::Vec<3> &rect_1_bottom_left,
const Spatial::Vec<3> &rect_1_bottom_right,
const Spatial::Vec<3> &rect_1_top_right,
const Spatial::Quaternion &rect_1_orientation,
const Spatial::Vec<3> &rect_2_top_left,
const Spatial::Vec<3> &rect_2_bottom_left,
const Spatial::Vec<3> &rect_2_bottom_right,
const Spatial::Vec<3> &rect_2_top_right,
const Spatial::Quaternion &rect_2_orientation,
Spatial::Vec<3> &near_point
) {
if (rect_1_orientation == rect_2_orientation) {
if (rect_1_orientation.isIdentity()) {
return
rect_1_top_left[0] < rect_2_bottom_right[0] &&
rect_1_bottom_right[0] > rect_2_top_left[0] &&
rect_1_top_left[1] < rect_2_bottom_right[1] &&
rect_1_bottom_right[1] > rect_2_top_left[1];
} else {
const Spatial::Quaternion inv_orientation = -rect_1_orientation;
const Spatial::Vec<3>
rot_rect_1_top_left = inv_orientation.rotated(rect_1_top_left),
rot_rect_1_bottom_right = inv_orientation.rotated(rect_1_bottom_right),
rot_rect_2_top_left = inv_orientation.rotated(rect_2_top_left),
rot_rect_2_bottom_right = inv_orientation.rotated(rect_2_bottom_right);
return
rot_rect_1_top_left[0] < rot_rect_2_bottom_right[0] &&
rot_rect_1_bottom_right[0] > rot_rect_2_top_left[0] &&
rot_rect_1_top_left[1] < rot_rect_2_bottom_right[1] &&
rot_rect_1_bottom_right[1] > rot_rect_2_top_left[1];
}
} else {
const std::array<std::array<Spatial::Vec<3>, 2>, 4>
rect_1_edges = edgesRectangle(rect_1_top_left, rect_1_bottom_left, rect_1_bottom_right, rect_1_top_right),
rect_2_edges = edgesRectangle(rect_2_top_left, rect_2_bottom_left, rect_2_bottom_right, rect_2_top_right);
Spatial::Vec<3> ray_end;
for (unsigned i = 0; i < rect_1_edges.size(); ++i) {
for (unsigned j = 0; j < rect_2_edges.size(); ++j) {
if (distanceRayRay(rect_1_edges[i][0], rect_1_edges[i][1], rect_2_edges[j][0], rect_2_edges[j][1], near_point, ray_end) <= Spatial::EPSILON) {
return true;
}
}
}
for (const auto &rect_2_vertex : { rect_2_top_left, rect_2_bottom_left, rect_2_bottom_right, rect_2_top_right }) {
if (intersectionPointRectangle2D(rect_1_top_left, rect_1_bottom_left, rect_1_bottom_right, rect_1_top_right, rect_2_vertex)) {
near_point = rect_2_vertex;
return true;
}
}
}
return false;
}
void Mesh::draw (const bool only_border) const {
Draw::push();
Draw::translate(this->getPosition());
Draw::rotate(this->getOrientation());
this->_draw(only_border);
if (!this->children.empty()) {
for (const auto &mesh : this->children) {
mesh->draw(only_border);
}
}
Draw::pop();
}
};