1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
package require vtk
package require vtkinteraction
# volume render a medical data set
# renderer and interactor
vtkRenderer ren
vtkRenderWindow renWin
renWin AddRenderer ren
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
# read the volume
vtkVolume16Reader v16
v16 SetDataDimensions 64 64
v16 SetImageRange 1 93
v16 SetDataByteOrderToLittleEndian
v16 SetFilePrefix "$VTK_DATA_ROOT/Data/headsq/quarter"
v16 SetDataSpacing 3.2 3.2 1.5
#---------------------------------------------------------
# set up the volume rendering
vtkVolumeRayCastCompositeFunction rayCastFunction
vtkVolumeRayCastMapper volumeMapper
volumeMapper SetInputConnection [v16 GetOutputPort]
volumeMapper SetVolumeRayCastFunction rayCastFunction
vtkColorTransferFunction volumeColor
volumeColor AddRGBPoint 0 0.0 0.0 0.0
volumeColor AddRGBPoint 180 0.3 0.1 0.2
volumeColor AddRGBPoint 1000 1.0 0.7 0.6
volumeColor AddRGBPoint 2000 1.0 1.0 0.9
vtkPiecewiseFunction volumeScalarOpacity
volumeScalarOpacity AddPoint 0 0.0
volumeScalarOpacity AddPoint 180 0.0
volumeScalarOpacity AddPoint 1000 0.2
volumeScalarOpacity AddPoint 2000 0.8
vtkPiecewiseFunction volumeGradientOpacity
volumeGradientOpacity AddPoint 0 0.0
volumeGradientOpacity AddPoint 90 0.5
volumeGradientOpacity AddPoint 100 1.0
vtkVolumeProperty volumeProperty
volumeProperty SetColor volumeColor
volumeProperty SetScalarOpacity volumeScalarOpacity
volumeProperty SetGradientOpacity volumeGradientOpacity
volumeProperty SetInterpolationTypeToLinear
volumeProperty ShadeOn
volumeProperty SetAmbient 0.6
volumeProperty SetDiffuse 0.6
volumeProperty SetSpecular 0.1
vtkVolume volume
volume SetMapper volumeMapper
volume SetProperty volumeProperty
#---------------------------------------------------------
# Do the surface rendering
vtkMarchingCubes boneExtractor
boneExtractor SetInputConnection [v16 GetOutputPort]
boneExtractor SetValue 0 1150
vtkPolyDataNormals boneNormals
boneNormals SetInputConnection [boneExtractor GetOutputPort]
boneNormals SetFeatureAngle 60.0
vtkStripper boneStripper
boneStripper SetInputConnection [boneNormals GetOutputPort]
vtkPolyDataMapper boneMapper
boneMapper SetInputConnection [boneStripper GetOutputPort]
boneMapper ScalarVisibilityOff
vtkProperty boneProperty
boneProperty SetColor 1.0 1.0 0.9
vtkActor bone
bone SetMapper boneMapper
bone SetProperty boneProperty
#---------------------------------------------------------
# Create an image actor
vtkLookupTable table
table SetRange 0 2000
table SetRampToLinear
table SetValueRange 0 1
table SetHueRange 0 0
table SetSaturationRange 0 0
vtkImageMapToColors mapToColors
mapToColors SetInputConnection [v16 GetOutputPort]
mapToColors SetLookupTable table
vtkImageActor imageActor
[imageActor GetMapper] SetInputConnection [mapToColors GetOutputPort]
imageActor SetDisplayExtent 32 32 0 63 0 92
#---------------------------------------------------------
# make a transform and some clipping planes
vtkTransform transform
transform RotateWXYZ -20 0.0 -0.7 0.7
volume SetUserTransform transform
bone SetUserTransform transform
imageActor SetUserTransform transform
set c [volume GetCenter]
vtkPlane volumeClip
volumeClip SetNormal 0 1 0
volumeClip SetOrigin [lindex $c 0] [lindex $c 1] [lindex $c 2]
vtkPlane boneClip
boneClip SetNormal 0 0 1
boneClip SetOrigin [lindex $c 0] [lindex $c 1] [lindex $c 2]
volumeMapper AddClippingPlane volumeClip
boneMapper AddClippingPlane boneClip
#---------------------------------------------------------
ren AddViewProp volume
ren AddViewProp bone
ren AddViewProp imageActor
set camera [ren GetActiveCamera]
$camera SetFocalPoint [lindex $c 0] [lindex $c 1] [lindex $c 2]
$camera SetPosition [expr [lindex $c 0] + 500] [expr [lindex $c 1] - 100] [expr [lindex $c 2] - 100]
$camera SetViewUp 0 0 -1
ren ResetCameraClippingRange
renWin Render
#---------------------------------------------------------
# the cone should point along the Z axis
vtkConeSource coneSource
coneSource CappingOn
coneSource SetHeight 12
coneSource SetRadius 5
coneSource SetResolution 31
coneSource SetCenter 6 0 0
coneSource SetDirection -1 0 0
#---------------------------------------------------------
vtkVolumePicker picker
picker SetTolerance 1e-6
picker SetVolumeOpacityIsovalue 0.01
# This should usually be left alone, but is used here to increase coverage
picker UseVolumeGradientOpacityOn
# A function to point an actor along a vector
proc PointCone {actor nx ny nz} {
if [expr $nx < 0.0] {
$actor RotateWXYZ 180 0 1 0
$actor RotateWXYZ 180 [expr ($nx - 1.0)*0.5] [expr $ny*0.5] [expr $nz*0.5]
} else {
$actor RotateWXYZ 180 [expr ($nx + 1.0)*0.5] [expr $ny*0.5] [expr $nz*0.5]
}
}
# Pick the actor
picker Pick 192 103 0 ren
#puts [picker Print]
set p [picker GetPickPosition]
set n [picker GetPickNormal]
vtkActor coneActor1
coneActor1 PickableOff
vtkDataSetMapper coneMapper1
coneMapper1 SetInputConnection [coneSource GetOutputPort]
coneActor1 SetMapper coneMapper1
[coneActor1 GetProperty] SetColor 1 0 0
coneActor1 SetPosition [lindex $p 0] [lindex $p 1] [lindex $p 2]
PointCone coneActor1 [lindex $n 0] [lindex $n 1] [lindex $n 2]
ren AddViewProp coneActor1
# Pick the volume
picker Pick 90 180 0 ren
set p [picker GetPickPosition]
set n [picker GetPickNormal]
vtkActor coneActor2
coneActor2 PickableOff
vtkDataSetMapper coneMapper2
coneMapper2 SetInputConnection [coneSource GetOutputPort]
coneActor2 SetMapper coneMapper2
[coneActor2 GetProperty] SetColor 1 0 0
coneActor2 SetPosition [lindex $p 0] [lindex $p 1] [lindex $p 2]
PointCone coneActor2 [lindex $n 0] [lindex $n 1] [lindex $n 2]
ren AddViewProp coneActor2
# Pick the image
picker Pick 200 200 0 ren
set p [picker GetPickPosition]
set n [picker GetPickNormal]
vtkActor coneActor3
coneActor3 PickableOff
vtkDataSetMapper coneMapper3
coneMapper3 SetInputConnection [coneSource GetOutputPort]
coneActor3 SetMapper coneMapper3
[coneActor3 GetProperty] SetColor 1 0 0
coneActor3 SetPosition [lindex $p 0] [lindex $p 1] [lindex $p 2]
PointCone coneActor3 [lindex $n 0] [lindex $n 1] [lindex $n 2]
ren AddViewProp coneActor3
# Pick a clipping plane
picker PickClippingPlanesOn
picker Pick 145 160 0 ren
set p [picker GetPickPosition]
set n [picker GetPickNormal]
vtkActor coneActor4
coneActor4 PickableOff
vtkDataSetMapper coneMapper4
coneMapper4 SetInputConnection [coneSource GetOutputPort]
coneActor4 SetMapper coneMapper4
[coneActor4 GetProperty] SetColor 1 0 0
coneActor4 SetPosition [lindex $p 0] [lindex $p 1] [lindex $p 2]
PointCone coneActor4 [lindex $n 0] [lindex $n 1] [lindex $n 2]
ren AddViewProp coneActor4
ren ResetCameraClippingRange
renWin Render
#---------------------------------------------------------
# test-related code
proc TkCheckAbort {} {
set foo [renWin GetEventPending]
if {$foo != 0} {renWin SetAbortRender 1}
}
renWin AddObserver AbortCheckEvent {TkCheckAbort}
iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize
wm withdraw .
|