[vtkusers] drawing points from inside an Observer

Greg Book gbook at gbook.org
Fri Jul 10 13:48:01 EDT 2009


I'm trying to draw points when the user clicks the mouse on a 
vtkImagePlaneWidget. I'm able to get the location that the user clicked 
and create a point at that location but I'm having a couple of problems:

1) The point isn't drawn until I click on another point or interact with 
the widget in some way.

2) I'm trying to trace (draw contour lines) in 3D, so I'm trying to 
create a model to use later on and not only display the points. So I'd 
like a single object like vtkPolyData that holds all the contour/tracing 
information. I'd like to keep that object outside of the observer, but 
it seems when I do that, I get nothing displayed.

3) I'm not really familiar with how vtkPolyData is created and how 
points are added and vertexes connected. What is the best way to do this 
if I am adding points by clicking... or are there any examples that 
create vtkPolyData from random points?

Here is the code from the main part of my program that calls the observer:

    points = vtkPoints::New();
    poly = vtkPolyData::New();
    conn = vtkCellArray::New();

    // create some test points
    x[0] = x[1] = x[2] = 100.0;
    points->InsertNextPoint(x);
    x[0] = x[1] = x[2] = 50.0;
    points->InsertNextPoint(x);
    x[0] = x[1] = x[2] = 200.0;
    points->InsertNextPoint(x);

    // Create topology of point cloud. This is necessary!
    for (int i = 0; i < points->GetNumberOfPoints(); i++)
        conn->InsertNextCell(1, &i);

    poly->SetPoints(points);
    poly->Update();
    poly->SetVerts(conn);
    vtkPolyDataMapper *polyMapper = vtkPolyDataMapper::New();
    polyMapper->SetInput(poly);
    polyMapper->Update();

    vtkActor* polyActor = vtkActor::New();
    polyActor->SetMapper(polyMapper);
    polyActor->GetProperty()->SetPointSize(3.0);
    ren1->AddActor(polyActor);

    ObserverMousePicker *observeMousePick = 
ObserverMousePicker::New(planeWidgetX, planeWidgetY, planeWidgetZ, 
points, poly, polyMapper, polyActor, conn, ren1, vtkWindow, log);
    
planeWidgetX->GetInteractor()->AddObserver(vtkCommand::LeftButtonPressEvent, 
observeMousePick, 1.0);
    observeMousePick->Delete();

Here is the code from the observer. If I create the vtkPolyDataMapper 
and vtkPolyActor variables locally, it displays correctly. But if I pass 
in pointers, the points don't display correctly.

    double *p_x;

    p_x = m_planeWidgetX->GetCurrentCursorPosition();
    m_points->InsertNextPoint(p_x);
    log->WriteLog(wxString::Format("Clicked at: (%f,%f,%f)", p_x[0], 
p_x[1], p_x[2]));

    for (int i = 0; i < m_points->GetNumberOfPoints(); i++)
        m_conn->InsertNextCell(1, &i);

    m_poly->SetPoints(m_points);
    m_poly->Update();
    m_poly->SetVerts(m_conn);
    vtkPolyDataMapper *polyMapper = vtkPolyDataMapper::New();
    polyMapper->SetInput(m_poly);
    polyMapper->Update();

    vtkActor* polyActor = vtkActor::New();
    polyActor->SetMapper(polyMapper);
    polyActor->GetProperty()->SetPointSize(3.0);
    m_ren1->AddActor(polyActor);
    m_vtkWindow->Render();




-Greg



More information about the vtkusers mailing list