1/**************************************************************************/
2/* convex_hull.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef CONVEX_HULL_H
32#define CONVEX_HULL_H
33
34/*
35Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
36This software is provided 'as-is', without any express or implied warranty.
37In no event will the authors be held liable for any damages arising from the use of this software.
38Permission is granted to anyone to use this software for any purpose,
39including commercial applications, and to alter it and redistribute it freely,
40subject to the following restrictions:
411. 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.
422. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
433. This notice may not be removed or altered from any source distribution.
44*/
45
46#include "core/math/geometry_3d.h"
47#include "core/math/vector3.h"
48#include "core/templates/local_vector.h"
49#include "core/templates/vector.h"
50
51/// Convex hull implementation based on Preparata and Hong
52/// See https://code.google.com/archive/p/bullet/issues/275
53/// Ole Kniemeyer, MAXON Computer GmbH
54class ConvexHullComputer {
55public:
56 class Edge {
57 private:
58 int32_t next = 0;
59 int32_t reverse = 0;
60 int32_t target_vertex = 0;
61
62 friend class ConvexHullComputer;
63
64 public:
65 int32_t get_next_relative() const {
66 return next;
67 }
68
69 int32_t get_source_vertex() const {
70 return (this + reverse)->target_vertex;
71 }
72
73 int32_t get_target_vertex() const {
74 return target_vertex;
75 }
76
77 const Edge *get_next_edge_of_vertex() const // clockwise list of all edges of a vertex
78 {
79 return this + next;
80 }
81
82 const Edge *get_next_edge_of_face() const // counter-clockwise list of all edges of a face
83 {
84 return (this + reverse)->get_next_edge_of_vertex();
85 }
86
87 const Edge *get_reverse_edge() const {
88 return this + reverse;
89 }
90 };
91
92 // Vertices of the output hull
93 LocalVector<Vector3> vertices;
94
95 // Edges of the output hull
96 LocalVector<Edge> edges;
97
98 // 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
99 LocalVector<int32_t> faces;
100
101 /*
102 Compute convex hull of "count" vertices stored in "coords".
103 If "shrink" is positive, the convex hull is shrunken by that amount (each face is moved by "shrink" length units
104 towards the center along its normal).
105 If "shrinkClamp" is positive, "shrink" is clamped to not exceed "shrinkClamp * innerRadius", where "innerRadius"
106 is the minimum distance of a face to the center of the convex hull.
107 The returned value is the amount by which the hull has been shrunken. If it is negative, the amount was so large
108 that the resulting convex hull is empty.
109 The output convex hull can be found in the member variables "vertices", "edges", "faces".
110 */
111 real_t compute(const Vector3 *p_coords, int32_t p_count, real_t p_shrink, real_t p_shrink_clamp);
112
113 static Error convex_hull(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_mesh);
114};
115
116#endif // CONVEX_HULL_H
117