clippedShellMapper.SetInputConnection(sampleImp.GetOutputPort()) clippedShellMapper.ScalarVisibilityOff() clippedShellActor = vtk.vtkActor() clippedShellActor.SetMapper(clippedShellMapper); trimMapper = vtk.vtkPolyDataMapper() trimMapper.SetInputConnection(sampleImp.GetOutputPort(0)) trimMapper.ScalarVisibilityOff() trimActor = vtk.vtkActor() trimActor.SetMapper(trimMapper); trimActor.GetProperty().SetColor(0,0,1) # Build a loop from the clipper buildLoops = vtk.vtkContourLoopExtraction() buildLoops.SetInputConnection(sampleImp.GetOutputPort(0)) buildLoops.Update() # Test cell data cookie0 = vtk.vtkCookieCutter() cookie0.SetInputConnection(pd2cd.GetOutputPort()) cookie0.SetLoopsConnection(buildLoops.GetOutputPort()) if coloring == 0: cookie0.PassCellDataOff() cookie0.PassPointDataOff() else: cookie0.PassCellDataOn() cookie0.PassPointDataOff() timer = vtk.vtkTimerLog()
extract = vtk.vtkImageExtractComponents() extract.SetInputConnection(red.GetOutputPort()) extract.SetComponents(0,1,2) # Quantize the image into an index quantize = vtk.vtkImageQuantizeRGBToIndex() quantize.SetInputConnection(extract.GetOutputPort()) quantize.SetNumberOfColors(3) # Create the pipeline discrete = vtk.vtkDiscreteFlyingEdges2D() discrete.SetInputConnection(quantize.GetOutputPort()) discrete.SetValue(0,0) # Create polgons polyLoops = vtk.vtkContourLoopExtraction() polyLoops.SetInputConnection(discrete.GetOutputPort()) # Triangle filter because concave polygons are not rendered correctly triF = vtk.vtkTriangleFilter() triF.SetInputConnection(polyLoops.GetOutputPort()) # Polylines are generated mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(discrete.GetOutputPort()) mapper.ScalarVisibilityOff() actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(0,0,0)
loopLines.InsertCellPoint(3) # Along y-left-side boundary loopLines.InsertNextCell(3) loopLines.InsertCellPoint(4) loopLines.InsertCellPoint(5) loopLines.InsertCellPoint(6) loopLines.InsertNextCell(2) loopLines.InsertCellPoint(6) loopLines.InsertCellPoint(7) loopLines.InsertNextCell(2) loopLines.InsertCellPoint(7) loopLines.InsertCellPoint(8) contours = vtk.vtkContourLoopExtraction() contours.SetInputData(loopData) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(contours.GetOutputPort()) #mapper.SetInputData(loopData) actor = vtk.vtkActor() actor.SetMapper(mapper) ren.AddActor(actor) renWin.Render() iren.Start()
extract = vtk.vtkImageExtractComponents() extract.SetInputConnection(red.GetOutputPort()) extract.SetComponents(0, 1, 2) # Quantize the image into an index quantize = vtk.vtkImageQuantizeRGBToIndex() quantize.SetInputConnection(extract.GetOutputPort()) quantize.SetNumberOfColors(3) # Create the pipeline discrete = vtk.vtkDiscreteFlyingEdges2D() discrete.SetInputConnection(quantize.GetOutputPort()) discrete.SetValue(0, 0) # Create polgons polyLoops = vtk.vtkContourLoopExtraction() polyLoops.SetInputConnection(discrete.GetOutputPort()) # Triangle filter because concave polygons are not rendered correctly triF = vtk.vtkTriangleFilter() triF.SetInputConnection(polyLoops.GetOutputPort()) # Polylines are generated mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(discrete.GetOutputPort()) mapper.ScalarVisibilityOff() actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(0, 0, 0)
loopLines.InsertNextCell(2) loopLines.InsertCellPoint(3) loopLines.InsertCellPoint(5) loopLines.InsertNextCell(2) loopLines.InsertCellPoint(5) loopLines.InsertCellPoint(8) loopLines.InsertNextCell(2) loopLines.InsertCellPoint(8) loopLines.InsertCellPoint(9) loopLines.InsertNextCell(2) loopLines.InsertCellPoint(3) loopLines.InsertCellPoint(6) contours = vtk.vtkContourLoopExtraction() contours.SetInputData(loopData) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(contours.GetOutputPort()) #mapper.SetInputData(loopData) actor = vtk.vtkActor() actor.SetMapper(mapper) ren.AddActor(actor) renWin.Render() #iren.Start()
def runExtrusionExample(seg, dsm, dtm, dest, debug=False, label=None, no_dec=False, no_render=False): # Read the terrain data dtmReader = vtk.vtkGDALRasterReader() dtmReader.SetFileName(dtm) dtmReader.Update() # Range of terrain data lo = dtmReader.GetOutput().GetScalarRange()[0] hi = dtmReader.GetOutput().GetScalarRange()[1] bds = dtmReader.GetOutput().GetBounds() #print("Bounds: {0}".format(bds)) extent = dtmReader.GetOutput().GetExtent() #print("Extent: {0}".format(extent)) origin = dtmReader.GetOutput().GetOrigin() #print("Origin: {0}".format(origin)) spacing = dtmReader.GetOutput().GetSpacing() #print("Spacing: {0}".format(spacing)) # Convert the terrain into a polydata. surface = vtk.vtkImageDataGeometryFilter() surface.SetInputConnection(dtmReader.GetOutputPort()) # Make sure the polygons are planar, so need to use triangles. tris = vtk.vtkTriangleFilter() tris.SetInputConnection(surface.GetOutputPort()) # Warp the surface by scalar values warp = vtk.vtkWarpScalar() warp.SetInputConnection(tris.GetOutputPort()) warp.SetScaleFactor(1) warp.UseNormalOn() warp.SetNormal(0, 0, 1) warp.Update() # Read the segmentation of buildings segmentationReader = vtk.vtkGDALRasterReader() segmentationReader.SetFileName(seg) segmentationReader.Update() segmentation = segmentationReader.GetOutput() scalarName = segmentation.GetPointData().GetScalars().GetName() segmentationNp = dsa.WrapDataObject(segmentation) scalars = segmentationNp.PointData[scalarName] labels = numpy.unique(scalars) print("All labels: {}".format(labels)) if (debug): segmentationWriter = vtk.vtkXMLImageDataWriter() segmentationWriter.SetFileName("segmentation.vti") segmentationWriter.SetInputConnection( segmentationReader.GetOutputPort()) segmentationWriter.Update() segmentation = segmentationReader.GetOutput() sb = segmentation.GetBounds() print("segmentation bounds: \t{}".format(sb)) # Extract polygons contours = vtk.vtkDiscreteFlyingEdges2D() #contours = vtk.vtkMarchingSquares() contours.SetInputConnection(segmentationReader.GetOutputPort()) if (label): labels = label contours.SetNumberOfContours(len(labels)) for i in range(len(labels)): contours.SetValue(i, labels[i]) #print("DFE: {0}".format(contours.GetOutput())) if (debug): contoursWriter = vtk.vtkXMLPolyDataWriter() contoursWriter.SetFileName("contours.vtp") contoursWriter.SetInputConnection(contours.GetOutputPort()) contoursWriter.Update() contoursData = contours.GetOutput() cb = contoursData.GetBounds() print("contours bounds: \t{}".format(cb)) if (not no_dec): # combine lines into a polyline stripperContours = vtk.vtkStripper() stripperContours.SetInputConnection(contours.GetOutputPort()) stripperContours.SetMaximumLength(3000) if (debug): stripperWriter = vtk.vtkXMLPolyDataWriter() stripperWriter.SetFileName("stripper.vtp") stripperWriter.SetInputConnection(stripperContours.GetOutputPort()) stripperWriter.Update() # decimate polylines decimateContours = vtk.vtkDecimatePolylineFilter() decimateContours.SetMaximumError(0.01) decimateContours.SetInputConnection(stripperContours.GetOutputPort()) if (debug): decimateWriter = vtk.vtkXMLPolyDataWriter() decimateWriter.SetFileName("decimate.vtp") decimateWriter.SetInputConnection(decimateContours.GetOutputPort()) decimateWriter.Update() contours = decimateContours # Create loops loops = vtk.vtkContourLoopExtraction() loops.SetInputConnection(contours.GetOutputPort()) if (debug): loopsWriter = vtk.vtkXMLPolyDataWriter() loopsWriter.SetFileName("loops.vtp") loopsWriter.SetInputConnection(loops.GetOutputPort()) loopsWriter.Update() # Read the DSM dsmReader = vtk.vtkGDALRasterReader() dsmReader.SetFileName(dsm) dsmReader.Update() fit = vtk.vtkFitToHeightMapFilter() fit.SetInputConnection(loops.GetOutputPort()) fit.SetHeightMapConnection(dsmReader.GetOutputPort()) fit.UseHeightMapOffsetOn() fit.SetFittingStrategyToPointMaximumHeight() # Extrude polygon down to surface extrude = vtk.vtkTrimmedExtrusionFilter() #extrude.SetInputData(polygons) extrude.SetInputConnection(fit.GetOutputPort()) extrude.SetTrimSurfaceConnection(warp.GetOutputPort()) extrude.SetExtrusionDirection(0, 0, 1) extrude.CappingOn() extrudeWriter = vtk.vtkXMLPolyDataWriter() extrudeWriter.SetFileName(dest) extrudeWriter.SetInputConnection(extrude.GetOutputPort()) extrudeWriter.Update() if (not no_render): # Create the RenderWindow, Renderer # ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Create pipeline. Load terrain data. lut = vtk.vtkLookupTable() lut.SetHueRange(0.6, 0) lut.SetSaturationRange(1.0, 0) lut.SetValueRange(0.5, 1.0) # Show the terrain dtmMapper = vtk.vtkPolyDataMapper() dtmMapper.SetInputConnection(warp.GetOutputPort()) dtmMapper.SetScalarRange(lo, hi) dtmMapper.SetLookupTable(lut) dtmActor = vtk.vtkActor() dtmActor.SetMapper(dtmMapper) # show the buildings trisExtrude = vtk.vtkTriangleFilter() trisExtrude.SetInputConnection(extrude.GetOutputPort()) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(trisExtrude.GetOutputPort()) mapper.ScalarVisibilityOff() actor = vtk.vtkActor() actor.SetMapper(mapper) # Render it ren.AddActor(dtmActor) ren.AddActor(actor) ren.GetActiveCamera().Elevation(-60) ren.ResetCamera() renWin.Render() iren.Start()
def main(args): parser = argparse.ArgumentParser( description='Generate building outlines given a segmentation map') parser.add_argument("segmentation", help="Input image file (tif) with labeled buildings") parser.add_argument( "outlines", help="Output vector file (geojson) with building outlines") parser.add_argument('-l', "--label", type=int, nargs="*", help="Label value(s) used for buildings outlines." "If not specified, all values are used.") parser.add_argument("--no_decimation", action="store_true", help="Do not decimate the contours") parser.add_argument("--debug", action="store_true", help="Save intermediate results") args = parser.parse_args(args) # Read the segmentation of buildings segmentationReader = vtk.vtkGDALRasterReader() segmentationReader.SetFileName(args.segmentation) segmentationReader.Update() # MAP_PROJECTION is not available for some reason. # proj4Srs = segmentationReader.GetOutput().GetFieldData(). # GetArray('MAP_PROJECTION').GetValue(0) segmentationReaderGdal = gdal.Open(args.segmentation, gdal.GA_ReadOnly) segmentationSrsWkt = segmentationReaderGdal.GetProjection() segmentationC2p = vtk.vtkCellDataToPointData() segmentationC2p.SetInputConnection(segmentationReader.GetOutputPort()) segmentationC2p.PassCellDataOn() segmentationC2p.Update() segmentation = segmentationC2p.GetOutput() scalarName = segmentation.GetCellData().GetScalars().GetName() segmentationNp = dsa.WrapDataObject(segmentation) scalars = segmentationNp.CellData[scalarName] labels = numpy.unique(scalars) if (args.debug): segmentationWriter = vtk.vtkXMLImageDataWriter() segmentationWriter.SetFileName("segmentation.vti") segmentationWriter.SetInputConnection( segmentationReader.GetOutputPort()) segmentationWriter.Update() sb = segmentation.GetBounds() print("segmentation bounds: \t{}".format(sb)) # Extract polygons contours = vtk.vtkDiscreteFlyingEdges2D() contours.SetInputConnection(segmentationC2p.GetOutputPort()) if (args.label): print("Contouring on {} of {}".format(args.label, labels)) labels = args.label else: print("Contouring on {} of {}".format(labels, labels)) contours.SetNumberOfContours(len(labels)) for i in range(len(labels)): contours.SetValue(i, labels[i]) if (args.debug): contoursWriter = vtk.vtkXMLPolyDataWriter() contoursWriter.SetFileName("contours.vtp") contoursWriter.SetInputConnection(contours.GetOutputPort()) contoursWriter.Update() contoursData = contours.GetOutput() cb = contoursData.GetBounds() print("contours bounds: \t{}".format(cb)) if (not args.no_decimation): # combine lines into a polyline stripperContours = vtk.vtkStripper() stripperContours.SetInputConnection(contours.GetOutputPort()) stripperContours.SetMaximumLength(3000) if (args.debug): stripperWriter = vtk.vtkXMLPolyDataWriter() stripperWriter.SetFileName("stripper.vtp") stripperWriter.SetInputConnection(stripperContours.GetOutputPort()) stripperWriter.Update() # decimate polylines decimateContours = vtk.vtkDecimatePolylineFilter() decimateContours.SetMaximumError(0.01) decimateContours.SetInputConnection(stripperContours.GetOutputPort()) if (args.debug): decimateWriter = vtk.vtkXMLPolyDataWriter() decimateWriter.SetFileName("decimate.vtp") decimateWriter.SetInputConnection(decimateContours.GetOutputPort()) decimateWriter.Update() contours = decimateContours # Create loops loopsExtraction = vtk.vtkContourLoopExtraction() loopsExtraction.SetInputConnection(contours.GetOutputPort()) loopsExtraction.Update() loops = loopsExtraction.GetOutput() if (args.debug): loopsWriter = vtk.vtkXMLPolyDataWriter() loopsWriter.SetFileName("loops.vtp") loopsWriter.SetInputConnection(loopsExtraction.GetOutputPort()) loopsWriter.Update() # Create the output Driver outDriver = ogr.GetDriverByName('GeoJSON') # spatial reference # outSrs = OGRSpatialReference() # Create the output GeoJSON outDataSource = outDriver.CreateDataSource(args.outlines) outSrs = osr.SpatialReference(segmentationSrsWkt) outLayer = outDataSource.CreateLayer("buildings", srs=outSrs, geom_type=ogr.wkbPolygon) # Get the output Layer's Feature Definition featureDefn = outLayer.GetLayerDefn() polys = loops.GetPolys() polys.InitTraversal() idList = vtk.vtkIdList() while polys.GetNextCell(idList): ring = ogr.Geometry(ogr.wkbLinearRing) for i in range(idList.GetNumberOfIds()): point = loops.GetPoint(idList.GetId(i)) ring.AddPoint(point[0], point[1]) idList.Initialize() # create a polygon geometry poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) # create a new feature that contains the geometry outFeature = ogr.Feature(featureDefn) outFeature.SetGeometry(poly) # add the feature to the layer outLayer.CreateFeature(outFeature) outFeature = None # Save and close DataSources outDataSource = None
def main(args): # Demonstrate generation of extruded objects from a segmentation map, where # the extrusion is trimmed by a terrain surface. parser = argparse.ArgumentParser( description= 'Generate extruded buildings given a segmentation map, DSM and DTM') parser.add_argument("segmentation", help="Image with labeled buildings") parser.add_argument("dsm", help="Digital surface model (DSM)") parser.add_argument("dtm", help="Digital terain model (DTM)") parser.add_argument("destination", help="Extruded buildings polygonal file (.vtp)") parser.add_argument( '-l', "--label", type=int, nargs="*", help="Label value(s) used for buildings outlines." "If not specified, [6, 17] (buildings, roads) are used.") parser.add_argument("--no_decimation", action="store_true", help="Do not decimate the contours") parser.add_argument("--no_render", action="store_true", help="Do not render") parser.add_argument("--debug", action="store_true", help="Save intermediate results") args = parser.parse_args(args) # Read the terrain data print("Reading and warping the DTM ...") dtmReader = vtk.vtkGDALRasterReader() dtmReader.SetFileName(args.dtm) dtmC2p = vtk.vtkCellDataToPointData() dtmC2p.SetInputConnection(dtmReader.GetOutputPort()) dtmC2p.Update() # Range of terrain data lo = dtmC2p.GetOutput().GetScalarRange()[0] hi = dtmC2p.GetOutput().GetScalarRange()[1] # Convert the terrain into a polydata. surface = vtk.vtkImageDataGeometryFilter() surface.SetInputConnection(dtmC2p.GetOutputPort()) # Make sure the polygons are planar, so need to use triangles. tris = vtk.vtkTriangleFilter() tris.SetInputConnection(surface.GetOutputPort()) # Warp the surface by scalar values warp = vtk.vtkWarpScalar() warp.SetInputConnection(tris.GetOutputPort()) warp.SetScaleFactor(1) warp.UseNormalOn() warp.SetNormal(0, 0, 1) warp.Update() # Read the segmentation of buildings. Original data in GDAL is cell data. # Point data is interpolated. print("Reading the segmentation ...") segmentationReader = vtk.vtkGDALRasterReader() segmentationReader.SetFileName(args.segmentation) segmentationC2p = vtk.vtkCellDataToPointData() segmentationC2p.SetInputConnection(segmentationReader.GetOutputPort()) segmentationC2p.PassCellDataOn() segmentationC2p.Update() segmentation = segmentationC2p.GetOutput() if (args.debug): segmentationWriter = vtk.vtkXMLImageDataWriter() segmentationWriter.SetFileName("segmentation.vti") segmentationWriter.SetInputConnection(segmentationC2p.GetOutputPort()) segmentationWriter.Update() segmentation = segmentationC2p.GetOutput() sb = segmentation.GetBounds() print("segmentation bounds: \t{}".format(sb)) # Extract polygons # as an alternative, could use vtk.vtkMarchingSquares() contours = vtk.vtkDiscreteFlyingEdges2D() contours.SetInputConnection(segmentationC2p.GetOutputPort()) # default labels # 2 -- no building # 6 -- building # 17 -- road or highway # 65 -- don't score labels = [6, 17] if (args.label): labels = args.label if (args.debug): scalarName = segmentation.GetCellData().GetScalars().GetName() segmentationNp = dsa.WrapDataObject(segmentation) scalars = segmentationNp.CellData[scalarName] allLabels = numpy.unique(scalars) print("Contouring on labels: {} of {}".format(labels, allLabels)) else: print("Contouring on labels: {}".format(labels)) contours.SetNumberOfContours(len(labels)) for i in range(len(labels)): contours.SetValue(i, labels[i]) if (args.debug): contoursWriter = vtk.vtkXMLPolyDataWriter() contoursWriter.SetFileName("contours.vtp") contoursWriter.SetInputConnection(contours.GetOutputPort()) contoursWriter.Update() contoursData = contours.GetOutput() cb = contoursData.GetBounds() print("contours bounds: \t{}".format(cb)) if (not args.no_decimation): print("Decimating the contours ...") # combine lines into a polyline stripperContours = vtk.vtkStripper() stripperContours.SetInputConnection(contours.GetOutputPort()) stripperContours.SetMaximumLength(3000) if (args.debug): stripperWriter = vtk.vtkXMLPolyDataWriter() stripperWriter.SetFileName("stripper.vtp") stripperWriter.SetInputConnection(stripperContours.GetOutputPort()) stripperWriter.Update() # decimate polylines decimateContours = vtk.vtkDecimatePolylineFilter() decimateContours.SetMaximumError(0.01) decimateContours.SetInputConnection(stripperContours.GetOutputPort()) if (args.debug): decimateWriter = vtk.vtkXMLPolyDataWriter() decimateWriter.SetFileName("decimate.vtp") decimateWriter.SetInputConnection(decimateContours.GetOutputPort()) decimateWriter.Update() contours = decimateContours # Create loops print("Creating the loops ...") loops = vtk.vtkContourLoopExtraction() loops.SetInputConnection(contours.GetOutputPort()) if (args.debug): loopsWriter = vtk.vtkXMLPolyDataWriter() loopsWriter.SetFileName("loops.vtp") loopsWriter.SetInputConnection(loops.GetOutputPort()) loopsWriter.Update() # Read the DSM print("Reading the DSM ...") dsmReader = vtk.vtkGDALRasterReader() dsmReader.SetFileName(args.dsm) dsmC2p = vtk.vtkCellDataToPointData() dsmC2p.SetInputConnection(dsmReader.GetOutputPort()) dsmC2p.Update() print("Extruding the buildings ...") fit = vtk.vtkFitToHeightMapFilter() fit.SetInputConnection(loops.GetOutputPort()) fit.SetHeightMapConnection(dsmC2p.GetOutputPort()) fit.UseHeightMapOffsetOn() fit.SetFittingStrategyToPointMaximumHeight() if (args.debug): fitWriter = vtk.vtkXMLPolyDataWriter() fitWriter.SetFileName("fit.vtp") fitWriter.SetInputConnection(fit.GetOutputPort()) fitWriter.Update() # Extrude polygon down to surface extrude = vtk.vtkTrimmedExtrusionFilter() extrude.SetInputConnection(fit.GetOutputPort()) extrude.SetTrimSurfaceConnection(warp.GetOutputPort()) extrude.SetExtrusionDirection(0, 0, 1) extrude.CappingOn() extrudeWriter = vtk.vtkXMLPolyDataWriter() extrudeWriter.SetFileName(args.destination) extrudeWriter.SetInputConnection(extrude.GetOutputPort()) extrudeWriter.Update() if (not args.no_render): # Create the RenderWindow, Renderer # ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Create pipeline. Load terrain data. lut = vtk.vtkLookupTable() lut.SetHueRange(0.6, 0) lut.SetSaturationRange(1.0, 0) lut.SetValueRange(0.5, 1.0) # Show the terrain dtmMapper = vtk.vtkPolyDataMapper() dtmMapper.SetInputConnection(warp.GetOutputPort()) dtmMapper.SetScalarRange(lo, hi) dtmMapper.SetLookupTable(lut) dtmActor = vtk.vtkActor() dtmActor.SetMapper(dtmMapper) # show the buildings trisExtrude = vtk.vtkTriangleFilter() trisExtrude.SetInputConnection(extrude.GetOutputPort()) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(trisExtrude.GetOutputPort()) mapper.ScalarVisibilityOff() actor = vtk.vtkActor() actor.SetMapper(mapper) # Render it ren.AddActor(dtmActor) ren.AddActor(actor) ren.GetActiveCamera().Elevation(-60) ren.ResetCamera() renWin.Render() iren.Start()
tubes.CappingOn() tubes.SidesShareVerticesOff() tubes.SetNumberOfSides(12) tubes.SetRadius(1.0) edgeMapper = vtk.vtkPolyDataMapper() edgeMapper.ScalarVisibilityOff() edgeMapper.SetInputConnection(tubes.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(edgeMapper) prop = actor.GetProperty() prop.SetColor(yellow) actors.append(actor) extractor = vtk.vtkContourLoopExtraction() extractor.SetInputConnection(cutStrips.GetOutputPort()) extractor.SetNormal(0,0.0,1.0) if os.name == 'nt': extractor.SetOutputModeToPolygons() extractor.SetLoopClosureToAll() extractor.Update() nextContour = extractor.GetOutput() appendFilter.AddInputData(nextContour) appendFilter.Update() renderers[2].SetBackground(.1,.2,.3)