virtualx-engine/core/math/quick_hull.cpp

463 lines
12 KiB
C++
Raw Normal View History

/**************************************************************************/
/* quick_hull.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 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. */
/**************************************************************************/
2014-02-10 02:10:30 +01:00
#include "quick_hull.h"
#include "core/templates/rb_map.h"
2014-02-10 02:10:30 +01:00
uint32_t QuickHull::debug_stop_after = 0xFFFFFFFF;
2014-02-10 02:10:30 +01:00
Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_mesh) {
2014-02-10 02:10:30 +01:00
/* CREATE AABB VOLUME */
2017-11-17 03:09:00 +01:00
AABB aabb;
for (int i = 0; i < p_points.size(); i++) {
if (i == 0) {
aabb.position = p_points[i];
2014-02-10 02:10:30 +01:00
} else {
aabb.expand_to(p_points[i]);
}
}
if (aabb.size == Vector3()) {
2014-02-10 02:10:30 +01:00
return ERR_CANT_CREATE;
}
Vector<bool> valid_points;
valid_points.resize(p_points.size());
HashSet<Vector3> valid_cache;
2014-02-10 02:10:30 +01:00
for (int i = 0; i < p_points.size(); i++) {
Vector3 sp = p_points[i].snappedf(0.0001);
2014-02-10 02:10:30 +01:00
if (valid_cache.has(sp)) {
valid_points.write[i] = false;
} else {
valid_points.write[i] = true;
2014-02-10 02:10:30 +01:00
valid_cache.insert(sp);
}
}
/* CREATE INITIAL SIMPLEX */
int longest_axis = aabb.get_longest_axis_index();
//first two vertices are the most distant
int simplex[4] = { 0 };
2014-02-10 02:10:30 +01:00
{
real_t max = 0, min = 0;
2014-02-10 02:10:30 +01:00
for (int i = 0; i < p_points.size(); i++) {
if (!valid_points[i]) {
2014-02-10 02:10:30 +01:00
continue;
}
real_t d = p_points[i][longest_axis];
if (i == 0 || d < min) {
simplex[0] = i;
min = d;
2014-02-10 02:10:30 +01:00
}
if (i == 0 || d > max) {
simplex[1] = i;
max = d;
2014-02-10 02:10:30 +01:00
}
}
}
//third vertex is one most further away from the line
{
real_t maxd = 0;
Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]];
2014-02-10 02:10:30 +01:00
for (int i = 0; i < p_points.size(); i++) {
if (!valid_points[i]) {
2014-02-10 02:10:30 +01:00
continue;
}
2014-02-10 02:10:30 +01:00
Vector3 n = rel12.cross(p_points[simplex[0]] - p_points[i]).cross(rel12).normalized();
real_t d = Math::abs(n.dot(p_points[simplex[0]]) - n.dot(p_points[i]));
2014-02-10 02:10:30 +01:00
if (i == 0 || d > maxd) {
maxd = d;
simplex[2] = i;
2014-02-10 02:10:30 +01:00
}
}
}
//fourth vertex is the one most further away from the plane
2014-02-10 02:10:30 +01:00
{
real_t maxd = 0;
Plane p(p_points[simplex[0]], p_points[simplex[1]], p_points[simplex[2]]);
2014-02-10 02:10:30 +01:00
for (int i = 0; i < p_points.size(); i++) {
if (!valid_points[i]) {
2014-02-10 02:10:30 +01:00
continue;
}
2014-02-10 02:10:30 +01:00
real_t d = Math::abs(p.distance_to(p_points[i]));
if (i == 0 || d > maxd) {
maxd = d;
simplex[3] = i;
2014-02-10 02:10:30 +01:00
}
}
}
//compute center of simplex, this is a point always warranted to be inside
Vector3 center;
for (int i = 0; i < 4; i++) {
center += p_points[simplex[i]];
2014-02-10 02:10:30 +01:00
}
center /= 4.0;
2014-02-10 02:10:30 +01:00
//add faces
List<Face> faces;
for (int i = 0; i < 4; i++) {
static const int face_order[4][3] = {
{ 0, 1, 2 },
{ 0, 1, 3 },
{ 0, 2, 3 },
{ 1, 2, 3 }
2014-02-10 02:10:30 +01:00
};
Face f;
for (int j = 0; j < 3; j++) {
f.vertices[j] = simplex[face_order[i][j]];
2014-02-10 02:10:30 +01:00
}
Plane p(p_points[f.vertices[0]], p_points[f.vertices[1]], p_points[f.vertices[2]]);
2014-02-10 02:10:30 +01:00
if (p.is_point_over(center)) {
//flip face to clockwise if facing inwards
SWAP(f.vertices[0], f.vertices[1]);
p = -p;
2014-02-10 02:10:30 +01:00
}
f.plane = p;
faces.push_back(f);
}
real_t over_tolerance = 3 * UNIT_EPSILON * (aabb.size.x + aabb.size.y + aabb.size.z);
2014-02-10 02:10:30 +01:00
/* COMPUTE AVAILABLE VERTICES */
for (int i = 0; i < p_points.size(); i++) {
if (i == simplex[0]) {
2014-02-10 02:10:30 +01:00
continue;
}
if (i == simplex[1]) {
2014-02-10 02:10:30 +01:00
continue;
}
if (i == simplex[2]) {
2014-02-10 02:10:30 +01:00
continue;
}
if (i == simplex[3]) {
2014-02-10 02:10:30 +01:00
continue;
}
if (!valid_points[i]) {
2014-02-10 02:10:30 +01:00
continue;
}
2014-02-10 02:10:30 +01:00
for (Face &E : faces) {
if (E.plane.distance_to(p_points[i]) > over_tolerance) {
E.points_over.push_back(i);
2014-02-10 02:10:30 +01:00
break;
}
}
}
faces.sort(); // sort them, so the ones with points are in the back
/* BUILD HULL */
//poop face (while still remain)
//find further away point
//find lit faces
//determine horizon edges
//build new faces with horizon edges, them assign points side from all lit faces
//remove lit faces
uint32_t debug_stop = debug_stop_after;
while (debug_stop > 0 && faces.back()->get().points_over.size()) {
2014-02-10 02:10:30 +01:00
debug_stop--;
Face &f = faces.back()->get();
2014-02-10 02:10:30 +01:00
//find vertex most outside
int next = -1;
real_t next_d = 0;
2014-02-10 02:10:30 +01:00
for (int i = 0; i < f.points_over.size(); i++) {
2014-02-10 02:10:30 +01:00
real_t d = f.plane.distance_to(p_points[f.points_over[i]]);
if (d > next_d) {
next_d = d;
next = i;
2014-02-10 02:10:30 +01:00
}
}
ERR_FAIL_COND_V(next == -1, ERR_BUG);
2014-02-10 02:10:30 +01:00
Vector3 v = p_points[f.points_over[next]];
//find lit faces and lit edges
List<List<Face>::Element *> lit_faces; //lit face is a death sentence
2014-02-10 02:10:30 +01:00
HashMap<Edge, FaceConnect, Edge> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot
2014-02-10 02:10:30 +01:00
for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
if (E->get().plane.distance_to(v) > 0) {
2014-02-10 02:10:30 +01:00
lit_faces.push_back(E);
for (int i = 0; i < 3; i++) {
2014-02-10 02:10:30 +01:00
uint32_t a = E->get().vertices[i];
uint32_t b = E->get().vertices[(i + 1) % 3];
Edge e(a, b);
2014-02-10 02:10:30 +01:00
HashMap<Edge, FaceConnect, Edge>::Iterator F = lit_edges.find(e);
2014-02-10 02:10:30 +01:00
if (!F) {
F = lit_edges.insert(e, FaceConnect());
2014-02-10 02:10:30 +01:00
}
if (e.vertices[0] == a) {
2014-02-10 02:10:30 +01:00
//left
F->value.left = E;
2014-02-10 02:10:30 +01:00
} else {
F->value.right = E;
2014-02-10 02:10:30 +01:00
}
}
}
}
//create new faces from horizon edges
List<List<Face>::Element *> new_faces; //new faces
2014-02-10 02:10:30 +01:00
2021-08-09 22:13:42 +02:00
for (KeyValue<Edge, FaceConnect> &E : lit_edges) {
FaceConnect &fc = E.value;
2014-02-10 02:10:30 +01:00
if (fc.left && fc.right) {
continue; //edge is uninteresting, not on horizon
2014-02-10 02:10:30 +01:00
}
//create new face!
Face face;
face.vertices[0] = f.points_over[next];
2021-08-09 22:13:42 +02:00
face.vertices[1] = E.key.vertices[0];
face.vertices[2] = E.key.vertices[1];
2014-02-10 02:10:30 +01:00
Plane p(p_points[face.vertices[0]], p_points[face.vertices[1]], p_points[face.vertices[2]]);
2014-02-10 02:10:30 +01:00
if (p.is_point_over(center)) {
//flip face to clockwise if facing inwards
SWAP(face.vertices[0], face.vertices[1]);
2014-02-10 02:10:30 +01:00
p = -p;
}
face.plane = p;
new_faces.push_back(faces.push_back(face));
2014-02-10 02:10:30 +01:00
}
//distribute points into new faces
for (List<Face>::Element *&F : lit_faces) {
Face &lf = F->get();
2014-02-10 02:10:30 +01:00
for (int i = 0; i < lf.points_over.size(); i++) {
if (lf.points_over[i] == f.points_over[next]) { //do not add current one
2014-02-10 02:10:30 +01:00
continue;
}
2014-02-10 02:10:30 +01:00
Vector3 p = p_points[lf.points_over[i]];
for (List<Face>::Element *&E : new_faces) {
Face &f2 = E->get();
if (f2.plane.distance_to(p) > over_tolerance) {
2014-02-10 02:10:30 +01:00
f2.points_over.push_back(lf.points_over[i]);
break;
}
}
}
}
//erase lit faces
while (lit_faces.size()) {
2014-02-10 02:10:30 +01:00
faces.erase(lit_faces.front()->get());
lit_faces.pop_front();
}
//put faces that contain no points on the front
for (List<Face>::Element *&E : new_faces) {
Face &f2 = E->get();
if (f2.points_over.size() == 0) {
faces.move_to_front(E);
2014-02-10 02:10:30 +01:00
}
}
//whew, done with iteration, go next
}
/* CREATE MESHDATA */
//make a map of edges again
HashMap<Edge, RetFaceConnect, Edge> ret_edges;
List<Geometry3D::MeshData::Face> ret_faces;
2014-02-10 02:10:30 +01:00
for (const Face &E : faces) {
Geometry3D::MeshData::Face f;
f.plane = E.plane;
2014-02-10 02:10:30 +01:00
for (int i = 0; i < 3; i++) {
f.indices.push_back(E.vertices[i]);
2014-02-10 02:10:30 +01:00
}
List<Geometry3D::MeshData::Face>::Element *F = ret_faces.push_back(f);
2014-02-10 02:10:30 +01:00
for (int i = 0; i < 3; i++) {
uint32_t a = E.vertices[i];
uint32_t b = E.vertices[(i + 1) % 3];
Edge e(a, b);
2014-02-10 02:10:30 +01:00
HashMap<Edge, RetFaceConnect, Edge>::Iterator G = ret_edges.find(e);
2014-02-10 02:10:30 +01:00
if (!G) {
G = ret_edges.insert(e, RetFaceConnect());
2014-02-10 02:10:30 +01:00
}
if (e.vertices[0] == a) {
2014-02-10 02:10:30 +01:00
//left
G->value.left = F;
2014-02-10 02:10:30 +01:00
} else {
G->value.right = F;
2014-02-10 02:10:30 +01:00
}
}
}
//fill faces
for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
Geometry3D::MeshData::Face &f = E->get();
2014-02-10 02:10:30 +01:00
for (uint32_t i = 0; i < f.indices.size(); i++) {
int a = E->get().indices[i];
int b = E->get().indices[(i + 1) % f.indices.size()];
Edge e(a, b);
2014-02-10 02:10:30 +01:00
HashMap<Edge, RetFaceConnect, Edge>::Iterator F = ret_edges.find(e);
2014-02-10 02:10:30 +01:00
ERR_CONTINUE(!F);
List<Geometry3D::MeshData::Face>::Element *O = F->value.left == E ? F->value.right : F->value.left;
ERR_CONTINUE(O == E);
2020-04-02 01:20:12 +02:00
ERR_CONTINUE(O == nullptr);
2014-02-10 02:10:30 +01:00
if (O->get().plane.is_equal_approx(f.plane)) {
2014-02-10 02:10:30 +01:00
//merge and delete edge and contiguous face, while repointing edges (uuugh!)
int o_index_size = O->get().indices.size();
2014-02-10 02:10:30 +01:00
for (int j = 0; j < o_index_size; j++) {
2014-02-10 02:10:30 +01:00
//search a
if (O->get().indices[j] == a) {
2014-02-10 02:10:30 +01:00
//append the rest
for (int k = 0; k < o_index_size; k++) {
int idx = O->get().indices[(k + j) % o_index_size];
int idxn = O->get().indices[(k + j + 1) % o_index_size];
if (idx == b && idxn == a) { //already have b!
2014-02-10 02:10:30 +01:00
break;
}
if (idx != a) {
f.indices.insert(i + 1, idx);
2014-02-10 02:10:30 +01:00
i++;
}
Edge e2(idx, idxn);
2014-02-10 02:10:30 +01:00
HashMap<Edge, RetFaceConnect, Edge>::Iterator F2 = ret_edges.find(e2);
2014-02-10 02:10:30 +01:00
ERR_CONTINUE(!F2);
//change faceconnect, point to this face instead
if (F2->value.left == O) {
F2->value.left = E;
} else if (F2->value.right == O) {
F2->value.right = E;
}
2014-02-10 02:10:30 +01:00
}
break;
}
}
// remove all edge connections to this face
2021-08-09 22:13:42 +02:00
for (KeyValue<Edge, RetFaceConnect> &G : ret_edges) {
if (G.value.left == O) {
G.value.left = nullptr;
}
2021-08-09 22:13:42 +02:00
if (G.value.right == O) {
G.value.right = nullptr;
}
}
ret_edges.remove(F); //remove the edge
2014-02-10 02:10:30 +01:00
ret_faces.erase(O); //remove the face
}
}
}
//fill mesh
r_mesh.faces.clear();
r_mesh.faces.resize(ret_faces.size());
HashMap<List<Geometry3D::MeshData::Face>::Element *, int> face_indices;
int idx = 0;
for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
face_indices[E] = idx;
r_mesh.faces[idx++] = E->get();
2014-02-10 02:10:30 +01:00
}
r_mesh.edges.resize(ret_edges.size());
idx = 0;
2021-08-09 22:13:42 +02:00
for (const KeyValue<Edge, RetFaceConnect> &E : ret_edges) {
Geometry3D::MeshData::Edge e;
e.vertex_a = E.key.vertices[0];
e.vertex_b = E.key.vertices[1];
ERR_CONTINUE(!face_indices.has(E.value.left));
ERR_CONTINUE(!face_indices.has(E.value.right));
e.face_a = face_indices[E.value.left];
e.face_b = face_indices[E.value.right];
r_mesh.edges[idx++] = e;
2014-02-10 02:10:30 +01:00
}
r_mesh.vertices = p_points;
2014-02-10 02:10:30 +01:00
return OK;
}