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