Example #1
0
        public void WriteVTP(string filePath, List <Vector3d> point_data, List <Vector3> cellPointsList, Dictionary <String, vtkDataArray> scalar_dataArray, List <String> arrayNames)
        {
            vtkPoints points = vtkPoints.New();

            for (int i = 0; i < point_data.Count(); i++)
            {
                points.InsertNextPoint(point_data[i].X, point_data[i].Y, point_data[i].Z);
            }

            vtkPolyData polyData = vtkPolyData.New();

            polyData.SetPoints(points);

            vtkCellArray cellArrayOne = vtkCellArray.New();

            for (int i = 0; i < cellPointsList.Count(); i++)
            {
                vtkTetra tetra = vtkTetra.New();

                tetra.GetPointIds().SetId(0, (long)cellPointsList[i][0]);
                tetra.GetPointIds().SetId(1, (long)cellPointsList[i][1]);
                tetra.GetPointIds().SetId(2, (long)cellPointsList[i][2]);
                tetra.GetPointIds().SetId(3, (long)cellPointsList[i][2]);
                cellArrayOne.InsertNextCell(tetra);
            }

            polyData.SetPolys(cellArrayOne);

            int numberOfScalarData = scalar_dataArray.Count();

            for (int i = 0; i < numberOfScalarData; i++)
            {
                scalar_dataArray.TryGetValue(arrayNames[i], out vtkDataArray scalars);
                polyData.GetPointData().AddArray(scalars);
            }

            vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();

            // add file ending if it is not existent
            string suffix = (".vtp");

            if (!(filePath.EndsWith(suffix)))
            {
                filePath += suffix;
            }

            writer.SetFileName(filePath);
            writer.SetInput(polyData);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.New();

            if (reader.CanReadFile(filePath) == 0)
            {
                //MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            Console.WriteLine("VTP file was writen and is saved at {0}", filePath);
        }
Example #2
0
        public void WriteStructuredGrid(string filePath, List <Vector3d> point_data, Dictionary <String, vtkDataArray> scalar_dataArray, List <String> arrayNames, int numberOfPoints, int[] dimensions)
        {
            vtkPoints points = vtkPoints.New();

            for (int i = 0; i < numberOfPoints; i++)
            {
                points.InsertNextPoint(point_data[i].X, point_data[i].Y, point_data[i].Z);
            }

            vtkStructuredGrid structuredGrid = vtkStructuredGrid.New();

            structuredGrid.SetDimensions(dimensions[0], dimensions[1], dimensions[2]);
            structuredGrid.SetPoints(points);

            int numberOfScalarData = scalar_dataArray.Count();

            for (int i = 0; i < numberOfScalarData; i++)
            {
                scalar_dataArray.TryGetValue(arrayNames[i], out vtkDataArray scalars);
                structuredGrid.GetPointData().AddArray(scalars);
            }

            // add file ending if it is not existent
            string suffix = (".vts");

            if (!(filePath.EndsWith(suffix)))
            {
                filePath += suffix;
            }

            // Write file
            vtkXMLStructuredGridWriter writer = vtkXMLStructuredGridWriter.New();

            writer.SetFileName(filePath);
            writer.SetInput(structuredGrid);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.New();

            if (reader.CanReadFile(filePath) == 0)
            {
                //MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            Console.WriteLine("VTU file was writen and is saved at {0}", filePath);
        }
        private void ReadStructuredGrid()
        {
            // 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\multicomb_0.vts");

            // reader
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.New();

            if (reader.CanReadFile(filePath) == 0)
            {
                MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            reader.SetFileName(filePath);
            reader.Update(); // here we read the file actually

            vtkStructuredGridGeometryFilter geometryFilter = vtkStructuredGridGeometryFilter.New();

            geometryFilter.SetInputConnection(reader.GetOutputPort());
            geometryFilter.Update();

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

            mapper.SetInputConnection(geometryFilter.GetOutputPort());

            //// mapper
            //vtkDataSetMapper mapper = vtkDataSetMapper.New();
            //mapper.SetInputConnection(reader.GetOutputPort());

            // actor
            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);
        }
Example #4
0
        public void Read_StructuredGrid_File(string filename)
        {
            vtkXMLStructuredGridReader reader = new vtkXMLStructuredGridReader();

            reader.SetFileName(filename);

            reader.Update();

            vtkStructuredGrid structuredGrid = reader.GetOutput();



            dimensions = structuredGrid.GetDimensions();

            vtkPoints points = structuredGrid.GetPoints();

            int             numPoints = (int)structuredGrid.GetNumberOfPoints();
            List <Vector3d> point_dat = new List <Vector3d>();

            if (numPoints != 0)
            {
                // Read Point Data
                double[] pt;
                for (int i = 0; i < numPoints; i++)
                {
                    pt = points.GetPoint(i);
                    point_dat.Add(new Vector3d((float)pt[0], (float)pt[1], (float)pt[2]));
                }

                if (this.vertex_data.ContainsKey("vertices"))
                {
                    this.vertex_data["vertices"] = point_dat;
                }
                else
                {
                    this.vertex_data.Add("vertices", point_dat);
                }
                Console.WriteLine("All points read in correctly!");
            }
            vtkPointData scalarValues = structuredGrid.GetPointData();

            // Load point attributes
            this.Load_Point_Attributes(scalarValues);
        }
Example #5
0
        public void WriteSimpleStructuredGrid(string filePath)
        {
            vtkPoints points = vtkPoints.New();

            double x, y, z;

            x = 0.0;
            y = 0.0;
            z = 0.0;

            for (uint k = 0; k < 2; k++)
            {
                z += 2.0;
                for (uint j = 0; j < 3; j++)
                {
                    y += 1.0;
                    for (uint i = 0; i < 2; i++)
                    {
                        x += .5;
                        points.InsertNextPoint(x, y, z);
                    }
                }
            }

            vtkDataArray dataArrayVx;
            vtkDataArray dataArrayVy;
            vtkDataArray dataArrayVz;

            vtkDoubleArray Vx = new vtkDoubleArray();
            vtkDoubleArray Vy = new vtkDoubleArray();
            vtkDoubleArray Vz = new vtkDoubleArray();

            for (int i = 0; i < 12; i++)
            {
                Vx.InsertNextValue(-1 + i);
                Vz.InsertNextValue(2 - i);
                Vy.InsertNextValue(-4 + i);
            }
            dataArrayVx = Vx;
            dataArrayVx.SetName("Vx");
            dataArrayVy = Vy;
            dataArrayVy.SetName("Vy");
            dataArrayVz = Vz;
            dataArrayVz.SetName("Vz");


            vtkStructuredGrid structuredGrid = vtkStructuredGrid.New();

            structuredGrid.SetDimensions(2, 3, 2);
            structuredGrid.SetPoints(points);

            structuredGrid.GetPointData().AddArray(dataArrayVx);
            structuredGrid.GetPointData().AddArray(dataArrayVy);
            structuredGrid.GetPointData().AddArray(dataArrayVz);

            // add file ending if it is not existent
            string suffix = (".vts");

            if (!(filePath.EndsWith(suffix)))
            {
                filePath += suffix;
            }
            // Write file
            vtkXMLStructuredGridWriter writer = vtkXMLStructuredGridWriter.New();

            writer.SetFileName(filePath);
            writer.SetInput(structuredGrid);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.New();

            if (reader.CanReadFile(filePath) == 0)
            {
                //MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            Console.WriteLine("VTU file was writen and is saved at {0}", filePath);
        }
        private void XMLStructuredGridWriter()
        {
            // 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\structuredgrid_test.vts");
            // Create a grid
            vtkStructuredGrid structuredGrid = vtkStructuredGrid.New();
            vtkPoints         points         = vtkPoints.New();

            points.InsertNextPoint(0, 0, 0);
            points.InsertNextPoint(1, 0, 0);
            points.InsertNextPoint(0, 1, 0);
            points.InsertNextPoint(1, 1, 0);
            points.InsertNextPoint(0, 2, 0);
            points.InsertNextPoint(1, 2, 1);

            // Specify the dimensions of the grid
            structuredGrid.SetDimensions(2, 3, 1);
            structuredGrid.SetPoints(points);

            // Write file
            vtkXMLStructuredGridWriter writer = vtkXMLStructuredGridWriter.New();

            writer.SetFileName(filePath);
            writer.SetInput(structuredGrid);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.New();

            if (reader.CanReadFile(filePath) == 0)
            {
                MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            reader.SetFileName(filePath);
            reader.Update(); // here we read the file actually

            vtkStructuredGridGeometryFilter geometryFilter = vtkStructuredGridGeometryFilter.New();

            geometryFilter.SetInputConnection(reader.GetOutputPort());
            geometryFilter.Update();

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

            mapper.SetInputConnection(geometryFilter.GetOutputPort());

            // actor
            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);
        }