[vtkusers] Draw a 3D line and coordinate conversion

Sankhesh Jhaveri sankhesh.jhaveri at kitware.com
Thu Jan 10 09:27:56 EST 2013


Pengdong,

I apologize if I was not clear earlier. Look at this example: 
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking

You can get the world coordinates of the mouse click in the same way 
(as they do in the example). You have a normal vector the screen. You 
then just need to translate the vector to that point.

Hope that helps,
Sankhesh

P.S.: I added the vtk-users list in CC for archival purposes.

On Thu 10 Jan 2013 03:38:38 AM EST, Pengdong Xiao wrote:
> Hi Sankhesh,
>
> Thank you very much for your reply. Yes, I would like to draw a 3D
> line perpendicular to the screen, but I also would like to have the
> world coordinate values of the two end points of the line in order to
> do some calculation.
>
> It seems your suggested method can not get the world coordinate values
> of the two end points of the line. Any other suggestions? Many thanks.
>
> Best regards,
> Pengdong
>
> ------------------------------------------------------------------------
> Date: Wed, 9 Jan 2013 13:03:09 -0500
> From: sankhesh.jhaveri at kitware.com
> To: xiaopd at hotmail.com
> Subject: Re: Draw a 3D line and coordinate conversion
>
> Hi Pengdong,
>
> If I understand correctly, you want to draw a line perpendicular to
> the screen, right? If so, you can get the camera view plane normal
> (this->GetDefaultRenderer()->GetActiveCamera()->GetViewPlaneNormal()).
> All you have to do then is to draw a normal at the clicked point to
> the view plane.
>
> Does that sound like what you need?
>
> Sankhesh
> On 01/09/2013 04:59 AM, Pengdong Xiao wrote:
>
>     Hi Sankhesh,
>
>     I am a researcher in A*Star Singapore doing biomedical image
>     analysis. I posted my message on vtkusers and vtk-developers
>     mailing lists, no one answers. So I would like to seek help from you.
>
>     The problem is with VTK. I would like 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.
>
>     The code is basically to
>         (1) get event position (position in display coordinate system)
>     from vtkRenderWindowInteractor,
>             iren->GetEventPosition();
>         (2) call a renderer's functions to get the first and second
>     points by setting -300 and 300 in Z axis of display coordinate for
>     the two points,
>             SetDisplayPoint(display1);
>             DisplayToWorld();
>             GetWorldPoint();
>         (3) use vtkLineSource to draw a 3D line from the first point
>     to the second point.
>
>     The code is as follows.
>
>     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 << "Pickin g pixel: " << iren->GetEventPosition()[0]
>             << " " << iren->GetEventPosition()[1] << std::endl;
>
>       // First point:
>       double display1[3];
>       display1[0] = iren->GetEventPosition()[0];
>       display1[1] = 1024-iren->GetEventPosition()[1];
>       display1[2] = -300;
>
>     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]/world1[3];
>
>       // Second point:
>       double display2[3];
>       display2[0] = display1[0];
>       display2[1] = display1[1];< br>  display2[2] = 300;
>
>     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]/world2[3];
>
>       // 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);
>     }
>
>
>



More information about the vtkusers mailing list