<div dir="ltr">I select part of vtkpolydata using vtkboxwidget and vtkclippolydata , then I want to change (do displacement) to the points be selected so I search for the points in the source polydata and change its position<div>
<br></div><div>my goal is to for example deform vtkpolydata by stretch certain part , the problem is that the shape doesn't change </div><div><br></div><div>below is the code any suggestion please </div><div><br></div>
<div><div> static vtkLODActor selectActor;</div><div> static vtkLODActor maceActor;</div><div> static vtkPlanes planes;</div><div> static vtkClipPolyData clipper;</div><div> static vtkAppendPolyData apd;</div>
<div><br></div><div> static double[] center_Before =new double[3];</div><div> static double[] center_After = new double[3];</div><div> </div><div> static void Main(string[] args)</div><div> {</div>
<div> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////</div><div> //////////////////////////////////////////////////////// the core of cutting tool /////////////////////////////////////////////////////////////</div>
<div> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////</div><div> // Create a mace out of filters.</div>
<div> vtkSphereSource sphere = new vtkSphereSource();</div><div> vtkConeSource cone = new vtkConeSource();</div><div> vtkGlyph3D glyph = new vtkGlyph3D();</div><div> glyph.SetInput(sphere.GetOutput());</div>
<div> glyph.SetSource(cone.GetOutput());</div><div> glyph.SetVectorModeToUseNormal();</div><div> glyph.SetScaleModeToScaleByVector();</div><div> glyph.SetScaleFactor(0.25);</div>
<div><br></div><div> // The sphere and spikes are appended into a single polydata. This just</div><div> // makes things simpler to manage.</div><div> apd = new vtkAppendPolyData();</div>
<div> apd.AddInput(glyph.GetOutput());</div><div> apd.AddInput(sphere.GetOutput());</div><div><br></div><div> vtkPolyDataMapper maceMapper = new vtkPolyDataMapper();</div><div> maceMapper.SetInput(apd.GetOutput());</div>
<div> maceActor = new vtkLODActor();</div><div> maceActor.SetMapper(maceMapper);</div><div> maceActor.GetProperty().SetColor(1, 0, 0);</div><div> maceActor.VisibilityOn();</div>
<div><br></div><div> // This portion of the code clips the mace with the vtkPlanes implicit</div><div> // function. The clipped region is colored green.</div><div> planes = new vtkPlanes();</div>
<div> clipper = new vtkClipPolyData();</div><div> clipper.SetInput(apd.GetOutput());</div><div> clipper.SetClipFunction(planes);</div><div> clipper.InsideOutOff();</div><div><br>
</div><div> vtkPolyDataMapper selectMapper = new vtkPolyDataMapper();</div><div> selectMapper.SetInput(clipper.GetOutput());</div><div> selectActor = new vtkLODActor();</div><div> selectActor.SetMapper(selectMapper);</div>
<div> selectActor.GetProperty().SetColor(0, 1, 0);</div><div> selectActor.VisibilityOff();</div><div> selectActor.SetScale(1.01, 1.01, 1.01);</div><div><br></div><div> // Create the RenderWindow, Renderer and both Actors</div>
<div> vtkRenderer ren = new vtkRenderer();</div><div> vtkRenderWindow renWin = new vtkRenderWindow();</div><div> renWin.AddRenderer(ren);</div><div><br></div><div> vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();</div>
<div> iren.SetRenderWindow(renWin);</div><div><br></div><div> //The SetInteractor method is how 3D widgets are associated with the</div><div> //render window interactor. Internally, SetInteractor sets up a bunch</div>
<div> //of callbacks using the Command/Observer mechanism (AddObserver()).</div><div> vtkBoxWidget boxWidget = new vtkBoxWidget();</div><div> boxWidget.SetInteractor(iren);</div><div> boxWidget.SetPlaceFactor(1.25);</div>
<div> boxWidget.TranslationEnabledOn();</div><div> ren.AddActor(maceActor);</div><div> ren.AddActor(selectActor);</div><div><br></div><div> //Add the actors to the renderer, set the background and size</div>
<div> ren.SetBackground(0.1, 0.2, 0.4);</div><div> // renWin.SetSize(300, 300);</div><div><br></div><div> // Place the interactor initially. The input to a 3D widget is used to</div><div> // initially position and scale the widget. The "EndInteractionEvent" is</div>
<div> // observed which invokes the SelectPolygons callback.</div><div> boxWidget.SetInput(glyph.GetOutput());</div><div> boxWidget.PlaceWidget();</div><div> boxWidget.AddObserver((uint)vtk.EventIds.InteractionEvent, new vtk.vtkDotNetCallback(InteractionEvent));</div>
<div> boxWidget.AddObserver((uint)vtk.EventIds.EndInteractionEvent, new vtk.vtkDotNetCallback(EndInteractionEvent));</div><div><br></div><div> boxWidget.On();</div><div> iren.Initialize();</div>
<div> renWin.Render();</div><div> iren.Start();</div><div> </div><div> </div><div> }</div><div> public static void InteractionEvent(vtk.vtkObject obj, uint eventId, Object data, IntPtr clientdata)</div>
<div> {</div><div> vtkBoxWidget widget = vtkBoxWidget.SafeDownCast(obj);</div><div> widget.GetPlanes(planes);</div><div> selectActor.VisibilityOn();</div><div><br></div><div><br></div>
<div> vtkPolyData before = new vtkPolyData();</div><div> widget.GetPolyData(before);</div><div> center_Before = GetBoundsCenter(before.GetBounds());</div><div><br></div><div> //maceActor.VisibilityOff();</div>
<div> ///////////// Test writting /////////////////////////////////////////////</div><div> //Write the file</div><div><br></div><div> /*vtkXMLPolyDataWriter writer = new vtkXMLPolyDataWriter ();</div>
<div> writer.SetInputConnection(clipper.GetOutputPort());</div><div> writer.SetFileName("disk.vtp");</div><div> writer.Write();*/</div><div><br></div><div><br></div><div> }</div>
<div> public static void EndInteractionEvent(vtk.vtkObject obj, uint eventId, Object data, IntPtr clientdata)</div><div> {</div><div> vtkBoxWidget widget = vtkBoxWidget.SafeDownCast(obj);</div><div>
vtkPolyData after = new vtkPolyData();</div><div> widget.GetPolyData(after);</div><div> center_After = GetBoundsCenter(after.GetBounds());</div><div><br></div><div> ModifyData();</div>
<div><br></div><div><br></div><div> }</div><div><br></div><div> private static double[] GetBoundsCenter(double[] bounds)</div><div> {</div><div> double[] center = new double[3];</div><div><br>
</div><div> center[0] = (bounds[0] + bounds[1]) / 2.0;</div><div> center[1] = (bounds[2] + bounds[3]) / 2.0;</div><div> center[2] = (bounds[4] + bounds[5]) / 2.0;</div><div><br></div><div>
return center;</div><div> }</div><div><br></div><div> private static void ModifyData()</div><div> {</div><div> double[] Displacement = { center_After[0] - center_Before[0], center_After[1] - center_Before[1], center_After[2] - center_Before[2] };</div>
<div><br></div><div> int NoOfPoints = clipper.GetOutput().GetNumberOfPoints();</div><div><br></div><div> for (int i = 0; i < NoOfPoints;i++ )</div><div> { </div><div><br></div><div> /////////////////////////////////////////////////////////////////////////////////////</div>
<div> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[0] =</div><div> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[0] + Displacement[0];</div>
<div><br></div><div> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[1] =</div><div> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[1] + Displacement[1];</div>
<div><br></div><div> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[2] =</div><div> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[2] + Displacement[2];</div>
<div><br></div><div> apd.Update();</div><div><br></div><div> }</div><div> </div><div> </div><div> </div><div> </div><div> }</div></div></div>