Merge pull request #48533 from mortarroad/3.x-convex-hull-ported
[3.x] Port Bullet's convex hull computer to replace of QuickHull
This commit is contained in:
commit
ac3417005d
@ -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
2290
core/math/convex_hull.cpp
Normal file
File diff suppressed because it is too large
Load Diff
116
core/math/convex_hull.h
Normal file
116
core/math/convex_hull.h
Normal file
@ -0,0 +1,116 @@
|
||||
/*************************************************************************/
|
||||
/* 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/local_vector.h"
|
||||
#include "core/math/geometry.h"
|
||||
#include "core/math/vector3.h"
|
||||
#include "core/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, Geometry::MeshData &r_mesh);
|
||||
};
|
||||
|
||||
#endif // CONVEX_HULL_H
|
51
core/os/spin_lock.h
Normal file
51
core/os/spin_lock.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*************************************************************************/
|
||||
/* spin_lock.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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef SPIN_LOCK_H
|
||||
#define SPIN_LOCK_H
|
||||
|
||||
#include "core/typedefs.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
class SpinLock {
|
||||
std::atomic_flag locked = ATOMIC_FLAG_INIT;
|
||||
|
||||
public:
|
||||
_ALWAYS_INLINE_ void lock() {
|
||||
while (locked.test_and_set(std::memory_order_acquire)) {
|
||||
;
|
||||
}
|
||||
}
|
||||
_ALWAYS_INLINE_ void unlock() {
|
||||
locked.clear(std::memory_order_release);
|
||||
}
|
||||
};
|
||||
#endif // SPIN_LOCK_H
|
133
core/paged_allocator.h
Normal file
133
core/paged_allocator.h
Normal file
@ -0,0 +1,133 @@
|
||||
/*************************************************************************/
|
||||
/* paged_allocator.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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef PAGED_ALLOCATOR_H
|
||||
#define PAGED_ALLOCATOR_H
|
||||
|
||||
#include "core/os/memory.h"
|
||||
#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;
|
||||
T ***available_pool = nullptr;
|
||||
uint32_t pages_allocated = 0;
|
||||
uint32_t allocs_available = 0;
|
||||
|
||||
uint32_t page_shift = 0;
|
||||
uint32_t page_mask = 0;
|
||||
uint32_t page_size = 0;
|
||||
SpinLock spin_lock;
|
||||
|
||||
public:
|
||||
T *alloc() {
|
||||
if (thread_safe) {
|
||||
spin_lock.lock();
|
||||
}
|
||||
if (unlikely(allocs_available == 0)) {
|
||||
uint32_t pages_used = pages_allocated;
|
||||
|
||||
pages_allocated++;
|
||||
page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated);
|
||||
available_pool = (T ***)memrealloc(available_pool, sizeof(T **) * pages_allocated);
|
||||
|
||||
page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size);
|
||||
available_pool[pages_used] = (T **)memalloc(sizeof(T *) * page_size);
|
||||
|
||||
for (uint32_t i = 0; i < page_size; i++) {
|
||||
available_pool[0][i] = &page_pool[pages_used][i];
|
||||
}
|
||||
allocs_available += page_size;
|
||||
}
|
||||
|
||||
allocs_available--;
|
||||
T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask];
|
||||
if (thread_safe) {
|
||||
spin_lock.unlock();
|
||||
}
|
||||
memnew_placement(alloc, T);
|
||||
return alloc;
|
||||
}
|
||||
|
||||
void free(T *p_mem) {
|
||||
if (thread_safe) {
|
||||
spin_lock.lock();
|
||||
}
|
||||
p_mem->~T();
|
||||
available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem;
|
||||
if (thread_safe) {
|
||||
spin_lock.unlock();
|
||||
}
|
||||
allocs_available++;
|
||||
}
|
||||
|
||||
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]);
|
||||
memfree(available_pool[i]);
|
||||
}
|
||||
memfree(page_pool);
|
||||
memfree(available_pool);
|
||||
page_pool = nullptr;
|
||||
available_pool = nullptr;
|
||||
pages_allocated = 0;
|
||||
allocs_available = 0;
|
||||
}
|
||||
}
|
||||
bool is_configured() const {
|
||||
return page_size > 0;
|
||||
}
|
||||
|
||||
void configure(uint32_t p_page_size) {
|
||||
ERR_FAIL_COND(page_pool != nullptr); //sanity check
|
||||
ERR_FAIL_COND(p_page_size == 0);
|
||||
page_size = nearest_power_of_2_templated(p_page_size);
|
||||
page_mask = page_size - 1;
|
||||
page_shift = get_shift_from_power_of_2(page_size);
|
||||
}
|
||||
|
||||
PagedAllocator(uint32_t p_page_size = 4096) { // power of 2 recommended because of alignment with OS page sizes. Even if element is bigger, its still a multiple and get rounded amount of pages
|
||||
configure(p_page_size);
|
||||
}
|
||||
|
||||
~PagedAllocator() {
|
||||
ERR_FAIL_COND_MSG(allocs_available < pages_allocated * page_size, "Pages in use exist at exit in PagedAllocator");
|
||||
reset();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // PAGED_ALLOCATOR_H
|
@ -30,8 +30,8 @@
|
||||
|
||||
#include "spatial_editor_gizmos.h"
|
||||
|
||||
#include "core/math/convex_hull.h"
|
||||
#include "core/math/geometry.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.h"
|
||||
@ -3550,7 +3550,7 @@ void CollisionShapeSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
|
||||
if (points.size() > 3) {
|
||||
Vector<Vector3> varr = Variant(points);
|
||||
Geometry::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);
|
||||
|
@ -31,8 +31,8 @@
|
||||
#include "test_physics.h"
|
||||
|
||||
#include "core/map.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/print_string.h"
|
||||
@ -168,7 +168,7 @@ protected:
|
||||
|
||||
RID convex_mesh = vs->mesh_create();
|
||||
Geometry::MeshData convex_data = Geometry::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[PhysicsServer::SHAPE_CONVEX_POLYGON] = convex_mesh;
|
||||
|
@ -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));
|
||||
|
||||
Geometry::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);
|
||||
|
@ -29,7 +29,7 @@
|
||||
/*************************************************************************/
|
||||
|
||||
#include "navigation_mesh_generator.h"
|
||||
#include "core/math/quick_hull.h"
|
||||
#include "core/math/convex_hull.h"
|
||||
#include "core/os/thread.h"
|
||||
#include "editor/editor_settings.h"
|
||||
#include "scene/3d/collision_shape.h"
|
||||
@ -213,7 +213,7 @@ void EditorNavigationMeshGenerator::_parse_geometry(Transform p_accumulated_tran
|
||||
Vector<Vector3> varr = Variant(convex_polygon->get_points());
|
||||
Geometry::MeshData md;
|
||||
|
||||
Error err = QuickHull::build(varr, md);
|
||||
Error err = ConvexHullComputer::convex_hull(varr, md);
|
||||
|
||||
if (err == OK) {
|
||||
PoolVector3Array faces;
|
||||
|
@ -29,7 +29,7 @@
|
||||
/*************************************************************************/
|
||||
|
||||
#include "convex_polygon_shape.h"
|
||||
#include "core/math/quick_hull.h"
|
||||
#include "core/math/convex_hull.h"
|
||||
#include "servers/physics_server.h"
|
||||
|
||||
Vector<Vector3> ConvexPolygonShape::get_debug_mesh_lines() {
|
||||
@ -38,7 +38,7 @@ Vector<Vector3> ConvexPolygonShape::get_debug_mesh_lines() {
|
||||
if (points.size() > 3) {
|
||||
Vector<Vector3> varr = Variant(points);
|
||||
Geometry::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);
|
||||
|
@ -31,8 +31,8 @@
|
||||
#include "shape_sw.h"
|
||||
|
||||
#include "core/image.h"
|
||||
#include "core/math/convex_hull.h"
|
||||
#include "core/math/geometry.h"
|
||||
#include "core/math/quick_hull.h"
|
||||
#include "core/sort_array.h"
|
||||
|
||||
// HeightMapShapeSW is based on Bullet btHeightfieldTerrainShape.
|
||||
@ -1090,9 +1090,9 @@ Vector3 ConvexPolygonShapeSW::get_moment_of_inertia(real_t p_mass) const {
|
||||
}
|
||||
|
||||
void ConvexPolygonShapeSW::_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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user