[vtkusers] why the vtkpolydata can't be updated

Jim Peterson jimcp at cox.net
Sun Aug 22 22:06:19 EDT 2010


Ali,
I did do just a little more experimentation with this in my Java 
environment. (not .NET, but close enough). What I found has a couple 
significant notes:
1.  it appears your clipper is not selecting any points. I reduced it to 
a single plane with a normal so it selects 1/2 of the mace.
2. The change calculation appears to result in a zero displacement 
resulting in no change to the input points. (I might be missing 
something in how you interact with this, but I was never able to get a 
non zero value for the Displacement Array. I hard coded some offsets to 
try to proceed.
3. in all cases you are modifying the output points from the append poly 
data filter. The problem here is the input is still some specified 
object generators, and so when the pipeline executes, it recreates the 
original objects, and the display is unchanged.
What to do to resolve this?
create a vtkPolyData object and set it to the output from the append 
poly data filter. Then use the poly data object as input to the data 
modification.
My significant changes to your test were:

add a poly data object to be able to modify the points, and a location 
and normal for the clip plane:
    static vtkPolyData gpd;
    double[] po = {0.,0.,0.};
    double[] pn = {0.,1.,1,};

adjust the pipeline to have a poly data object of your generated mace:
    // The sphere and spikes are appended into a single polydata. This just
    // makes things simpler to manage.
      apd = new vtkAppendPolyData();
    apd.AddInput(glyph.GetOutput());
    apd.AddInput(sphere.GetOutput());
     gpd = apd.GetOutput();
    vtkPolyDataMapper maceMapper = new vtkPolyDataMapper();
    maceMapper.SetInput(gpd);

I rewrote your modify data to simplify the number of appended calls to 
get to the points to be modified.
    private static void ModifyData()
    {
        double[] Displacement = { center_After[0] - center_Before[0], 
center_After[1] - center_Before[1],center_After[2] - center_Before[2] };
        Displacement[0] = .5; // fixed values for testing
        Displacement[1] = .25; // fixed values for testing
        Displacement[2] = .125; // fixed values for testing
        int NoOfPoints = clipper.GetOutput().GetNumberOfPoints();

        for (int i = 0; i < NoOfPoints;i++ )
        {
// get the point data from the clipper
       double[] ci = clipper.GetOutput().GetPoints().GetPoint(i);
// get the index of the original data point.
       int pxi = gpd.FindPoint(ci);
// get the value of the poly  data point.
       double[] ppi = gpd.GetPoints().GetPoint(pxi);
// transpose the point
       ppi[0] += Displacement[0];
       ppi[1] += Displacement[1];
       ppi[2] += Displacement[2];
// replace the point in the poly data
     gpd.GetPoints().InsertPoint(pxi,ppi);
        }
// mark the poly data as modified for the render processing.
        gpd.Modified();
      }

Hope that helps, Good luck.
Jim

Jim Peterson wrote:
> Ali,
>
> I had originally thought this was Java source, but the addObserver 
> syntax is clearly different. In my interactions I use the vtkPoints 
> objects to address and modify the points, and I use the 
> insertPoint(....) function that uses the point number and the point 
> data to replace point values. Looking at this logic again, I am not at 
> all sure why the center_Before and center_After are expected to have 
> any different values, and even if they do, all that appears to be 
> happening if in fact the differences are non zero is to move the mace 
> slightly. This may not be apparent without some non moving reference 
> point in the display.  You might replace that logic with something 
> that will use vtkTransform to actually scale or rotate the points to 
> make sure you are making a change and it will be visible.
>
> I am beyond my knowlege to try to help at this point.
> best of luck.
> Jim
>  
> Ali Habib wrote
>> Dear Jim,
>>
>> I made sure that the observers I used work and the algorithm use them
>> but the shape not be affected
>> Best regards
>>
>> On Sat, Aug 21, 2010 at 5:17 PM, Jim Peterson <jimcp at cox.net 
>> <mailto:jimcp at cox.net>> wrote:
>>
>>     Ali,
>>     Are you sure your interaction events are being invoked? I would
>>     put a System.out.Println("....."); message in the callback
>>     routines to be sure you are going through there first. I am a
>>     relative Vtk rookie myself, but I do use java callbacks to animate
>>     vtk objects from Java tabular data. I use the callbacks from the
>>     renderer and interaction window respectivelym iren is an instance
>>     of vtkRenderWindowInteractor and ren1 is an instance of vtkRenderer:
>>
>>         iren.AddObserver("UserEvent",this,"updmeth");
>>         ren1.AddObserver("StartEvent",this,"startRender");
>>         iren.AddObserver("StartPickEvent",this,"startPick");
>>         iren.AddObserver("EndPickEvent",this,"endPick");
>>
>>     The UserEvent responds to the U key interaction and allows me to
>>     toggle the animation on and off, the StartEvent of the renderer is
>>     where I  modify my pipeline elements.
>>
>>     I perform the .Modified() call for all of the affected elements of
>>     the pipeline. Points, Scalars, and PolyData objects.
>>
>>
>>     Hope that helps,
>>     Jim
>>     Ali Habib wrote:
>>
>>         Dear Jim
>>
>>         it didn't affect , do u have any suggestion or a way to reach
>>         my goal
>>         Best regards
>>         On Sat, Aug 21, 2010 at 4:44 PM, Jim Peterson <jimcp at cox.net
>>         <mailto:jimcp at cox.net> <mailto:jimcp at cox.net
>>         <mailto:jimcp at cox.net>>> wrote:
>>
>>            Ali,
>>            I think you need to invoke apd.Modified() before
>>         apd.Update() to
>>            get the changes incorporated in the pipeline.
>>
>>            Hope that helps,
>>            Jim.
>>
>>            Ali Habib wrote:
>>
>>                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
>>
>>                my goal is to for example deform vtkpolydata by stretch
>>                certain part , the problem is that the shape doesn't 
>> change
>>                below is the code any suggestion please
>>                       static vtkLODActor selectActor;
>>                       static vtkLODActor maceActor;
>>                       static vtkPlanes planes;
>>                       static vtkClipPolyData clipper;
>>                       static vtkAppendPolyData apd;
>>
>>                      static double[] center_Before =new double[3];
>>                       static double[] center_After = new double[3];
>>                         static void Main(string[] args)
>>                       {
>>                                         
>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
>>
>>                                         
>> //////////////////////////////////////////////////////// the
>>                core of cutting tool
>>                       
>> /////////////////////////////////////////////////////////////
>>                                         
>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
>>
>>                           // Create a mace out of filters.
>>                           vtkSphereSource sphere = new 
>> vtkSphereSource();
>>                           vtkConeSource cone = new vtkConeSource();
>>                           vtkGlyph3D glyph = new vtkGlyph3D();
>>                           glyph.SetInput(sphere.GetOutput());
>>                           glyph.SetSource(cone.GetOutput());
>>                           glyph.SetVectorModeToUseNormal();
>>                           glyph.SetScaleModeToScaleByVector();
>>                           glyph.SetScaleFactor(0.25);
>>
>>                           // The sphere and spikes are appended into a
>>         single
>>                polydata. This just
>>                           // makes things simpler to manage.
>>                             apd = new vtkAppendPolyData();
>>                           apd.AddInput(glyph.GetOutput());
>>                           apd.AddInput(sphere.GetOutput());
>>
>>                           vtkPolyDataMapper maceMapper = new
>>         vtkPolyDataMapper();
>>                           maceMapper.SetInput(apd.GetOutput());
>>                           maceActor = new vtkLODActor();
>>                           maceActor.SetMapper(maceMapper);
>>                           maceActor.GetProperty().SetColor(1, 0, 0);
>>                           maceActor.VisibilityOn();
>>
>>                           // This portion of the code clips the mace
>>         with the
>>                vtkPlanes implicit
>>                           // function.  The clipped region is colored
>>         green.
>>                           planes = new vtkPlanes();
>>                             clipper = new vtkClipPolyData();
>>                           clipper.SetInput(apd.GetOutput());
>>                           clipper.SetClipFunction(planes);
>>                           clipper.InsideOutOff();
>>
>>                           vtkPolyDataMapper selectMapper = new
>>                vtkPolyDataMapper();
>>                           selectMapper.SetInput(clipper.GetOutput());
>>                           selectActor = new vtkLODActor();
>>                           selectActor.SetMapper(selectMapper);
>>                           selectActor.GetProperty().SetColor(0, 1, 0);
>>                           selectActor.VisibilityOff();
>>                           selectActor.SetScale(1.01, 1.01, 1.01);
>>
>>                           // Create the RenderWindow, Renderer and
>>         both Actors
>>                           vtkRenderer ren = new vtkRenderer();
>>                           vtkRenderWindow renWin = new 
>> vtkRenderWindow();
>>                           renWin.AddRenderer(ren);
>>
>>                           vtkRenderWindowInteractor iren = new
>>                vtkRenderWindowInteractor();
>>                           iren.SetRenderWindow(renWin);
>>
>>                           //The SetInteractor method is how 3D 
>> widgets are
>>                associated with the
>>                           //render window interactor.  Internally,
>>                SetInteractor sets up a bunch
>>                           //of callbacks using the Command/Observer
>>         mechanism
>>                (AddObserver()).
>>                           vtkBoxWidget boxWidget = new vtkBoxWidget();
>>                           boxWidget.SetInteractor(iren);
>>                           boxWidget.SetPlaceFactor(1.25);
>>                           boxWidget.TranslationEnabledOn();
>>                           ren.AddActor(maceActor);
>>                           ren.AddActor(selectActor);
>>
>>                           //Add the actors to the renderer, set the
>>                background and size
>>                           ren.SetBackground(0.1, 0.2, 0.4);
>>                           // renWin.SetSize(300, 300);
>>
>>                           // Place the interactor initially. The input
>>         to a
>>                3D widget is used to
>>                           // initially position and scale the widget. 
>> The
>>                "EndInteractionEvent" is
>>                           // observed which invokes the SelectPolygons
>>         callback.
>>                           boxWidget.SetInput(glyph.GetOutput());
>>                           boxWidget.PlaceWidget();
>>                                         
>> boxWidget.AddObserver((uint)vtk.EventIds.InteractionEvent,
>>                new vtk.vtkDotNetCallback(InteractionEvent));
>>                                         
>> boxWidget.AddObserver((uint)vtk.EventIds.EndInteractionEvent,
>>                new vtk.vtkDotNetCallback(EndInteractionEvent));
>>
>>                           boxWidget.On();
>>                           iren.Initialize();
>>                           renWin.Render();
>>                           iren.Start();
>>                                       }
>>                       public static void
>>         InteractionEvent(vtk.vtkObject obj,
>>                uint eventId, Object data, IntPtr clientdata)
>>                       {
>>                           vtkBoxWidget widget =
>>         vtkBoxWidget.SafeDownCast(obj);
>>                           widget.GetPlanes(planes);
>>                           selectActor.VisibilityOn();
>>
>>
>>                           vtkPolyData before = new vtkPolyData();
>>                           widget.GetPolyData(before);
>>                           center_Before =
>>         GetBoundsCenter(before.GetBounds());
>>
>>                           //maceActor.VisibilityOff();
>>                           ///////////// Test writting
>>                /////////////////////////////////////////////
>>                           //Write the file
>>
>>                           /*vtkXMLPolyDataWriter writer = new
>>                vtkXMLPolyDataWriter ();
>>                                  
>> writer.SetInputConnection(clipper.GetOutputPort());
>>                          writer.SetFileName("disk.vtp");
>>                          writer.Write();*/
>>
>>
>>                       }
>>                       public static void 
>> EndInteractionEvent(vtk.vtkObject
>>                obj, uint eventId, Object data, IntPtr clientdata)
>>                       {
>>                           vtkBoxWidget widget =
>>         vtkBoxWidget.SafeDownCast(obj);
>>                            vtkPolyData after = new vtkPolyData();
>>                            widget.GetPolyData(after);
>>                            center_After =
>>         GetBoundsCenter(after.GetBounds());
>>
>>                            ModifyData();
>>
>>
>>                       }
>>
>>                       private static double[] GetBoundsCenter(double[]
>>         bounds)
>>                       {
>>                           double[] center = new double[3];
>>
>>                           center[0] = (bounds[0] + bounds[1]) / 2.0;
>>                           center[1] = (bounds[2] + bounds[3]) / 2.0;
>>                           center[2] = (bounds[4] + bounds[5]) / 2.0;
>>
>>                           return center;
>>                       }
>>
>>                       private static void ModifyData()
>>                       {
>>                           double[] Displacement = { center_After[0] -
>>                center_Before[0], center_After[1] - center_Before[1],
>>                center_After[2] - center_Before[2] };
>>
>>                           int NoOfPoints =
>>                clipper.GetOutput().GetNumberOfPoints();
>>
>>                           for (int i = 0; i < NoOfPoints;i++ )
>>                           {
>>                                             
>> ///////////////////////////////////////////////////////////////////////////////////// 
>>
>>                                             
>> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[0] 
>>
>>                =
>>                                                 
>> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[0] 
>>
>>                + Displacement[0];
>>
>>                                             
>> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[1] 
>>
>>                =
>>                                 
>> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[1] 
>>
>>                + Displacement[1];
>>
>>                                             
>> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[2] 
>>
>>                =
>>                                 
>> apd.GetOutput().GetPoints().GetPoint(apd.GetOutput().FindPoint(clipper.GetOutput().GetPoints().GetPoint(i)))[2] 
>>
>>                + Displacement[2];
>>
>>                               apd.Update();
>>
>>                           }
>>                                                       }
>>                       
>> ------------------------------------------------------------------------
>>
>>                _______________________________________________
>>                Powered by www.kitware.com <http://www.kitware.com>
>>         <http://www.kitware.com>
>>
>>
>>                Visit other Kitware open-source projects at
>>                http://www.kitware.com/opensource/opensource.html
>>
>>                Please keep messages on-topic and check the VTK FAQ at:
>>                http://www.vtk.org/Wiki/VTK_FAQ
>>
>>                Follow this link to subscribe/unsubscribe:
>>                http://www.vtk.org/mailman/listinfo/vtkusers
>>               
>>
>>
>>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: 
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>




More information about the vtkusers mailing list