public void SetDefaultColor() { vtkUnsignedCharArray cellColors = new vtkUnsignedCharArray(); cellColors.SetNumberOfComponents(3); int cellNum = this.GetMapper().GetInput().GetNumberOfCells(); cellColors.SetNumberOfTuples(cellNum); for (int i = 0; i < cellNum; ++i) { cellColors.SetTuple3(i, this.m_defaultColor[0], this.m_defaultColor[1], this.m_defaultColor[2]); } this.GetMapper().GetInput().GetCellData().SetScalars(cellColors); vtkUnsignedCharArray ptColors = new vtkUnsignedCharArray(); ptColors.SetNumberOfComponents(3); int ptNum = this.GetMapper().GetInput().GetNumberOfPoints(); ptColors.SetNumberOfTuples(ptNum); for (int i = 0; i < ptNum; i++) { ptColors.SetTuple3(i, this.m_defaultColor[0], this.m_defaultColor[1], this.m_defaultColor[2]); } this.GetMapper().GetInput().GetPointData().SetScalars(ptColors); // this.GetMapper().SetScalarModeToUsePointData(); this.GetMapper().SetScalarModeToUseCellData(); this.GetMapper().Update(); }
/// <summary> /// 当有点和cell被选择是更新颜色 /// </summary> public override void UpdateSelectedColor() { ///cell if (m_selectedCellsIds != null && m_selectedCellsIds.Count > 0) { vtkUnsignedCharArray colors = new vtkUnsignedCharArray(); colors.SetNumberOfComponents(3); int cellNum = this.GetMapper().GetInput().GetNumberOfCells(); colors.SetNumberOfTuples(cellNum); for (int i = 0; i < cellNum; ++i) { colors.SetTuple3(i, m_defaultColor [0], m_defaultColor [1], m_defaultColor [2]); } foreach (int cellID in m_selectedCellsIds.Keys) { colors.SetTuple3(cellID, ModelUtils.SelectedColor[0], ModelUtils.SelectedColor[1], ModelUtils.SelectedColor[2]); } this.GetMapper().GetInput().GetCellData().SetScalars(colors); } ///point else if (m_selectedPointsIds != null && m_selectedPointsIds.Count > 0) { vtkUnsignedCharArray ptcolors = new vtkUnsignedCharArray(); ptcolors.SetNumberOfComponents(3); int ptNum = this.GetMapper().GetInput().GetNumberOfPoints(); ptcolors.SetNumberOfTuples(ptNum); for (int i = 0; i < ptNum; ++i) { ptcolors.SetTuple3(i, m_defaultColor[0], m_defaultColor[1], m_defaultColor[2]); } foreach (int ptid in m_selectedPointsIds.Keys) { ptcolors.SetTuple3(ptid, ModelUtils.SelectedColor[0], ModelUtils.SelectedColor[1], ModelUtils.SelectedColor[2]); } this.GetMapper().SetScalarModeToUsePointData(); this.GetMapper().GetInput().GetPointData().SetScalars(ptcolors); //this.GetProperty().SetPointSize(4); //this.GetProperty().SetRepresentationToPoints(); } this.GetMapper().Update(); }
private void ColoredElevationMap() { // Create a grid of points (height/terrian map) vtkPoints points = vtkPoints.New(); uint GridSize = 20; double xx, yy, zz; for (uint x = 0; x < GridSize; x++) { for (uint y = 0; y < GridSize; y++) { xx = x + vtkMath.Random(-.2, .2); yy = y + vtkMath.Random(-.2, .2); zz = vtkMath.Random(-.5, .5); points.InsertNextPoint(xx, yy, zz); } } // Add the grid points to a polydata object vtkPolyData inputPolyData = vtkPolyData.New(); inputPolyData.SetPoints(points); // Triangulate the grid points vtkDelaunay2D delaunay = vtkDelaunay2D.New(); #if VTK_MAJOR_VERSION_5 delaunay.SetInput(inputPolyData); #else delaunay.SetInputData(inputPolyData); #endif delaunay.Update(); vtkPolyData outputPolyData = delaunay.GetOutput(); double[] bounds = outputPolyData.GetBounds(); // Find min and max z double minz = bounds[4]; double maxz = bounds[5]; Debug.WriteLine("minz: " + minz); Debug.WriteLine("maxz: " + maxz); // Create the color map vtkLookupTable colorLookupTable = vtkLookupTable.New(); colorLookupTable.SetTableRange(minz, maxz); colorLookupTable.Build(); // Generate the colors for each point based on the color map vtkUnsignedCharArray colors = vtkUnsignedCharArray.New(); colors.SetNumberOfComponents(3); colors.SetName("Colors"); Debug.WriteLine("There are " + outputPolyData.GetNumberOfPoints() + " points."); #if UNSAFE // fastest way to fill color array colors.SetNumberOfTuples(outputPolyData.GetNumberOfPoints()); unsafe { byte *pColor = (byte *)colors.GetPointer(0).ToPointer(); for (int i = 0; i < outputPolyData.GetNumberOfPoints(); i++) { double[] p = outputPolyData.GetPoint(i); double[] dcolor = colorLookupTable.GetColor(p[2]); Debug.WriteLine("dcolor: " + dcolor[0] + " " + dcolor[1] + " " + dcolor[2]); byte[] color = new byte[3]; for (uint j = 0; j < 3; j++) { color[j] = (byte)(255 * dcolor[j]); } Debug.WriteLine("color: " + color[0] + " " + color[1] + " " + color[2]); *(pColor + 3 * i) = color[0]; *(pColor + 3 * i + 1) = color[1]; *(pColor + 3 * i + 2) = color[2]; } } #else for (int i = 0; i < outputPolyData.GetNumberOfPoints(); i++) { double[] p = outputPolyData.GetPoint(i); double[] dcolor = colorLookupTable.GetColor(p[2]); Debug.WriteLine("dcolor: " + dcolor[0] + " " + dcolor[1] + " " + dcolor[2]); byte[] color = new byte[3]; for (uint j = 0; j < 3; j++) { color[j] = (byte)(255 * dcolor[j]); } Debug.WriteLine("color: " + color[0] + " " + color[1] + " " + color[2]); colors.InsertNextTuple3(color[0], color[1], color[2]); //IntPtr pColor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * 3); //Marshal.Copy(color, 0, pColor, 3); //colors.InsertNextTupleValue(pColor); //Marshal.FreeHGlobal(pColor); } #endif outputPolyData.GetPointData().SetScalars(colors); // Create a mapper and actor vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); #if VTK_MAJOR_VERSION_5 mapper.SetInputConnection(outputPolyData.GetProducerPort()); #else mapper.SetInputData(outputPolyData); #endif vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(0.2, 0.3, 0.4); // add our actor to the renderer renderer.AddActor(actor); }