### Python Virtual Environment Setup Source: https://github.com/kitware/vtk-book/blob/master/README.md Set up a Python virtual environment and install dependencies for building the VTK Book documentation locally. This is typically done on Linux/macOS. ```bash python -m venv env source env/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Render a Cone using VTK Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/03Chapter3.md This C++ example demonstrates how to create and render a cone using VTK objects. It covers setting up the cone source, mapper, actor, renderer, render window, and interactor. Ensure VTK is installed and configured for compilation. ```C++ #include #include #include #include #include #include #include #include #include #include int main(int, char *[]) { //Create a cone auto coneSource = vtkSmartPointer::New(); coneSource->SetHeight( 3.0); coneSource->SetRadius( 1.0); coneSource->SetResolution( 10); coneSource->Update(); //Create a mapper and actor auto mapper = vtkSmartPointer::New(); mapper->SetInputConnection(coneSource->GetOutputPort()); auto colors = vtkSmartPointer::New(); auto actor = vtkSmartPointer::New(); actor->SetMapper(mapper); actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("bisque").GetData()); //Create a renderer, render window, and interactor auto renderer = vtkSmartPointer::New(); auto renderWindow = vtkSmartPointer::New(); renderWindow->AddRenderer(renderer); renderWindow->SetSize(640, 480); auto renderWindowInteractor = vtkSmartPointer::New(); renderWindowInteractor->SetRenderWindow(renderWindow); //Add the actors to the scene renderer->AddActor(actor); renderer->SetBackground(colors->GetColor3d("Salmon").GetData()); //Render and interact renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; } ``` -------------------------------- ### Initialize and Start Interactor in C++ Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/03Chapter3.md Instantiate and configure vtkRenderWindowInteractor and vtkInteractorStyleTrackballCamera to enable user interaction with the rendering window. Call Initialize() and Start() to begin the event loop. ```cpp vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWin); vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New(); iren->SetInteractorStyle(style); iren->Initialize(); iren->Start(); ``` -------------------------------- ### Render Cone with Observer in VTK Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/03Chapter3.md Creates a cone, sets up a renderer, render window, and an observer to print camera position on render start. Handles object cleanup. ```c++ #include "vtkCommand.h" // Callback for the interaction class vtkMyCallback : public vtkCommand { public: static vtkMyCallback *New() { return new vtkMyCallback; } virtual void Execute(vtkObject *caller, unsigned long, void*) { vtkRenderer *ren = reinterpret_cast(caller); cout << ren->GetActiveCamera()->GetPosition()[0] << " " ren->GetActiveCamera()->GetPosition()[1] << " " ren->GetActiveCamera()->GetPosition()[2] << "n"; } }; int main( int argc, char *argv[] ) { vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 ); vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper->SetInputConnection( cone->GetOutputPort() ); vtkActor *coneActor = vtkActor::New(); coneActor->SetMapper( coneMapper ); vtkRenderer *ren1= vtkRenderer::New(); ren1->AddActor( coneActor ); ren1->SetBackground( 0.1, 0.2, 0.4 ); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer( ren1 ); renWin->SetSize( 300, 300 ); vtkMyCallback *mo1 = vtkMyCallback::New(); ren1->AddObserver(vtkCommand::StartEvent,mo1); mo1->Delete(); int i; for (i = 0; i < 360; ++i) { // render the image renWin->Render(); // rotate the active camera by one degree ren1->GetActiveCamera()->Azimuth( 1 ); } cone->Delete(); coneMapper->Delete(); coneActor->Delete(); ren1->Delete(); renWin->Delete(); return 0; } ``` -------------------------------- ### Initialize VRML Scene and Get Node Handles (Java) Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/11Chapter11.md Sets up the VRML scene by obtaining handles to specific nodes like 'SPHERE' and 'ROOT'. It also retrieves event-in handles for manipulating node properties, such as 'set_scale' for the sphere. ```java /** Set up some stuff in the VRML scene */ public void initScene() { System.out.println("initScene()..."); // wait a couple seconds try { Thread.currentThread().sleep(5000); } catch(InterruptedException e) {} // Get the "SPHERE" node try { sphere_node = browser.getNode("SPHERE"); } catch(InvalidNodeException e) { System.out.println("InvalidNodeException: " + e); die("initScene: SPHERE node not found!"); return; } System.out.println("- Got the SPHERE node: " + sphere_node); try { setScale = (EventInSFVec3f) sphere_node.getEventIn("set_scale"); } catch (InvalidEventInException e) { die("initScene: InvalidEventInException " + e); return; } ``` ```java // Get the "ROOT" node (a Group which we add to) try { root_node = browser.getNode("ROOT"); } catch(InvalidNodeException e) { System.out.println("InvalidNodeException: " + e); die("initScene: ROOT node not found!"); return; } System.out.println("- Got the ROOT node: " + root_node); // Get the ROOT node's add/removeChildren EventIns EventInMFNode addChildren; EventInMFNode removeChildren; try { addChildren = (EventInMFNode) root_node.getEventIn("addChildren"); removeChildren = (EventInMFNode) root_node.getEventIn("removeChildren"); } catch (InvalidEventInException e) { die("initScene: InvalidEventInException " + e); return; } ``` -------------------------------- ### Volume Rendering Pipeline Setup (Tcl) Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/07Chapter7.md Sets up a volume rendering pipeline using Tcl. It configures a reader, transfer functions for opacity and color, a volume property, and a ray cast mapper. This is useful for visualizing volumetric data. ```tcl # Create the standard renderer, render window and interactor vtkRenderer ren1 vtkRenderWindow renWin renWin AddRenderer ren1 vtkRenderWindowInteractor iren iren SetRenderWindow renWin # Create the reader for the data vtkStructuredPointsReader reader reader SetFileName "$VTK_DATA_ROOT/Data/ironProt.vtk" # Create transfer mapping scalar value to opacity vtkPiecewiseFunction opacityTransferFunction opacityTransferFunction AddPoint 20 0.0 opacityTransferFunction AddPoint 255 0.2 # Create transfer mapping scalar value to color vtkColorTransferFunction colorTransferFunction colorTransferFunction AddRGBPoint 0.0 0.0 0.0 0.0 colorTransferFunction AddRGBPoint 64.0 1.0 0.0 0.0 colorTransferFunction AddRGBPoint 128.0 0.0 0.0 1.0 colorTransferFunction AddRGBPoint 192.0 0.0 1.0 0.0 colorTransferFunction AddRGBPoint 255.0 0.0 0.2 0.0 # The property describes how the data will look vtkVolumeProperty volumeProperty volumeProperty SetColor colorTransferFunction volumeProperty SetScalarOpacity opacityTransferFunction volumeProperty ShadeOn volumeProperty SetInterpolationTypeToLinear # The mapper / ray cast function know how to render the data vtkVolumeRayCastCompositeFunction compositeFunction vtkVolumeRayCastMapper volumeMapper volumeMapper SetVolumeRayCastFunction compositeFunction volumeMapper SetInputConnection [reader GetOutputPort] # Set the mapper and the property and vtkVolume volume volume SetMapper volumeMapper volume SetProperty volumeProperty ren1 AddVolume volume renWin Render ``` -------------------------------- ### Actor Transformation using Instance Variables Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/03Chapter3.md This example demonstrates achieving the same transformation as the previous snippet but using the actor's built-in instance variables for origin, rotation, and position. ```c++ vtkActor *cow=vtkActor::New(); cow->SetOrigin(0,0,-5); cow->RotateY(20); cow->SetPosition(0,0,5); ``` -------------------------------- ### Using vtkGlyph3D with Multiple Inputs Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/04Chapter4.md Example of using vtkGlyph3D in C++ to copy geometry from a source to input points. It demonstrates setting input and source connections. ```c++ glyph = vtkGlyph3D::New(); glyph->SetInputConnection(foo->GetOutputPort()); glyph->SetSourceConnection(bar->GetOutputPort());... ``` -------------------------------- ### Using vtkExtractVectorComponents with Multiple Outputs Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/04Chapter4.md Example of using vtkExtractVectorComponents in C++ to extract vector components into separate scalar outputs. It shows how to access specific output ports. ```c++ vz = vtkExtractVectorComponents::New(); foo = vtkDataSetMapper::New(); foo->SetInputConnection(vz->GetOutputPort(2)); ``` -------------------------------- ### Actor Transformation with vtkTransform and UserMatrix Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/03Chapter3.md This example shows how to use vtkTransform to create a matrix and apply it to an actor using SetUserMatrix(). The actor is translated along the z-axis and then rotated about the origin. ```c++ vtkTransform *walk = vtkTransform::New(); walk->RotateY(0,20,0); walk->Translate(0,0,5); vtkActor *cow=vtkActor::New(); cow->SetUserMatrix(walk->GetMatrix()); ``` -------------------------------- ### Initialize visApplet and Connect to VRML Browser Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/11Chapter11.md Initializes the Java applet, establishes a connection to the VRML browser using the EAI, and sets up basic UI elements like buttons. ```java public void init() { // Connect to the browser browser = Browser.getBrowser(this); if (browser == null) { die("init: NULL browser!"); } System.out.println("Got the browser: "+browser); // Initialize some VRML stuff initScene(); // Build a simple UI grow_button = new Button("Grow Sphere"); add(grow_button); shrink_button = new Button("Shrink Sphere"); add(shrink_button); // Misc other UI setup Color c = Color.white; System.out.println("Setting bg color to: " + c); setBackground(c); } ``` -------------------------------- ### Scalar, Vector, Normal, Tensor, and Texture Coordinate Management Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/08Chapter8.md Methods for setting and getting specific types of attribute data. ```APIDOC ## Set/Get Scalars ### Description Set or return scalar data. The GetScalars() method may return a NULL value, in which case the scalars are not defined. ### Method `SetScalars()` / `GetScalars()` ``` ```APIDOC ## Set/Get Vectors ### Description Set or return vector data. The GetVectors() method may return a NULL value, in which case the vectors are not defined. ### Method `SetVectors()` / `GetVectors()` ``` ```APIDOC ## Set/Get Normals ### Description Set or return normal data. The GetNormals() method may return a NULL value, in which case the normals are not defined. ### Method `SetNormals()` / `GetNormals()` ``` ```APIDOC ## Set/Get Tensors ### Description Set or return tensor data. The GetTensors() method may return a NULL value, in which case the tensors are not defined. ### Method `SetTensors()` / `GetTensors()` ``` ```APIDOC ## Set/Get Texture Coords ### Description Set or return texture coordinate data. The GetTextureCoords() method may return a NULL value, in which case the texture coordinates are not defined. ### Method `SetTextureCoords()` / `GetTextureCoords()` ``` -------------------------------- ### Set and Get Tensor Data Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/08Chapter8.md Methods for setting and retrieving tensor attribute data. GetTensors() may return NULL if tensors are not defined. ```default SetTensors() GetTensors() ``` -------------------------------- ### Java Applet Initialization with VTK Panel Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/11Chapter11.md Initializes the Java applet, sets up the layout, and integrates a VTK rendering panel. This method processes HTML parameters and constructs the visualization pipeline. ```java public void init() { GridBagLayout grid = new GridBagLayout(); this.setLayout(grid); panel = new vtkPanel(); panel.resize(400,400); constrain(this,panel,0,0,8,8); ``` -------------------------------- ### Set and Get Normal Data Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/08Chapter8.md Methods for setting and retrieving normal attribute data. GetNormals() may return NULL if normals are not defined. ```default SetNormals() GetNormals() ``` -------------------------------- ### Set and Get Vector Data Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/08Chapter8.md Methods for setting and retrieving vector attribute data. GetVectors() may return NULL if vectors are not defined. ```default SetVectors() GetVectors() ``` -------------------------------- ### Set and Get Scalar Data Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/08Chapter8.md Methods for setting and retrieving scalar attribute data. GetScalars() may return NULL if scalars are not defined. ```default SetScalars() GetScalars() ``` -------------------------------- ### Contour Quadric Function with VTK Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/06Chapter6.md Defines an implicit quadric function, samples it, and generates isosurfaces using vtkContourFilter. Includes setup for rendering the contour and its outline. ```c++ vtkQuadric *quadric = vtkQuadric::New(); quadric->SetCoefficients(.5,1,.2,0,.1,0,0,.2,0,0); vtkSampleFunction *sample = vtkSampleFunction::New(); sample->SetSampleDimensions(50,50,50); sample->SetImplicitFunction(quadric); vtkContourFilter *contour = vtkContourFilter::New(); contour->SetInputConnection(sample->GetOutputPort()); contour->GenerateValues(5,0,1.2); vtkPolyDataMapper *contourMapper = vtkPolyDataMapper::New(); contourMapper->SetInputConnection( contour->GetOutputPort()); contourMapper->SetScalarRange(0,1.2); vtkActor *contourActor = vtkActor::New(); contourActor->SetMapper(contourMapper); // Create outline vtkOutlineFilter *outline = vtkOutlineFilter::New(); outline->SetInputConnection(sample->GetOutputPort()); vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New(); outlineMapper->SetInputConnection(outline->GetOutputPort()); vtkActor *outlineActor = vtkActor::New(); outlineActor->SetMapper(outlineMapper); outlineActor->GetProperty()->SetColor(0,0,0); ``` -------------------------------- ### Connecting Filters (VTK 5.0 and Beyond) Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/04Chapter4.md This C++ code demonstrates the recommended way to connect filters in VTK 5.0 and later using SetInputConnection() and GetOutputPort(). This approach supports deferred dataset type checking. ```c++ filter2->SetInputConnection(filter1->GetOutputPort()); //VTK 5.0 ``` -------------------------------- ### Create a VTK Cone Visualization in Python Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/03Chapter3.md This Python script sets up a basic VTK visualization pipeline to render a cone. It initializes VTK, creates a cone source, mapper, and actor, then configures renderers and a render window for interactive display. ```python #!/usr/bin/env python import vtk colors = vtk.vtkNamedColors () cone = vtk.vtkConeSource () cone.SetHeight ( 3.0 ) cone.SetRadius ( 1.0 ) cone.SetResolution ( 10 ) coneMapper = vtk.vtkPolyDataMapper () coneMapper.SetInputConnection (cone.GetOutputPort ()) coneActor = vtk.vtkActor () coneActor.SetMapper ( coneMapper ) ren1 = vtk.vtkRenderer () ren1.AddActor( coneActor ) ren1.SetBackground (colors.GetColor3d ("MidnightBlue")) renWin = vtk.vtkRenderWindow () renWin.AddRenderer (ren1) renWin.SetSize (300 , 300) iren = vtk.vtkRenderWindowInteractor () iren.SetRenderWindow (renWin) style = vtk.vtkInteractorStyleTrackballCamera () iren.SetInteractorStyle (style) iren.Initialize () iren.Start () ``` -------------------------------- ### Instance Creation in C++ (Stack) Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/02Chapter2.md Shows how to create an instance of a C++ class, specifically vtkActor, by declaring it directly. This allocates the object on the program stack. ```c++ vtkActor aBall; ``` -------------------------------- ### Set and Get Texture Coordinate Data Source: https://github.com/kitware/vtk-book/blob/master/VTKBook/08Chapter8.md Methods for setting and retrieving texture coordinate attribute data. GetTextureCoords() may return NULL if texture coordinates are not defined. ```default SetTextureCoords() GetTextureCoords() ```