File: vmtkmeshclipper.py

package info (click to toggle)
vmtk 1.3%2Bdfsg-2.3
  • links: PTS, VCS
  • area: non-free
  • in suites: buster
  • size: 8,932 kB
  • sloc: cpp: 82,947; ansic: 31,817; python: 21,462; perl: 381; makefile: 93; ruby: 41; sh: 19
file content (162 lines) | stat: -rw-r--r-- 5,524 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

## Program:   VMTK
## Module:    $RCSfile: vmtkmeshclipper.py,v $
## Language:  Python
## Date:      $Date: 2006/05/26 12:35:13 $
## Version:   $Revision: 1.8 $

##   Copyright (c) Luca Antiga, David Steinman. All rights reserved.
##   See LICENCE file for details.

##      This software is distributed WITHOUT ANY WARRANTY; without even 
##      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
##      PURPOSE.  See the above copyright notices for more information.

import vtk
import sys

import vmtkrenderer
import pypes

vmtkmeshclipper = 'vmtkMeshClipper'

class vmtkMeshClipper(pypes.pypeScript):

    def __init__(self):

        pypes.pypeScript.__init__(self)

        self.Mesh = None
        self.ClippedMesh = None
        self.Surface = None
        self.vmtkRenderer = None
        self.OwnRenderer = 0

        self.Actor = None
        self.BoxWidget = None
        self.Planes = None

        self.InsideOut = 0

        self.Interactive = 1
        self.ClipArrayName = None
        self.ClipValue = 0.0

        self.SetScriptName('vmtkmeshclipper')
        self.SetScriptDoc('interactively clip a mesh with a box')
        self.SetInputMembers([
            ['Mesh','i','vtkUnstructuredGrid',1,'','the input mesh','vmtkmeshreader'],
            ['InsideOut','insideout','bool',1,'','toggle switching output and clipped surfaces'],
            ['Interactive','interactive','bool',1,'','toggle clipping with a widget or with a pre-defined scalar field'],
            ['ClipArrayName','array','str',1,'','name of the array with which to clip in case of non-interactive clipping'],
            ['ClipValue','value','float',1,'','scalar value at which to perform clipping in case of non-interactive clipping'],
            ['vmtkRenderer','renderer','vmtkRenderer',1,'','external renderer']
            ])
        self.SetOutputMembers([
            ['Mesh','o','vtkUnstructuredGrid',1,'','the output mesh','vmtkmeshwriter'],
            ['Surface','osurface','vtkPolyData',1,'','the output surface corresponding to the cut','vmtksurfacewriter'],
            ['ClippedMesh','oclipped','vtkUnstructuredGrid',1,'','the clipped mesh','vmtkmeshwriter']
            ])

    def InteractCallback(self, obj):
        pass
        #if self.BoxWidget.GetEnabled() == 1:
        #    self.BoxWidget.SetEnabled(0)
        #else:
        #    self.BoxWidget.SetEnabled(1)

    def ClipCallback(self, obj):
        if self.BoxWidget.GetEnabled() != 1:
            return
        self.BoxWidget.GetPlanes(self.Planes)
        self.Clipper.Update()
        self.Mesh.DeepCopy(self.Clipper.GetOutput())
        self.ClippedMesh.DeepCopy(self.Clipper.GetClippedOutput())
        self.Cutter.Update()
        self.Surface.DeepCopy(self.Cutter.GetOutput())
        mapper = vtk.vtkDataSetMapper()
        mapper.SetInputData(self.Mesh)
        mapper.ScalarVisibilityOff()
        self.Actor.SetMapper(mapper)
        self.BoxWidget.Off()

    def Display(self):

      	self.BoxWidget.SetInputData(self.Mesh)
      	self.BoxWidget.PlaceWidget()

        self.vmtkRenderer.Render()

    def Execute(self):

        if (self.Mesh == None):
            self.PrintError('Error: no Mesh.')

        self.Clipper = vtk.vtkClipDataSet()
        self.Clipper.SetInputData(self.Mesh)
        self.Clipper.GenerateClippedOutputOn()
        self.Clipper.SetInsideOut(self.InsideOut)
        
        if self.Interactive:

            self.Planes = vtk.vtkPlanes()
            self.Clipper.SetClipFunction(self.Planes)

            self.Cutter = vtk.vtkCutter()
            self.Cutter.SetInputData(self.Mesh)
            self.Cutter.SetCutFunction(self.Planes)

            self.ClippedMesh = vtk.vtkUnstructuredGrid()
            self.Surface = vtk.vtkPolyData()

            if not self.vmtkRenderer:
                self.vmtkRenderer = vmtkrenderer.vmtkRenderer()
                self.vmtkRenderer.Initialize()
                self.OwnRenderer = 1

            self.vmtkRenderer.RegisterScript(self) 

            mapper = vtk.vtkDataSetMapper()
            mapper.SetInputData(self.Mesh)
            mapper.ScalarVisibilityOff()
            self.Actor = vtk.vtkActor()
            self.Actor.SetMapper(mapper)
            self.vmtkRenderer.Renderer.AddActor(self.Actor)

            self.BoxWidget = vtk.vtkBoxWidget()
            self.BoxWidget.SetInteractor(self.vmtkRenderer.RenderWindowInteractor)
            self.BoxWidget.GetFaceProperty().SetColor(0.6,0.6,0.2)
            self.BoxWidget.GetFaceProperty().SetOpacity(0.25)

            self.vmtkRenderer.AddKeyBinding('i','Interact.', self.InteractCallback)
            self.vmtkRenderer.AddKeyBinding('space','Clip.', self.ClipCallback)

            self.Display()

            if self.OwnRenderer:
                self.vmtkRenderer.Deallocate()

        else:

            self.Mesh.GetPointData().SetActiveScalars(self.ClipArrayName)

            self.Clipper.GenerateClipScalarsOff()
            self.Clipper.SetValue(self.ClipValue)
            self.Clipper.Update()

            self.Cutter = vtk.vtkContourFilter()
            self.Cutter.SetInputData(self.Mesh)
            self.Cutter.SetValue(0,self.ClipValue)
            self.Cutter.Update()

            self.Mesh = self.Clipper.GetOutput()
            self.Surface = self.Cutter.GetOutput()
            self.ClippedMesh = self.Clipper.GetClippedOutput()
        


if __name__=='__main__':
    main = pypes.pypeMain()
    main.Arguments = sys.argv
    main.Execute()