Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math: add AABB and bounding sphere algorithms #557

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Magnum/MeshTools/BoundingVolume.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
This file is part of Magnum.

Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022 Vladimír Vondruš <[email protected]>
Copyright © 2022 Pablo Escobar <[email protected]>

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.
*/

#include "BoundingVolume.h"

#include <Corrade/Containers/StridedArrayView.h>

#include "Magnum/Math/FunctionsBatch.h"

namespace Magnum { namespace MeshTools {

Range3D boundingBoxAxisAligned(const Containers::StridedArrayView1D<const Vector3>& points) {
return Math::minmax(points);
}

Containers::Pair<Vector3, Float> boundingSphereBouncingBubble(const Containers::StridedArrayView1D<const Vector3>& points) {
/* See comment about radius below, this is done for consistency */
if(points.isEmpty()) return {{}, Math::TypeTraits<Float>::epsilon()};

/** @todo Skip NaNs here? To match behaviour of boundingBoxAxisAligned()
which uses minmax(). */
Vector3 center = points[0];
/* Radius ends up in the denominator in the first loop so we can't
initialize it to 0. Unfortunately this also means the returned radius is
always above or equal to epsilon. */
Float radius = Math::TypeTraits<Float>::epsilon();
Float radiusSquared = radius*radius;

for(int i = 0; i < 2; ++i) {
for(const Vector3& p : points) {
const Float ds = (p - center).dot();
if(ds > radiusSquared) {
const Float alphaInv = radius/Math::sqrt(ds);
/* Not using alphaInv*alphaInv since that may lose precision */
const Float alphaSqInv = radiusSquared/ds;
radius = (1.0f/alphaInv + alphaInv)*0.5f*radius;
center = ((1.0f + alphaSqInv)*center + (1.0f - alphaSqInv)*p)*0.5f;
radiusSquared = radius*radius;
}
}
}

for(const Vector3& p : points) {
const Vector3 diff = p - center;
const Float ds = diff.dot();
if(ds > radiusSquared) {
const Float d = Math::sqrt(ds);
radius = (radius + d)*0.5f;
center = center + ((d - radius)/d*diff);
radiusSquared = radius*radius;
}
}

return {center, radius};
}

}}
72 changes: 72 additions & 0 deletions src/Magnum/MeshTools/BoundingVolume.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef Magnum_MeshTools_BoundingVolume_h
#define Magnum_MeshTools_BoundingVolume_h
/*
This file is part of Magnum.

Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022 Vladimír Vondruš <[email protected]>
Copyright © 2022 Pablo Escobar <[email protected]>

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.
*/

/** @file
* @brief Function @ref Magnum::MeshTools::boundingBoxAxisAligned(), @ref Magnum::MeshTools::boundingSphereBouncingBubble()
* @m_since_latest
*/

#include <Corrade/Containers/Pair.h>

#include "Magnum/Magnum.h"
#include "Magnum/Math/Range.h"
#include "Magnum/Math/Vector3.h"
#include "Magnum/MeshTools/visibility.h"

namespace Magnum { namespace MeshTools {

/**
@brief Calculate an axis-aligned bounding box
@param positions Vertex positions
@return Bounding box
@m_since_latest

Same as @ref Math::minmax(const Corrade::Containers::StridedArrayView1D<const T>&).
*/
MAGNUM_MESHTOOLS_EXPORT Range3D boundingBoxAxisAligned(const Containers::StridedArrayView1D<const Vector3>& positions);

/**
@brief Calculate an approximate bounding sphere using the Bouncing Bubble
algorithm
@param positions Vertex positions
@return Sphere center and radius
@m_since_latest

The resulting bounding sphere is not usually minimal. According to the author,
a 1% to 2% error can be expected. Due to the nature of the algorithm, the
radius is never below @ref Math::TypeTraits::epsilon(), even for empty or
entirely overlapping lists of points. <em>NaN</em>s are ignored, unless the
first position is <em>NaN</em> in which case it is propagated. Algorithm used:
* *Bo Tian --- Bouncing Bubble: A fast algorithm for Minimal Enclosing Ball
problem, 2012, https://www.grin.com/document/204869*
*/
MAGNUM_MESHTOOLS_EXPORT Containers::Pair<Vector3, Float> boundingSphereBouncingBubble(const Containers::StridedArrayView1D<const Vector3>& positions);

}}

#endif
2 changes: 2 additions & 0 deletions src/Magnum/MeshTools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(MagnumMeshTools_SRCS

# Files compiled with different flags for main library and unit test library
set(MagnumMeshTools_GracefulAssert_SRCS
BoundingVolume.cpp
Combine.cpp
CompressIndices.cpp
Concatenate.cpp
Expand All @@ -47,6 +48,7 @@ set(MagnumMeshTools_GracefulAssert_SRCS
Transform.cpp)

set(MagnumMeshTools_HEADERS
BoundingVolume.h
Combine.h
CompressIndices.h
Concatenate.h
Expand Down
Loading