// Code for rendering a 2D graph using vtk // First include the required header files for the VTK classes we are using. #include "vtkMath.h" #include "vtkPoints.h" #include "vtkPolyData.h" #include "vtkDelaunay2D.h" #include "vtkPolyDataMapper.h" #include "vtkExtractEdges.h" #include "vtkTubeFilter.h" #include "vtkProperty.h" #include "vtkRenderWindow.h" #include "vtkCamera.h" #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkSphereSource.h" #include "vtkTestingColors.h" #include "vtkGlyph3D.h" #include "vtkRenderWindowInteractor.h" #include #define N_VERTICES 100 double vertices[N_VERTICES][3]; // main routine int main() { // main() // This example demonstrates how to use 2D Delaunay triangulation. // We create a fancy image of a 2D Delaunay triangulation. Points are // randomly generated. // Generate some random points vtkMath *math = vtkMath::New(); vtkPoints *points = vtkPoints::New(); for (int i=0; i < N_VERTICES; i++) { // for each point vertices[i][0] = math->Random(0,1); vertices[i][1] = math->Random(0,1); vertices[i][2] = 0.0; points->InsertPoint(i, vertices[i][0], vertices[i][1], vertices[i][2]); } // for each point // Create a polydata with the points we just created. vtkPolyData *profile = vtkPolyData::New(); profile->SetPoints(points); // Perform a 2D Delaunay triangulation on them. vtkDelaunay2D *del = vtkDelaunay2D::New(); del->SetInput(profile); del->SetTolerance(0.001); // create a polydata mapper to convert the delaunay triangulation to polygons vtkPolyDataMapper *mapMesh = vtkPolyDataMapper::New(); mapMesh->SetInputConnection(del->GetOutputPort()); vtkActor *meshActor = vtkActor::New(); meshActor->SetMapper(mapMesh); meshActor->GetProperty()->SetColor(0.1, 0.2, 0.4); // We will now create a nice looking mesh by wrapping the edges in tubes, // and putting fat spheres at the points. // extract edges from the triangulation vtkExtractEdges *extract = vtkExtractEdges::New(); extract->SetInputConnection(del->GetOutputPort()); // set up a tube filter to wrap around the edges vtkTubeFilter *tubes = vtkTubeFilter::New(); tubes->SetInputConnection(extract->GetOutputPort()); tubes->SetRadius(0.01); tubes->SetNumberOfSides(6); // and set up a polydata mapper to convert to renderable polygons vtkPolyDataMapper *mapEdges = vtkPolyDataMapper::New(); mapEdges->SetInputConnection(tubes->GetOutputPort()); // set up an actor to make the edges part of the scene vtkActor *edgeActor = vtkActor::New(); edgeActor->SetMapper(mapEdges); edgeActor->GetProperty()->SetColor(vtk_peacock[0],vtk_peacock[1],vtk_peacock[2]); edgeActor->GetProperty()->SetSpecularColor(1, 1, 1); edgeActor->GetProperty()->SetSpecular(0.3); edgeActor->GetProperty()->SetSpecularPower(20); edgeActor->GetProperty()->SetAmbient(0.2); edgeActor->GetProperty()->SetDiffuse(0.8); // set up a source to generate a standard ball vtkSphereSource *ball = vtkSphereSource::New(); ball->SetRadius(0.025); ball->SetThetaResolution(12); ball->SetPhiResolution(12); // now convert it into a glyph by setting it as the paradigm shape vtkGlyph3D *balls = vtkGlyph3D::New(); balls->SetSource(ball->GetOutput()); // and use it as a glyph for all of the input points balls->SetInputConnection(del->GetOutputPort()); // and set up a polydata mapper to convert to renderable polygons vtkPolyDataMapper *mapBalls = vtkPolyDataMapper::New(); mapBalls->SetInputConnection(balls->GetOutputPort()); // and an actor to put the balls in the scene vtkActor *ballActor = vtkActor::New(); ballActor->SetMapper(mapBalls); ballActor->GetProperty()->SetColor(vtk_hot_pink[0],vtk_hot_pink[1],vtk_hot_pink[2]); ballActor->GetProperty()->SetSpecularColor(1,1,1); ballActor->GetProperty()->SetSpecular(0.3); ballActor->GetProperty()->SetSpecularPower(20); ballActor->GetProperty()->SetAmbient(0.2); ballActor->GetProperty()->SetDiffuse(0.8); // Create graphics objects // Create the rendering window, renderer, and interactive renderer vtkRenderer *renderer = vtkRenderer::New(); vtkRenderWindow *renderWindow = vtkRenderWindow::New(); renderWindow->AddRenderer(renderer); // vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New(); // interactor->SetRenderWindow(renderWindow); // Add the actors to the renderer, set the background and size renderer->AddActor(ballActor); renderer->AddActor(edgeActor); renderer->SetBackground(1,1,1); renderWindow->SetSize(450,450); // render the image // interactor->AddObserver(UserEvent {wm deiconify .vtkInteract} renderer->ResetCamera(); renderer->GetActiveCamera()->Zoom(1.5); // interactor->Initialize(); // interactor->Start(); for (int i = 0; i < 1000; i++) { // for each frame points->Delete(); points = vtkPoints::New(); // points->Reset(); for (int j = 0; j < N_VERTICES; j++) { // for each point // double aPoint[3]; // points->GetPoint(j, aPoint); // printf("%2d: (%8.5f,%8.5f,%8.5f)\n", j, aPoint[0], aPoint[1], aPoint[2]); // aPoint[0] += 0.1; // points->SetPoint(j, aPoint); vertices[j][0] += 0.001; points->InsertPoint(j, vertices[j][0], vertices[j][1], vertices[j][2]); // printf("%2d: (%8.5f,%8.5f,%8.5f)\n", j, vertices[j][0], vertices[j][1], vertices[j][2]); } // for each point profile->SetPoints(points); // printf("i: %d\n", i); // points->Print(cout); // points->GetData()->Print(cout); // profile->Print(cout); // del->Print(cout); // mapMesh->Print(cout); // meshActor->Print(cout); // extract->Print(cout); // tubes->Print(cout); // mapEdges->Print(cout); // edgeActor->Print(cout); // ball->Print(cout); // balls->Print(cout); // mapBalls->Print(cout); // ballActor->Print(cout); // renderer->Print(cout); // renderWindow->Print(cout); renderWindow->Render(); } // for each frame } // main()