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> /// 绘制立长方体的边缘 /// </summary> /// <param name="bgColor">边缘的颜色</param> /// <param name="actors">添加至该对象</param> public void AddContainerEdgeToActorCollection(byte[] bgColor, ref vtkActorCollection actors) { vtkProperty pp = vtkProperty.New(); pp.SetOpacity(0.5); pp.SetColor(bgColor[0], bgColor[1], bgColor[2]); pp.SetLineWidth(5); pp.SetLighting(false); vtkCubeSource cs = vtkCubeSource.New(); cs.SetCenter(sideLen / 2, sideLen / 2, height / 2); cs.SetXLength(sideLen); cs.SetYLength(sideLen); cs.SetZLength(height); #region 采用描点的方式绘制长方体 只需要添加到mapper.SetInput(pd);即可 //vtkPoints points = vtkPoints.New(); //points.InsertNextPoint(0 + offset[0], 0 + offset[1], 0 + offset[2]); //points.InsertNextPoint(sideLen + offset[0], 0 + offset[1], 0 + offset[2]); //points.InsertNextPoint(sideLen + offset[0], sideLen + offset[1], 0 + offset[2]); //points.InsertNextPoint(0 + offset[0], sideLen + offset[1], 0 + offset[2]); //points.InsertNextPoint(0 + offset[0], sideLen + offset[1], height + offset[2]); //points.InsertNextPoint(sideLen + offset[0], sideLen + offset[1], height + offset[2]); //points.InsertNextPoint(sideLen + offset[0], 0 + offset[1], height + offset[2]); //points.InsertNextPoint(0 + offset[0], 0 + offset[1], height + offset[2]); //vtkPolyData pd = vtkPolyData.New(); //vtkLine line = vtkLine.New(); //vtkCellArray cellArr = vtkCellArray.New(); ////描出边框 //for (int i = 0; i < 4; i++) //{ // line.GetPointIds().SetId(0, i); // line.GetPointIds().SetId(1, (i + 1) % 4); // cellArr.InsertNextCell(line); // line.GetPointIds().SetId(0, i + 4); // line.GetPointIds().SetId(1, (i + 1) % 4 + 4); // cellArr.InsertNextCell(line); // line.GetPointIds().SetId(0, i); // line.GetPointIds().SetId(1, 7 - i); // cellArr.InsertNextCell(line); //} //IntPtr iColor = Marshal.AllocHGlobal(bgColor.Length); //Marshal.Copy(bgColor, 0, iColor, bgColor.Length); //vtkUnsignedCharArray colors = vtkUnsignedCharArray.New(); //colors.SetNumberOfComponents(3); //for (int i = 0; i < cellArr.GetNumberOfCells(); i++) //{ // colors.InsertNextTupleValue(iColor); //} //pd.SetPoints(points); //pd.SetLines(cellArr); //pd.GetCellData().SetScalars(colors); //Marshal.FreeHGlobal(iColor); #endregion vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); //mapper.SetInput(pd); mapper.SetInputConnection(cs.GetOutputPort()); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.SetProperty(pp); actors.AddItem(actor); }
private void SetUpRenderWindow() { // create a VTK output control and make the forms host point to it rwc = new RenderWindowControl(); rwc.CreateGraphics(); windowsFormsHost.Child = rwc; // set up basic viewing vtkRenderer ren = rwc.RenderWindow.GetRenderers().GetFirstRenderer(); // background color ren.SetBackground(0.0, 0.0, 0.0); // interactor style vtkInteractorStyleSwitch istyle = vtkInteractorStyleSwitch.New(); rwc.RenderWindow.GetInteractor().SetInteractorStyle(istyle); rwc.RenderWindow.GetInteractor().SetPicker(vtkCellPicker.New()); (istyle).SetCurrentStyleToTrackballCamera(); // Demonstrate how to use the vtkBoxWidget 3D widget, vtkSphereSource sphere = vtkSphereSource.New(); sphere.SetRadius(0.25); vtkPolyDataMapper sphereMapper = vtkPolyDataMapper.New(); sphereMapper.SetInputConnection(sphere.GetOutputPort()); vtkActor sphereActor; vtkTransform widgetTransform = vtkTransform.New(); List <Region> region_list = configurator.SimConfig.scenario.regions.ToList(); for (int ii = 0; ii < region_list.Count; ++ii) { this.TransferMatrixToVTKTransform(region_list[ii].region_box_spec.transform_matrix, widgetTransform); sphereActor = vtkActor.New(); sphereActor.SetMapper(sphereMapper); sphereActor.SetUserTransform(widgetTransform); sphereActor.GetProperty().SetOpacity(0.5); sphereActor.SetVisibility(region_list[ii].region_visibility ? 1 : 0); sphereActorList.Add(sphereActor); ren.AddActor(sphereActorList[ii]); } vtkCubeSource cube = vtkCubeSource.New(); cube.SetXLength(5.0); cube.SetYLength(5.0); cube.SetZLength(5.0); vtkOutlineSource outline = vtkOutlineSource.New(); outline.SetBounds(-2, 2, -2, 2, -2, 2); vtkPolyDataMapper cubeMapper = vtkPolyDataMapper.New(); cubeMapper.SetInputConnection(outline.GetOutputPort()); vtkLODActor cubeActor = vtkLODActor.New(); cubeActor.SetMapper(cubeMapper); cubeActor.VisibilityOn(); ren.AddActor(cubeActor); boxRep = vtkBoxRepresentation.New(); boxRep.SetTransform(widgetTransform); boxWidget = vtkBoxWidget2.New(); boxWidget.SetInteractor(rwc.RenderWindow.GetInteractor()); boxWidget.SetRepresentation(boxRep); boxWidget.SetPriority(1); boxWidget.InteractionEvt += this.boxInteractionCallback; ren.ResetCamera(); }
public c3DWell(cPoint3D MinPt, cPoint3D MaxPt, Color Colour, cWell CurrentWell) { this.SetPosition(new cPoint3D(0, 0, 0)); this.Colour = Colour; VTK_Cube = vtkCubeSource.New(); //VTK_Cube.SetBounds(MinPt.X, MaxPt.X, MinPt.Y, MaxPt.Y, MinPt.Z, MaxPt.Z); VTK_Cube.SetXLength(MaxPt.X - MinPt.X); VTK_Cube.SetYLength(MaxPt.Y - MinPt.Y); VTK_Cube.SetZLength(MaxPt.Z - MinPt.Z); vtk_PolyDataMapper = vtkPolyDataMapper.New(); vtk_PolyDataMapper.SetInputConnection(VTK_Cube.GetOutputPort()); this.SetPosition(MinPt); this.Colour = Colour; this.AssociatedWell = CurrentWell; CreateVTK3DObject(1); Information = new cInformation(this); }