-
Notifications
You must be signed in to change notification settings - Fork 466
Math: add AABB and bounding sphere algorithms #557
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
83aed61
MeshTools: add initial AABB and sphere bounding volume algorithms
pezcode 5725761
MeshTools: add benchmark for boundingBoxAxisAligned()
pezcode 641912c
MeshTools: move BoundingVolume NaN checks into their own test case
pezcode 53487f5
MeshTools: also test NaN behavior of boundingBoxAxisAligned()
pezcode 4694b59
MeshTools: avoid unnecessary precision loss in boundingSphereBouncing…
pezcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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š <mosra@centrum.cz> | ||
| Copyright © 2022 Pablo Escobar <mail@rvrs.in> | ||
|
|
||
| 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}; | ||
| } | ||
|
|
||
| }} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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š <mosra@centrum.cz> | ||
| Copyright © 2022 Pablo Escobar <mail@rvrs.in> | ||
|
|
||
| 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 | ||
| */ | ||
|
pezcode marked this conversation as resolved.
|
||
|
|
||
| #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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.