[vtkusers] Drawing objects interactively in VTK..??

Darshan Pai darshanpai at gmail.com
Tue Apr 13 16:54:31 EDT 2010


If in 2D , why not just use a vtkImageCanvas2D and the vtkImageTracerWidget
?

Regards
Darshan

On Mon, Apr 12, 2010 at 3:22 AM, Rakesh Patil <rakeshthp at in.com> wrote:

>
> Hello Darshan,
>
> I see that your code works fine.. There was only a small change in yours
> and mine ... The difference was that i was displaying the actors outside the
> mouse click event and in your case, you are displaying actors in the mouse
> click event..
>
> When i rendered my actors it also worked fine.. But i found a small
> problem.. As i go deeper and deeper by zooming.. points the clicking point
> and displaying points are not matching each others. Then i though there
> might be some problem in my code.. So, i carried out the same test with your
> code, and found that as I click away from the center point, the mouse
> position and display position are not matching... Just zoom out as much as
> possible and check it once...
>
> Ya and i forgot to tell one more thing... I want to implement it in 2D
> display.. So i changed the followin part of code:
>
>
> sphereSource->SetCenter(pt[0],pt[1],pt[2]*1*n[0]);
>
> to
>
> sphereSource->SetCenter(pt[0],pt[1],0 );
>
> Will this change create some problem..??
>
> Thanks
>
> Regards
> Rakesh Patil
>
>
> ---------- Original message ----------
> From:Darshan Pai< darshanpai at gmail.com >
> Date: 12 Apr 10 06:27:53
> Subject: Re: [vtkusers] Drawing objects interactively in VTK..??
> To: Rakesh Patil
>
> #include <vtkRenderWindow.h>
> #include <vtkRenderWindowInteractor.h>
> #include <vtkRenderer.h>
> #include <vtkSphereSource.h>
> #include <vtkPolyDataMapper.h>
> #include <vtkActor.h>
> #include <vtkSmartPointer.h>
> #include <vtkPointPicker.h>
> #include <vtkCamera.h>
> #include <vtkInteractorStyleTrackballCamera.h>
> #include <vtkObjectFactory.h>
> #include <vtkCoordinate.h>
> #include <vtkProperty.h>
>
> // Define interaction style
> class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
> {
> public:
> static MouseInteractorStyle* New();
> vtkTypeRevisionMacro(MouseInteractorStyle,
> vtkInteractorStyleTrackballCamera);
>
> virtual void OnLeftButtonDown()
> {
> cout << "Pressed left mouse button." << endl;
> // forward events
> double x = this->Interactor->GetEventPosition()[0];
> double y = this->Interactor->GetEventPosition()[1];
> cout << x << " " << y << endl;
> vtkSmartPointer<vtkCoordinate> coordinate =
> vtkSmartPointer<vtkCoordinate>::New();
> coordinate->SetCoordinateSystemToDisplay();
> coordinate->SetValue(x,y);
> double *pt = new double[3];
> pt = coordinate->GetComputedWorldValue(render);
> cout << pt[0] << " " <<pt[1] << " " << pt[2] << endl;
>
> vtkCamera *camera = render->GetActiveCamera();
> double *n = new double[2];
> n = camera->GetClippingRange();
> cout << "Cuureent n[0] " << n[0]<< endl;
> vtkSmartPointer<vtkSphereSource> sphereSource =
> vtkSmartPointer<vtkSphereSource>::New();
> sphereSource->SetCenter(pt[0],pt[1],pt[2]*1*n[0]);
> sphereSource->SetRadius(0.1);
> sphereSource->Update();
>
> vtkSmartPointer<vtkPolyDataMapper> mapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
> mapper->SetInputConnection(sphereSource->GetOutputPort());
>
> vtkSmartPointer<vtkActor> actor =
> vtkSmartPointer<vtkActor>::New();
> actor->SetMapper(mapper);
>
>
> render->AddActor(actor);
> actor->GetProperty()->SetColor(1,0,0);
> //render->ResetCamera();
> //render->ResetCameraClippingRange();
> render->GetRenderWindow()->GetInteractor()->Render();
>
> // vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
> }
>
> virtual void OnMiddleButtonDown()
> {
> cout << "Pressed middle mouse button." << endl;
> // forward events
> //vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
> }
>
> virtual void OnRightButtonDown()
> {
> cout << "Pressed right mouse button." << endl;
> // forward events
>
> vtkInteractorStyleTrackballCamera::OnRightButtonDown();
> }
>
> virtual void OnRightButtonUp()
> {
> vtkCamera *camera = render->GetActiveCamera();
> camera->SetClippingRange(1,30);
> vtkInteractorStyleTrackballCamera::OnRightButtonUp();
> }
> vtkSmartPointer<vtkRenderer> render;
>
> };
> vtkCxxRevisionMacro(MouseInteractorStyle, "$Revision: 1.1 $");
> vtkStandardNewMacro(MouseInteractorStyle);
>
> int main ( int argc, char* argv[] )
> {
>
> vtkSmartPointer<vtkRenderer> renderer =
> vtkSmartPointer<vtkRenderer>::New();
> renderer->SetBackground(1,1,1); // Background color white
> vtkSmartPointer<vtkRenderWindow> renderWindow =
> vtkSmartPointer<vtkRenderWindow>::New();
> renderWindow->AddRenderer(renderer);
> renderWindow->SetSize(500,500);
> // renderWindow->SetS
>
> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
> renderWindowInteractor->SetRenderWindow ( renderWindow );
>
> vtkSmartPointer<MouseInteractorStyle> style =
> vtkSmartPointer<MouseInteractorStyle>::New();
> renderWindowInteractor->SetInteractorStyle( style );
> style->render = renderer;
> renderWindowInteractor->Initialize();
> renderer->ResetCamera();
> //renderer->ResetCameraClippingRange();
> renderWindow->Render();
>
> vtkCamera *camera = renderer->GetActiveCamera();
> camera->SetClippingRange(1,30);
> double *n = new double[2];
> n = camera->GetClippingRange();
>
>
> vtkSmartPointer<vtkCoordinate> coordinate =
> vtkSmartPointer<vtkCoordinate>::New();
> coordinate->SetCoordinateSystemToDisplay();
> coordinate->SetValue(250,147);
> double *pt = new double[3];
> pt = coordinate->GetComputedWorldValue(renderer);
> cout << pt[0] << " " <<pt[1] << " " << pt[2] << endl;
> vtkSmartPointer<vtkSphereSource> sphereSource =
> vtkSmartPointer<vtkSphereSource>::New();
> sphereSource->SetCenter(pt[0],pt[1],pt[2]*1*n[0]);
> sphereSource->SetRadius(0.1);
> sphereSource->Update();
>
> vtkSmartPointer<vtkPolyDataMapper> mapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
> mapper->SetInputConnection(sphereSource->GetOutputPort());
>
> vtkSmartPointer<vtkActor> actor =
> vtkSmartPointer<vtkActor>::New();
> actor->SetMapper(mapper);
>
>
> renderer->AddActor(actor);
>
> //renderer->ResetCamera();
>
>
>
> renderWindowInteractor->Start();
>
> return EXIT_SUCCESS;
> }
>
> On Thu, Apr 8, 2010 at 9:25 PM, Rakesh Patil <rakeshthp at in.com> wrote:
>
>>
>>
>> Hi,
>>
>> I tried with that also... But no use.. By the way, I'm using
>> wxVTKRenderWindowInteractor.. So wil this create any problem..??
>>
>> Because you said, you reproduced the same without any actor.. can you
>> attach your piece of code ..??
>>
>>
>> thanks
>>
>> regards
>> rakesh patil
>>
>> ---------- Original message ----------
>> From:Darshan Pai< darshanpai at gmail.com >
>> Date: 09 Apr 10 00:18:54
>> Subject: Re: [vtkusers] Drawing objects interactively in VTK..??
>> To: Rakesh Patil
>>
>> This should be
>> mainPG::getInstance()->newArc->InsertNextPoint( pt[0], pt[1],
>> pt[2]*-1*n[0]);
>>
>> Regards
>> DP
>>
>> On Thu, Apr 8, 2010 at 1:05 PM, Rakesh Patil <rakeshthp at in.com> wrote:
>>
>>> Hi Darshan,
>>>
>>> In the mouse click event, i have the followin block of code..
>>>
>>> vtkCamera *camera =
>>> this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActiveCamera();
>>>
>>> double *n = new double[2];
>>> n = camera->GetClippingRange();
>>>
>>>
>>> vtkSmartPointer<vtkCoordinate> coordinate =
>>> vtkSmartPointer<vtkCoordinate>::New();
>>> coordinate->SetCoordinateSystemToDisplay();
>>> coordinate->SetValue(x,y); // display coordinates
>>> double *pt = new double[3];
>>> pt = coordinate->GetComputedWorldValue(
>>> this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()
>>> );
>>> mainPG::getInstance()->newArc->InsertNextPoint( pt[0]*pt[2], pt[1]*pt[2],
>>> pt[2]*-1*n[0]);
>>> mainPG::getInstance()->newArc->EndOfDrawing(false);
>>>
>>> So here, for each mouse click event, the values are stored in an object
>>> called newArc.. I use right click to stop the iteractively drawing..
>>> Thus for the right click event, i call,
>>>
>>> mainPG::getInstance()->newArc->EndOfDrawing(true);
>>> mainPG::getInstance()->newArc->Update();
>>>
>>> Here the Update() function causes that object to create an actor and
>>> display it in the render window. But i still not getting the way it comes
>>> using vtkImageTracerWidget.. I mean, i ckick at one place and it really get
>>> displayed somewhere else..
>>>
>>> Any more ideas or suggestions..??
>>>
>>> Thanks
>>>
>>> Regards
>>> Rakesh Patil
>>>
>>>
>>> ---------- Original message ----------
>>> From:Darshan Pai< darshanpai at gmail.com >
>>>  Date: 08 Apr 10 08:13:18
>>> Subject: Re: [vtkusers] Drawing objects interactively in VTK..??
>>> To: Rakesh Patil
>>>
>>> Did you multiply z with the near clipping plane . because I could
>>> reproduce what you want to do without the actor . Anyway if that is not
>>> working for you then I don't have any other explanations at this time :)
>>>
>>> Regards
>>>
>>> Darshan
>>>
>>> On Wed, Apr 7, 2010 at 9:58 PM, Rakesh Patil <rakeshthp at in.com> wrote:
>>>
>>>>
>>>>
>>>> Yes..
>>>>
>>>> I did try that at the very beginning... Not only this but i tried out
>>>> the various combination of the coordinates.. none of
>>>> them matched for me... :-)
>>>>
>>>> vtkImageTracerWidget is performing the task what i want.. but it needs a
>>>> prop beneath it.... and for me, it is with or without prop object.. wither
>>>> ways it should be possible for me to draw...
>>>>
>>>> Any suggestions..??
>>>>
>>>>
>>>> Thanks
>>>>
>>>> Regards
>>>> Rakesh Patil
>>>> ---------- Original message ----------
>>>> From:Darshan Pai< darshanpai at gmail.com >
>>>> Date: 08 Apr 10 04:55:02
>>>> Subject: Re: [vtkusers] Drawing objects interactively in VTK..??
>>>> To: Rakesh Patil
>>>>
>>>> replied too fast . The displaytoview() and viewToWorld does not take
>>>> points as input.
>>>>
>>>> But you can use the vtkCoordinate . Try this particular snippet of code.
>>>>
>>>> vtkCamera *camera = renderer->GetActiveCamera();
>>>> double *n = new double[2];
>>>> n = camera->GetClippingRange();
>>>>
>>>>
>>>> vtkSmartPointer<vtkCoordinate> coordinate =
>>>> vtkSmartPointer<vtkCoordinate>::New();
>>>> coordinate->SetCoordinateSystemToDisplay();
>>>> coordinate->SetValue(x,y); // display coordinates
>>>> double *pt = new double[3];
>>>> pt = coordinate->GetComputedWorldValue(renderer);
>>>> cout << pt[0] << " " <<pt[1] << " " << pt[2] << endl;
>>>> vtkSmartPointer<vtkSphereSource> sphereSource =
>>>> vtkSmartPointer<vtkSphereSource>::New();
>>>> sphereSource->SetCenter(pt[0],pt[1],pt[2]*-1*n[0]);
>>>> sphereSource->SetRadius(1.0);
>>>> sphereSource->Update();
>>>>
>>>> depth is assumed to be 1 and n[0] is the near clip..
>>>>
>>>>
>>>> On Wed, Apr 7, 2010 at 6:26 PM, Darshan Pai <darshanpai at gmail.com>wrote:
>>>>
>>>>> I don't know how to get the matrices defined . Maybe someone can help .
>>>>> But vtkRenderer has some functions like DisplayToView() and
>>>>> ViewToWorld() which can get you the world coordinate for that point.
>>>>> The default camera has the clipping information . You provide the depth
>>>>> of the point
>>>>> The world point will be <x*depth, y*depth, depth*-1*nearclipplane>
>>>>>
>>>>> See if it works .
>>>>>
>>>>> Regards
>>>>> Darshan
>>>>>
>>>>>  On Wed, Apr 7, 2010 at 4:37 AM, Rakesh Patil <rakeshthp at in.com>wrote:
>>>>>
>>>>>>
>>>>>> Hi David,
>>>>>>
>>>>>> That is exactly what i want.. But how can i get the points or lines
>>>>>> which are traced..?? And also it should work without any actor..
>>>>>> I mean this class works only for some actors. But i need for with
>>>>>> actor and without actor..
>>>>>>
>>>>>> Thanks
>>>>>>
>>>>>> Regards
>>>>>> Rakesh Patil
>>>>>>
>>>>>>
>>>>>> ---------- Original message ----------
>>>>>> From:David Doria< daviddoria+vtk at gmail.com<daviddoria%2Bvtk at gmail.com>>
>>>>>> Date: 06 Apr 10 17:59:52
>>>>>> Subject: Re: [vtkusers] Drawing objects interactively in VTK..??
>>>>>> To: Rakesh Patil
>>>>>>
>>>>>> On Tue, Apr 6, 2010 at 5:13 AM, Rakesh Patil wrote:
>>>>>> > Hello,
>>>>>> >
>>>>>> > I am trying to draw a polyline interactively (with help of mouse).
>>>>>> What i'm
>>>>>> > doing is at every left button down event, i get the mouse position
>>>>>> and store
>>>>>> > it is a vector list. Later using this points am displaying on the
>>>>>> rendering
>>>>>> > window.
>>>>>> >
>>>>>> > But when i do this, i noticed the following:
>>>>>> >
>>>>>> > The mouse position values are totally different from the the points
>>>>>> which
>>>>>> > are displayed.. I mean, i click at one location, and it is displayed
>>>>>> at far
>>>>>> > end another location..
>>>>>> >
>>>>>> > I tried changing the coordinate system also.. Still the result is
>>>>>> > unchanged..
>>>>>> >
>>>>>> > Any suggestions how it is to be done..??
>>>>>> >
>>>>>> > Thanks
>>>>>> > Rakesh Patil
>>>>>> >
>>>>>>
>>>>>> Sounds like you may need a coordinate system conversion in there
>>>>>> somewhere, check out:
>>>>>> http://vtk.org/Wiki/VTK/Examples/Coordinate
>>>>>>
>>>>>> It also sounds like you are trying to duplicate already working code
>>>>>> in:
>>>>>> http://vtk.org/Wiki/VTK/Examples/Widgets/ImageTracerWidget
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>> David
>>>>>>
>>>>>>
>>>>>>
>>>>>>  Get Yourself a cool, short *@in.com* Email ID now!<http://mail.in.com/mails/new_reg.php?utm_source=invite&utm_medium=outgoing>
>>>>>>
>>>>>> _______________________________________________
>>>>>> 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
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>>
>> Dear *Darshan Pai !* Get Yourself a cool, short *@in.com* Email ID now!<http://mail.in.com/mails/new_reg.php?utm_source=invite&utm_medium=outgoing>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100413/dd5fa23f/attachment.htm>


More information about the vtkusers mailing list