-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Added functions to get distances to primatives and meshes #148
Open
weymouth
wants to merge
2
commits into
JuliaGeometry:master
Choose a base branch
from
weymouth:master
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#= | ||
Functions to compute distances from points to primatives | ||
=# | ||
|
||
using LinearAlgebra: normalize,norm,⋅ | ||
""" | ||
closest_point_to_tri(p, a, b, c) | ||
|
||
This method from Ericson, "Real-time collision detection" | ||
2005, doesn't require the normal or cross products. | ||
""" | ||
@fastmath function closest_point_to_tri(p,a,b,c) | ||
# is point `a` closest? | ||
ab,ac,ap = b-a,c-a,p-a | ||
d1,d2 = ab ⋅ ap, ac ⋅ ap | ||
d1 <= 0 && d2 <= 0 && return a | ||
|
||
# is point `b` closest? | ||
bp = p-b | ||
d3,d4 = ab ⋅ bp, ac ⋅ bp | ||
d3 >= 0 && d4 <= d3 && return b | ||
|
||
# is point `c` closest? | ||
cp = p-c | ||
d5,d6 = ab ⋅ cp, ac ⋅ cp | ||
d6 >= 0 && d5 <= d6 && return c | ||
|
||
# is segment `ab` closest? | ||
vc = d1*d4 - d3*d2 | ||
if (vc <= 0 && d1 >= 0 && d3 <= 0) | ||
d1==d3 && @show d1,d3 | ||
return a + ab * d1 * inv(d1 - d3) | ||
end | ||
|
||
# is segment `ac` closest? | ||
vb = d5*d2 - d1*d6 | ||
if (vb <= 0 && d2 >= 0 && d6 <= 0) | ||
return a + ac * d2 * inv(d2 - d6) | ||
end | ||
|
||
# is segment `bc` closest? | ||
va = d3*d6 - d5*d4 | ||
if (va <= 0 && d4 >= d3 && d5 >= d6) | ||
return b + (c - b) * (d4 - d3) * inv(d4 - d3 + d5 - d6) | ||
end | ||
|
||
# closest is interior to `abc` | ||
denom = inv(va + vb + vc) | ||
v,w = vb * denom, vc * denom | ||
return a + ab * v + ac * w | ||
end | ||
""" | ||
closest(p,tri::Triangle) | ||
|
||
Determine the closest point on triangle `tri` to point `p`. | ||
""" | ||
closest(p,tri::Triangle) = closest_point_to_tri(p,tri.points.data...) | ||
|
||
""" | ||
absolute_distance(p,tri::Triangle) | ||
|
||
Determine the absolute distance from point `p` to the closest point on triangle `tri`. | ||
""" | ||
absolute_distance(p,tri::Triangle) = norm(p-closest(p,tri)) | ||
|
||
""" | ||
signed_distance(p,tri::Triangle) | ||
|
||
Determine the signed distance from point `p` to the plane defined by triangle `tri`. | ||
Note that the sign depends on triangle point ordering and `d=0` only implies `p` | ||
lies on the plane, not that it is within the triangle. | ||
""" | ||
signed_distance(p,tri::Triangle) = signed_distance_to_tri(p,tri.points.data...) | ||
signed_distance_to_tri(p,a,b,c) = normalize(orthogonal_vector(a,b,c))⋅(p-a) | ||
|
||
nonzero(mesh::AbstractMesh) = (i for i in mesh if sum(abs2,orthogonal_vector(i.points.data...))>0) | ||
""" | ||
absolute_distance(p, mesh::AbstractMesh) | ||
|
||
Return the minimum absolute distance from point `p` to any Triangle in `mesh`. | ||
""" | ||
absolute_distance(p,mesh::AbstractMesh) = minimum(t->absolute_distance(p,t), nonzero(mesh)) | ||
|
||
""" | ||
signed_distance(p, mesh::AbstractMesh) | ||
|
||
Return the signed distance to the geometry defined by the union of planes | ||
passing through each `mesh` face. | ||
""" | ||
signed_distance(p,mesh::AbstractMesh) = maximum(t->signed_distance(p,t), nonzero(mesh)) | ||
|
||
""" | ||
signed_distance(p,s::HyperSphere) | ||
|
||
Return the signed distance from `p` to `s`. | ||
""" | ||
signed_distance(p,s::HyperSphere) = norm(p-origin(s))-radius(s) | ||
|
||
distance_to_corner(q) = norm(max.(q, 0.))+min(maximum(q),0.) | ||
""" | ||
signed_distance(p,r::Rect) | ||
|
||
Return the signed distance from `p` to `r`. | ||
""" | ||
function signed_distance(p,r::Rect) | ||
# Get vector relative to Rect corner | ||
q = abs.(p-origin(r).-0.5*width(r)).-0.5*width(r) | ||
return distance_to_corner(q) | ||
end | ||
|
||
""" | ||
signed_distance(p,c::Cylinder) | ||
|
||
Return the signed distance from `p` to `c`. | ||
""" | ||
function signed_distance(p,c::Cylinder) | ||
e = (extremity(c)-origin(c))/2 # center to edge (defines length direction) | ||
r = p-(extremity(c)+origin(c))/2 # center to point | ||
e₁ = norm(e) # cylinder half-length | ||
r₁ = abs(e ⋅ r) / e₁ # projected length to point | ||
r₂ = √(r ⋅ r - r₁^2) # height to point | ||
return distance_to_corner(Point(r₁-e₁,r₂-radius(c))) | ||
end | ||
|
||
""" | ||
signed_distance(p,prim) | ||
|
||
Fallback signed distance function. | ||
""" | ||
signed_distance(p,prim::GeometryPrimitive) = signed_distance(p,GeometryBasics.mesh(prim)) | ||
|
||
""" | ||
absolute_distance(p,prim) = |signed_distance(p,prim)| | ||
|
||
Fallback absolute distance function. | ||
""" | ||
absolute_distance(p,prim) = abs(signed_distance(p,prim)) |
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
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.
This is going to scale poorly on performance, so I'm not sure if this will give the right experience. IIUC for Mesh->SDF you normally want to bin faces in an octree, kd tree, or other, to try to reduce the search space first.
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.
Very true. This is where some help from people more familiar with these kind of approaches would be great.