[vtkusers] Picking and dragging a vertex - points not moving to appropriate position
Dominik Szczerba
dominik at itis.ethz.ch
Fri Jun 17 16:08:52 EDT 2011
I meant Modified() not Update()... do check again...
Dominik
On Fri, Jun 17, 2011 at 5:35 PM, Vashist Purbhoo <vashist14 at hotmail.com> wrote:
> Hi Dominik!
>
> Thank you for your reply. I tried what you suggested and included the
> update just before I get the release position of the mouse. It seems to be
> the most logical place to do so to me. However, there is still no change in
> the output.
>
> I was thinking maybe the problem lies with the coordinates returned
> themselves.
>
> Does anyone have any previous experience with this?
>
> Vashist
>
>> Date: Fri, 17 Jun 2011 14:14:25 +0200
>> From: dominik at itis.ethz.ch
>> To: vashist14 at yahoo.com
>> CC: vtkusers at vtk.org
>> Subject: Re: [vtkusers] Picking and dragging a vertex - points not moving
>> to appropriate position
>>
>> Sounds very much like you miss dataset->Modified() somewhere.
>>
>> Dominik
>>
>> On Fri, Jun 17, 2011 at 10:41 AM, Vashist Purbhoo <vashist14 at hotmail.com>
>> wrote:
>> > Hi vtkusers!
>> >
>> > I am trying to deform a 3D object by clicking and dragging
>> > its vertices. I
>> > was able to adapt the "SelectAVertex.cxx" example
>> > (http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/MoveAVertex) on
>> > the
>> > vtk website to make it work with a polydata I create using
>> > vtkSphereSource.
>> > To summarise, to deform the object I select a vertex by clicking on the
>> > middle mouse button and drag the point to the new location I want. I
>> > render
>> > the vertices of my object using
>> > vtkVertexGlyphFilter as well as the surface of the object.
>> >
>> > The program works correctly if I select the points without changing the
>> > initial view of the rendered object. However, once I change the view,
>> > for
>> > example to access a point at the back of my object, the point no longer
>> > moves to where I drag it. The coordinates returned do not correspond to
>> > the
>> > position where i release the mouse button. I would highly appreciate if
>> > any
>> > help/suggestions could be provided on how to solve this.
>> >
>> > Thank you.
>> >
>> > Vashist
>> >
>> >
>> > #include <vtkSmartPointer.h>
>> > #include <vtkPointPicker.h>
>> > #include <vtkRendererCollection.h>
>> > #include <vtkProperty.h>
>> > #include <vtkObjectFactory.h>
>> > #include <vtkPolyDataMapper.h>
>> > #include <vtkActor.h>
>> > #include <vtkRenderWindow.h>
>> > #include <vtkRenderer.h>
>> > #include <vtkRenderWindowInteractor.h>
>> > #include <vtkPolyData.h>
>> > #include <vtkPointSource.h>
>> > #include <vtkInteractorStyleJoystickActor.h>
>> > #include <vtkAreaPicker.h>
>> > #include <vtkVertexGlyphFilter.h>
>> > #include <vtkIdFilter.h>
>> > #include <vtkAssembly.h>
>> > #include <vtkSphereSource.h>
>> > #include <vtkExtractGeometry.h>
>> > #include <vtkDataSetMapper.h>
>> > #include <vtkUnstructuredGrid.h>
>> > #include <vtkGlyph3D.h>
>> > #include <vtkPointData.h>
>> > #include <vtkIdTypeArray.h>
>> > #include <vtkDataSetSurfaceFilter.h>
>> > #include <vtkPlanes.h>
>> >
>> > class InteractorStyle : public vtkInteractorStyleJoystickActor
>> > {
>> > public:
>> > static InteractorStyle* New();
>> > vtkTypeMacro(InteractorStyle,vtkInteractorStyleJoystickActor);
>> >
>> > InteractorStyle()
>> > {
>> > this->Move = false;
>> > this->PointPicker = vtkSmartPointer<vtkPointPicker>::New();
>> >
>> > // Setup ghost glyph
>> > vtkSmartPointer<vtkPoints> points =
>> > vtkSmartPointer<vtkPoints>::New();
>> > points->InsertNextPoint(0,0,0);
>> > this->MovePolyData = vtkSmartPointer<vtkPolyData>::New();
>> > this->MovePolyData->SetPoints(points);
>> > this->MoveGlyphFilter =
>> > vtkSmartPointer<vtkVertexGlyphFilter>::New();
>> >
>> >
>> > this->MoveGlyphFilter->SetInputConnection(this->MovePolyData->GetProducerPort());
>> > this->MoveGlyphFilter->Update();
>> >
>> > this->MoveMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
>> >
>> >
>> > this->MoveMapper->SetInputConnection(this->MoveGlyphFilter->GetOutputPort());
>> >
>> > this->MoveActor = vtkSmartPointer<vtkActor>::New();
>> > this->MoveActor->SetMapper(this->MoveMapper);
>> > this->MoveActor->VisibilityOff();
>> > this->MoveActor->GetProperty()->SetPointSize(5);
>> > this->MoveActor->GetProperty()->SetColor(1,0,0);
>> > }
>> >
>> > //_____________________________________________________________________________________
>> > void OnMouseMove()
>> > {
>> > if(!this->Move)
>> > {
>> > return;
>> > }
>> > vtkInteractorStyleJoystickActor::OnMouseMove();
>> > }
>> >
>> > //_____________________________________________________________________________________
>> > void OnMiddleButtonUp()
>> > {
>> > this->EndPan();
>> >
>> > this->Move = false;
>> > this->MoveActor->VisibilityOff();
>> > this->Data->GetPoints()->SetPoint(this->SelectedPoint,
>> > this->MoveActor->GetPosition());
>> > this->Data->Modified();
>> > this->GetCurrentRenderer()->Render();
>> > this->GetCurrentRenderer()->GetRenderWindow()->Render();
>> >
>> > }
>> >
>> > //_____________________________________________________________________________________
>> > void OnMiddleButtonDown()
>> > {
>> > // Get the selected point
>> > int x = this->Interactor->GetEventPosition()[0];
>> > int y = this->Interactor->GetEventPosition()[1];
>> > this->FindPokedRenderer(x, y);
>> >
>> > this->PointPicker->Pick(this->Interactor->GetEventPosition()[0],
>> > this->Interactor->GetEventPosition()[1],
>> > 0, // always zero.
>> >
>> >
>> > this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
>> > if(this->PointPicker->GetPointId() >= 0)
>> > {
>> > this->StartPan();
>> > this->MoveActor->VisibilityOn();
>> > this->Move = true;
>> > this->SelectedPoint = this->PointPicker->GetPointId();
>> >
>> > std::cout << "Dragging point " << this->SelectedPoint <<
>> > std::endl;
>> >
>> > double p[3];
>> > this->Data->GetPoint(this->SelectedPoint, p);
>> > std::cout << "p: " << p[0] << " " << p[1] << " " << p[2] <<
>> > std::endl;
>> > this->MoveActor->SetPosition(p);
>> >
>> > this->GetCurrentRenderer()->AddActor(this->MoveActor);
>> > this->InteractionProp = this->MoveActor;
>> > }
>> > }
>> >
>> > //_____________________________________________________________________________________
>> > vtkPolyData* Data;
>> > vtkPolyData* GlyphData;
>> >
>> > vtkSmartPointer<vtkPolyDataMapper> MoveMapper;
>> > vtkSmartPointer<vtkActor> MoveActor;
>> > vtkSmartPointer<vtkPolyData> MovePolyData;
>> > vtkSmartPointer<vtkVertexGlyphFilter> MoveGlyphFilter;
>> > vtkSmartPointer<vtkPointPicker> PointPicker;
>> > bool Move;
>> > vtkIdType SelectedPoint;
>> > };
>> >
>> > //_____________________________________________________________________________________
>> > vtkStandardNewMacro(InteractorStyle);
>> >
>> > //_____________________________________________________________________________________
>> >
>> > int main (int, char *[])
>> > {
>> > // Create a sphere (object to be deformed)
>> > vtkSmartPointer<vtkSphereSource> sphereSource =
>> > vtkSmartPointer<vtkSphereSource>::New();
>> > sphereSource->SetCenter(20.0, 20.0, 20.0);
>> > sphereSource->SetRadius(20.0);
>> > sphereSource->SetThetaResolution(10);
>> > sphereSource->SetPhiResolution(10);
>> > sphereSource->Update();
>> >
>> > //Create polydata from sphereSource
>> > vtkSmartPointer<vtkPolyData> points_poly =
>> > vtkSmartPointer<vtkPolyData>::New();
>> > points_poly->CopyStructure(sphereSource->GetOutput());
>> >
>> > vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter
>> > = vtkSmartPointer<vtkVertexGlyphFilter>::New();
>> > glyphFilter->SetInputConnection(points_poly->GetProducerPort());
>> > glyphFilter->Update();
>> > // Create a mapper and actor
>> > vtkSmartPointer<vtkPolyDataMapper> points_mapper =
>> > vtkSmartPointer<vtkPolyDataMapper>::New();
>> > points_mapper->SetInputConnection(glyphFilter->GetOutputPort());
>> >
>> > vtkSmartPointer<vtkActor> points_actor =
>> > vtkSmartPointer<vtkActor>::New();
>> > points_actor->SetMapper(points_mapper);
>> > points_actor->GetProperty()->SetPointSize(10);
>> > vtkSmartPointer<vtkPolyDataMapper> Mapper2 =
>> > vtkSmartPointer<vtkPolyDataMapper>::New();
>> > Mapper2->SetInput(points_poly);
>> >
>> > vtkSmartPointer<vtkActor> Actor2 = vtkSmartPointer<vtkActor>::New();
>> > Actor2->SetMapper(Mapper2);
>> >
>> > //assembly to synchronise interaction of points & polydata
>> > vtkSmartPointer<vtkAssembly> assembly
>> > = vtkSmartPointer<vtkAssembly>::New();
>> > assembly->AddPart(points_actor);
>> > assembly->AddPart(Actor2);
>> > // Visualize
>> > vtkSmartPointer<vtkRenderer> renderer =
>> > vtkSmartPointer<vtkRenderer>::New();
>> > vtkSmartPointer<vtkRenderWindow> renderWindow =
>> > vtkSmartPointer<vtkRenderWindow>::New();
>> > renderWindow->AddRenderer(renderer);
>> >
>> > vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
>> > vtkSmartPointer<vtkRenderWindowInteractor>::New();
>> > renderWindowInteractor->SetRenderWindow(renderWindow);
>> >
>> > renderer->AddActor(assembly);
>> >
>> > renderWindow->Render();
>> >
>> > vtkSmartPointer<InteractorStyle> style =
>> > vtkSmartPointer<InteractorStyle>::New();
>> > renderWindowInteractor->SetInteractorStyle( style );
>> > style->Data = points_poly;
>> >
>> > renderWindowInteractor->Start();
>> > return EXIT_SUCCESS;
>> > }
>> >
>> > _______________________________________________
>> > 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
>> >
>> >
>> _______________________________________________
>> 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