-
Notifications
You must be signed in to change notification settings - Fork 5
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
Fix #179 #178
Open
janbridley
wants to merge
8
commits into
master
Choose a base branch
from
fix-minimal_bounding_sphere
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix #179 #178
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0b8598
test: Fix assumption on quaternions.
b-butler 9e6c088
Changed miniball solver to a faster and more accurate variant
janbridley 921a0a8
Merge branch 'master' into fix-minimal_bounding_sphere
bdice df17105
Fixed typo.
janbridley a69457c
Changed miniball random rotations to apply to original vertices
janbridley f050e4b
Merge branch 'master' into fix-minimal_bounding_sphere
janbridley f027272
Merge branch 'master' into fix-minimal_bounding_sphere
janbridley 7b1b62c
Merge branch 'master' into fix-minimal_bounding_sphere
janbridley 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 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
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -18,7 +18,7 @@ | |||||
from .utils import _generate_ax, _set_3d_axes_equal, translate_inertia_tensor | ||||||
|
||||||
try: | ||||||
import miniball | ||||||
import cyminiball | ||||||
|
||||||
MINIBALL = True | ||||||
except ImportError: | ||||||
|
@@ -550,27 +550,40 @@ def minimal_bounding_sphere(self): | |||||
""":class:`~.Sphere`: Get the polyhedron's bounding sphere.""" | ||||||
if not MINIBALL: | ||||||
raise ImportError( | ||||||
"The miniball module must be installed. It can " | ||||||
"The cyminiball module must be installed. It can " | ||||||
"be installed as an extra with coxeter (e.g. " | ||||||
'with "pip install coxeter[bounding_sphere]") or ' | ||||||
'directly from PyPI using "pip install miniball".' | ||||||
'directly from PyPI using "pip install cyminiball".' | ||||||
) | ||||||
|
||||||
# The algorithm in miniball involves solving a linear system and | ||||||
# can therefore occasionally be somewhat unstable. Applying a | ||||||
# random rotation will usually fix the issue. | ||||||
max_attempts = 10 | ||||||
# The miniball algorithm (in this case, Gärtner's miniball) can | ||||||
# experience numerical instability of ~10x machine epsilon. If an incorrect | ||||||
# miniball is found, applying random rotations will usually fix the issue. | ||||||
|
||||||
# The following subfunction checks if all vertices lie within the | ||||||
# minimal bounding sphere, to a tolerance of 1e-15. | ||||||
def verify_inside(points, center, radius, eps=1e-15): | ||||||
return np.all(np.linalg.norm(points - center, axis=1) <= radius + eps) | ||||||
|
||||||
# Simple polyhedra (e.g. dodecahedra) will require far less than 200 attempts | ||||||
# to compute a correct miniball. Polyhedra with augmentations and large numbers | ||||||
# of vertices will take more, but none are likely to exceed 200 attempts. Worst- | ||||||
# case runtime is approximately 0.25s, and occurrs in ~5/1e5 samples. | ||||||
janbridley marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
max_attempts = 200 | ||||||
attempt = 0 | ||||||
current_rotation = [1, 0, 0, 0] | ||||||
vertices = self.vertices | ||||||
while attempt < max_attempts: | ||||||
attempt += 1 | ||||||
try: | ||||||
center, r2 = miniball.get_bounding_ball(vertices) | ||||||
center, r2 = cyminiball.compute(vertices) | ||||||
assert verify_inside(vertices, center, np.sqrt(r2)) | ||||||
break | ||||||
except np.linalg.LinAlgError: | ||||||
current_rotation = rowan.random.rand(1) | ||||||
vertices = rowan.rotate(current_rotation, vertices) | ||||||
except AssertionError: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s combine this with the previous LinAlgError and handle both at the same time, since the subsequent code paths are identical. Should look something like:
Suggested change
|
||||||
current_rotation = rowan.random.rand(1) | ||||||
vertices = rowan.rotate(current_rotation, vertices) | ||||||
janbridley marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
else: | ||||||
raise RuntimeError("Unable to solve for a bounding sphere.") | ||||||
|
||||||
|
This file contains 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 |
---|---|---|
|
@@ -29,7 +29,7 @@ | |
] | ||
|
||
bounding_deps = [ | ||
"miniball", | ||
"cyminiball", | ||
] | ||
|
||
extras = { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we compare squared distances? If we avoid the square root in the
norm
here and just use sum-of-squares, we can also reuse the value ofr2
below without incurring another sqrt. Just depends on whether you think the epsilon is more meaningful in terms of distance rather than distance squared, I suppose.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For small distances (e.g. for points that are very close to the bounding sphere), ε will be more significant and therefore represent a larger tolerance range for accepted points. This is magnified for square distances, so if we do make this change we should probably test a smaller ε. I will set this aside while I figure out hbf/miniball and we can revisit later