Replace QuickHull with Bullet's convex hull computer.

The code is based on the current version of thirdparty/vhacd and modified to use Godot's types and code style.

Additional changes:
- extended PagedAllocator to allow leaked objects
- applied patch from https://github.com/bulletphysics/bullet3/pull/3037
This commit is contained in:
Morris Tabor 2021-05-21 09:58:01 +02:00
parent d5f9f58b61
commit d1bc88d426
10 changed files with 2429 additions and 15 deletions

View File

@ -55,6 +55,14 @@ Comment: Godot Engine logo
Copyright: 2017, Andrea Calabró
License: CC-BY-4.0
Files: ./core/math/convex_hull.cpp
./core/math/convex_hull.h
Comment: Bullet Continuous Collision Detection and Physics Library
Copyright: 2011, Ole Kniemeyer, MAXON, www.maxon.net
2007-2021, Juan Linietsky, Ariel Manzur.
2014-2021, Godot Engine contributors.
License: Expat and Zlib
Files: ./modules/fbx/fbx_parser/
Comment: Open Asset Import Library (FBX parser)
Copyright: 2006-2020, assimp team

2290
core/math/convex_hull.cpp Normal file

File diff suppressed because it is too large Load Diff

112
core/math/convex_hull.h Normal file
View File

@ -0,0 +1,112 @@
/*************************************************************************/
/* convex_hull.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
/*
Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef CONVEX_HULL_H
#define CONVEX_HULL_H
#include "core/math/geometry_3d.h"
#include "core/math/vector3.h"
#include "core/templates/local_vector.h"
#include "core/templates/vector.h"
/// Convex hull implementation based on Preparata and Hong
/// See http://code.google.com/p/bullet/issues/detail?id=275
/// Ole Kniemeyer, MAXON Computer GmbH
class ConvexHullComputer {
public:
class Edge {
private:
int32_t next = 0;
int32_t reverse = 0;
int32_t target_vertex = 0;
friend class ConvexHullComputer;
public:
int32_t get_source_vertex() const {
return (this + reverse)->target_vertex;
}
int32_t get_target_vertex() const {
return target_vertex;
}
const Edge *get_next_edge_of_vertex() const // clockwise list of all edges of a vertex
{
return this + next;
}
const Edge *get_next_edge_of_face() const // counter-clockwise list of all edges of a face
{
return (this + reverse)->get_next_edge_of_vertex();
}
const Edge *get_reverse_edge() const {
return this + reverse;
}
};
// Vertices of the output hull
Vector<Vector3> vertices;
// Edges of the output hull
LocalVector<Edge> edges;
// Faces of the convex hull. Each entry is an index into the "edges" array pointing to an edge of the face. Faces are planar n-gons
LocalVector<int32_t> faces;
/*
Compute convex hull of "count" vertices stored in "coords".
If "shrink" is positive, the convex hull is shrunken by that amount (each face is moved by "shrink" length units
towards the center along its normal).
If "shrinkClamp" is positive, "shrink" is clamped to not exceed "shrinkClamp * innerRadius", where "innerRadius"
is the minimum distance of a face to the center of the convex hull.
The returned value is the amount by which the hull has been shrunken. If it is negative, the amount was so large
that the resulting convex hull is empty.
The output convex hull can be found in the member variables "vertices", "edges", "faces".
*/
real_t compute(const Vector3 *p_coords, int32_t p_count, real_t p_shrink, real_t p_shrink_clamp);
static Error convex_hull(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_mesh);
};
#endif // CONVEX_HULL_H

View File

@ -35,6 +35,8 @@
#include "core/os/spin_lock.h"
#include "core/typedefs.h"
#include <type_traits>
template <class T, bool thread_safe = false>
class PagedAllocator {
T **page_pool = nullptr;
@ -89,8 +91,10 @@ public:
allocs_available++;
}
void reset() {
void reset(bool p_allow_unfreed = false) {
if (!p_allow_unfreed || !std::is_trivially_destructible<T>::value) {
ERR_FAIL_COND(allocs_available < pages_allocated * page_size);
}
if (pages_allocated) {
for (uint32_t i = 0; i < pages_allocated; i++) {
memfree(page_pool[i]);

View File

@ -30,9 +30,9 @@
#include "node_3d_editor_gizmos.h"
#include "core/math/convex_hull.h"
#include "core/math/geometry_2d.h"
#include "core/math/geometry_3d.h"
#include "core/math/quick_hull.h"
#include "scene/3d/audio_stream_player_3d.h"
#include "scene/3d/baked_lightmap.h"
#include "scene/3d/collision_polygon_3d.h"
@ -4161,7 +4161,7 @@ void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
if (points.size() > 3) {
Vector<Vector3> varr = Variant(points);
Geometry3D::MeshData md;
Error err = QuickHull::build(varr, md);
Error err = ConvexHullComputer::convex_hull(varr, md);
if (err == OK) {
Vector<Vector3> points2;
points2.resize(md.edges.size() * 2);

View File

@ -32,7 +32,7 @@
#include "navigation_mesh_generator.h"
#include "core/math/quick_hull.h"
#include "core/math/convex_hull.h"
#include "core/os/thread.h"
#include "scene/3d/collision_shape_3d.h"
#include "scene/3d/mesh_instance_3d.h"
@ -220,7 +220,7 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform,
Vector<Vector3> varr = Variant(convex_polygon->get_points());
Geometry3D::MeshData md;
Error err = QuickHull::build(varr, md);
Error err = ConvexHullComputer::convex_hull(varr, md);
if (err == OK) {
PackedVector3Array faces;

View File

@ -29,7 +29,7 @@
/*************************************************************************/
#include "convex_polygon_shape_3d.h"
#include "core/math/quick_hull.h"
#include "core/math/convex_hull.h"
#include "servers/physics_server_3d.h"
Vector<Vector3> ConvexPolygonShape3D::get_debug_mesh_lines() const {
@ -38,7 +38,7 @@ Vector<Vector3> ConvexPolygonShape3D::get_debug_mesh_lines() const {
if (points.size() > 3) {
Vector<Vector3> varr = Variant(points);
Geometry3D::MeshData md;
Error err = QuickHull::build(varr, md);
Error err = ConvexHullComputer::convex_hull(varr, md);
if (err == OK) {
Vector<Vector3> lines;
lines.resize(md.edges.size() * 2);

View File

@ -31,8 +31,8 @@
#include "shape_3d_sw.h"
#include "core/io/image.h"
#include "core/math/convex_hull.h"
#include "core/math/geometry_3d.h"
#include "core/math/quick_hull.h"
#include "core/templates/sort_array.h"
// HeightMapShape3DSW is based on Bullet btHeightfieldTerrainShape.
@ -1089,9 +1089,9 @@ Vector3 ConvexPolygonShape3DSW::get_moment_of_inertia(real_t p_mass) const {
}
void ConvexPolygonShape3DSW::_setup(const Vector<Vector3> &p_vertices) {
Error err = QuickHull::build(p_vertices, mesh);
Error err = ConvexHullComputer::convex_hull(p_vertices, mesh);
if (err != OK) {
ERR_PRINT("Failed to build QuickHull");
ERR_PRINT("Failed to build convex hull");
}
AABB _aabb;

View File

@ -30,8 +30,8 @@
#include "test_physics_3d.h"
#include "core/math/convex_hull.h"
#include "core/math/math_funcs.h"
#include "core/math/quick_hull.h"
#include "core/os/main_loop.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
@ -169,7 +169,7 @@ protected:
RID convex_mesh = vs->mesh_create();
Geometry3D::MeshData convex_data = Geometry3D::build_convex_mesh(convex_planes);
QuickHull::build(convex_data.vertices, convex_data);
ConvexHullComputer::convex_hull(convex_data.vertices, convex_data);
vs->mesh_add_surface_from_mesh_data(convex_mesh, convex_data);
type_mesh_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_mesh;

View File

@ -30,8 +30,8 @@
#include "test_render.h"
#include "core/math/convex_hull.h"
#include "core/math/math_funcs.h"
#include "core/math/quick_hull.h"
#include "core/os/keyboard.h"
#include "core/os/main_loop.h"
#include "core/os/os.h"
@ -118,7 +118,7 @@ public:
vts.push_back(Vector3(-1, -1, -1));
Geometry3D::MeshData md;
Error err = QuickHull::build(vts, md);
Error err = ConvexHullComputer::convex_hull(vts, md);
print_line("ERR: " + itos(err));
test_cube = vs->mesh_create();
vs->mesh_add_surface_from_mesh_data(test_cube, md);