class vtkPointCloud(): shperical_to_xyz_trans = vtk.vtkSphericalTransform() radian_scaling = math.pi / 180.0 def __init__(self, pcIndex=0, nPartitions=1): self.nPartitions = nPartitions self.polydata = None self.vardata = None self.vrange = None self.np_index_seq = None self.np_cell_data = None self.points = None self.pcIndex = pcIndex self.earth_radius = 100.0 self.spherical_scaling = 0.4 self.vtk_planar_points = None self.vtk_spherical_points = None self.np_points_data = None self.topo = PlotType.Planar self.grid = None # self.threshold_target = "vardata" self.current_scalar_range = None self.nlevels = None self.current_subset_specs = None self.updated_subset_specs = None self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetScalarModeToUsePointData() self.mapper.SetColorModeToMapScalars() self.actor = vtk.vtkActor() self.actor.SetMapper(self.mapper) def getPoint(self, iPt): try: dval = self.vardata[iPt] pt = self.vtk_planar_points.GetPoint(iPt) self.printLogMessage( " getPoint[%d/%d]: dval=%s, pt=%s " % (iPt, self.vardata.shape[0], str(dval), str(pt))) except CDMSError, err: print >> sys.stderr, "Pick Error for point %d: %s" % (iPt, str(err)) print >> sys.stderr, "Vardata(%s) shape: %s " % ( self.vardata.__class__.__name__, str(self.vardata.shape)) return None, None return pt, dval
# Create the RenderWindow, Renderer and both Actors # ren1 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # avoid the singularity at the Z axis using 0.0001 radian offset plane = vtk.vtkPlaneSource() plane.SetOrigin(1.0,expr.expr(globals(), locals(),["3.14159265359","-","0.0001"]),0.0) plane.SetPoint1(1.0,expr.expr(globals(), locals(),["3.14159265359","-","0.0001"]),6.28318530719) plane.SetPoint2(1.0,0.0001,0.0) plane.SetXResolution(19) plane.SetYResolution(9) transform = vtk.vtkSphericalTransform() tpoly = vtk.vtkTransformPolyDataFilter() tpoly.SetInputConnection(plane.GetOutputPort()) tpoly.SetTransform(transform) # also cover the inverse transformation by going back and forth tpoly2 = vtk.vtkTransformPolyDataFilter() tpoly2.SetInputConnection(tpoly.GetOutputPort()) tpoly2.SetTransform(transform.GetInverse()) tpoly3 = vtk.vtkTransformPolyDataFilter() tpoly3.SetInputConnection(tpoly2.GetOutputPort()) tpoly3.SetTransform(transform) mapper = vtk.vtkDataSetMapper() mapper.SetInputConnection(tpoly3.GetOutputPort()) earth = vtk.vtkPNMReader() earth.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/earth.ppm") texture = vtk.vtkTexture()
ren1 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # avoid the singularity at the Z axis using 0.0001 radian offset plane = vtk.vtkPlaneSource() plane.SetOrigin( 1.0, expr.expr(globals(), locals(), ["3.14159265359", "-", "0.0001"]), 0.0) plane.SetPoint1( 1.0, expr.expr(globals(), locals(), ["3.14159265359", "-", "0.0001"]), 6.28318530719) plane.SetPoint2(1.0, 0.0001, 0.0) plane.SetXResolution(19) plane.SetYResolution(9) transform = vtk.vtkSphericalTransform() tpoly = vtk.vtkTransformPolyDataFilter() tpoly.SetInputConnection(plane.GetOutputPort()) tpoly.SetTransform(transform) # also cover the inverse transformation by going back and forth tpoly2 = vtk.vtkTransformPolyDataFilter() tpoly2.SetInputConnection(tpoly.GetOutputPort()) tpoly2.SetTransform(transform.GetInverse()) tpoly3 = vtk.vtkTransformPolyDataFilter() tpoly3.SetInputConnection(tpoly2.GetOutputPort()) tpoly3.SetTransform(transform) mapper = vtk.vtkDataSetMapper() mapper.SetInputConnection(tpoly3.GetOutputPort()) earth = vtk.vtkPNMReader() earth.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/earth.ppm") texture = vtk.vtkTexture()
def main(): namedColors = vtk.vtkNamedColors() sphericalTransform = vtk.vtkSphericalTransform() dims = [SIZE_X, SIZE_Y, SIZE_Z] # Structured Grid because space between points is always the same structuredGrid = vtk.vtkStructuredGrid() structuredGrid.SetDimensions(dims) # Points to insert into Structured Grid points = vtk.vtkPoints() # Array that saves only the heights, used for color heights = vtk.vtkIntArray() x = 0 with open(FILENAME) as f: next(f) for line in f: loadingBar(x, SIZE_X, 'Loading file') x += 1 y = 0 for i in line.split(): y += 1 # Allows to modify the sea level currentValue = max(SEA_LEVEL, int(i)) height = EARTH_RADIUS + currentValue heights.InsertNextValue(currentValue) longitude = MIN_LONG + y * ((MAX_LONG - MIN_LONG) / SIZE_Y) latitude = MIN_LAT + x * ((MAX_LAT - MIN_LAT) / SIZE_X) p = [height, math.radians(latitude), math.radians(longitude)] # Applies a spherical transform to the point, given a distance (height) # and two angles (longitude, latitude) expressed in radians # Apply the transform to the point p p = sphericalTransform.TransformPoint(p) points.InsertNextPoint(p) structuredGrid.SetPoints(points) # Get the range (min, max) values of the array a, b = heights.GetValueRange() # Water detection, we save a new array of index to modify after compute of said points waterIndexes = [] numberOfPoints = structuredGrid.GetNumberOfPoints() for i in range(0, numberOfPoints): loadingBar(i, numberOfPoints, 'Detecting water') if isPointWater(heights, i): waterIndexes.append(i) # Setting height of water to 0 for index in waterIndexes: heights.SetValue(index, 0) structuredGrid.GetPointData().SetScalars(heights) def addRGBPoint(p, r, g, b): """ Adds a RGB point to the lookup table. Divides the color by 255 Example: (3000, 45, 45, 45) :param p: point (height) :param r: red :param g: green :param b: blue :return: """ lut.AddRGBPoint(p, r / 255, g / 255, b / 255) lut = vtk.vtkColorTransferFunction() # The water scalars has a value 0 which is below the color range lut.SetBelowRangeColor(64 / 255, 61 / 255, 128 / 255) lut.SetUseBelowRangeColor(True) # Minimal altitude is forest addRGBPoint(a, 53, 96, 48) # Forest limit addRGBPoint(FOREST_LIMIT, 237, 215, 187) # Snow limit addRGBPoint(SNOW_LIMIT, 255, 255, 255) # Upper bound of scale addRGBPoint(b, 255, 255, 255) lut.Build() structuredGridMapper = vtk.vtkDataSetMapper() structuredGridMapper.SetInputData(structuredGrid) structuredGridMapper.SetLookupTable(lut) structuredGridMapper.SetScalarRange(a, b) structuredGridMapper.SetScalarModeToUsePointData() # A colorbar to display the colormap scalarBar = vtk.vtkScalarBarActor() scalarBar.SetLookupTable(structuredGridMapper.GetLookupTable()) scalarBar.SetTitle("Altitude") scalarBar.SetOrientationToVertical() scalarBar.GetLabelTextProperty().SetColor(0, 0, 0) scalarBar.GetTitleTextProperty().SetColor(0, 0, 0) scalarBar.DrawBelowRangeSwatchOn() scalarBar.SetBelowRangeAnnotation("Water") scalarBar.AnnotationTextScalingOn() scalarBar.GetAnnotationTextProperty().SetColor(0, 0, 0) scalarBar.SetLabelFormat("%4.0f") # position it in window coord = scalarBar.GetPositionCoordinate() coord.SetCoordinateSystemToNormalizedViewport() coord.SetValue(0.82, 0.1) scalarBar.SetWidth(.15) scalarBar.SetHeight(.7) structuredGridActor = vtk.vtkActor() structuredGridActor.SetMapper(structuredGridMapper) # Create the usual rendering stuff renderer = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(renderer) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) renderer.AddActor(structuredGridActor) renderer.AddActor(scalarBar) renderer.SetBackground(namedColors.GetColor3d("Grey")) camera = vtk.vtkCamera() # The focal point is the center of the map center = [EARTH_RADIUS, math.radians(MIN_LAT + (MAX_LAT - MIN_LAT) / 2.0), math.radians(MIN_LONG + (MAX_LONG - MIN_LONG) / 2.0) ] camera.SetFocalPoint(sphericalTransform.TransformPoint(center)) # The camera is positioned in front of the center of the map center[0] += CAMERA_ALTITUDE camera.SetPosition(sphericalTransform.TransformPoint(center)) # Rotate the camera to display the map correctly camera.Roll(-94) renderer.SetActiveCamera(camera) renderer.ResetCameraClippingRange() renWin.SetSize(1000, 1000) # Interact with the data. renWin.Render() iren.Start()