示例#1
0
            internal void CreatePolydata(ref vtkPolyData polydata)
            {
                vtkPoints points = vtkPoints.New();

                points.InsertNextPoint(this.origin[0], this.origin[1], this.origin[2]);

                float[] x = new float[3];
                float[] y = new float[3];
                float[] z = new float[3];

                Add(this.origin, this.xDirection, ref x);
                Add(this.origin, this.yDirection, ref y);
                Add(this.origin, this.zDirection, ref z);

                points.InsertNextPoint(x[0], x[1], x[2]);
                points.InsertNextPoint(y[0], y[1], y[2]);
                points.InsertNextPoint(z[0], z[1], z[2]);

                polydata.SetPoints(points);

                vtkVertexGlyphFilter vertexGlyphFilter = vtkVertexGlyphFilter.New();

#if VTK_MAJOR_VERSION_5
                vertexGlyphFilter.AddInput(polydata);
#else
                vertexGlyphFilter.AddInputData(polydata);
#endif
                vertexGlyphFilter.Update();
                polydata.ShallowCopy(vertexGlyphFilter.GetOutput());
            }
示例#2
0
        public void SetMapper(vtkPolyData data)
        {
            vtkVertexGlyphFilter filter = vtkVertexGlyphFilter.New();

            filter.SetInput(data);
            filter.Update();


            polyDataMapper = vtkPolyDataMapper.New();
            polyDataMapper.SetInputConnection(filter.GetOutputPort());
            polyDataMapper.Update();
        }
示例#3
0
        void AddVertex(vtkPoints points, vtkUnstructuredGrid grid)
        {//在每个points的位置上填上一个vertex
         //m_verticesCell = new vtkCellArray();
         //for (int i = 0; i < points.GetNumberOfPoints(); i++)
         //{
         //    vtkVertex vertex = new vtkVertex();

            //    vertex.GetPointIds().SetId(0, i);
            //    grid.InsertNextCell((int)VTKCellType.VTK_VERTEX, vertex.GetPointIds());

            //   // m_cells .InsertNextCell(vertex);
            //}
            vtkVertexGlyphFilter filter = new vtkVertexGlyphFilter();

            this.GetProperty().SetPointSize(5);
        }
示例#4
0
        public void ReadPointIntoObject(RenderWindowControl renderWindowControl, List <nvmPointModel> listPointModel)
        {
            vtkUnsignedCharArray colors = vtkUnsignedCharArray.New();

            colors.SetNumberOfComponents(3);
            colors.SetName("Colors");
            vtkPoints points = vtkPoints.New();

            foreach (var point in listPointModel)
            {
                colors.InsertNextValue(byte.Parse(point.color.X.ToString(), CultureInfo.InvariantCulture));
                colors.InsertNextValue(byte.Parse(point.color.Y.ToString(), CultureInfo.InvariantCulture));
                colors.InsertNextValue(byte.Parse(point.color.Z.ToString(), CultureInfo.InvariantCulture));
                points.InsertNextPoint(
                    double.Parse(point.position.X.ToString(), CultureInfo.InvariantCulture),
                    double.Parse(point.position.Y.ToString(), CultureInfo.InvariantCulture),
                    double.Parse(point.position.Z.ToString(), CultureInfo.InvariantCulture));
            }

            vtkPolyData polydata = vtkPolyData.New();

            polydata.SetPoints(points);
            polydata.GetPointData().SetScalars(colors);
            vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();

            glyphFilter.SetInputConnection(polydata.GetProducerPort());

            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(glyphFilter.GetOutputPort());
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            actor.GetProperty().SetPointSize(2);
            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl.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);
            renderer.ResetCamera();
        }
示例#5
0
        private void ReadPlainText()
        {
            // Path to vtk data must be set as an environment variable
            // VTK_DATA_ROOT = "C:\VTK\vtkdata-5.8.0"
            vtkTesting test     = vtkTesting.New();
            string     root     = test.GetDataRoot();
            string     filePath = System.IO.Path.Combine(root, @"Data\teapot.xyz");

            FileStream   fs = null;
            StreamReader sr = null;
            String       sLineBuffer;

            String[]  sXYZ;
            char[]    chDelimiter = new char[] { ' ', '\t', ';' };
            double[]  xyz         = new double[3];
            vtkPoints points      = vtkPoints.New();
            int       cnt         = 0;

            try {
                // in case file must be open in another application too use "FileShare.ReadWrite"
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr = new StreamReader(fs);
                while (!sr.EndOfStream)
                {
                    sLineBuffer = sr.ReadLine();
                    cnt++;
                    sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries);
                    if (sXYZ == null || sXYZ.Length != 3)
                    {
                        MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK);
                        return;
                    }
                    xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture);
                    xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture);
                    xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture);
                    points.InsertNextPoint(xyz[0], xyz[1], xyz[2]);
                }
                vtkPolyData polydata = vtkPolyData.New();
                polydata.SetPoints(points);
                vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
                glyphFilter.SetInputConnection(polydata.GetProducerPort());

                // Visualize
                vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
                mapper.SetInputConnection(glyphFilter.GetOutputPort());

                vtkActor actor = vtkActor.New();
                actor.SetMapper(mapper);
                actor.GetProperty().SetPointSize(4);
                actor.GetProperty().SetColor(1, 0.5, 0);
                // 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);
            }
            catch (IOException ex) {
                MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK);
            }
            finally {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                    sr = null;
                }
            }
        }
        private void RenderXYZColor()
        {
            FileStream   fs = null;
            StreamReader sr = null;
            String       sLineBuffer;

            String[]  sXYZ;
            char[]    chDelimiter = new char[] { ' ', '\t', ';' };
            double[]  xyz         = new double[3];
            double[]  rgb         = new double[3];
            vtkPoints points      = vtkPoints.New();
            vtkPoints colors      = vtkPoints.New();
            int       cnt         = 0;

            try
            {
                // in case file must be open in another application too use "FileShare.ReadWrite"
                fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr = new StreamReader(fs);

                vtkDoubleArray colorScalor = new vtkDoubleArray();
                int            n           = 1;
                while (!sr.EndOfStream)
                {
                    sLineBuffer = sr.ReadLine();
                    cnt++;
                    sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries);
                    if (sXYZ == null || sXYZ.Length != 6)
                    {
                        MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK);
                        return;
                    }
                    xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture) * 11100;
                    xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture) * 11100;
                    xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture);

                    rgb[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture);
                    rgb[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture);
                    rgb[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture);

                    points.InsertNextPoint(xyz[0], xyz[1], xyz[2]);
                    colors.InsertNextPoint(rgb[0], rgb[1], rgb[2]);
                    colorScalor.InsertNextTuple1(n++);
                }
                vtkPolyData polydata = vtkPolyData.New();
                polydata.SetPoints(points);
                polydata.GetPointData().SetScalars(colorScalor); //设置点的Scalar(标量)属性

                vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
                glyphFilter.SetInputConnection(polydata.GetProducerPort());

                vtkLookupTable lookupTable = new vtkLookupTable();
                lookupTable.SetNumberOfColors(n);
                // SetSetTableValue(vtkIdType  indx, double r, double g, double  b, double a);
                Random random = new Random();
                for (int i = 0; i < n; i++)
                {
                    double[] tmp = colors.GetPoint(i);
                    double   r   = tmp[0];
                    double   g   = tmp[1];
                    double   b   = tmp[2];
                    lookupTable.SetTableValue(i, r, g, b, 1);
                }

                // Visualize
                vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
                mapper.SetInputConnection(glyphFilter.GetOutputPort());
                mapper.SetLookupTable(lookupTable);
                mapper.SetScalarRange(1, n);

                vtkActor actor = vtkActor.New();
                actor.SetMapper(mapper);
                actor.GetProperty().SetPointSize(1);
                //actor.GetProperty().SetColor(1, 0.5, 0);
                // add our actor to the renderer
                m_Renderer.AddActor(actor);
                imgPropList.Add(actor);

                m_Renderer.ResetCamera();

                //Rerender the screen
                m_RenderWindow.Render();
                m_Renderer.Render();
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                    sr = null;
                }
            }
        }
示例#7
0
        private void SelectAreaClick(vtkObject sender, vtkObjectEventArgs e)
        {
            int[]         clickPos = Inter.GetEventPosition();
            vtkAreaPicker picker   = vtkAreaPicker.New();

            picker.AreaPick(clickPos[0], clickPos[1], clickPos[0] + 100, clickPos[1] + 100, Viewport);

            if (picker.GetActor() != null)
            {
                vtkPlanes          Boundary = picker.GetFrustum();
                vtkExtractGeometry Box      = vtkExtractGeometry.New();
                Box.SetImplicitFunction(Boundary);
                Box.SetInput(picker.GetActor().GetMapper().GetInput());

                vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
                glyphFilter.SetInputConnection(Box.GetOutputPort());
                glyphFilter.Update();

                vtkPolyData         selected = glyphFilter.GetOutput();
                vtkPoints           points   = vtkPoints.New();
                vtkUnstructuredGrid grid     = vtkUnstructuredGrid.New();
                for (int i = 0; i < selected.GetNumberOfPoints(); i++)
                {
                    points.InsertNextPoint(selected.GetPoint(i)[0], selected.GetPoint(i)[1], selected.GetPoint(i)[2]);
                }
                grid.SetPoints(points);
                vtkSphereSource sphere = vtkSphereSource.New();
                sphere.SetPhiResolution(6);
                sphere.SetThetaResolution(6);
                sphere.SetRadius(0.1);
                vtkGlyph3D glyph3D = vtkGlyph3D.New();
                glyph3D.SetInput(grid);
                glyph3D.SetSourceConnection(sphere.GetOutputPort());

                vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
                mapper.SetInputConnection(glyph3D.GetOutputPort());

                //double[] P = new double[3];
                //bool selected = false;
                //vtkPoints points = Faces.GetPoints();
                //double[] ClickedPoint = PointPicker.GetActor().GetMapper().GetInput().GetPoint(PointPicker.GetPointId());
                //for (int i = 0; i < points.GetNumberOfPoints(); i++)
                //{
                //    if (Math.Abs(points.GetPoint(i)[0] - ClickedPoint[0]) < 1e-6 &&
                //        Math.Abs(points.GetPoint(i)[1] - ClickedPoint[1]) < 1e-6 &&
                //        Math.Abs(points.GetPoint(i)[2] - ClickedPoint[2]) < 1e-6)
                //    {
                //        selected = true;
                //        P = points.GetPoint(i);
                //        break;
                //    }
                //}
                //
                //if (selected == true)
                //{
                //    SelectionPoints.InsertNextPoint(P[0], P[1], P[2]);
                //
                //    SelectionGlyph = vtkGlyph3D.New();
                //    SelectionGlyph.SetInput(SelectionPolyData);
                //    SelectionGlyph.SetSourceConnection(SelectionSphere.GetOutputPort());
                //    SelectionMapper.SetInputConnection(SelectionGlyph.GetOutputPort());
                //
                //    // Refresh Viewport
                //    Refresh();
                //}
            }
        }