Beispiel #1
0
        public static void SaveData(int ratio, string filename)
        {
            try
            {
                SimplePointFile.OpenWriteFile(filename);

                var color = PolyData.GetPointData().GetScalars();

                for (var i = 0; i < ImageHeight; i += ratio)
                {
                    for (var j = 0; j < ImageWidth; j += ratio)
                    {
                        var data = PolyData.GetPoint(i * ImageWidth + j);
                        var c    = color.GetComponent(i * ImageWidth + j, 0);
                        SimplePointFile.Writeline(new[] { data[0] / ratio, data[1] / ratio, data[2], c, c, c });
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                SimplePointFile.CloseWriteFile();
            }
        }
Beispiel #2
0
        private static void NullPoint()
        {
            vtkPoints points = vtkPoints.New();

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

            vtkPolyData polydata = vtkPolyData.New();

            polydata.SetPoints(points);

            vtkFloatArray floatArray = vtkFloatArray.New();

            floatArray.SetNumberOfValues(3);
            floatArray.SetNumberOfComponents(1);
            floatArray.SetName("FloatArray");
            for (int i = 0; i < 3; i++)
            {
                floatArray.SetValue(i, 2);
            }
            polydata.GetPointData().AddArray(floatArray);

            vtkIntArray intArray = vtkIntArray.New();

            intArray.SetNumberOfValues(3);
            intArray.SetNumberOfComponents(1);
            intArray.SetName("IntArray");
            for (int i = 0; i < 3; i++)
            {
                intArray.SetValue(i, 2);
            }

            polydata.GetPointData().AddArray(intArray);

            Console.WriteLine("PointIdx   x y z " + "floatArray" + " " + "intArray");
            Console.WriteLine("----------------------------------------");
            for (int i = 0; i < 3; i++)
            {
                double[]      p = polydata.GetPoint(i);
                vtkFloatArray pointsFloatArray = vtkFloatArray.SafeDownCast(polydata.GetPointData().GetArray("FloatArray"));
                vtkIntArray   pointsIntArray   = vtkIntArray.SafeDownCast(polydata.GetPointData().GetArray("IntArray"));
                Console.WriteLine("   " + i + "       " + p[0] + " " + p[1] + " " + p[2] + "    "
                                  + pointsFloatArray.GetValue(i) + "          " + pointsIntArray.GetValue(i));
            }

            polydata.GetPointData().NullPoint(1);
            polydata.Modified();
            Console.WriteLine("");

            for (int i = 0; i < 3; i++)
            {
                double[]      p = polydata.GetPoint(i);
                vtkFloatArray pointsFloatArray = vtkFloatArray.SafeDownCast(polydata.GetPointData().GetArray("FloatArray"));
                vtkIntArray   pointsIntArray   = vtkIntArray.SafeDownCast(polydata.GetPointData().GetArray("IntArray"));
                Console.WriteLine("   " + i + "       " + p[0] + " " + p[1] + " " + p[2] + "    "
                                  + pointsFloatArray.GetValue(i) + "          " + pointsIntArray.GetValue(i));
            }
        }
Beispiel #3
0
        public static void GetRectData(vtkPolyData data, Rectangle rect)
        {
            var points     = vtkPoints.New();
            var colorArray = vtkUnsignedCharArray.New();

            colorArray.SetNumberOfComponents(3);

            RectImageData = vtkImageData.New();
            RectImageData.SetExtent(0, rect.Size.Width - 1, 0, rect.Size.Height - 1, 0, 0);
            RectImageData.SetNumberOfScalarComponents(1);
            RectImageData.SetScalarTypeToUnsignedChar();

            for (var i = rect.Top; i < rect.Bottom; i++)
            {
                for (var j = rect.Left; j < rect.Right; j++)
                {
                    double[] p = data.GetPoint(i * ImageWidth + j);
                    points.InsertNextPoint(j - rect.Left, i - rect.Top, p[2]);

                    double[] c = data.GetPointData().GetScalars().GetTuple3(i * ImageWidth + j);
                    colorArray.InsertNextTuple3(c[0], c[1], c[2]);

                    RectImageData.SetScalarComponentFromDouble(j - rect.Left, i - rect.Top, 0, 0, c[0]);
                }
            }


            RectPolyData = vtkPolyData.New();
            RectPolyData.SetPoints(points);
            RectPolyData.GetPointData().SetScalars(colorArray);
            RectPolyData.Update();
        }
Beispiel #4
0
        public static void SaveRectData(string filename, vtkPolyData polyData, Rectangle rect)
        {
            try
            {
                SimplePointFile.OpenWriteFile(filename);

                var color = polyData.GetPointData().GetScalars();

                for (var i = 0; i < rect.Height; i++)
                {
                    for (var j = 0; j < rect.Width; j++)
                    {
                        var data = polyData.GetPoint(i * rect.Width + j);
                        var c    = color.GetComponent(i * rect.Width + j, 0);

                        SimplePointFile.Writeline(new[] { data[0], data[1], data[2], c, c, c });
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                SimplePointFile.CloseWriteFile();
            }
        }
Beispiel #5
0
        public static void SetHSectionPoints(Rectangle rect)
        {
            HZArray = vtkFloatArray.New();
            HZArray.SetName("HZArray");
            HZArray.SetNumberOfComponents(1);

            HArray = vtkFloatArray.New();
            HArray.SetName("HArray");
            HArray.SetNumberOfComponents(1);

            HSectionNum = rect.Height / 2;

            for (var i = 0; i < rect.Width; ++i)
            {
                HArray.InsertNextTuple1(i);
                HZArray.InsertNextTuple1(RectPolyData.GetPoint(HSectionNum * rect.Width + i)[2]);
            }
        }
        public static List <double[]> ReadPolyDataPoints(this vtkPolyData polyData)
        {
            List <double[]> ret = new List <double[]>();

            for (int i = 0; i < polyData.GetNumberOfPoints(); ++i)
            {
                ret.Add(polyData.GetPoint(i));
            }

            return(ret);
        }
Beispiel #7
0
        public static void SetColorByHeight(vtkPolyData data, Rectangle rect)
        {
            double zmin = data.GetPoint(0)[2];
            double zmax = zmin;

            for (int i = 0; i < rect.Width * rect.Height; i++)
            {
                double z = data.GetPoint(i)[2];
                if (z < zmin)
                {
                    zmin = z;
                }

                if (z > zmax)
                {
                    zmax = z;
                }
            }

            //Console.WriteLine("Zmin:" + zmin + " Zmax:" + zmax);

            vtkColorTransferFunction colorFunction = vtkColorTransferFunction.New();

            colorFunction.AddRGBPoint(zmin, 0, 0, 1);
            colorFunction.AddRGBPoint(zmax, 1, 0, 0);

            //Console.WriteLine("Color:" + colorFunction.GetColor(0)[0] + " "
            //                  + colorFunction.GetColor(0)[1] + " "
            //                  + colorFunction.GetColor(0)[2]);

            for (int i = 0; i < rect.Width * rect.Height; i++)
            {
                double   z = data.GetPoint(i)[2];
                double[] c = colorFunction.GetColor(z);
                data.GetPointData().GetScalars().SetTuple3(i, c[0] * 255, c[1] * 255, c[2] * 255);
            }

            data.Modified();
        }
        private void PolyDataGetPoint()
        {
            // Create a sphere
            vtkSphereSource sphereSource = vtkSphereSource.New();

            sphereSource.Update();
            vtkPolyData polydata = sphereSource.GetOutput();

            // Write all of the coordinates of the points in the vtkPolyData to the console.
            for (int i = 0; i < polydata.GetNumberOfPoints(); i++)
            {
                double[] p = polydata.GetPoint(i);
                // This is identical to:
                // double[] p = polydata.GetPoints().GetPoint(i);
                Console.WriteLine("Point " + i + " : (" + p[0] + " " + p[1] + " " + p[2] + ")");
            }
        }
Beispiel #9
0
        public static void SetMedianFilter(ref vtkPolyData polyData, Rectangle rect)
        {
            var image = vtkImageData.New();

            image.SetExtent(0, rect.Width - 1, 0, rect.Height - 1, 0, 0);
            image.SetNumberOfScalarComponents(1);
            image.SetScalarTypeToDouble();

            for (int i = 0; i < rect.Width * rect.Height; i++)
            {
                double[] d = polyData.GetPoint(i);
                image.SetScalarComponentFromDouble(i % rect.Width, i / rect.Width, 0, 0, d[2]);
            }
            image.Update();


            var cast = vtkImageCast.New();

            cast.SetInput(image);
            cast.SetOutputScalarTypeToUnsignedChar();
            cast.Update();

            var w = vtkBMPWriter.New();

            w.SetInput(cast.GetOutput());
            w.SetFileName("imgcastbyheight_beforefilter.bmp");
            w.Update();

            var medianFilter = vtkImageGaussianSmooth.New();

            medianFilter.SetInput(image);
            medianFilter.SetDimensionality(2);
            medianFilter.SetRadiusFactor(10);
            medianFilter.SetStandardDeviation(3);
            medianFilter.Update();
            //var medianFilter = vtkImageHybridMedian2D.New();
            //medianFilter.SetInput(image);
            //medianFilter.Update();

            var cast2 = vtkImageCast.New();

            cast2.SetInput(medianFilter.GetOutput());
            cast2.SetOutputScalarTypeToUnsignedChar();
            cast2.Update();

            var w2 = vtkBMPWriter.New();

            w2.SetInput(cast2.GetOutput());
            w2.SetFileName("imgcastbyheight_afterfilter.bmp");
            w2.Update();

            for (int i = 0; i < rect.Height; i++)
            {
                for (int j = 0; j < rect.Width; j++)
                {
                    double d = medianFilter.GetOutput().GetScalarComponentAsFloat(j, i, 0, 0);
                    polyData.GetPoints().SetPoint(i * rect.Width + j, j, i, (int)d);
                }
            }

            polyData.Update();
        }
Beispiel #10
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;
        }
Beispiel #11
0
        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);
        }
Beispiel #12
0
        private void WeightedTransformFilter()
        {
            // Use a sphere as a basis of the shape
            vtkSphereSource sphere = vtkSphereSource.New();

            sphere.SetPhiResolution(40);
            sphere.SetThetaResolution(40);
            sphere.Update();

            vtkPolyData sphereData = sphere.GetOutput();

            // Create a data array to hold the weighting coefficients
            vtkFloatArray tfarray = vtkFloatArray.New();
            int           npoints = sphereData.GetNumberOfPoints();

            tfarray.SetNumberOfComponents(2);
            tfarray.SetNumberOfTuples(npoints);

            // Parameterize the sphere along the z axis, and fill the weights
            // with (1.0-a, a) to linearly interpolate across the shape
            IntPtr pPoint = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * 3);

            double[] point = new double[3];
            for (int i = 0; i < npoints; i++)
            {
                sphereData.GetPoint(i, pPoint);
                Marshal.Copy(pPoint, point, 0, 3);
                double x = point[0];
                double y = point[1];
                double z = point[2];

                double zn  = z + 0.5;
                double zn1 = 1.0 - zn;
                if (zn > 1.0)
                {
                    zn = 1.0;
                }
                if (zn1 < 0.0)
                {
                    zn1 = 0.0;
                }

                tfarray.SetComponent(i, 0, zn1);
                tfarray.SetComponent(i, 1, zn);
            }
            Marshal.FreeHGlobal(pPoint);

            // Create field data to hold the array, and bind it to the sphere
            vtkFieldData fd = vtkFieldData.New();

            tfarray.SetName("weights");
            sphereData.GetPointData().AddArray(tfarray);

            // Use an ordinary transform to stretch the shape
            vtkTransform stretch = vtkTransform.New();

            stretch.Scale(1, 1, 3.2);

            vtkTransformFilter stretchFilter = vtkTransformFilter.New();

            stretchFilter.SetInputConnection(sphereData.GetProducerPort());
            stretchFilter.SetTransform(stretch);

            // Now, for the weighted transform stuff
            vtkWeightedTransformFilter weightedTrans = vtkWeightedTransformFilter.New();

            // Create two transforms to interpolate between
            vtkTransform identity = vtkTransform.New();

            identity.Identity();

            vtkTransform rotated      = vtkTransform.New();
            double       rotatedAngle = 45;

            rotated.RotateX(rotatedAngle);

            weightedTrans.SetNumberOfTransforms(2);
            weightedTrans.SetTransform(identity, 0);
            weightedTrans.SetTransform(rotated, 1);
            // which data array should the filter use ?
            weightedTrans.SetWeightArray("weights");

            weightedTrans.SetInputConnection(stretchFilter.GetOutputPort());

            vtkPolyDataMapper weightedTransMapper = vtkPolyDataMapper.New();

            weightedTransMapper.SetInputConnection(weightedTrans.GetOutputPort());
            vtkActor weightedTransActor = vtkActor.New();

            weightedTransActor.SetMapper(weightedTransMapper);
            weightedTransActor.GetProperty().SetDiffuseColor(0.8, 0.8, 0.1);
            weightedTransActor.GetProperty().SetRepresentationToSurface();

            // 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(weightedTransActor);

            renderer.ResetCamera();
            renderer.GetActiveCamera().Azimuth(90);
            renderer.GetActiveCamera().Dolly(1);
        }
Beispiel #13
0
        public bool Read_Poly_Data_File(string filename)
        {
            //Initalize VTK Reader
            vtkXMLPolyDataReader reader = new vtkXMLPolyDataReader();

            reader.SetFileName(filename);

            reader.Update();

            vtkPolyData polydata = reader.GetOutput();

            if (polydata == null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Invalid Poly data Input");

                return(false);
            }

            // Read Point Coordinates
            int numPoints = (int)polydata.GetNumberOfPoints();

            List <Vector3d> point_dat = new List <Vector3d>();

            if (numPoints != 0)
            {
                double[] pt;

                for (int i = 0; i < numPoints; i++)
                {
                    pt = polydata.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!");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("---------------No Points existent");
            }

            // Read Point Indices
            int numpolydatacells = (int)polydata.GetNumberOfCells();

            vtkCell   polydataCell;
            vtkIdList pts;

            if (numpolydatacells != 0)
            {
                int counter = 0;
                cells.SetNumberOfCells(numpolydatacells);

                for (int i = 0; i < numpolydatacells; i++)
                {
                    polydataCell = polydata.GetCell(i);

                    int numCellPoints = (int)polydataCell.GetNumberOfPoints();
                    cells.InsertNextCell(polydataCell);

                    Vector3 trianglePoints = new Vector3();
                    if (numCellPoints == 3)
                    {
                        pts = polydataCell.GetPointIds();

                        int one   = (int)pts.GetId(0);
                        int two   = (int)pts.GetId(1);
                        int three = (int)pts.GetId(2);
                        //this.Get_Triangle(counter, pts);
                        trianglePoints = new Vector3(one, two, three);
                        counter++;
                    }
                    triangleList.Add(trianglePoints);
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("---------------No Triangles existent");
            }

            // Read point data
            vtkPointData pointData = polydata.GetPointData();

            // Load point attributes
            this.Load_Point_Attributes(pointData);

            return(true);
        }
Beispiel #14
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();
                //}
            }
        }