[vtkusers] Picking and dragging a vertex - points not moving to appropriate position

Vashist Purbhoo vashist14 at hotmail.com
Fri Jun 17 04:41:55 EDT 2011


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;
} 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110617/2942ed17/attachment.htm>


More information about the vtkusers mailing list