Esempio n. 1
0
        private void Arrow()
        {
            // Create two arrows.
            vtkArrowSource arrowSource01 = vtkArrowSource.New();
            vtkArrowSource arrowSource02 = vtkArrowSource.New();

            arrowSource02.SetShaftResolution(24); // default = 6
            arrowSource02.SetTipResolution(36);   // default = 6

            // Visualize
            vtkPolyDataMapper mapper01 = vtkPolyDataMapper.New();
            vtkPolyDataMapper mapper02 = vtkPolyDataMapper.New();

            mapper01.SetInputConnection(arrowSource01.GetOutputPort());
            mapper02.SetInputConnection(arrowSource02.GetOutputPort());
            vtkActor actor01 = vtkActor.New();
            vtkActor actor02 = vtkActor.New();

            actor01.SetMapper(mapper01);
            actor02.SetMapper(mapper02);
            actor01.SetPosition(0.0, 0.25, 0.0);
            actor02.SetPosition(0.0, -0.25, 0.0);
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            vtkRenderer     renderer     = renderWindow.GetRenderers().GetFirstRenderer();

            renderer.SetBackground(0.2, 0.3, 0.4);
            renderer.AddActor(actor01);
            renderer.AddActor(actor02);
            renderer.ResetCamera();
        }
Esempio n. 2
0
        private void VectorFieldNonZeroExtraction()
        {
            // Create an image
            vtkImageData image = vtkImageData.New();

            CreateVectorField(ref image);

            // This filter produces a vtkImageData with an array named "Magnitude"
            vtkImageMagnitude magnitudeFilter = vtkImageMagnitude.New();

            magnitudeFilter.SetInputConnection(image.GetProducerPort());
            magnitudeFilter.Update();

            image.GetPointData().AddArray(magnitudeFilter.GetOutput().GetPointData().GetScalars());
            image.GetPointData().SetActiveScalars("Magnitude");

            vtkThresholdPoints thresholdVector = vtkThresholdPoints.New();

            thresholdVector.SetInput(image);
            thresholdVector.SetInputArrayToProcess(
                0,
                0,
                (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS,
                (int)vtkDataSetAttributes.AttributeTypes.SCALARS,
                "Magnitude");
            thresholdVector.ThresholdByUpper(0.00001);
            thresholdVector.Update();

            // in case you want to save imageData
            //vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();
            //writer.SetFileName("output.vtp");
            //writer.SetInputConnection(thresholdPoints.GetOutputPort());
            //writer.Write();

            // repesents the pixels
            vtkCubeSource cubeSource = vtkCubeSource.New();

            cubeSource.SetXLength(2.0);
            cubeSource.SetYLength(2.0);
            cubeSource.SetZLength(2.0);
            vtkGlyph3D glyph = vtkGlyph3D.New();

            glyph.SetInput(image);
            glyph.SetSourceConnection(cubeSource.GetOutputPort());
            // don't scale glyphs according to any scalar data
            glyph.SetScaleModeToDataScalingOff();

            vtkPolyDataMapper glyphMapper = vtkPolyDataMapper.New();

            glyphMapper.SetInputConnection(glyph.GetOutputPort());
            // don't color glyphs according to scalar data
            glyphMapper.ScalarVisibilityOff();
            glyphMapper.SetScalarModeToDefault();

            vtkActor actor = vtkActor.New();

            actor.SetMapper(glyphMapper);

            // represent vector field
            vtkGlyph3D        vectorGlyph       = vtkGlyph3D.New();
            vtkArrowSource    arrowSource       = vtkArrowSource.New();
            vtkPolyDataMapper vectorGlyphMapper = vtkPolyDataMapper.New();

            int n = image.GetPointData().GetNumberOfArrays();

            for (int i = 0; i < n; i++)
            {
                Debug.WriteLine("name of array[" + i + "]: " + image.GetPointData().GetArrayName(i));
            }

            vtkPolyData tmp = thresholdVector.GetOutput();

            Debug.WriteLine("number of thresholded points: " + tmp.GetNumberOfPoints());
            vectorGlyph.SetInputConnection(thresholdVector.GetOutputPort());

            // in case you want the point glyphs to be oriented according to
            // scalar values in array "ImageScalars" uncomment the following line
            image.GetPointData().SetActiveVectors("ImageScalars");

            vectorGlyph.SetSourceConnection(arrowSource.GetOutputPort());
            vectorGlyph.SetScaleModeToScaleByVector();
            vectorGlyph.SetVectorModeToUseVector();
            vectorGlyph.ScalingOn();
            vectorGlyph.OrientOn();
            vectorGlyph.SetInputArrayToProcess(
                1,
                0,
                (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS,
                (int)vtkDataSetAttributes.AttributeTypes.SCALARS,
                "ImageScalars");

            vectorGlyph.Update();

            vectorGlyphMapper.SetInputConnection(vectorGlyph.GetOutputPort());
            vectorGlyphMapper.Update();

            vtkActor vectorActor = vtkActor.New();

            vectorActor.SetMapper(vectorGlyphMapper);


            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // set background color
            renderer.SetBackground(.2, .6, .3);
            //Add the actors to the renderer, set the background and size
            renderer.AddActor(actor);
            renderer.AddActor(vectorActor);
        }
Esempio n. 3
0
        private void OrientedArrow()
        {
            //Create an arrow.
            vtkArrowSource arrowSource = vtkArrowSource.New();

            // Generate a random start and end point
            vtkMath.RandomSeed(8775070);
            double[] startPoint = new double[] {
                vtkMath.Random(-10, 10),
                vtkMath.Random(-10, 10),
                vtkMath.Random(-10, 10)
            };

            double[] endPoint = new double[] {
                vtkMath.Random(-10, 10),
                vtkMath.Random(-10, 10),
                vtkMath.Random(-10, 10)
            };

            // Compute a basis
            double[] normalizedX = new double[3];
            double[] normalizedY = new double[3];
            double[] normalizedZ = new double[3];

            // The X axis is a vector from start to end
            myMath.Subtract(endPoint, startPoint, ref normalizedX);
            double length = myMath.Norm(normalizedX);

            myMath.Normalize(ref normalizedX);

            // The Z axis is an arbitrary vector cross X
            double[] arbitrary = new double[] {
                vtkMath.Random(-10, 10),
                vtkMath.Random(-10, 10),
                vtkMath.Random(-10, 10)
            };
            myMath.Cross(normalizedX, arbitrary, ref normalizedZ);
            myMath.Normalize(ref normalizedZ);
            // The Y axis is Z cross X
            myMath.Cross(normalizedZ, normalizedX, ref normalizedY);
            vtkMatrix4x4 matrix = vtkMatrix4x4.New();

            // Create the direction cosine matrix
            matrix.Identity();
            for (int i = 0; i < 3; i++)
            {
                matrix.SetElement(i, 0, normalizedX[i]);
                matrix.SetElement(i, 1, normalizedY[i]);
                matrix.SetElement(i, 2, normalizedZ[i]);
            }

            // Apply the transforms
            vtkTransform transform = vtkTransform.New();

            transform.Translate(startPoint[0], startPoint[1], startPoint[2]);
            transform.Concatenate(matrix);
            transform.Scale(length, length, length);


            //Create a mapper and actor for the arrow
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
            vtkActor          actor  = vtkActor.New();

#if USER_MATRIX
            mapper.SetInputConnection(arrowSource.GetOutputPort());
            actor.SetUserMatrix(transform.GetMatrix());
#else
            // Transform the polydata
            vtkTransformPolyDataFilter transformPD = vtkTransformPolyDataFilter.New();
            transformPD.SetTransform(transform);
            transformPD.SetInputConnection(arrowSource.GetOutputPort());
            mapper.SetInputConnection(transformPD.GetOutputPort());
#endif
            actor.SetMapper(mapper);

            // Create spheres for start and end point
            vtkSphereSource sphereStartSource = vtkSphereSource.New();
            sphereStartSource.SetCenter(startPoint[0], startPoint[1], startPoint[2]);
            vtkPolyDataMapper sphereStartMapper = vtkPolyDataMapper.New();
            sphereStartMapper.SetInputConnection(sphereStartSource.GetOutputPort());
            vtkActor sphereStart = vtkActor.New();
            sphereStart.SetMapper(sphereStartMapper);
            sphereStart.GetProperty().SetColor(1.0, 1.0, .3);

            vtkSphereSource sphereEndSource = vtkSphereSource.New();
            sphereEndSource.SetCenter(endPoint[0], endPoint[1], endPoint[2]);
            vtkPolyDataMapper sphereEndMapper = vtkPolyDataMapper.New();
            sphereEndMapper.SetInputConnection(sphereEndSource.GetOutputPort());
            vtkActor sphereEnd = vtkActor.New();
            sphereEnd.SetMapper(sphereEndMapper);
            sphereEnd.GetProperty().SetColor(1.0, .3, .3);

            vtkRenderWindow renderWindow = myRenderWindowControl.RenderWindow;
            vtkRenderer     renderer     = renderWindow.GetRenderers().GetFirstRenderer();
            renderer.SetBackground(0.2, 0.3, 0.4);
            renderer.AddActor(actor);
            renderer.AddActor(sphereStart);
            renderer.AddActor(sphereEnd);
            renderer.ResetCamera();
        }