Skip to content

Instantly share code, notes, and snippets.

@somada141
Last active August 29, 2015 14:06
Show Gist options
  • Save somada141/42d85846a9a2e4bd7eec to your computer and use it in GitHub Desktop.
Save somada141/42d85846a9a2e4bd7eec to your computer and use it in GitHub Desktop.
How to render glyphs on triangle normals with VTK #tcl #vtk #visualization
# This example shows how to manually construct triangle cell using unstructured grids
# and display its surface normal.
#
package require vtk
package require vtkinteraction
# Create an unstructured grids containing a triangle cell.
vtkPoints trianglePoints
trianglePoints SetNumberOfPoints 3
trianglePoints InsertPoint 0 0 0 0
trianglePoints InsertPoint 1 1 0 0
trianglePoints InsertPoint 2 .5 .5 0
vtkTriangle triangle
[triangle GetPointIds] SetId 0 0
[triangle GetPointIds] SetId 1 1
[triangle GetPointIds] SetId 2 2
vtkUnstructuredGrid triangleGrid
triangleGrid Allocate 1 1
triangleGrid InsertNextCell [triangle GetCellType] [triangle GetPointIds]
triangleGrid SetPoints trianglePoints
vtkDataSetMapper triangleMapper
triangleMapper SetInput triangleGrid
vtkActor triangleActor
triangleActor SetMapper triangleMapper
[triangleActor GetProperty] SetDiffuseColor .3 1 .5
# Create an arrow glyph to represent the surface normal for the triangle cell.
vtkDataSetSurfaceFilter triangleCellDataSetSurfaceFilter
triangleCellDataSetSurfaceFilter SetInput triangleGrid
vtkPolyDataNormals triangleCellNormals
triangleCellNormals SetInputConnection [triangleCellDataSetSurfaceFilter GetOutputPort]
triangleCellNormals ComputePointNormalsOff
triangleCellNormals ComputeCellNormalsOn
triangleCellNormals SplittingOff
triangleCellNormals FlipNormalsOff
triangleCellNormals ConsistencyOn
triangleCellNormals AutoOrientNormalsOn
# Generate the point normal at the center of the triangle cell
vtkCellCenters triangleCellNormalsAtCellCenters
triangleCellNormalsAtCellCenters SetInputConnection [triangleCellNormals GetOutputPort]
# Specify the shape and location of the surface normal glyph
vtkArrowSource arrowSource
vtkGlyph3D arrowGlyph
arrowGlyph ScalingOn
arrowGlyph SetScaleFactor 0.7
arrowGlyph SetVectorModeToUseNormal
arrowGlyph SetScaleModeToScaleByVector
arrowGlyph OrientOn
arrowGlyph SetSourceConnection [arrowSource GetOutputPort]
arrowGlyph SetInputConnection [triangleCellNormalsAtCellCenters GetOutputPort]
vtkDataSetMapper arrowGlyphMapper
arrowGlyphMapper SetInputConnection [arrowGlyph GetOutputPort]
vtkActor glyphActor
glyphActor SetMapper arrowGlyphMapper
# Create the usual rendering stuff.
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
renWin SetSize 300 150
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
ren1 SetBackground .1 .2 .4
ren1 AddActor triangleActor
ren1 AddActor glyphActor
ren1 ResetCamera
ren1 ResetCameraClippingRange
renWin Render
# render the image
#
iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize
wm withdraw .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment