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); }
/// <summary> /// Update Boundary Condition actor (arrows) in Viewport /// </summary> public void Update_Arrows(Dictionary <int, Node> NodeLib, double scale, int Step, bool ClipMode) { vtkPoints PointsX = vtkPoints.New(); vtkPoints PointsY = vtkPoints.New(); vtkPoints PointsZ = vtkPoints.New(); // Create Cone Sources for X, Y and Z direction vtkConeSource ConeSourceX = vtkConeSource.New(); vtkConeSource ConeSourceY = vtkConeSource.New(); vtkConeSource ConeSourceZ = vtkConeSource.New(); ConeSourceX.SetAngle(15); ConeSourceX.SetHeight(scale); ConeSourceX.SetRadius(scale / 4); ConeSourceX.SetResolution(12); ConeSourceX.SetDirection(1, 0, 0); ConeSourceY.SetAngle(15); ConeSourceY.SetHeight(scale); ConeSourceY.SetRadius(scale / 4); ConeSourceY.SetResolution(12); ConeSourceY.SetDirection(0, 1, 0); ConeSourceZ.SetAngle(15); ConeSourceZ.SetHeight(scale); ConeSourceZ.SetRadius(scale / 4); ConeSourceZ.SetResolution(12); ConeSourceZ.SetDirection(0, 0, 1); // Create Points foreach (int i in NodalValues.Keys) { double X = NodeLib[i].X + NodeLib[i].GetDisp(Step, 0); double Y = NodeLib[i].Y + NodeLib[i].GetDisp(Step, 1); double Z = NodeLib[i].Z + NodeLib[i].GetDisp(Step, 2); if (NodalValues[i].Get(0, 0) != 0) { PointsX.InsertNextPoint(X - scale / 2, Y, Z); } if (NodalValues[i].Get(1, 0) != 0) { PointsY.InsertNextPoint(X, Y - scale / 2, Z); } if (NodalValues[i].Get(2, 0) != 0) { PointsZ.InsertNextPoint(X, Y, Z - scale / 2); } } // Set Points to PolyData vtkPolyData PolyX = vtkPolyData.New(); PolyX.SetPoints(PointsX); vtkPolyData PolyY = vtkPolyData.New(); PolyY.SetPoints(PointsY); vtkPolyData PolyZ = vtkPolyData.New(); PolyZ.SetPoints(PointsZ); // Create Glyphs 3D GlyphX = vtkGlyph3D.New(); GlyphY = vtkGlyph3D.New(); GlyphZ = vtkGlyph3D.New(); GlyphX.SetSourceConnection(ConeSourceX.GetOutputPort()); GlyphX.SetInput(PolyX); GlyphX.Update(); GlyphY.SetSourceConnection(ConeSourceY.GetOutputPort()); GlyphY.SetInput(PolyY); GlyphY.Update(); GlyphZ.SetSourceConnection(ConeSourceZ.GetOutputPort()); GlyphZ.SetInput(PolyZ); GlyphZ.Update(); // Set Mapper based on Clip Mode if (ClipMode == true) { // Add Clippers to Mapper ClipperX.SetInputConnection(GlyphX.GetOutputPort()); ClipperX.Update(); MapperX.SetInputConnection(ClipperX.GetOutputPort()); MapperX.Update(); ClipperY.SetInputConnection(GlyphY.GetOutputPort()); ClipperY.Update(); MapperY.SetInputConnection(ClipperY.GetOutputPort()); MapperY.Update(); ClipperZ.SetInputConnection(GlyphZ.GetOutputPort()); ClipperZ.Update(); MapperZ.SetInputConnection(ClipperZ.GetOutputPort()); MapperZ.Update(); } else { // Add Glyphs to Mapper MapperX.SetInputConnection(GlyphX.GetOutputPort()); MapperY.SetInputConnection(GlyphY.GetOutputPort()); MapperZ.SetInputConnection(GlyphZ.GetOutputPort()); MapperX.Update(); MapperY.Update(); MapperZ.Update(); } // Update Actor color ActorX.GetProperty().SetColor( GetColor()[0] / 255.0, GetColor()[1] / 255.0, GetColor()[2] / 255.0); ActorY.GetProperty().SetColor( GetColor()[0] / 255.0, GetColor()[1] / 255.0, GetColor()[2] / 255.0); ActorZ.GetProperty().SetColor( GetColor()[0] / 255.0, GetColor()[1] / 255.0, GetColor()[2] / 255.0); }