Program: Visualization Toolkit
Module: DiffuseSpheres.cxx
Copyright(c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.=========================================================================*///// This examples demonstrates the effect of diffuse lighting.//#include"vtkSmartPointer.h"#include"vtkSphereSource.h"#include"vtkPolyDataMapper.h"#include"vtkActor.h"#include"vtkRenderer.h"#include"vtkRenderWindow.h"#include"vtkRenderWindowInteractor.h"#include"vtkProperty.h"#include"vtkCamera.h"#include"vtkLight.h"intmain(){// The following lines create a sphere represented by polygons.//
vtkSmartPointer<vtkSphereSource> sphere =vtkSmartPointer<vtkSphereSource>::New();
sphere->SetThetaResolution(10);
sphere->SetPhiResolution(500);// The mapper is responsible for pushing the geometry into the graphics// library. It may also do color mapping, if scalars or other attributes// are defined.//
vtkSmartPointer<vtkPolyDataMapper> sphereMapper =vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphere->GetOutputPort());// The actor is a grouping mechanism: besides the geometry (mapper), it// also has a property, transformation matrix, and/or texture map.// In this example we create eight different spheres (two rows of four// spheres) and set the specular lighting coefficients. A little ambient// is turned on so the sphere is not completely black on the back side.//
vtkSmartPointer<vtkActor> sphere1 =vtkSmartPointer<vtkActor>::New();
sphere1->SetMapper(sphereMapper);
sphere1->GetProperty()->SetColor(1,0,0);
sphere1->GetProperty()->SetAmbient(0.3);
sphere1->GetProperty()->SetDiffuse(0.0);
sphere1->GetProperty()->SetSpecular(0.0);
sphere1->GetProperty()->SetSpecularPower(5.0);
vtkSmartPointer<vtkActor> sphere2 =vtkSmartPointer<vtkActor>::New();
sphere2->SetMapper(sphereMapper);
sphere2->GetProperty()->SetColor(1,0,0);
sphere2->GetProperty()->SetAmbient(0.3);
sphere2->GetProperty()->SetDiffuse(0.125);
sphere2->GetProperty()->SetSpecular(0.0);
sphere2->GetProperty()->SetSpecularPower(10.0);
sphere2->AddPosition(1.25,0,0);
vtkSmartPointer<vtkActor> sphere3 =vtkSmartPointer<vtkActor>::New();
sphere3->SetMapper(sphereMapper);
sphere3->GetProperty()->SetColor(1,0,0);
sphere3->GetProperty()->SetAmbient(0.3);
sphere3->GetProperty()->SetDiffuse(0.25);
sphere3->GetProperty()->SetSpecular(0.0);
sphere3->AddPosition(2.5,0,0);
vtkSmartPointer<vtkActor> sphere4 =vtkSmartPointer<vtkActor>::New();
sphere4->SetMapper(sphereMapper);
sphere4->GetProperty()->SetColor(1,0,0);
sphere4->GetProperty()->SetAmbient(0.3);
sphere4->GetProperty()->SetDiffuse(0.375);
sphere4->GetProperty()->SetSpecular(0.0);
sphere4->AddPosition(3.75,0,0);
vtkSmartPointer<vtkActor> sphere5 =vtkSmartPointer<vtkActor>::New();
sphere5->SetMapper(sphereMapper);
sphere5->GetProperty()->SetColor(1,0,0);
sphere5->GetProperty()->SetAmbient(0.3);
sphere5->GetProperty()->SetDiffuse(0.5);
sphere5->GetProperty()->SetSpecular(0.0);
sphere5->AddPosition(0.0,1.25,0);
vtkSmartPointer<vtkActor> sphere6 =vtkSmartPointer<vtkActor>::New();
sphere6->SetMapper(sphereMapper);
sphere6->GetProperty()->SetColor(1,0,0);
sphere6->GetProperty()->SetAmbient(0.3);
sphere6->GetProperty()->SetDiffuse(0.625);
sphere6->GetProperty()->SetSpecular(0.0);
sphere6->AddPosition(1.25,1.25,0);
vtkSmartPointer<vtkActor> sphere7 =vtkSmartPointer<vtkActor>::New();
sphere7->SetMapper(sphereMapper);
sphere7->GetProperty()->SetColor(1,0,0);
sphere7->GetProperty()->SetAmbient(0.3);
sphere7->GetProperty()->SetDiffuse(0.75);
sphere7->GetProperty()->SetSpecular(0.0);
sphere7->AddPosition(2.5,1.25,0);
vtkSmartPointer<vtkActor> sphere8 =vtkSmartPointer<vtkActor>::New();
sphere8->SetMapper(sphereMapper);
sphere8->GetProperty()->SetColor(1,0,0);
sphere8->GetProperty()->SetAmbient(0.3);
sphere8->GetProperty()->SetDiffuse(0.875);
sphere8->GetProperty()->SetSpecular(0.0);
sphere8->AddPosition(3.75,1.25,0);// Create the graphics structure. The renderer renders into the// render window. The render window interactor captures mouse events// and will perform appropriate camera or actor manipulation// depending on the nature of the events.//
vtkSmartPointer<vtkRenderer> ren1 =vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
vtkSmartPointer<vtkRenderWindowInteractor> iren =vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);// Add the actors to the renderer, set the background and size.//
ren1->AddActor(sphere1);
ren1->AddActor(sphere2);
ren1->AddActor(sphere3);
ren1->AddActor(sphere4);
ren1->AddActor(sphere5);
ren1->AddActor(sphere6);
ren1->AddActor(sphere7);
ren1->AddActor(sphere8);
ren1->SetBackground(0.1,0.2,0.4);
renWin->SetSize(400,200);// Set up the lighting.//
vtkSmartPointer<vtkLight> light =vtkSmartPointer<vtkLight>::New();
light->SetFocalPoint(1.875,0.6125,0);
light->SetPosition(0.875,1.6125,1);
ren1->AddLight(light);// We want to eliminate perspective effects on the apparent lighting.// Parallel camera projection will be used. To zoom in parallel projection// mode, the ParallelScale is set.//
ren1->GetActiveCamera()->SetFocalPoint(0,0,0);
ren1->GetActiveCamera()->SetPosition(0,0,1);
ren1->GetActiveCamera()->SetViewUp(0,1,0);
ren1->GetActiveCamera()->ParallelProjectionOn();
ren1->ResetCamera();
ren1->GetActiveCamera()->SetParallelScale(1.5);// This starts the event loop and invokes an initial render.//
iren->Initialize();
iren->Start();return EXIT_SUCCESS;}
/*=========================================================================
Program: Visualization Toolkit
Module: Cylinder.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*///// This simple example shows how to do basic rendering and pipeline// creation using C++.//#include"vtkCylinderSource.h"#include"vtkPolyDataMapper.h"#include"vtkActor.h"#include"vtkRenderer.h"#include"vtkRenderWindow.h"#include"vtkRenderWindowInteractor.h"#include"vtkProperty.h"#include"vtkCamera.h"intmain(){// This creates a polygonal cylinder model with eight circumferential facets.//
vtkCylinderSource *cylinder = vtkCylinderSource::New();
cylinder->SetResolution(6);// The mapper is responsible for pushing the geometry into the graphics// library. It may also do color mapping, if scalars or other attributes// are defined.//
vtkPolyDataMapper *cylinderMapper = vtkPolyDataMapper::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());// The actor is a grouping mechanism: besides the geometry (mapper), it// also has a property, transformation matrix, and/or texture map.// Here we set its color and rotate it -22.5 degrees.
vtkActor *cylinderActor = vtkActor::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(1.0000,0.3882,0.2784);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);// Create the graphics structure. The renderer renders into the// render window. The render window interactor captures mouse events// and will perform appropriate camera or actor manipulation// depending on the nature of the events.//
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);// Add the actors to the renderer, set the background and size//
ren1->AddActor(cylinderActor);
ren1->SetBackground(0.1,0.2,0.4);
renWin->SetSize(200,200);// We'll zoom in a little by accessing the camera and invoking a "Zoom"// method on it.
ren1->ResetCamera();
ren1->GetActiveCamera()->Zoom(1.5);
renWin->Render();// This starts the event loop and as a side effect causes an initial render.
iren->Start();// Exiting from here, we have to delete all the instances that// have been created.
cylinder->Delete();
cylinderMapper->Delete();
cylinderActor->Delete();
ren1->Delete();
renWin->Delete();
iren->Delete();return0;}
/*=========================================================================
Program: Visualization Toolkit
Module: LabeledMesh.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/// This example was translated into C++ from its TCL counterpart// (VTK/Examples/Annotation/Tcl/labeledMesh.tcl) by Jake Nickel from// the University of Iowa. It demonstrates the use of vtkLabeledDataMapper.// This class is used for displaying numerical data from an underlying data// set. In the case of this example, the underlying data are the point and// cell ids.// First we include the necessary header files.#include"vtkSmartPointer.h"#include"vtkPoints.h"#include"vtkCellArray.h"#include"vtkPolyData.h"#include"vtkPolyDataMapper2D.h"#include"vtkActor2D.h"#include"vtkSphereSource.h"#include"vtkPolyDataMapper.h"#include"vtkActor.h"#include"vtkIdFilter.h"#include"vtkRenderer.h"#include"vtkSelectVisiblePoints.h"#include"vtkLabeledDataMapper.h"#include"vtkCellCenters.h"#include"vtkLabeledDataMapper.h"#include"vtkRenderWindow.h"#include"vtkRenderWindowInteractor.h"#include"vtkTextProperty.h"staticint xLength;staticint yLength;static vtkSmartPointer<vtkSelectVisiblePoints> visPts;static vtkSmartPointer<vtkSelectVisiblePoints> visCells;static vtkSmartPointer<vtkPoints> pts;static vtkSmartPointer<vtkRenderWindow> renWin;// Create a procedure to draw the selection window at each location it// is moved to.voidPlaceWindow(int xmin,int ymin ){int xmax = xmin + xLength;int ymax = ymin + yLength;
visPts->SetSelection( xmin, xmax, ymin, ymax );
visCells->SetSelection( xmin, xmax, ymin, ymax );
pts->InsertPoint(0, xmin, ymin,0);
pts->InsertPoint(1, xmin, ymin,0);
pts->InsertPoint(2, xmin, ymin,0);
pts->InsertPoint(3, xmin, ymin,0);// Call Modified because InsertPoints does not modify vtkPoints// (for performance reasons).
pts->Modified();
renWin->Render();}// Create a procedure to move the selection window across the data set.voidMoveWindow(){for(int y =100; y <300; y +=25){for(int x =100; x <300; x +=25){PlaceWindow( x, y );}}}intmain(int,char*[]){// Create a selection window. We will display the point and cell ids that// lie within this window.int xmin =200;
xLength =100;int xmax = xmin + xLength;int ymin =200;
yLength =100;int ymax = ymin + yLength;
pts =vtkSmartPointer<vtkPoints>::New();
pts->InsertPoint(0, xmin, ymin,0);
pts->InsertPoint(1, xmax, ymin,0);
pts->InsertPoint(2, xmax, ymax,0);
pts->InsertPoint(3, xmin, ymax,0);
vtkSmartPointer<vtkCellArray> rect =vtkSmartPointer<vtkCellArray>::New();
rect->InsertNextCell(5);
rect->InsertCellPoint(0);
rect->InsertCellPoint(1);
rect->InsertCellPoint(2);
rect->InsertCellPoint(3);
rect->InsertCellPoint(0);
vtkSmartPointer<vtkPolyData> selectRect =vtkSmartPointer<vtkPolyData>::New();
selectRect->SetPoints( pts );
selectRect->SetLines( rect );
vtkSmartPointer<vtkPolyDataMapper2D> rectMapper =vtkSmartPointer<vtkPolyDataMapper2D>::New();
rectMapper->SetInputData(selectRect);
vtkSmartPointer<vtkActor2D> rectActor =vtkSmartPointer<vtkActor2D>::New();
rectActor->SetMapper( rectMapper );// Create a sphere and its associated mapper and actor.
vtkSmartPointer<vtkSphereSource> sphere =vtkSmartPointer<vtkSphereSource>::New();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper =vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphere->GetOutputPort());
vtkSmartPointer<vtkActor> sphereActor =vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper( sphereMapper );// Generate data arrays containing point and cell ids
vtkSmartPointer<vtkIdFilter> ids =vtkSmartPointer<vtkIdFilter>::New();
ids->SetInputConnection( sphere->GetOutputPort());
ids->PointIdsOn();
ids->CellIdsOn();
ids->FieldDataOn();// Create the renderer here because vtkSelectVisiblePoints needs it.
vtkSmartPointer<vtkRenderer> ren1 =vtkSmartPointer<vtkRenderer>::New();// Create labels for points
visPts =vtkSmartPointer<vtkSelectVisiblePoints>::New();
visPts->SetInputConnection( ids->GetOutputPort());
visPts->SetRenderer( ren1 );
visPts->SelectionWindowOn();
visPts->SetSelection( xmin, xmin + xLength, ymin, ymin + yLength );// Create the mapper to display the point ids. Specify the// format to use for the labels. Also create the associated actor.
vtkSmartPointer<vtkLabeledDataMapper> ldm =vtkSmartPointer<vtkLabeledDataMapper>::New();
ldm->SetInputConnection( visPts->GetOutputPort());
ldm->SetLabelModeToLabelFieldData();
vtkSmartPointer<vtkActor2D> pointLabels =vtkSmartPointer<vtkActor2D>::New();
pointLabels->SetMapper( ldm );// Create labels for cells
vtkSmartPointer<vtkCellCenters> cc =vtkSmartPointer<vtkCellCenters>::New();
cc->SetInputConnection( ids->GetOutputPort());
visCells =vtkSmartPointer<vtkSelectVisiblePoints>::New();
visCells->SetInputConnection( cc->GetOutputPort());
visCells->SetRenderer( ren1 );
visCells->SelectionWindowOn();
visCells->SetSelection( xmin, xmin + xLength, ymin, ymin + yLength );// Create the mapper to display the cell ids. Specify the// format to use for the labels. Also create the associated actor.
vtkSmartPointer<vtkLabeledDataMapper> cellMapper =vtkSmartPointer<vtkLabeledDataMapper>::New();
cellMapper->SetInputConnection( visCells->GetOutputPort());
cellMapper->SetLabelModeToLabelFieldData();
cellMapper->GetLabelTextProperty()->SetColor(0,1,0);
vtkSmartPointer<vtkActor2D> cellLabels =vtkSmartPointer<vtkActor2D>::New();
cellLabels->SetMapper( cellMapper );// Create the RenderWindow and RenderWindowInteractor
renWin =vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer( ren1 );
vtkSmartPointer<vtkRenderWindowInteractor> iren =vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow( renWin );// Add the actors to the renderer; set the background and size; render
ren1->AddActor( sphereActor );
ren1->AddActor2D( rectActor );
ren1->AddActor2D( pointLabels );
ren1->AddActor2D( cellLabels );
ren1->SetBackground(1,1,1);
renWin->SetSize(500,500);
renWin->Render();// Move the selection window across the data set.MoveWindow();// Put the selection window in the center of the render window.// This works because the xmin = ymin = 200, xLength = yLength = 100, and// the render window size is 500 x 500.PlaceWindow( xmin, ymin );
iren->Initialize();
iren->Start();return0;}