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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/*************************************************************************/
/* face3.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef FACE3_H
#define FACE3_H
#include "vector3.h"
#include "plane.h"
#include "aabb.h"
#include "transform.h"
class Face3 {
public:
enum Side {
SIDE_OVER,
SIDE_UNDER,
SIDE_SPANNING,
SIDE_COPLANAR
};
Vector3 vertex[3];
/**
*
* @param p_plane plane used to split the face
* @param p_res array of at least 3 faces, amount used in functio return
* @param p_is_point_over array of at least 3 booleans, determining which face is over the plane, amount used in functio return
* @param _epsilon constant used for numerical error rounding, to add "thickness" to the plane (so coplanar points can happen)
* @return amount of faces generated by the split, either 0 (means no split possible), 2 or 3
*/
int split_by_plane(const Plane& p_plane,Face3 *p_res,bool *p_is_point_over) const;
Plane get_plane(ClockDirection p_dir=CLOCKWISE) const;
Vector3 get_random_point_inside() const;
Side get_side_of(const Face3& p_face,ClockDirection p_clock_dir=CLOCKWISE) const;
bool is_degenerate() const;
real_t get_area() const;
Vector3 get_median_point() const;
Vector3 get_closest_point_to(const Vector3& p_point) const;
bool intersects_ray(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
bool intersects_segment(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
ClockDirection get_clock_dir() const; ///< todo, test if this is returning the proper clockwisity
void get_support(const Vector3& p_normal,const Transform& p_transform,Vector3 *p_vertices,int* p_count,int p_max) const;
void project_range(const Vector3& p_normal,const Transform& p_transform,float& r_min, float& r_max) const;
Rect3 get_aabb() const {
Rect3 aabb( vertex[0], Vector3() );
aabb.expand_to( vertex[1] );
aabb.expand_to( vertex[2] );
return aabb;
}
bool intersects_aabb(const Rect3& p_aabb) const;
_FORCE_INLINE_ bool intersects_aabb2(const Rect3& p_aabb) const;
operator String() const;
inline Face3() {}
inline Face3(const Vector3 &p_v1,const Vector3 &p_v2,const Vector3 &p_v3) { vertex[0]=p_v1; vertex[1]=p_v2; vertex[2]=p_v3; }
};
bool Face3::intersects_aabb2(const Rect3& p_aabb) const {
Vector3 perp = (vertex[0]-vertex[2]).cross(vertex[0]-vertex[1]);
Vector3 half_extents = p_aabb.size * 0.5;
Vector3 ofs = p_aabb.pos + half_extents;
Vector3 sup =Vector3(
(perp.x>0) ? -half_extents.x : half_extents.x,
(perp.y>0) ? -half_extents.y : half_extents.y,
(perp.z>0) ? -half_extents.z : half_extents.z
);
float d = perp.dot(vertex[0]);
float dist_a = perp.dot(ofs+sup)-d;
float dist_b = perp.dot(ofs-sup)-d;
if (dist_a*dist_b > 0)
return false; //does not intersect the plane
#define TEST_AXIS(m_ax)\
{\
float aabb_min=p_aabb.pos.m_ax;\
float aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
float tri_min,tri_max;\
for (int i=0;i<3;i++) {\
if (i==0 || vertex[i].m_ax > tri_max)\
tri_max=vertex[i].m_ax;\
if (i==0 || vertex[i].m_ax < tri_min)\
tri_min=vertex[i].m_ax;\
}\
\
if (tri_max<aabb_min || aabb_max<tri_min)\
return false;\
}
TEST_AXIS(x);
TEST_AXIS(y);
TEST_AXIS(z);
#undef TEST_AXIS
Vector3 edge_norms[3]={
vertex[0]-vertex[1],
vertex[1]-vertex[2],
vertex[2]-vertex[0],
};
for (int i=0;i<12;i++) {
Vector3 from,to;
switch(i) {
case 0:{
from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
to=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
} break;
case 1:{
from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
} break;
case 2:{
from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
} break;
case 3:{
from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
to=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
} break;
case 4:{
from=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
} break;
case 5:{
from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
} break;
case 6:{
from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
} break;
case 7:{
from=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
} break;
case 8:{
from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
} break;
case 9:{
from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
} break;
case 10:{
from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
} break;
case 11:{
from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
} break;
}
Vector3 e1=from-to;
for (int j=0;j<3;j++) {
Vector3 e2=edge_norms[j];
Vector3 axis=vec3_cross( e1, e2 );
if (axis.length_squared()<0.0001)
continue; // coplanar
//axis.normalize();
Vector3 sup2 =Vector3(
(axis.x>0) ? -half_extents.x : half_extents.x,
(axis.y>0) ? -half_extents.y : half_extents.y,
(axis.z>0) ? -half_extents.z : half_extents.z
);
float maxB = axis.dot(ofs+sup2);
float minB = axis.dot(ofs-sup2);
if (minB>maxB) {
SWAP(maxB,minB);
}
float minT=1e20,maxT=-1e20;
for (int k=0;k<3;k++) {
float d=axis.dot(vertex[k]);
if (d > maxT)
maxT=d;
if (d < minT)
minT=d;
}
if (maxB<minT || maxT<minB)
return false;
}
}
return true;
}
//this sucks...
#endif // FACE3_H
|