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

import director.visualization as vis 

from director import filterUtils 

import director.vtkAll as vtk 

import director.vtkNumpy as vnp 

from director.shallowCopy import shallowCopy 

from director import ioUtils 

import numpy as np 

 

 

def createTexturedPlane(): 

    source = vtk.vtkPlaneSource() 

    textureMap = vtk.vtkTextureMapToPlane() 

    textureMap.SetInput(source.GetOutput()) 

 

    textureMap.Update() 

    return shallowCopy(textureMap.GetOutput()) 

 

 

def getSkyboxSides(): 

    return ['top', 'bottom', 'front', 'back', 'left', 'right'] 

 

 

def createSkyboxPlane(side): 

 

    pd = createTexturedPlane() 

 

    t = vtk.vtkTransform() 

    t.PostMultiply() 

 

    if side == 'top': 

        t.Translate(0,0,0.5) 

        t.RotateZ(180) 

 

    elif side == 'bottom': 

        t.RotateX(180) 

        t.RotateY(180) 

        t.RotateZ(-270) 

        t.Translate(0,0,-0.5) 

 

    elif side == 'front': 

        t.RotateY(90) 

        t.RotateX(90) 

        t.RotateZ(180) 

        t.Translate(0.5,0.0,0.0) 

 

    elif side == 'back': 

        t.RotateY(90) 

        t.RotateX(90) 

        t.RotateZ(0) 

        t.Translate(-0.5,0.0,0.0) 

 

    elif side == 'left': 

        t.RotateY(90) 

        t.RotateX(90) 

        t.RotateZ(-90) 

        t.Translate(0.0,0.5,0.0) 

 

    elif side == 'right': 

        t.RotateY(90) 

        t.RotateX(90) 

        t.RotateZ(90) 

        t.Translate(0.0,-0.5,0.0) 

 

    pd = filterUtils.transformPolyData(pd, t) 

    return pd 

 

 

def createSkyboxPlanes(): 

    planes = {} 

    for side in getSkyboxSides(): 

        planes[side] = createSkyboxPlane(side) 

    return planes 

 

 

def createTexture(imageFilename): 

    image = ioUtils.readImage(imageFilename) 

    tex = vtk.vtkTexture() 

    tex.SetInput(image) 

    tex.EdgeClampOn() 

    tex.RepeatOff() 

    return tex 

 

 

def createSkybox(imageMap, view): 

 

    objs = {} 

    planes = createSkyboxPlanes() 

 

    for side, imageFilename in imageMap.iteritems(): 

        texture = createTexture(imageFilename) 

        obj = vis.PolyDataItem('skybox %s' % side, planes[side], view=None) 

        obj.actor.SetTexture(texture) 

        obj.actor.GetProperty().LightingOff() 

        view.backgroundRenderer().AddActor(obj.actor) 

        objs[side] = obj 

 

    return objs 

 

 

def getSkyboxImages(baseDir): 

    imageMap =  dict( 

        top    = baseDir + '/topmars1.jpg', 

        bottom = baseDir + '/botmars1.jpg', 

        front  = baseDir + '/frontmars1.jpg', 

        back   = baseDir + '/backmars1.jpg', 

        left   = baseDir + '/leftmars1.jpg', 

        right  = baseDir + '/rightmars1.jpg') 

 

    return imageMap 

 

 

def createTextureGround(imageFilename, view): 

 

    pd = createTexturedPlane() 

    texture = createTexture(imageFilename) 

    texture.RepeatOn() 

 

    tcoords = vnp.getNumpyFromVtk(pd, 'Texture Coordinates') 

    tcoords *= 60 

 

    t = vtk.vtkTransform() 

    t.PostMultiply() 

    t.Scale(200,200,200) 

    t.Translate(0,0,-0.005) 

 

    pd = filterUtils.transformPolyData(pd, t) 

 

    obj = vis.showPolyData(pd, 'ground', view=view, alpha=1.0, parent='skybox') 

    obj.actor.SetTexture(texture) 

    obj.actor.GetProperty().LightingOff() 

 

 

def connectSkyboxCamera(view, debug=False): 

 

    baseRen = view.backgroundRenderer() 

 

    def updateSkyboxCamera(o, e): 

        c = baseRen.GetActiveCamera() 

        c2 = view.camera() 

 

        viewDirection = np.array(c2.GetFocalPoint()) - np.array(c2.GetPosition()) 

        viewDirection /= np.linalg.norm(viewDirection) 

 

        if debug: 

            c.SetPosition(c2.GetPosition()) 

            c.SetFocalPoint(c2.GetFocalPoint()) 

 

        else: 

            c.SetPosition(0,0,0) 

            c.SetFocalPoint(viewDirection) 

 

        c.SetViewUp(c2.GetViewUp()) 

        c.SetViewAngle(c2.GetViewAngle()) 

 

 

    view.renderWindow().AddObserver('StartEvent', updateSkyboxCamera)