def test_project_point_on_curve(self): '''Test: project point on curve''' P = gp_Pnt(1., 2., 3.) radius = 5. C = Geom_Circle(gp_XOY(), radius) PPC = GeomAPI_ProjectPointOnCurve(P, C) N = PPC.NearestPoint() self.assertIsInstance(N, gp_Pnt) NbResults = PPC.NbPoints() edg = make_edge(C) self.assertFalse(edg.IsNull()) if NbResults > 0: for i in range(1, NbResults + 1): Q = PPC.Point(i) self.assertIsInstance(Q, gp_Pnt) distance = PPC.Distance(i) # in any case, it should be > 1 self.assertGreater(distance, 1.) pstring = "N : at Distance : " + repr(PPC.LowerDistance()) for i in range(1, NbResults + 1): Q = PPC.Point(i) self.assertIsInstance(Q, gp_Pnt) distance = PPC.Distance(i) # in any case, it should be > 1 self.assertGreater(distance, 1.) pstring = "Q" + repr(i) + ": at Distance :" + repr(PPC.Distance(i)) print(pstring)
def project_mesh_to_cad_2d(mesh, cad): from OCC.Core.BRepAdaptor import BRepAdaptor_Curve from OCC.Core.gp import gp_Pnt from OCC.Core.GeomAPI import GeomAPI_ProjectPointOnCurve coorddata = mesh.coordinates.dat.data ids = mesh.exterior_facets.unique_markers filt = lambda arr: arr[numpy.where(arr < mesh.coordinates.dof_dset.size)[0]] boundary_nodes = {id: filt(mesh.coordinates.function_space().boundary_nodes(int(id))) for id in ids} for (id, edge) in zip(ids, cad.edges()): owned_nodes = boundary_nodes[id] for other_id in ids: if id == other_id: continue owned_nodes = numpy.setdiff1d(owned_nodes, boundary_nodes[other_id]) curve = BRepAdaptor_Curve(edge) for node in owned_nodes: pt = gp_Pnt(*coorddata[node, :], 0) proj = GeomAPI_ProjectPointOnCurve(pt, curve.Curve().Curve()) if proj.NbPoints() > 0: projpt = proj.NearestPoint() coorddata[node, :] = projpt.Coord()[0:2] else: warnings.warn("Projection of point %s onto curve failed" % coorddata[node, :])
def project_point_on_curve(crv, pnt): if isinstance(crv, TopoDS_Shape): # get the curve handle... crv = adapt_edge_to_curve(crv).Curve().Curve() else: raise NotImplementedError('expected a TopoDS_Edge...') rrr = GeomAPI_ProjectPointOnCurve(pnt, crv) return rrr.LowerDistanceParameter(), rrr.NearestPoint()
def project_vertex(self, pnt_or_vertex): ''' returns the closest orthogonal project on `pnt` on edge ''' if isinstance(pnt_or_vertex, TopoDS_Vertex): pnt_or_vertex = vertex2pnt(pnt_or_vertex) poc = GeomAPI_ProjectPointOnCurve(pnt_or_vertex, self.curve_handle) return poc.LowerDistanceParameter(), poc.NearestPoint()
def project_point_on_curve(): ''' ''' point_to_project = gp_Pnt(1., 2., 3.) radius = 5. # create a circle, centered at origin with a given radius circle = Geom_Circle(gp_XOY(), radius) display.DisplayShape(circle) display.DisplayShape(point_to_project, update=True) display.DisplayMessage(point_to_project, "P") # project the point P on the circle projection = GeomAPI_ProjectPointOnCurve(point_to_project, circle) # get the results of the projection # the point projected_point = projection.NearestPoint() # the number of possible results nb_results = projection.NbPoints() print("NbResults : %i" % nb_results) pstring = "N : at Distance : %f" % projection.LowerDistance() display.DisplayMessage(projected_point, pstring) # thre maybe many different possible solutions if nb_results > 0: for i in range(1, nb_results+1): Q = projection.Point(i) distance = projection.Distance(i) pstring = "Q%i: at Distance :%f" % (i, distance) display.DisplayShape(Q) display.DisplayMessage(Q, pstring)
def other(): for (id, edge) in zip(ids, cad.edges()): owned_nodes = boundary_nodes[id] for other_id in ids: if id == other_id: continue owned_nodes = numpy.setdiff1d(owned_nodes, boundary_nodes[other_id]) curve = BRepAdaptor_Curve(edge) for node in owned_nodes: pt = gp_Pnt(*coorddata[node, :], 0) proj = GeomAPI_ProjectPointOnCurve(pt, curve.Curve().Curve()) if proj.NbPoints() > 0: projpt = proj.NearestPoint() coorddata[node, :] = projpt.Coord()[0:2] else: warnings.warn("Projection of point %s onto curve failed" % coorddata[node, :])
def project_mesh_to_cad_2d(mesh, cad): from OCC.Core.BRepAdaptor import BRepAdaptor_Curve from OCC.Core.gp import gp_Pnt from OCC.Core.GeomAPI import GeomAPI_ProjectPointOnCurve coorddata = mesh.coordinates.dat.data ids = mesh.exterior_facets.unique_markers filt = lambda arr: arr[numpy.where(arr < mesh.coordinates.dof_dset.size)[0] ] boundary_nodes = { id: filt(mesh.coordinates.function_space().boundary_nodes( int(id), "topological")) for id in ids } for id_ in ids: for node in boundary_nodes[id_]: #print(node) coords = coorddata[node, :] best_coords = coords dist_old = np.inf for edge in cad.edges(): curve = BRepAdaptor_Curve(edge) pt = gp_Pnt(*coorddata[node, :], 0) proj = GeomAPI_ProjectPointOnCurve(pt, curve.Curve().Curve()) if proj.NbPoints() > 0: projpt = proj.NearestPoint() projected_coords = np.array(projpt.Coord()[0:2]) dist = np.linalg.norm(coords - projected_coords) if dist_old > dist: best_coords = projected_coords dist_old = dist #print(coords, projected_coords, np.linalg.norm(coords-projected_coords)) #print(distances) coorddata[node, :] = best_coords return
def project_mesh_to_cad_3d(mesh, cad): from OCC.Core.BRepAdaptor import BRepAdaptor_Surface, BRepAdaptor_Curve from OCC.Core.gp import gp_Pnt from OCC.Core.GeomAPI import GeomAPI_ProjectPointOnSurf, GeomAPI_ProjectPointOnCurve coorddata = mesh.coordinates.dat.data ids = mesh.exterior_facets.unique_markers filt = lambda arr: arr[numpy.where(arr < mesh.coordinates.dof_dset.size)[0] ] boundary_nodes = { id: filt(mesh.coordinates.function_space().boundary_nodes( int(id), "topological")) for id in ids } for (id, face) in zip(ids, cad.faces()): owned_nodes = boundary_nodes[id] for other_id in ids: if id == other_id: continue owned_nodes = numpy.setdiff1d(owned_nodes, boundary_nodes[other_id]) surf = BRepAdaptor_Surface(face) for node in owned_nodes: pt = gp_Pnt(*coorddata[node, :]) proj = GeomAPI_ProjectPointOnSurf(pt, surf.Surface().Surface()) if proj.NbPoints() > 0: projpt = proj.NearestPoint() coorddata[node, :] = projpt.Coord() else: warnings.warn("Projection of point %s onto face %d failed" % (coorddata[node, :], id)) edges = set(cad.edges_from_face(face)) for (other_id, other_face) in zip(ids, cad.faces()): if other_id <= id: continue intersecting_nodes = numpy.intersect1d(boundary_nodes[id], boundary_nodes[other_id]) if len(intersecting_nodes) == 0: continue other_edges = set(cad.edges_from_face(other_face)) intersecting_edges = [] for edge in edges: s = str( edge ) # FIXME: is there a more elegant way to get the OCC id? for other_edge in other_edges: other_s = str(other_edge) if s == other_s: intersecting_edges.append(edge) if len(intersecting_edges) == 0: warnings.warn( "face: %s other_face: %s intersecting_edges: %s" % (face, other_face, intersecting_edges)) warnings.warn( "Warning: no intersecting edges in CAD, even though vertices on both faces?" ) continue for node in intersecting_nodes: pt = gp_Pnt(*coorddata[node, :]) projections = [] for edge in intersecting_edges: curve = BRepAdaptor_Curve(edge) proj = GeomAPI_ProjectPointOnCurve(pt, curve.Curve().Curve()) if proj.NbPoints() > 0: projpt = proj.NearestPoint() sqdist = projpt.SquareDistance(pt) projections.append((projpt, sqdist)) else: warnings.warn( "Projection of point %s onto curve failed" % coorddata[node, :]) (projpt, sqdist) = min(projections, key=lambda x: x[1]) coorddata[node, :] = projpt.Coord()
# discretize the wire and interpolate using a C2 wireAdaptor = BRepAdaptor_CompCurve(wire) curve = BRepAdaptor_HCompCurve(wireAdaptor) tol = 1e-7 max_segments = 200 max_degrees = 12 approx = Approx_Curve3d(curve, tol, GeomAbs_C2, max_segments, max_degrees) if (approx.IsDone() and approx.HasResult()): an_approximated_curve = approx.Curve() # there are two ways to project a point on this curve, # they both give the same restult # 1st solution: using GeomAPI_ProjectPointOnCurve point_to_project = gp_Pnt(1., 2., 3.) projection = GeomAPI_ProjectPointOnCurve(point_to_project, an_approximated_curve) # get the results of the projection projected_point = projection.NearestPoint() # the number of possible results nb_results = projection.NbPoints() print("NbResults : %i" % nb_results) print("Distance :", projection.LowerDistance()) # 2nd solution : using ShapeAnalysis_Curve().Project tolerance = 1e-7 proj = gp_Pnt() distance, parameter = ShapeAnalysis_Curve().Project(an_approximated_curve, point_to_project, tolerance, proj) print("Distance :", distance)
def project_point_on_curve(pnt, crv): algo = GeomAPI_ProjectPointOnCurve(to_Pnt(pnt), crv.Curve()) return point3(algo.NearestPoint())
def lower_distance_parameter(self, pnt): """Evalute parameter of curve's point that has minimal distance to pnt""" algo = GeomAPI_ProjectPointOnCurve(pnt.Pnt(), self.Curve()) return algo.LowerDistanceParameter()