[vtkusers] 3D line and coordinate system

Pengdong Xiao xiaopd at hotmail.com
Mon Jan 7 21:16:52 EST 2013


Hi All,

I meet some difficulty with VTK while trying 
to draw a 3D line vertical to the screen passing through the mouse clicked 
point. My source code is as follows. After I run the code, I couldn't 
get a 3D line vertical to the screen, but get a 3D line drawn away from 
the mouse clicked point and not vertical to the screen. Would like to 
know how to solve the problem. Many thanks.

void MousepressCallbackFunction2 ( vtkObject* caller, long unsigned int vtkNotUsed(eventId), 
                                   void* vtkNotUsed(clientData), void* vtkNotUsed(callData) )
{
  std::cout << "Mousepress2 callback" << std::endl;
 
  vtkRenderWindowInteractor *iren = 
    static_cast<vtkRenderWindowInteractor*>(caller);

  std::cout << "Picking pixel: " << iren->GetEventPosition()[0] 
        << " " << iren->GetEventPosition()[1] << std::endl;

  // First point:
  double display1[3];
  display1[0] = iren->GetEventPosition()[0];
  display1[1] = iren->GetEventPosition()[1];
  display1[2] = -1;

  iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->SetDisplayPoint(display1);
  iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->DisplayToWorld();
  double* world1 = iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetWorldPoint();
  double world_coordinate1[3];
  for (int i=0; i<3; i++)
      world_coordinate1[i] = world1[i];

  // Second point:
  double display2[3];
  display2[0] = display1[0];
  display2[1] = display1[1];
  display2[2] = 1;

  iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->SetDisplayPoint(display2);
  iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->DisplayToWorld();
  double* world2 = iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetWorldPoint();
  double world_coordinate2[3];
  for (int i=0; i<3; i++)
      world_coordinate2[i] = world2[i];

  // 3D line from first point to second point:
  vtkLineSource* line = vtkLineSource::New();
  vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
  vtkActor* actor = vtkActor::New();

  line->SetPoint1(world_coordinate1[0],
                  world_coordinate1[1],
                  world_coordinate1[2]);
  line->SetPoint2(world_coordinate2[0],
                  world_coordinate2[1],
                  world_coordinate2[2]);

  mapper->SetInput(line->GetOutput());
  actor->SetMapper(mapper);

  iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
}
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130108/6d459673/attachment.htm>


More information about the vtkusers mailing list