def testWirePixmap2(face): """ tests drawing a wire while adjusting sharp borders """ PIXEL = 0.01 DEFLECTION = PIXEL / 4.0 #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) g = nx.Graph() #adjust boundaries a bit BUFFER = PIXEL * 5 #make pixmap pixmap = pixmaptest.pixmap((xMin - BUFFER, yMin - BUFFER), (xMax + BUFFER, yMax + BUFFER), PIXEL) ow = brt.OuterWire(face) boundary = tuplesFromWire(ow, DEFLECTION) for et in Wrappers.pairwise(boundary): p1 = et[0] p2 = et[1] g.add_edge(p1, p2) i1 = pixmap.index(p1) i2 = pixmap.index(p2) #print i1,i2,p1,p2 pixmap.drawLine(p1, p2, 1, 2) pixmap.saveImage("c:\\temp\\thickboundary.jpg")
def testWirePixmap2(face): """ tests drawing a wire while adjusting sharp borders """ PIXEL = 0.01 ; DEFLECTION = PIXEL / 4.0; #get bounding box (xMin,yMin,zMin,xMax,yMax,zMax) = boundingBox([face]); g = nx.Graph(); #adjust boundaries a bit BUFFER=PIXEL*5; #make pixmap pixmap = pixmaptest.pixmap((xMin-BUFFER,yMin-BUFFER),(xMax+BUFFER,yMax+BUFFER),PIXEL); ow = brt.OuterWire(face); boundary = tuplesFromWire(ow,DEFLECTION); for et in Wrappers.pairwise(boundary): p1 = et[0]; p2 = et[1]; g.add_edge(p1,p2); i1 = pixmap.index(p1); i2 = pixmap.index(p2); #print i1,i2,p1,p2 pixmap.drawLine(p1,p2,1,2); pixmap.saveImage("c:\\temp\\thickboundary.jpg");
def pixmapFromFace2(face, fillpattern=None): """ create a filled pixmap from a defined face. ideally, this is really really fast, based on a triangulation of the face. a face with 800x600 pixels took 56ms to raster with this algo. @ 0.012" that is a 6x8 " part! this creates a pixel map that has all the interior pixels filled This version uses the boundaries and PIL.polygon instead of the triangulagion, primarily becaseu the OCC triangulation seems to suck the general approach is: compute a filled shape containging a bit mask draw borders using a value tile a fill pattern onto the background """ PIXEL = 0.012 DEFLECTION = PIXEL / 2.0 #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) #adjust boundaries a bit BUFFER = PIXEL * 5 #make pixmap pixmap = pixmaptest.pixmap((xMin - BUFFER, yMin - BUFFER), (xMax + BUFFER, yMax + BUFFER), PIXEL) #we will draw the outer wire as a filled polygon, then #draw the inner wire as another filled polygon ow = brt.OuterWire(face) outerPoints = tuplesFromWire(ow, DEFLECTION) #outerPoints.pop(2); #print outerPoints pixmap.drawPolygon(outerPoints, 1, 1) #drawWire(outerPoints,pixmap); #pixmap.saveImage("c:\\temp\\heartborder-1.bmp"); #get the other wires. wires = [] wires.append(outerPoints) for w in Topo(face).wires(): if not w.IsSame(ow): wp = tuplesFromWire(w, DEFLECTION) pixmap.drawPolygon(wp, 0, 0) wires.append(wp) return pixmap
def pixmapFromFace2(face,fillpattern=None): """ create a filled pixmap from a defined face. ideally, this is really really fast, based on a triangulation of the face. a face with 800x600 pixels took 56ms to raster with this algo. @ 0.012" that is a 6x8 " part! this creates a pixel map that has all the interior pixels filled This version uses the boundaries and PIL.polygon instead of the triangulagion, primarily becaseu the OCC triangulation seems to suck the general approach is: compute a filled shape containging a bit mask draw borders using a value tile a fill pattern onto the background """ PIXEL = 0.012 ; DEFLECTION = PIXEL / 2.0; #get bounding box (xMin,yMin,zMin,xMax,yMax,zMax) = boundingBox([face]); #adjust boundaries a bit BUFFER=PIXEL*5; #make pixmap pixmap = pixmaptest.pixmap((xMin-BUFFER,yMin-BUFFER),(xMax+BUFFER,yMax+BUFFER),PIXEL); #we will draw the outer wire as a filled polygon, then #draw the inner wire as another filled polygon ow = brt.OuterWire(face); outerPoints = tuplesFromWire(ow,DEFLECTION); #outerPoints.pop(2); #print outerPoints pixmap.drawPolygon(outerPoints,1,1); #drawWire(outerPoints,pixmap); #pixmap.saveImage("c:\\temp\\heartborder-1.bmp"); #get the other wires. wires = [] wires.append(outerPoints); for w in Topo(face).wires(): if not w.IsSame(ow): wp = tuplesFromWire(w,DEFLECTION); pixmap.drawPolygon(wp,0,0); wires.append(wp); return pixmap;
def testHugePixmap(face): """ tests drawing a wire while adjusting sharp borders """ PIXEL = 0.005 ; DEFLECTION = PIXEL / 2.0; #get bounding box (xMin,yMin,zMin,xMax,yMax,zMax) = boundingBox([face]); g = nx.Graph(); #adjust boundaries a bit BUFFER=PIXEL*5; #make pixmap pixmap = pixmaptest.pixmap((xMin-BUFFER,yMin-BUFFER),(xMax+BUFFER,yMax+BUFFER),PIXEL); ow = brt.OuterWire(face); boundary = tuplesFromWire(ow,DEFLECTION); for et in Wrappers.pairwise(boundary): p1 = et[0]; p2 = et[1]; g.add_edge(p1,p2); i1 = pixmap.index(p1); i2 = pixmap.index(p2); #print i1,i2,p1,p2 pixmap.drawLine(p1,p2,1,2); #now, lets see how long it takes to find the intersection of a line by painting on the pixmap q = time.clock(); p1 = ( -4.0,2.0 ) p2 = ( 4.0, 2.0 ) results = pixmap.drawLine2(p1,p2 ) print results print "Found Intersections in %0.3f seconds" % ( time.clock() - q ); #how fast is it to determine if a point is on the face? q = time.clock() p1 = ( -0.5,2.0) for i in range(1,1000): pixmap.get(p1) print "Tested 1000 points in %0.3f seconds" % ( time.clock() - q ); pixmap.saveImage( "c:\\temp\scanline.jpg" );
def testHugePixmap(face): """ tests drawing a wire while adjusting sharp borders """ PIXEL = 0.005 DEFLECTION = PIXEL / 2.0 #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) g = nx.Graph() #adjust boundaries a bit BUFFER = PIXEL * 5 #make pixmap pixmap = pixmaptest.pixmap((xMin - BUFFER, yMin - BUFFER), (xMax + BUFFER, yMax + BUFFER), PIXEL) ow = brt.OuterWire(face) boundary = tuplesFromWire(ow, DEFLECTION) for et in Wrappers.pairwise(boundary): p1 = et[0] p2 = et[1] g.add_edge(p1, p2) i1 = pixmap.index(p1) i2 = pixmap.index(p2) #print i1,i2,p1,p2 pixmap.drawLine(p1, p2, 1, 2) #now, lets see how long it takes to find the intersection of a line by painting on the pixmap q = time.clock() p1 = (-4.0, 2.0) p2 = (4.0, 2.0) results = pixmap.drawLine2(p1, p2) print results print "Found Intersections in %0.3f seconds" % (time.clock() - q) #how fast is it to determine if a point is on the face? q = time.clock() p1 = (-0.5, 2.0) for i in range(1, 1000): pixmap.get(p1) print "Tested 1000 points in %0.3f seconds" % (time.clock() - q) pixmap.saveImage("c:\\temp\scanline.jpg")
def boundarypixmapFromFace(face): """ creates a pixel map containing only the boundaries of the object. the shape is approximated with straight lines, and vertices are marked with different values to help re-construct the lines later on. Some c++ optimizaion woudl really speed this up. """ PIXEL = 0.02; DEFLECTION = PIXEL / 4.0; #get bounding box (xMin,yMin,zMin,xMax,yMax,zMax) = boundingBox([face]); #make pixmap pixmap = pixmaptest.pixmap((xMin,yMin),(xMax,yMax),PIXEL); #approximate each wire with a set of segments bb = TopExp.TopExp_Explorer(); bb.Init(face,TopAbs.TopAbs_WIRE); while bb.More(): #print "plotting wire" w = Wrappers.Wire(Wrappers.cast(bb.Current())); #divide the edge into discrete segments lastPnt = None; for e in w.edges2(): #for each edge, set vertices, and compute points on the edge ew = Wrappers.Edge(e); lastPnt = None; for pnt in ew.discretePoints(DEFLECTION): pixmap.set(tP(pnt),2); #plot the line if lastPnt != None: pixmap.drawLine(tP(lastPnt),tP(pnt) ); lastPnt = pnt; bb.Next(); return pixmap;
def boundarypixmapFromFace(face): """ creates a pixel map containing only the boundaries of the object. the shape is approximated with straight lines, and vertices are marked with different values to help re-construct the lines later on. Some c++ optimizaion woudl really speed this up. """ PIXEL = 0.02 DEFLECTION = PIXEL / 4.0 #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) #make pixmap pixmap = pixmaptest.pixmap((xMin, yMin), (xMax, yMax), PIXEL) #approximate each wire with a set of segments bb = TopExp.TopExp_Explorer() bb.Init(face, TopAbs.TopAbs_WIRE) while bb.More(): #print "plotting wire" w = Wrappers.Wire(Wrappers.cast(bb.Current())) #divide the edge into discrete segments lastPnt = None for e in w.edges2(): #for each edge, set vertices, and compute points on the edge ew = Wrappers.Edge(e) lastPnt = None for pnt in ew.discretePoints(DEFLECTION): pixmap.set(tP(pnt), 2) #plot the line if lastPnt != None: pixmap.drawLine(tP(lastPnt), tP(pnt)) lastPnt = pnt bb.Next() return pixmap
def testWirePixmap(face): """ tests drawing a wire while adjusting sharp borders """ PIXEL = 0.01 ; DEFLECTION = PIXEL / 4.0; #get bounding box (xMin,yMin,zMin,xMax,yMax,zMax) = boundingBox([face]); g = nx.Graph(); #adjust boundaries a bit BUFFER=PIXEL*5; #make pixmap pixmap = pixmaptest.pixmap((xMin-BUFFER,yMin-BUFFER),(xMax+BUFFER,yMax+BUFFER),PIXEL); ow = brt.OuterWire(face); boundary = tuplesFromWire(ow,DEFLECTION); for et in Wrappers.pairwise(boundary): p1 = et[0]; p2 = et[1]; g.add_edge(p1,p2); i1 = pixmap.index(p1); i2 = pixmap.index(p2); #print i1,i2,p1,p2 lines = bres.piecewise_bresenham_line(pixmap.p,i1,i2); #add to the graph. this is tricky. we want to add the original locations #to the graph if nothing changed, but we want to add the new locations #to the graph is something did change or new points were added for l in lines: #print "adding edge." g.add_edge(l[0],l[1]); return g;
def testWirePixmap(face): """ tests drawing a wire while adjusting sharp borders """ PIXEL = 0.01 DEFLECTION = PIXEL / 4.0 #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) g = nx.Graph() #adjust boundaries a bit BUFFER = PIXEL * 5 #make pixmap pixmap = pixmaptest.pixmap((xMin - BUFFER, yMin - BUFFER), (xMax + BUFFER, yMax + BUFFER), PIXEL) ow = brt.OuterWire(face) boundary = tuplesFromWire(ow, DEFLECTION) for et in Wrappers.pairwise(boundary): p1 = et[0] p2 = et[1] g.add_edge(p1, p2) i1 = pixmap.index(p1) i2 = pixmap.index(p2) #print i1,i2,p1,p2 lines = bres.piecewise_bresenham_line(pixmap.p, i1, i2) #add to the graph. this is tricky. we want to add the original locations #to the graph if nothing changed, but we want to add the new locations #to the graph is something did change or new points were added for l in lines: #print "adding edge." g.add_edge(l[0], l[1]) return g
def pixmapFromFaceTriangulation(face): """ create a filled pixmap from a defined face. ideally, this is really really fast, based on a triangulation of the face. a face with 800x600 pixels took 56ms to raster with this algo. @ 0.012" that is a 6x8 " part! this creates a pixel map that has all the interior pixels filled this might fix: TopoDS_Face tFace = BRepBuilderAPI_MakeFace( tFrame ); BRepBuilderAPI_MakeFace MF( tFace ); MF.Add( tHole ); if ( MF.IsDone()) { Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape; sfs->Init ( MF.Shape() ); sfs->Perform(); Handle(AIS_Shape) aShape = new AIS_Shape(sfs->Shape()); myAISContext->Display(aShape); """ PIXEL = 0.005; DEFLECTION = PIXEL / 20.0; triangulateAtDeflection(face,DEFLECTION); #get bounding box (xMin,yMin,zMin,xMax,yMax,zMax) = boundingBox([face]); #adjust boundaries a bit BUFFER=PIXEL*5; #make pixmap pixmap = pixmaptest.pixmap((xMin-BUFFER,yMin-BUFFER),(xMax+BUFFER,yMax+BUFFER),PIXEL); transformPoints = True; loc = TopLoc.TopLoc_Location(); hT = btool.Triangulation(face,loc).GetObject(); trsf = loc.Transformation(); if loc.IsIdentity(): transformPoints = False; nodes = hT.Nodes(); triangles = hT.Triangles(); #mark pixels to fill for i in range(triangles.Lower(),triangles.Upper() ): tr = triangles.Value(i); (n1,n2,n3) = tr.Get(); p1 = nodes.Value(n1); p2 = nodes.Value(n2); p3 = nodes.Value(n3); if transformPoints: p1.Transform(trsf); p2.Transform(trsf); p3.Transform(trsf); #fill triangle. easy peasy! pixmap.fillTriangle(tP(p1),tP(p2),tP(p3),7); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p1,p2)); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p2,p3)); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p3,p1)); #time.sleep(1); #TestDisplay.display.showShape(Wrappers.make_vertex(p1)); #TestDisplay.display.showShape(Wrappers.make_vertex(p2)); #TestDisplay.display.showShape(Wrappers.make_vertex(p3)); #tile hex pattern onto the filling #pixmap.tileOnto(fillpattern); #mark boundaries #approximate each wire with a set of segments """ bb = TopExp.TopExp_Explorer(); bb.Init(face,TopAbs.TopAbs_WIRE); edgeNum = 1; #while False: while bb.More(): #print "plotting wire" w = Wrappers.Wire(Wrappers.cast(bb.Current())); #divide the edge into discrete segments lastPnt = None; for e in w.edges2(): #for each edge, set vertices, and compute points on the edge ew = Wrappers.Edge(e); lastPnt = None; for pnt in ew.discretePoints(DEFLECTION): pixmap.set(tP(pnt),7); edgeNum += 1; #plot the line #if lastPnt != None: pixmap.drawLine(tP(lastPnt),tP(pnt),edgeNum ); lastPnt = pnt; bb.Next(); """ return pixmap;
def pixmapFromFaceTriangulation(face): """ create a filled pixmap from a defined face. ideally, this is really really fast, based on a triangulation of the face. a face with 800x600 pixels took 56ms to raster with this algo. @ 0.012" that is a 6x8 " part! this creates a pixel map that has all the interior pixels filled this might fix: TopoDS_Face tFace = BRepBuilderAPI_MakeFace( tFrame ); BRepBuilderAPI_MakeFace MF( tFace ); MF.Add( tHole ); if ( MF.IsDone()) { Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape; sfs->Init ( MF.Shape() ); sfs->Perform(); Handle(AIS_Shape) aShape = new AIS_Shape(sfs->Shape()); myAISContext->Display(aShape); """ PIXEL = 0.005 DEFLECTION = PIXEL / 20.0 triangulateAtDeflection(face, DEFLECTION) #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) #adjust boundaries a bit BUFFER = PIXEL * 5 #make pixmap pixmap = pixmaptest.pixmap((xMin - BUFFER, yMin - BUFFER), (xMax + BUFFER, yMax + BUFFER), PIXEL) transformPoints = True loc = TopLoc.TopLoc_Location() hT = btool.Triangulation(face, loc).GetObject() trsf = loc.Transformation() if loc.IsIdentity(): transformPoints = False nodes = hT.Nodes() triangles = hT.Triangles() #mark pixels to fill for i in range(triangles.Lower(), triangles.Upper()): tr = triangles.Value(i) (n1, n2, n3) = tr.Get() p1 = nodes.Value(n1) p2 = nodes.Value(n2) p3 = nodes.Value(n3) if transformPoints: p1.Transform(trsf) p2.Transform(trsf) p3.Transform(trsf) #fill triangle. easy peasy! pixmap.fillTriangle(tP(p1), tP(p2), tP(p3), 7) #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p1,p2)); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p2,p3)); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p3,p1)); #time.sleep(1); #TestDisplay.display.showShape(Wrappers.make_vertex(p1)); #TestDisplay.display.showShape(Wrappers.make_vertex(p2)); #TestDisplay.display.showShape(Wrappers.make_vertex(p3)); #tile hex pattern onto the filling #pixmap.tileOnto(fillpattern); #mark boundaries #approximate each wire with a set of segments """ bb = TopExp.TopExp_Explorer(); bb.Init(face,TopAbs.TopAbs_WIRE); edgeNum = 1; #while False: while bb.More(): #print "plotting wire" w = Wrappers.Wire(Wrappers.cast(bb.Current())); #divide the edge into discrete segments lastPnt = None; for e in w.edges2(): #for each edge, set vertices, and compute points on the edge ew = Wrappers.Edge(e); lastPnt = None; for pnt in ew.discretePoints(DEFLECTION): pixmap.set(tP(pnt),7); edgeNum += 1; #plot the line #if lastPnt != None: pixmap.drawLine(tP(lastPnt),tP(pnt),edgeNum ); lastPnt = pnt; bb.Next(); """ return pixmap