How to find the closest point on a solid/face to another point in 3d space #1450
Unanswered
bradphelan
asked this question in
Q&A
Replies: 1 comment
-
I've come up with a working solution that involves calling into the OCP library import cadquery as cq
from math import sin, cos, pi
from numpy import linspace
from OCP.BRepExtrema import BRepExtrema_DistShapeShape
SEGMENTS = 48
def sinusoidal_ring(rad=25, segments=SEGMENTS):
outline = []
for i in range(segments):
angle = i * 2 * pi / segments
x = rad * (cos(angle) + i/segments)
y = rad * sin(angle)
z = 2 * rad * (sin(angle) / 5 + i / segments)
outline.append((x, y, z))
return outline
def triangle(base=15, ratio=2):
return [(-base/2, 0), (base/2, 0), (0, base*ratio)]
def distance(self, other: "Shape") -> float:
return BRepExtrema_DistShapeShape(self.wrapped, other.wrapped)
def extrude_example():
shape = triangle()
path = sinusoidal_ring(rad=50)
solid = cq.Workplane("YZ").polyline(shape).close().sweep(cq.Workplane("XY").spline(path, includeCurrent=True))
# Project all points in a grid onto the shape and display
for x in linspace(-100,100,3, endpoint=True):
for y in linspace(-100, 100,3, endpoint=True):
for z in linspace(-100, 100, 3, endpoint=True):
point = cq.Vertex.makeVertex(x, y, z)
# Find the closest point on any face of 'solid'
closest_point = distance(point, solid.val())
closest_point.Perform()
log(closest_point.PointOnShape1(1))
log(closest_point.PointOnShape2(1))
target = closest_point.PointOnShape2(1)
target_vertex = cq.Vertex.makeVertex(target.X(), target.Y(), target.Z())
show_object(target_vertex)
e =cq.Edge.makeLine((x,y,z), (target.X(), target.Y(), target.Z()))
show_object(e)
show_object(solid)
extrude_example() Any suggestions on how to make the code better would be appreciated. It was hacked together trying random things. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have some code here that creates a random solid.
What I'd like to be able to do is given some random point in space P(x,y,z) find the closest point on
solid
S(x,y,z). Is this possible through the CQ api?Beta Was this translation helpful? Give feedback.
All reactions