/**************************************************************************/1/* convex_hull.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132/*33Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net34This software is provided 'as-is', without any express or implied warranty.35In no event will the authors be held liable for any damages arising from the use of this software.36Permission is granted to anyone to use this software for any purpose,37including commercial applications, and to alter it and redistribute it freely,38subject to the following restrictions:391. 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.402. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.413. This notice may not be removed or altered from any source distribution.42*/4344#include "core/math/geometry_3d.h"45#include "core/math/vector3.h"46#include "core/templates/local_vector.h"47#include "core/templates/vector.h"4849/// Convex hull implementation based on Preparata and Hong50/// See https://code.google.com/archive/p/bullet/issues/27551/// Ole Kniemeyer, MAXON Computer GmbH52class ConvexHullComputer {53public:54class Edge {55private:56int32_t next = 0;57int32_t reverse = 0;58int32_t target_vertex = 0;5960friend class ConvexHullComputer;6162public:63int32_t get_next_relative() const {64return next;65}6667int32_t get_source_vertex() const {68return (this + reverse)->target_vertex;69}7071int32_t get_target_vertex() const {72return target_vertex;73}7475const Edge *get_next_edge_of_vertex() const // clockwise list of all edges of a vertex76{77return this + next;78}7980const Edge *get_next_edge_of_face() const // counter-clockwise list of all edges of a face81{82return (this + reverse)->get_next_edge_of_vertex();83}8485const Edge *get_reverse_edge() const {86return this + reverse;87}88};8990// Vertices of the output hull91LocalVector<Vector3> vertices;9293// Edges of the output hull94LocalVector<Edge> edges;9596// 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-gons97LocalVector<int32_t> faces;9899/*100Compute convex hull of "count" vertices stored in "coords".101If "shrink" is positive, the convex hull is shrunken by that amount (each face is moved by "shrink" length units102towards the center along its normal).103If "shrinkClamp" is positive, "shrink" is clamped to not exceed "shrinkClamp * innerRadius", where "innerRadius"104is the minimum distance of a face to the center of the convex hull.105The returned value is the amount by which the hull has been shrunken. If it is negative, the amount was so large106that the resulting convex hull is empty.107The output convex hull can be found in the member variables "vertices", "edges", "faces".108*/109real_t compute(const Vector3 *p_coords, int32_t p_count, real_t p_shrink, real_t p_shrink_clamp);110111static Error convex_hull(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_mesh);112};113114115