[vtkusers] a problem when draw a rectangle dynamically

455535440 455535440 at qq.com
Fri Jun 10 12:58:25 EDT 2011


Hello everyone~~
I want draw a recrangle dynamically with vtk ,
here is my way:
1,ceate a VtkPolydata and use it diaplay the rectangle
2,add the observer to the Interactor ,when the vtkCommand::LeftButtonPressEvent,LeftButtonReleaseEvent,MouseMoveEvent
happened call the command
3,in the execute() change the property of vtkpolydata and then render again
the main code is like below:

1    points = vtkPoints::New();
    points->SetNumberOfPoints(4);
    points->SetPoint(0,90,90,0);
    points->SetPoint(1,90,120,0);
    points->SetPoint(2,120,120,0);
    points->SetPoint(3,120,90,0);
    lines = vtkCellArray::New();
    lines->InsertNextCell(5); 
    lines->InsertCellPoint(0); 
    lines->InsertCellPoint(1); 
    lines->InsertCellPoint(2); 
    lines->InsertCellPoint(3); 
    lines->InsertCellPoint(0); 
    polyData = vtkPolyData::New();
    polyData->SetPoints(points);
    polyData->SetLines(lines);
    vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
    mapper->SetInput(polyData);
    vtkActor* rectActor = vtkActor::New();
    rectActor->SetMapper(mapper);
    rectActor->GetProperty()->SetColor( 1, 0, 0 ); 
    render->AddActor(rectActor);

2,    MyInteractorCallback* m_pInteractorCallback = MyInteractorCallback::New();
    iren->AddObserver( vtkCommand::LeftButtonPressEvent, m_pInteractorCallback);
    iren->AddObserver( vtkCommand::LeftButtonReleaseEvent, m_pInteractorCallback);
    iren->AddObserver( vtkCommand::MouseMoveEvent, m_pInteractorCallback);

3,    virtual void Execute(vtkObject *caller, unsigned long eventId, void *callData)
    {
        if (eventId==vtkCommand::LeftButtonPressEvent)
        {
            vtkWin32RenderWindowInteractor  *interactor = reinterpret_cast<vtkWin32RenderWindowInteractor *>(caller);
            double x = interactor->GetEventPosition()[0];
            double y = interactor->GetEventPosition()[1];
            MyLeftButtonDownEvent(x,y);
        }
        else if(eventId==vtkCommand::LeftButtonReleaseEvent)
        {
            vtkWin32RenderWindowInteractor  *interactor = reinterpret_cast<vtkWin32RenderWindowInteractor *>(caller);
            double x = interactor->GetEventPosition()[0];
            double y = interactor->GetEventPosition()[1];
            MyLeftButtonUpEvent(x,y);
        }
        else if(eventId ==vtkCommand::MouseMoveEvent)
        {   
            vtkWin32RenderWindowInteractor  *interactor = reinterpret_cast<vtkWin32RenderWindowInteractor *>(caller);
            double x = interactor->GetEventPosition()[0];
            double y = interactor->GetEventPosition()[1];
            MyMouseMoveEvent(x,y);
        }
        
    }
    void MyLeftButtonDownEvent(double x,double y)
    {
        rectable = 1;
        x_lt = x;
        y_lt = y;
        points->SetPoint(0,x_lt,y_lt,0);

    }
    void MyLeftButtonUpEvent(double x,double y)
    {

        rectable =0;
            
    }
    void MyMouseMoveEvent(double x,double y)
    {
        if (1 == rectable)
        {
            x_rb = x;
            y_rb = y;
            points->SetPoint(1,x_lt,y_rb,0);
            points->SetPoint(2,x_rb,y_rb,0);
            points->SetPoint(3,x_rb,y_lt,0);
            
            renWin->Render();
        }
    }
but the rectangle didn't changed as I wish,
I debug the code and  find the point has been changed but the rectangle didn't changed.
can anyone solve this problem?  thx ~~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110611/90d0cc79/attachment.htm>


More information about the vtkusers mailing list