简体   繁体   中英

How to draw primitives (points, lines etc) in vtk?

I need to draw primitives on a vtkActor, but I don't know how. Maybe I may use opengl functions for this? In this example http://www.vtk.org/Wiki/VTK/Examples/Cxx/Plotting/Diagram it's working, but I need to draw it interactively. Thanks for answers.

I was looking for a way to draw a grid in the screen with VTK and I came up with this piece of code:

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> lineIndices = vtkSmartPointer<vtkCellArray>::New();

for(int iIndex = 0, connectivityIndex = 0; iIndex <= m_resolution; ++iIndex, connectivityIndex += 4)
{
    double pointCoordinate = m_range[0] + (m_range[1] - m_range[0]) * (iIndex / static_cast<double>(m_resolution));
    points->InsertNextPoint(pointCoordinate, -m_range[1], 0.0);
    points->InsertNextPoint(pointCoordinate,  m_range[1], 0.0);

    points->InsertNextPoint(-m_range[1], pointCoordinate, 0.0);
    points->InsertNextPoint( m_range[1], pointCoordinate, 0.0);

    lineIndices->InsertNextCell(2);
    lineIndices->InsertCellPoint(connectivityIndex + 0);
    lineIndices->InsertCellPoint(connectivityIndex + 1);

    lineIndices->InsertNextCell(2);
    lineIndices->InsertCellPoint(connectivityIndex + 2);
    lineIndices->InsertCellPoint(connectivityIndex + 3);
}

vtkSmartPointer<vtkPolyData> data = vtkSmartPointer<vtkPolyData>::New();
data->SetPoints(points);
data->SetLines(lineIndices);

vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(data);

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);

It was based on @shash code and it is working.

What do you mean by drawing primitives?

You see, VTK is more tuned towards rendering (as opposed to drawing) datasets of various types. For example, 3D meshes may be drawn using vtkPolyDataMapper and vtkActor, and images using vtkImageActor and similar classes.

Generally, you have one or more datasets, for which you create actors and show them on screen.

You don't really draw primitives on-screen like (say) with GDI or the HTML5 canvas. Depending on what you want, you'd either add another actor with the appropriate dataset, a widget (which is kind of an actor with interactivity), or a 2D actor, which gets drawn as an overlay on top of the whole scene, and is described in screen coordinates.

If you could describe what you're trying to achieve, I can point you to some more specific things.

Disclaimer: Untested code. I am writing this from memory. So test and read the manual as required. Note that the following code draws a single line, For performance reasons I suggest. instead of creating an Actor per line. Create multiple lines (if not all your lines) in one Actor. Just add points and set the correct indices...

vtkPoints* points = vtkPoints::New();
vtkCellArray* lineIndices = vtkCellArray::New();

points->InsertNextPoint( 0, 0, 0);
points->InsertNextPoint(10, 0, 0);

lineIndices->InsertNextCell(2);
lineIndices->InsertNextCellPoint(0); // indices to points array.
lineIndices->InsertNextCellPoint(1);

vtkPolyData* polyData = vtkPolyData::New();
polyData->SetPoints(points);
polyData->SetLines(lines);

// continue with standard mapper, actor setup...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM