def test_point_from_projections(self): """Test: point from projections""" P = gp_Pnt(7.0, 8.0, 9.0) radius = 5 SP = Geom_SphericalSurface(gp_Ax3(gp_XOY()), radius) PPS = GeomAPI_ProjectPointOnSurf(P, SP) N = PPS.NearestPoint() self.assertTrue(isinstance(N, gp_Pnt)) NbResults = PPS.NbPoints() if NbResults > 0: for i in range(1, NbResults + 1): Q = PPS.Point(i) self.assertTrue(isinstance(Q, gp_Pnt)) distance = PPS.Distance(i) # in any case, it should be > 1 self.assertGreater(distance, 1.0) lower_d = PPS.LowerDistance() self.assertGreater(lower_d, 1.0) if NbResults > 0: for i in range(1, NbResults + 1): Q = PPS.Point(i) distance = PPS.Distance(i) pstring = "Q" + repr(i) + ": at Distance :" + repr( PPS.Distance(i)) print(pstring)
def test_point_from_projections(self): '''Test: point from projections''' P = gp_Pnt(7., 8., 9.) radius = 5 SP = Geom_SphericalSurface(gp_Ax3(gp_XOY()), radius) PPS = GeomAPI_ProjectPointOnSurf(P, SP) N = PPS.NearestPoint() NbResults = PPS.NbPoints() if NbResults > 0: for i in range(1, NbResults + 1): Q = PPS.Point(i) distance = PPS.Distance(i) pstring = "N : at Distance : " + repr(PPS.LowerDistance()) if NbResults > 0: for i in range(1, NbResults + 1): Q = PPS.Point(i) distance = PPS.Distance(i) pstring = "Q" + repr(i) + ": at Distance :" + repr( PPS.Distance(i))
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()
gp_Dir(0.1, 0.2, 1.0), gp_Dir(0.0, 0.5, 1.0)) px = np.linspace(-1, 1, 500) * 100 + 50 py = np.linspace(-1, 1, 700) * 100 - 50 mesh = np.meshgrid(px, py) data = mesh[0]**2 / 1000 + mesh[1]**2 / 2000 face = spl_face(*mesh, data, ax0) surf = BRep_Tool.Surface(face) surf.Rotate(gp_Ax1(ax0.Location(), ax0.Direction()), np.deg2rad(15)) ax1 = gp_Ax3(gp_Pnt(150, 150, -20), gp_Dir(0.5, 0.1, 1.0), gp_Dir(0.0, 0.1, 1.0)) pnt = ax1.Location() api = GeomAPI_ProjectPointOnSurf(pnt, surf) print(api.NbPoints(), api.NearestPoint()) for i in range(api.NbPoints()): idx = i + 1 u, v = api.Parameters(idx) obj.display.DisplayShape(api.Point(idx)) print(idx, u, v) print(GeomAPI_IntCS(Geom_Line(ax1.Axis()), surf).NbPoints()) u, v, w = GeomAPI_IntCS(Geom_Line(ax1.Axis()), surf).Parameters(1) p, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() api = GeomLProp_SLProps(surf, u, v, 1, 0.1E-03) pnt = api.Value() dst = pnt.Distance(ax1.Location()) ax2 = obj.prop_axs(ax1, dst) rim_u = surf.UIso(u) rim_v = surf.VIso(v)