vtk C# imagedata转polydata

2023-12-20 09:31:03

1.效果图
在这里插入图片描述
2.代码

 private void imageDataToPoly()
        {
            // Create an image
            vtkImageData imageData = vtkImageData.New();
            //定义容差
            double resolution = 0.2;

            //移除所有actor
            //读取文件解析
            string file = @"0202-shuixia.XYZ";
            string xyzStr = File.ReadAllText(file);
            string[] xyzStrArr = xyzStr.Split('\n');
            int len = xyzStrArr.Length - 1;
            //计算最大最小值
            double min_value = double.MaxValue;
            double max_value = double.MinValue;
            float xMin = float.MaxValue;
            float xMax = float.MinValue;
            float yMin = float.MaxValue;
            float yMax = float.MinValue;
            for (int i = 0; i < len; i++)
            {
                string str = xyzStrArr[i];
                string[] xyz = str.Split(' ');
                double x = double.Parse(xyz[0]);
                double y = double.Parse(xyz[1]);
                double z = double.Parse(xyz[2]);
                if (z > max_value) max_value = z;
                if (z < min_value) min_value = z;
                if (x > xMax) xMax = (float)x;
                if (x < xMin) xMin = (float)x;
                if (y > yMax) yMax = (float)y;
                if (y < yMin) yMin = (float)y;

            }
            float xdiff = xMax - xMin;
            float ydiff = yMax - yMin;
            int width = (int)(xdiff / resolution) + 1;
            int height = (int)(ydiff / resolution) + 1;


            //设置图像的信息
            imageData.SetDimensions(width, height, 1);
            imageData.SetSpacing(1, 1, 1);
            imageData.SetOrigin(0, 0, 0);
            imageData.AllocateScalars(11, 1);
            var scalars = imageData.GetPointData().GetScalars();
            double[,] imageValues = new double[height, width];
            for (int y = 0; y < imageValues.GetLength(0); y++)
            {
                for (int x = 0; x < imageValues.GetLength(1); x++)
                {
                    imageValues[y, x] = min_value;
                }
            }
            for (int i = 0; i < len; i++)
            {
                string str = xyzStrArr[i];
                string[] xyz = str.Split(' ');
                double x = double.Parse(xyz[0]) - xMin;
                double y = double.Parse(xyz[1]) - yMin;
                double z = double.Parse(xyz[2]);
                int w = (int)(x / resolution);
                int h = (int)(y / resolution);
                imageValues[h, w] = z;
            }

            for (int y = 0; y < imageValues.GetLength(0); y++)
            {
                for (int x = 0; x < imageValues.GetLength(1); x++)
                {
                    scalars.SetTuple1(width * y + x, imageValues[y, x]);
                }
            }


            vtkGreedyTerrainDecimation decimation = vtkGreedyTerrainDecimation.New();
            decimation.SetInputData(imageData);
            decimation.Update();
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
            mapper.SetInputConnection(decimation.GetOutputPort());
            var lut = VtkUtils.getLookupTable(0, min_value, max_value, 256);
            mapper.SetLookupTable(lut);
            mapper.UseLookupTableScalarRangeOn();
            // actor
            vtkActor actor = vtkActor.New();
            actor.SetMapper(mapper);
            actor.GetProperty().SetInterpolationToFlat();
            actor.GetProperty().EdgeVisibilityOff();
            actor.GetProperty().SetEdgeColor(1, 0, 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);
        }

3.vtkGreedyTerrainDecimation
主要是使用这个类来进行imagedata转polydata,如果你的图像数据类似于地形等数据,可以使用该方法来将imagedata转换成polydata,此外,该类还可以进行减面和插值。

文章来源:https://blog.csdn.net/weixin_44265800/article/details/135080260
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。