Commit f85a9f20 authored by Will Schroeder's avatar Will Schroeder

ENH:UG Example

parent 08346b9c
// This example shows how to manually create a structured grid.
// The basic idea is to instantiate vtkStructuredGrid, set its dimensions,
// and then assign points defining the grid coordinate. The number of
// points must equal the number of points implicit in the dimensions
// (i.e., dimX*dimY*dimZ). Also, data attributes (either point or cell)
// can be added to the dataset.
#include "vtkMath.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkStructuredGrid.h"
#include "vtkFloatArray.h"
#include "vtkPoints.h"
#include "vtkHedgeHog.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
int main( int argc, char *argv[] )
int i, j, k, kOffset, jOffset, offset;
float x[3], v[3], rMin=0.5, rMax=1.0, deltaRad, deltaZ;
float radius, theta;
static int dims[3]={13,11,11};
// Create the structured grid.
vtkStructuredGrid *sgrid = vtkStructuredGrid::New();
// We also create the points and vectors. The points
// form a hemi-cylinder of data.
vtkFloatArray *vectors = vtkFloatArray::New();
vtkPoints *points = vtkPoints::New();
deltaZ = 2.0 / (dims[2]-1);
deltaRad = (rMax-rMin) / (dims[1]-1);
for ( k=0; k<dims[2]; k++)
x[2] = -1.0 + k*deltaZ;
kOffset = k * dims[0] * dims[1];
for (j=0; j<dims[1]; j++)
radius = rMin + j*deltaRad;
jOffset = j * dims[0];
for (i=0; i<dims[0]; i++)
theta = i * 15.0 * vtkMath::DegreesToRadians();
x[0] = radius * cos(theta);
x[1] = radius * sin(theta);
v[0] = -x[1];
v[1] = x[0];
offset = i + jOffset + kOffset;
// We create a simple pipeline to display the data.
vtkHedgeHog *hedgehog = vtkHedgeHog::New();
vtkPolyDataMapper *sgridMapper = vtkPolyDataMapper::New();
vtkActor *sgridActor = vtkActor::New();
// Create the usual rendering stuff
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
// interact with data
return 0;
# In this example vtkClipPolyData is used to cut a polygonal model
# of a cow in half. In addition, the open clip is closed by triangulating
# the resulting complex polygons.
package require vtk
package require vtkinteraction
package require vtktesting
# First start by reading a cow model. We also generate surface normals for
# prettier rendering.
vtkBYUReader cow
cow SetGeometryFileName "$VTK_DATA_ROOT/Data/Viewpoint/cow.g"
vtkPolyDataNormals cowNormals
cowNormals SetInput [cow GetOutput]
# We clip with an implicit function. Here we use a plane positioned near
# the center of the cow model and oriented at an arbitrary angle.
vtkPlane plane
plane SetOrigin 0.25 0 0
plane SetNormal -1 -1 0
# vtkClipPolyData requires an implicit function to define what it is to
# clip with. Any implicit function, including complex boolean combinations
# can be used. Notice that we can specify the value of the implicit function
# with the SetValue method.
vtkClipPolyData clipper
clipper SetInput [cowNormals GetOutput]
clipper SetClipFunction plane
clipper GenerateClipScalarsOn
clipper GenerateClippedOutputOn
clipper SetValue 0.5
vtkPolyDataMapper clipMapper
clipMapper SetInput [clipper GetOutput]
clipMapper ScalarVisibilityOff
vtkProperty backProp
eval backProp SetDiffuseColor $tomato
vtkActor clipActor
clipActor SetMapper clipMapper
eval [clipActor GetProperty] SetColor $peacock
clipActor SetBackfaceProperty backProp
# Here we are cutting the cow. Cutting creates lines where the cut function
# intersects the model. (Clipping removes a portion of the model but the
# dimension of the data does not change.)
# The reason we are cutting is to generate a closed polygon at the boundary
# of the clipping process. The cutter generates line segments, the stripper
# then puts them together into polylines. We then pull a trick and define
# polygons using the closed line segements that the stripper created.
vtkCutter cutEdges; #Generate cut lines
cutEdges SetInput [cowNormals GetOutput]
cutEdges SetCutFunction plane
cutEdges GenerateCutScalarsOn
cutEdges SetValue 0 0.5
vtkStripper cutStrips; #Forms loops (closed polylines) from cutter
cutStrips SetInput [cutEdges GetOutput]
cutStrips Update
vtkPolyData cutPoly; #This trick defines polygons as polyline loop
cutPoly SetPoints [[cutStrips GetOutput] GetPoints]
cutPoly SetPolys [[cutStrips GetOutput] GetLines]
# Triangle filter is robust enough to ignore the duplicate point at the
# beginning and end of the polygons and triangulate them.
vtkTriangleFilter cutTriangles
cutTriangles SetInput cutPoly
vtkPolyDataMapper cutMapper
cutMapper SetInput cutPoly
cutMapper SetInput [cutTriangles GetOutput]
vtkActor cutActor
cutActor SetMapper cutMapper
eval [cutActor GetProperty] SetColor $peacock
# The clipped part of the cow is rendered wireframe.
vtkPolyDataMapper restMapper
restMapper SetInput [clipper GetClippedOutput]
restMapper ScalarVisibilityOff
vtkActor restActor
restActor SetMapper restMapper
[restActor GetProperty] SetRepresentationToWireframe
# Create graphics stuff
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
# Add the actors to the renderer, set the background and size
ren1 AddActor clipActor
ren1 AddActor cutActor
ren1 AddActor restActor
ren1 SetBackground 1 1 1
[ren1 GetActiveCamera] Azimuth 30
[ren1 GetActiveCamera] Elevation 30
[ren1 GetActiveCamera] Dolly 1.5
ren1 ResetCameraClippingRange
renWin SetSize 300 300
iren Initialize
# render the image
iren SetUserMethod {wm deiconify .vtkInteract}
# prevent the tk window from showing up then start the event loop
wm withdraw .
# Lets you move the cut plane back and forth by invoking the proc Cut with
# the appropriate plane value (essentially a distance from the original
# plane.
proc Cut {v} {
clipper SetValue $v
cutEdges SetValue 0 $v
cutStrips Update
cutPoly SetPoints [[cutStrips GetOutput] GetPoints]
cutPoly SetPolys [[cutStrips GetOutput] GetLines]
cutMapper Update
renWin Render
# This example shows how to generate and manipulate texture coordinates.
# A random cloud of points is generated and then triangulated with
# vtkDelaunay3D. Since these points do not have texture coordinates,
# we generate them with vtkTextureMapToCylinder.
package require vtk
# Begin by generating 25 random points in the unit sphere.
vtkPointSource sphere
sphere SetNumberOfPoints 25
# Triangulate the points with vtkDelaunay3D. This generates a convex hull
# of tetrahedron.
vtkDelaunay3D del
del SetInput [sphere GetOutput]
del SetTolerance 0.01
# The triangulation has texture coordinates generated so we can map
# a texture onto it.
vtkTextureMapToCylinder tmapper
tmapper SetInput [del GetOutput]
tmapper PreventSeamOn
# We scale the texture coordinate to get some repeat patterns.
vtkTransformTextureCoords xform
xform SetInput [tmapper GetOutput]
xform SetScale 4 4 1
# vtkDataSetMapper internally uses a vtkGeometryFilter to extract the
# surface from the trinagulation. The output (which is vtkPolyData) is
# then passed to an internal vtkPolyDataMapper which does the
# rendering.
vtkDataSetMapper mapper
mapper SetInput [xform GetOutput]
# A texture is loaded using an image reader. Textures are simply images.
# The texture is eventually associated with an actor.
vtkBMPReader bmpReader
bmpReader SetFileName "$VTK_DATA_ROOT/Data/masonry.bmp"
vtkTexture atext
atext SetInput [bmpReader GetOutput]
atext InterpolateOn
vtkActor triangulation
triangulation SetMapper mapper
triangulation SetTexture atext
# Create the standard rendering stuff.
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
# Add the actors to the renderer, set the background and size
ren1 AddActor triangulation
ren1 SetBackground 1 1 1
renWin SetSize 300 300
renWin Render
# render the image
renWin Render
# prevent the tk window from showing up then start the event loop
wm withdraw .
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment