#include #include #include #include #include #include #include #include #include #include #include #include #include // Helper function called by createActorFromVTKDataSet () methods. // This function determines the default setting of vtkMapper::InterpolateScalarsBeforeMapping. // Return 0, interpolation off, if data is a vtkPolyData that contains only vertices. // Return 1, interpolation on, for anything else. int getDefaultScalarInterpolationForDataSet (vtkDataSet* data) { vtkPolyData* polyData = vtkPolyData::SafeDownCast (data); // Check that polyData != NULL in case of segfault return (polyData && polyData->GetNumberOfCells () != polyData->GetNumberOfVerts ()); } void createActorFromVTKDataSet (const vtkSmartPointer &data, vtkSmartPointer &actor, bool use_scalars=true) { // If actor is not initialized, initialize it here if (!actor) actor = vtkSmartPointer::New (); vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInputData (data); if (use_scalars) { vtkSmartPointer scalars = data->GetPointData ()->GetScalars (); if (scalars) { double minmax[2]; scalars->GetRange (minmax); mapper->SetScalarRange (minmax); mapper->SetScalarModeToUsePointData (); mapper->SetInterpolateScalarsBeforeMapping (getDefaultScalarInterpolationForDataSet (data)); mapper->ScalarVisibilityOn (); } } actor->SetNumberOfCloudPoints (static_cast(std::max (1, data->GetNumberOfPoints () / 10))); actor->GetProperty ()->SetInterpolationToFlat (); /// FIXME disabling backface culling due to known VTK bug: vtkTextActors are not /// shown when there is a vtkActor with backface culling on present in the scene /// Please see VTK bug tracker for more details: http://www.vtk.org/Bug/view.php?id=12588 // actor->GetProperty ()->BackfaceCullingOn (); actor->SetMapper (mapper); } int main() { int n_lines = 500; vtkSmartPointer line_data = vtkSmartPointer::New (); // Prepare colors vtkSmartPointer line_colors = vtkSmartPointer::New (); line_colors->SetNumberOfComponents (3); line_colors->SetName ("Colors"); line_colors->SetNumberOfTuples (n_lines); // Prepare coordinates vtkSmartPointer line_points = vtkSmartPointer::New (); line_points->SetNumberOfPoints (2 * n_lines); vtkSmartPointer line_cells_id = vtkSmartPointer::New (); line_cells_id->SetNumberOfComponents (3); line_cells_id->SetNumberOfTuples (n_lines); vtkIdType *line_cell_id = line_cells_id->GetPointer (0); vtkSmartPointer line_cells = vtkSmartPointer::New (); vtkSmartPointer line_tcoords = vtkSmartPointer::New (); line_tcoords->SetNumberOfComponents (1); line_tcoords->SetNumberOfTuples (n_lines * 2); line_tcoords->SetName ("Texture Coordinates"); double tc[3] = {0.0, 0.0, 0.0}; // Draw lines between the best corresponding points for (std::size_t i = 0; i < n_lines; ++i) { int id1 = i * 2 + 0, id2 = i * 2 + 1; // Set the points line_points->SetPoint (id1, std::sin(2.0*M_PI*i/n_lines), std::cos(2.0*M_PI*i/n_lines), 0.0); line_points->SetPoint (id2, std::sin(2.0*M_PI*(i+1)/n_lines), std::cos(2.0*M_PI*(i+1)/n_lines), 0.0); // Set the cell ID *line_cell_id++ = 2; *line_cell_id++ = id1; *line_cell_id++ = id2; // Set the texture coords tc[0] = 0.; line_tcoords->SetTuple (id1, tc); tc[0] = 1.; line_tcoords->SetTuple (id2, tc); float rgb[3]; rgb[0] = vtkMath::Random (32, 255); // min / max rgb[1] = vtkMath::Random (32, 255); rgb[2] = vtkMath::Random (32, 255); line_colors->InsertTuple (i, rgb); } line_colors->SetNumberOfTuples (n_lines); line_cells_id->SetNumberOfTuples (n_lines); line_cells->SetCells (n_lines, line_cells_id); line_points->SetNumberOfPoints (n_lines*2); line_tcoords->SetNumberOfTuples (n_lines*2); // Fill in the lines line_data->SetPoints (line_points); line_data->SetLines (line_cells); line_data->GetPointData ()->SetTCoords (line_tcoords); line_data->GetCellData ()->SetScalars (line_colors); // Create an Actor vtkSmartPointer actor; createActorFromVTKDataSet (line_data, actor); actor->GetProperty ()->SetRepresentationToWireframe (); actor->GetProperty ()->SetOpacity (0.5); vtkNew renderer; vtkNew renderWindow; renderWindow->AddRenderer(renderer); renderWindow->SetWindowName("vtktest"); vtkNew renderWindowInteractor; renderWindowInteractor->SetRenderWindow(renderWindow); // Add the actor to the scene renderer->AddActor(actor); renderer->SetBackground(0, 0, 0); // Render and interact renderWindow->Render(); renderWindowInteractor->Start(); std::cout << "done" << std::endl; return 0; }