# VTK/Examples

## Contents

- 1 Example Usage (C++)
- 1.1 System Configuration/General Information
- 1.2 Preliminaries
- 1.3 Wrapping
- 1.4 Simple Operations
- 1.5 Input and Output
- 1.6 Geometric Objects
- 1.7 Working with 3D Data (vtkPolyData)
- 1.8 Working with Stuctured (grid aligned) 3D Data (vtkImageData)
- 1.9 Utilities
- 1.10 Math Operations
- 1.11 Graphs
- 1.12 Data Structures
- 1.13 Filters
- 1.14 Visualization
- 1.15 User Interaction
- 1.16 Selecting in 3D (Picking)
- 1.17 Working with Images
- 1.18 Working with Meshes
- 1.19 Using VTK Classes as Member Variables
- 1.20 Widgets
- 1.21 Broken Examples (Please fix me!)
- 1.22 Needed/Missing Examples!

- 2 Example Usage (Python)

## Example Usage (C++)

These are fully independent, compilable examples. There is significant overlap in the examples, but they are each intended to illustrate a different concept and be fully stand alone compilable.

Please add examples in your areas of expertise!

### System Configuration/General Information

- Which Libraries Do I Link To?
- Which Header Files Do I Include?
- Typical CMakeLists.txt file
- Environment Setup
- [A nice tutorial]

### Preliminaries

### Wrapping

### Simple Operations

- Distance between two points
- Random number (uniform distribution)
- Random number (Gaussian distribution)

### Input and Output

#### Input

- Read a simple "xyz" file of points
- Read a plain text file into a polydata
- Read a delimited file into a polydata
- Read a VTU file
- Read an OBJ File
- Convert a series of DICOM files into a VTI File
- VRML (WRL)
- Read DICOM file

#### Output

### Geometric Objects

In this section, the object is created, added to a polydata object, then written to a .vtp file. The file can be opened in Paraview to see the result.

- Point
- Line
- Colored Lines
- Add colored lines to a Polydata
- Plane
- Triangle (on large point array)
- Triangle (self contained)
- Sphere
- Cube
- Polygon
- Frustum

### Working with 3D Data (vtkPolyData)

- Convex hull
- Check if a point is inside an object
- Check if a point is inside an object (alternate method) - this uses a Delaunay triangulation to compute a volume. This gives more of an "is inside convex hull" effect than an "is inside object"

#### Input/Output (Reading/Writing)

#### Geometry and Topology - Adding Points and Shapes to Polydata

A big confusion among VTK beginners is "I added points to my file - why are there no points when I visualize it??". VTK strongly divides GEOMETRY from TOPOLOGY. What most users would think of as "points" are actually "points + vertices" in VTK. The geometry is ALWAYS simply a list of coordinates - the topology represents the connectedness of these coordinates. If no topology at all is specified, then, as far as VTK is aware, there is NOTHING to show. If you want to see points, you must tell VTK that each point is independent of the rest by creating a vertex (a 0-D topology) at each point. The following examples use a triangle to demonstrate a step at a time how to add geometry and topology to polydata.

- Triangle - Geometry only - Create a polydata consisting of the three corners of a triangle. There is nothing to visualize as there is no topology.
- Triangle - Geometry + Vertices - Create a polydata consisting of the three corners of a triangle. A vertex is added at each point so there is now 3 "points" for the user to see. This is 0-D topology.
- Triangle - Geometry + Lines - Create a polydata consisting of three corners of a triangle. A line is added between each point. This is 1-D topology.
- Triangle - Geometry + Polygon - Create a polydata consisting of three corners of a triangle. A polygon (in this case, a triangle) is added on the three points. This is 2-D topology.

#### Adding Colors to Polydata

- Colored Points - Add three points to a polydata and associate a color with each of them.
- Triangle - Colored Points - Set the color of each point of a triangle. You will be able to interpolate the colors across the triangle.
- Triangle - Solid Color - Create a solid colored triangle.

#### Adding Normals to Polydata

#### Non-standard Data

These examples show how to attach your own, nonstandard fields to every point or cell in a polydata.

- Add Miscellaneous Data to Points in a Polydata
- Get Miscellaneous Data from Points in a Polydata
- Add Miscellaneous Data to Cells in a Polydata
- Get Miscellaneous Data from Cells in a Polydata

#### Point cloud operations

- Downsample a point cloud - remove points so that there are no points within a tolerance of any point.

#### Miscellaneous (Field) Data

Three types of data can be stored in a polydata object, PointData, CellData, and FieldData. For PointData, there must be a piece of data associated with each point (e.g. a temperature data array with the temperature at each point). For CellData, there must be a piece of data associated with each cell (e.g. the area of each triangle). For data that does not align with either points or cells, FieldData should be used. This is typically data that describes the dataset as a whole. An example could be the name of the dataset, or the center of mass of the points, etc.

- Using FieldData Add Global Miscellaneous Data to a Polydata

#### Global Operations on PolyData

- Extract Normals from a Polydata
- Get the names of all of the data arrays
- Determine data types of arrays
- Embed points into a volume

### Working with Stuctured (grid aligned) 3D Data (vtkImageData)

### Utilities

- Color Lookup Table
- Construct a Table - A table is a 2D array of any type of elements. They do not all have to be the same type. This is achieved using vtkVariant.
- Delaunay Triangulation (2D) - Perform a 2D Delaunay triangulation on a point set.
- Constrained Delaunay Triangulation (2D) - Perform a 2D Delaunay triangulation on a point set respecting a specified boundary.
- Known Length Array
- Unknown Length Array
- Array of Vectors (Known Length)
- Array of Vectors (Unknown Length)
- 2D Array
- Custom type 2D Array
- Determine the type of a VTK variable
- Screenshot

### Math Operations

### Graphs

- Construct a graph
- Minimum spanning tree of a graph
- Depth First Search iterator
- Visualize a graph
- Label vertices and edges
- Find Connected Components
- Convert a graph to a PolyData

### Data Structures

- KDTree
- Create a KDTree
- KDTree - Closest Point
- KDTree - Closest Point (vtkKDTreePointLocator)
- KDTree - Closest N Points
- OBBTree - Oriented Bounding Box Tree
- Generate a mesh of each level of a KDTree
- Generate a mesh of each level of an OBBTree

### Filters

- Apply a Transformation to Points
- Landmark Transform
- Iterative Closest Points (ICP) Transform
- Triangulate a Terrain Map
- Create a surface from Unorganized Points
- Create a surface from Unorganized Points (Gaussian Splat)
- Create models from labeled volume data (Discrete MarchingCubes)
- Create cubes from labeled volume data

### Visualization

See [this] for a brief explanation of the VTK terminology of mappers, actors, etc.

- Visualize a Sphere
- Visualize a VTP File
- Visualize a 2D Set of Points
- Display coordinate axes
- "Trackball" mode
- Display text
- Texture map a plane
- Texture map a quad
- Moving an Actor
- Moving the Camera
- Multiple Actors
- A little bit easier rendering
- Hide an actor
- Hide all actors

### User Interaction

### Selecting in 3D (Picking)

### Working with Images

- Read/Write JPG
- Read/Write PNG
- Display a static image - this will display the image, but not allow you to interact with it.
- Visualize and interact with an image
- Select a region of the window

### Working with Meshes

- Decimation - Reduce the number of triangles in a mesh.
- Subdivision - Increase the number of triangles in a mesh.
- Finding Boundary Edges - Find the edges that are used by only one face.
- Fill Holes - Close holes in a mesh.

### Using VTK Classes as Member Variables

- Non-SmartPointer Member Variable
- Non-SmartPointer Template Member Variable
- SmartPointer Member Variable

### Widgets

### Broken Examples (Please fix me!)

- vtkDistanceToCamera - why aren't the points visible? How to trigger the DistanceToCamera computation?
- Compass - Which library do you need to link to? What is the compass supposed to do? The only documentation says "set a value by manipulating something". I guess the representation controls the "something", but which representations are possible?
- Quadric Surface Fitting
- LU Factorization - how do you get the resulting L and U?

### Needed/Missing Examples!

- vtkApplyColors - How is this different from just adding a Colors array to a vtkDataSet? Example of how to use this.
- vtkDepthSortPolyData - When would this be used? Example?
- vtkFrustumCoverageCuller - Doesn't seem to have a property to specify a frustum? What is it for? How to use it?
- vtkKdTreeSelector - What is this used for? How to use it?
- vtkSelectionSource - What is this used for? Need example.
- vtkKMeansStatistics - Is this KMeans clustering? Can you specify a point set (n-D?) to cluster? How to get the cluster IDs after clustering? Example in 3D so we can color the clusters to visualize the result.
- vtkPCAStatistics - We need an example that takes a set of N-D data and project it into the best M-D space. We should do an example from 2D to 1D so we can visualize the result.
- vtkPCAAnalysisFilter - What is the difference between this and vtkPCAStatistics? Example?
- vtkTransposeMatrix - What is this supposed to operate on? There is no vtkMatrix class (yet!) - Example?
- What is the difference between vtkKdTreePointLocator and vtkKdTree?
- What does vtkHyperOctree do? Example?

#### QT widgets

There seems to be no tests to look at for hints on how to use any of these things?

- vtkQtBarChart
- vtkQtChartView
- vtkQtChartWidget
- vtkQtTreeView
- vtkQtTableView
- vtkQtListView
- vtkQtChartArea

## Example Usage (Python)

It would be appreciated if there are any Python VTK experts who could convert any of the above c++ examples to Python!

### Getting Started

### Working with PolyData

- Write the corners of a triangle to a file (python)
- Write the corners of a triangle to a file (+vertices) (python)
- Write a triangle with colored points (python)
- Write a colored triangle (python)
- Write a solid colored triangle (python)
- Write a triangle to a file (python)
- Iterative Closest Points (ICP) (python)