2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* face3.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2021-01-02 03:13:46 +08:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#ifndef FACE3_H
|
|
|
|
#define FACE3_H
|
|
|
|
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/math/aabb.h"
|
|
|
|
#include "core/math/plane.h"
|
2021-02-27 12:26:56 +08:00
|
|
|
#include "core/math/transform_3d.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/math/vector3.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
class Face3 {
|
|
|
|
public:
|
2017-03-05 23:44:50 +08:00
|
|
|
enum Side {
|
|
|
|
SIDE_OVER,
|
|
|
|
SIDE_UNDER,
|
|
|
|
SIDE_SPANNING,
|
|
|
|
SIDE_COPLANAR
|
|
|
|
};
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 vertex[3];
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
/**
|
2014-02-10 09:10:30 +08:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int split_by_plane(const Plane &p_plane, Face3 *p_res, bool *p_is_point_over) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Plane get_plane(ClockDirection p_dir = CLOCKWISE) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
Vector3 get_random_point_inside() const;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Side get_side_of(const Face3 &p_face, ClockDirection p_clock_dir = CLOCKWISE) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool is_degenerate() const;
|
2014-02-10 09:10:30 +08:00
|
|
|
real_t get_area() const;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 get_median_point() const;
|
|
|
|
Vector3 get_closest_point_to(const Vector3 &p_point) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-14 16:15:48 +08:00
|
|
|
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const;
|
|
|
|
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClockDirection get_clock_dir() const; ///< todo, test if this is returning the proper clockwisity
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-10-17 13:08:21 +08:00
|
|
|
void get_support(const Vector3 &p_normal, const Transform3D &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const;
|
|
|
|
void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const;
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-11-17 10:09:00 +08:00
|
|
|
AABB get_aabb() const {
|
|
|
|
AABB aabb(vertex[0], Vector3());
|
2017-03-05 23:44:50 +08:00
|
|
|
aabb.expand_to(vertex[1]);
|
|
|
|
aabb.expand_to(vertex[2]);
|
|
|
|
return aabb;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-11-17 10:09:00 +08:00
|
|
|
bool intersects_aabb(const AABB &p_aabb) const;
|
|
|
|
_FORCE_INLINE_ bool intersects_aabb2(const AABB &p_aabb) const;
|
2014-02-10 09:10:30 +08:00
|
|
|
operator String() const;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
2017-11-17 10:09:00 +08:00
|
|
|
bool Face3::intersects_aabb2(const AABB &p_aabb) const {
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
Vector3 half_extents = p_aabb.size * 0.5;
|
2017-06-07 02:33:51 +08:00
|
|
|
Vector3 ofs = p_aabb.position + half_extents;
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
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);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2017-01-15 04:35:39 +08:00
|
|
|
real_t d = perp.dot(vertex[0]);
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t dist_a = perp.dot(ofs + sup) - d;
|
|
|
|
real_t dist_b = perp.dot(ofs - sup) - d;
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (dist_a * dist_b > 0) {
|
2014-06-11 21:41:03 +08:00
|
|
|
return false; //does not intersect the plane
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2017-07-31 04:53:40 +08:00
|
|
|
#define TEST_AXIS(m_ax) \
|
|
|
|
{ \
|
2017-06-07 02:33:51 +08:00
|
|
|
real_t aabb_min = p_aabb.position.m_ax; \
|
|
|
|
real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \
|
2017-07-31 04:53:40 +08:00
|
|
|
real_t 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; \
|
2014-06-11 21:41:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_AXIS(x);
|
|
|
|
TEST_AXIS(y);
|
|
|
|
TEST_AXIS(z);
|
|
|
|
|
|
|
|
#undef TEST_AXIS
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 edge_norms[3] = {
|
|
|
|
vertex[0] - vertex[1],
|
|
|
|
vertex[1] - vertex[2],
|
|
|
|
vertex[2] - vertex[0],
|
2014-06-11 21:41:03 +08:00
|
|
|
};
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
Vector3 from, to;
|
|
|
|
switch (i) {
|
|
|
|
case 0: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
|
|
|
|
to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 1: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
|
|
|
|
to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 2: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
|
|
|
|
to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 3: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
|
|
|
|
to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 4: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
|
|
|
|
to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 5: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
|
|
|
|
to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 6: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
|
|
|
|
to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 7: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
|
|
|
|
to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 8: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
|
|
|
|
to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 9: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
|
|
|
|
to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 10: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
|
|
|
|
to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 11: {
|
2017-06-07 02:33:51 +08:00
|
|
|
from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
|
|
|
|
to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 e1 = from - to;
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
Vector3 e2 = edge_norms[j];
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 axis = vec3_cross(e1, e2);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (axis.length_squared() < 0.0001) {
|
2014-06-11 21:41:03 +08:00
|
|
|
continue; // coplanar
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-06-11 21:41:03 +08:00
|
|
|
//axis.normalize();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
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);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t maxB = axis.dot(ofs + sup2);
|
|
|
|
real_t minB = axis.dot(ofs - sup2);
|
|
|
|
if (minB > maxB) {
|
|
|
|
SWAP(maxB, minB);
|
2014-06-11 21:41:03 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t minT = 1e20, maxT = -1e20;
|
|
|
|
for (int k = 0; k < 3; k++) {
|
2019-02-13 04:10:08 +08:00
|
|
|
real_t vert_d = axis.dot(vertex[k]);
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (vert_d > maxT) {
|
2019-02-13 04:10:08 +08:00
|
|
|
maxT = vert_d;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-06-11 21:41:03 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (vert_d < minT) {
|
2019-02-13 04:10:08 +08:00
|
|
|
minT = vert_d;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-06-11 21:41:03 +08:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (maxB < minT || maxT < minB) {
|
2014-06-11 21:41:03 +08:00
|
|
|
return false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-06-11 21:41:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#endif // FACE3_H
|