forked from ktbolt/cardiovascular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale-polydata.py
55 lines (46 loc) · 1.37 KB
/
scale-polydata.py
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
'''
This script is used to scale polydata around its center.
Usage:
scale-polydata.py POLYDATA.vtp
Writes scaled polydata to file named 'scaled_POLYDATA.vtp'
'''
import vtk
import sys
## Read in the .vtp file.
#
file_name = sys.argv[1];
reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName(file_name)
reader.Update()
surface = reader.GetOutput()
points = surface.GetPoints()
num_points = surface.GetPoints().GetNumberOfPoints()
num_polys = surface.GetPolys().GetNumberOfCells()
print("Number of points: %d" % num_points)
print("Number of triangles: %d" % num_polys)
## Get the model center.
#
centerFilter = vtk.vtkCenterOfMass()
centerFilter.SetInputData(surface)
centerFilter.SetUseScalarsAsWeights(False)
centerFilter.Update()
center = centerFilter.GetCenter()
## Scale the model.
#
transform = vtk.vtkTransform()
transform.Translate(center[0], center[1], center[2])
scale_factor = 0.1
transform.Scale(scale_factor, scale_factor, scale_factor)
transform.Translate(-center[0], -center[1], -center[2])
transformFilter = vtk.vtkTransformFilter()
transformFilter.SetInputData(surface)
transformFilter.SetTransform(transform)
transformFilter.Update()
## Write the scaled model.
#
scaled_file_name = "scaled_" + file_name
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(scaled_file_name)
writer.SetInputData(transformFilter.GetOutput())
writer.Update()
writer.Write()