Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

''' Convienence methods on VTK routines only ''' 

 

import director.vtkAll as vtk 

import director.vtkNumpy as vnp 

from director.shallowCopy import shallowCopy 

import numpy as np 

 

 

def thresholdPoints(polyData, arrayName, thresholdRange): 

    assert(polyData.GetPointData().GetArray(arrayName)) 

    f = vtk.vtkThresholdPoints() 

    f.SetInput(polyData) 

    f.ThresholdBetween(thresholdRange[0], thresholdRange[1]) 

    f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, arrayName) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

def thresholdCells(polyData, arrayName, thresholdRange): 

    assert(polyData.GetCellData().GetArray(arrayName)) 

    f = vtk.vtkThreshold() 

    # f.SetAttributeModeToUseCellData() 

    f.SetInput(polyData) 

    f.ThresholdBetween(thresholdRange[0], thresholdRange[1]) 

    f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS, arrayName) 

    f.Update() 

 

    g = vtk.vtkGeometryFilter() 

    g.SetInput(f.GetOutput()) 

    g.Update() 

    return shallowCopy(g.GetOutput()) 

 

 

def transformPolyData(polyData, transform): 

 

    t = vtk.vtkTransformPolyDataFilter() 

    t.SetTransform(transform) 

    t.SetInput(shallowCopy(polyData)) 

    t.Update() 

    return shallowCopy(t.GetOutput()) 

 

 

def computeDelaunay3D(polyData): 

    f = vtk.vtkDelaunay3D() 

    f.SetInput(polyData) 

    f.SetOffset(100.0) 

    f.Update() 

 

    surface = vtk.vtkGeometryFilter() 

    surface.SetInput(f.GetOutput()) 

    surface.Update() 

 

    clean = vtk.vtkCleanPolyData() 

    clean.SetInput(surface.GetOutput()) 

    clean.Update() 

 

    return shallowCopy(clean.GetOutput()) 

 

 

def computeDelaunay2D(polyData): 

    f = vtk.vtkDelaunay2D() 

    f.SetInput(polyData) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

 

def computeCentroid(polyData): 

    return np.average(vnp.getNumpyFromVtk(polyData, 'Points'), axis=0) 

 

 

def appendPolyData(polyDataList): 

    assert len(polyDataList) 

    append = vtk.vtkAppendPolyData() 

    for polyData in polyDataList: 

        append.AddInput(polyData) 

    append.Update() 

    return shallowCopy(append.GetOutput()) 

 

 

def computeNormals(polyData, featureAngle=45): 

    normals = vtk.vtkPolyDataNormals() 

    normals.SetFeatureAngle(featureAngle) 

    normals.SetInput(polyData) 

    normals.Update() 

    return shallowCopy(normals.GetOutput()) 

 

 

def cleanPolyData(polyData): 

    clean = vtk.vtkCleanPolyData() 

    clean.SetInput(polyData) 

    clean.Update() 

    return shallowCopy(clean.GetOutput()) 

 

 

def triangulatePolyData(polyData): 

    f = vtk.vtkTriangleFilter() 

    f.SetInput(polyData) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

 

def decimateMesh(polyData, targetReduction=0.1): 

    ''' 

    Reduce the number of  triangles in the input mesh by targetReduction. 

    0.1 = 10% reduction (if there was 100 triangles, now there will be 90) 

    ''' 

    f = vtk.vtkDecimatePro() 

    f.SetInput(polyData) 

    f.SetTargetReduction(targetReduction) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

 

def hasNonFinitePoints(polyData, arrayName='Points'): 

    pts = vnp.getNumpyFromVtk(polyData, arrayName) 

    return np.isfinite(pts).any() 

 

 

def labelNonFinitePoints(polyData, arrayName='Points'): 

    ''' 

    adds is_nonfinite label to polyData.  non finite includes nan and +/- inf. 

    ''' 

    pts = vnp.getNumpyFromVtk(polyData, arrayName) 

    labels = np.logical_not(np.isfinite(pts)).any(axis=1) 

    vnp.addNumpyToVtk(polyData, np.array(labels, dtype=np.int32), 'is_nonfinite') 

 

 

def removeNonFinitePoints(polyData, arrayName='Points'): 

    polyData = shallowCopy(polyData) 

    labelNonFinitePoints(polyData, arrayName) 

    return thresholdPoints(polyData, 'is_nonfinite', [0, 0]) 

 

 

def flipImage(image, flipAxis=1): 

    ''' 

    Flip a vtkImageData using the vtkImageFlip filter. 

    The flipAxis can be 0 or 1 to flip horizontally or vertically. 

    ''' 

    assert flipAxis in (0, 1) 

    f = vtk.vtkImageFlip() 

    f.SetFilteredAxis(flipAxis) 

    f.SetInput(image) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

 

def rotateImage180(image): 

    ''' 

    rotates an image by 180 degrees 

    ''' 

    r1 = vtk.vtkImageFlip() 

    r1.SetInput(image) 

    r1.SetFilteredAxis(0) 

    r1.Update() 

    r2 = vtk.vtkImageFlip() 

    r2.SetInput(r1.GetOutput()) 

    r2.SetFilteredAxis(1) 

    r2.Update() 

    return shallowCopy(r2.GetOutput())