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(): ''' ''' 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
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)