Ejemplo n.º 1
0
        private static void IterateOverLines()
        {
            double[] origin = new double[] { 0.0, 0.0, 0.0 };
            double[,] p = new double[, ] {
                { 1.0, 0.0, 0.0 },
                { 0.0, 1.0, 0.0 },
                { 0.0, 1.0, 2.0 },
                { 1.0, 2.0, 3.0 }
            };

            // Create a vtkPoints object and store the points in it
            vtkPoints points = vtkPoints.New();

            points.InsertNextPoint(origin[0], origin[1], origin[2]);
            for (int i = 0; i < 4; i++)
            {
                points.InsertNextPoint(p[i, 0], p[i, 1], p[i, 2]);
            }

            // Create a cell array to store the lines in and add the lines to it
            vtkCellArray lines = vtkCellArray.New();

            // Create four lines
            for (int i = 0; i < 4; i++)
            {
                vtkLine line = vtkLine.New();
                line.GetPointIds().SetId(0, i);
                line.GetPointIds().SetId(1, i + 1);
                lines.InsertNextCell(line);
            }

            // Create a polydata to store everything in
            vtkPolyData linesPolyData = vtkPolyData.New();

            // Add the points to the dataset
            linesPolyData.SetPoints(points);

            // Add the lines to the dataset
            linesPolyData.SetLines(lines);

            Console.WriteLine("There are " + linesPolyData.GetNumberOfLines() + " lines.");
            linesPolyData.GetLines().InitTraversal();
            vtkIdList idList = vtkIdList.New();

            while (linesPolyData.GetLines().GetNextCell(idList) != 0)
            {
                Console.WriteLine("Line has " + idList.GetNumberOfIds() + " points.");

                for (int pointId = 0; pointId < idList.GetNumberOfIds(); pointId++)
                {
                    Console.Write(idList.GetId(pointId) + " ");
                }
                Console.Write(Environment.NewLine);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a Unity Mesh from a vtkPolyData.
        /// </summary>
        /// <param name="pd">The vtk poly data.</param>
        /// <returns>The Unity Mesh (without colors).</returns>
        private static Mesh PolyDataToMesh(vtkPolyData pd)
        {
            if (pd == null)
            {
                Debug.LogWarning("No PolyData passed!");
                return null;
            }

            var numVertices = pd.GetNumberOfPoints();
            if (numVertices == 0)
            {
                Debug.LogWarning("No vertices to convert!");
                return null;
            }

            var mesh = new Mesh();

            // Points / Vertices
            var vertices = new Vector3[numVertices];
            for (var i = 0; i < numVertices; ++i)
            {
                var pnt = pd.GetPoint(i);
                // Flip z-up to y-up
                vertices[i] = new Vector3(-(float) pnt[0], (float) pnt[2], (float) pnt[1]);
            }
            mesh.vertices = vertices;

            // Normals
            var vtkNormals = pd.GetPointData().GetNormals();
            if (vtkNormals != null)
            {
                var numNormals = vtkNormals.GetNumberOfTuples();
                var normals = new Vector3[numNormals];
                for (var i = 0; i < numNormals; i++)
                {
                    var normal = vtkNormals.GetTuple3(i);
                    // flip normals ?
                    normals[i] = new Vector3(-(float) normal[0], -(float) normal[1], -(float) normal[2]);
                }
                mesh.normals = normals;
            }
            else
            {
                Debug.Log("No Normals!");
            }

            // Texture coordinates
            var vtkTexCoords = pd.GetPointData().GetTCoords();
            if (vtkTexCoords != null)
            {
                var numCoords = vtkTexCoords.GetNumberOfTuples();
                var uvs = new Vector2[numCoords];
                for (var i = 0; i < numCoords; ++i)
                {
                    var texCoords = vtkTexCoords.GetTuple2(i);
                    uvs[i] = new Vector2((float) texCoords[0], (float) texCoords[1]);
                }
                mesh.uv = uvs;
            }

            // Triangles / Cells
            var numTriangles = pd.GetNumberOfPolys();
            var polys = pd.GetPolys();
            if (polys.GetNumberOfCells() > 0)
            {
                var triangles = new int[numTriangles*3];
                var prim = 0;
                var pts = vtkIdList.New();
                polys.InitTraversal();
                while (polys.GetNextCell(pts) != 0)
                {
                    for (var i = 0; i < pts.GetNumberOfIds(); ++i)
                        triangles[prim*3 + i] = pts.GetId(i);

                    ++prim;
                }
                mesh.SetTriangles(triangles, 0);
                //Mesh.RecalculateNormals();
                mesh.RecalculateBounds();
                return mesh;
            }

            // Lines
            var lines = pd.GetLines();
            if (lines.GetNumberOfCells() > 0)
            {
                var idList = new ArrayList();
                var pts = vtkIdList.New();
                lines.InitTraversal();
                while (lines.GetNextCell(pts) != 0)
                {
                    for (var i = 0; i < pts.GetNumberOfIds() - 1; ++i)
                    {
                        idList.Add(pts.GetId(i));
                        idList.Add(pts.GetId(i + 1));
                    }
                }

                mesh.SetIndices(idList.ToArray(typeof (int)) as int[], MeshTopology.Lines, 0);
                mesh.RecalculateBounds();
                return mesh;
            }

            // Points
            var points = pd.GetVerts();
            var numPointCells = points.GetNumberOfCells();
            if (numPointCells > 0)
            {
                var idList = new ArrayList();
                var pts = vtkIdList.New();
                points.InitTraversal();
                while (points.GetNextCell(pts) != 0)
                {
                    for (int i = 0; i < pts.GetNumberOfIds(); ++i)
                    {
                        idList.Add(pts.GetId(i));
                    }
                }

                mesh.SetIndices(idList.ToArray(typeof (int)) as int[], MeshTopology.Points, 0);
                mesh.RecalculateBounds();
            }

            return mesh;
        }