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

159

160

161

162

163

import director.applogic as app 

from director import lcmUtils 

from director import visualization as vis 

from director import filterUtils 

from director import drcargs 

from director.shallowCopy import shallowCopy 

from director.timercallback import TimerCallback 

from director import objectmodel as om 

import director.vtkAll as vtk 

import PythonQt 

from PythonQt import QtCore, QtGui 

 

 

class KinectItem(om.ObjectModelItem): 

 

    def __init__(self, model): 

 

        om.ObjectModelItem.__init__(self, 'Kinect', om.Icons.Eye) 

 

        self.model = model 

        self.scalarBarWidget = None 

        self.addProperty('Color By', 1, 

                         attributes=om.PropertyAttributes(enumNames=['Solid Color', 'rgb_colors'])) 

        self.addProperty('Updates Enabled', True) 

        self.addProperty('Framerate', model.targetFps, 

                         attributes=om.PropertyAttributes(decimals=0, minimum=1.0, maximum=30.0, singleStep=1, hidden=False)) 

        self.addProperty('Visible', model.visible) 

        #self.addProperty('Point Size', model.pointSize, 

        #                 attributes=om.PropertyAttributes(decimals=0, minimum=1, maximum=20, singleStep=1, hidden=False)) 

        #self.addProperty('Alpha', model.alpha, 

        #                 attributes=om.PropertyAttributes(decimals=2, minimum=0, maximum=1.0, singleStep=0.1, hidden=False)) 

        #self.addProperty('Color', QtGui.QColor(255,255,255)) 

 

    def _onPropertyChanged(self, propertySet, propertyName): 

        om.ObjectModelItem._onPropertyChanged(self, propertySet, propertyName) 

 

        if propertyName == 'Updates Enabled': 

            if self.getProperty('Updates Enabled'): 

                self.model.start() 

            else: 

                self.model.stop() 

 

        #elif propertyName == 'Alpha': 

        #    self.model.setAlpha(self.getProperty(propertyName)) 

 

        elif propertyName == 'Visible': 

            self.model.setVisible(self.getProperty(propertyName)) 

 

        #elif propertyName == 'Point Size': 

        #    self.model.setPointSize(self.getProperty(propertyName)) 

 

        elif propertyName == 'Framerate': 

            self.model.setFPS(self.getProperty('Framerate')) 

 

        elif propertyName == 'Color By': 

            self._updateColorBy() 

 

        self.model.polyDataObj._renderAllViews() 

 

 

    def _updateColorBy(self): 

 

        arrayMap = { 

          0 : 'Solid Color', 

          1 : 'rgb_colors' 

          } 

 

        colorBy = self.getProperty('Color By') 

        arrayName = arrayMap.get(colorBy) 

 

        self.model.polyDataObj.setProperty('Color By', arrayName) 

 

 

 

class KinectSource(TimerCallback): 

 

    def __init__(self, view, _KinectQueue): 

        self.view = view 

        self.KinectQueue = _KinectQueue 

 

        self.visible = True 

 

        self.p = vtk.vtkPolyData() 

        utime = KinectQueue.getPointCloudFromKinect(self.p) 

        self.polyDataObj = vis.PolyDataItem('kinect source', shallowCopy(self.p), view) 

        self.polyDataObj.actor.SetPickable(1) 

        self.polyDataObj.initialized = False 

 

        om.addToObjectModel(self.polyDataObj) 

 

        self.queue = PythonQt.dd.ddBotImageQueue(lcmUtils.getGlobalLCMThread()) 

        self.queue.init(lcmUtils.getGlobalLCMThread(), drcargs.args().config_file) 

 

        self.targetFps = 30 

        self.timerCallback = TimerCallback(targetFps=self.targetFps) 

        self.timerCallback.callback = self._updateSource 

        #self.timerCallback.start() 

 

    def start(self): 

        self.timerCallback.start() 

 

    def stop(self): 

        self.timerCallback.stop() 

 

    def setFPS(self, framerate): 

        self.targetFps = framerate 

        self.timerCallback.stop() 

        self.timerCallback.targetFps = framerate 

        self.timerCallback.start() 

 

    def setVisible(self, visible): 

        self.polyDataObj.setProperty('Visible', visible) 

 

    def _updateSource(self): 

        p = vtk.vtkPolyData() 

        utime = self.KinectQueue.getPointCloudFromKinect(p) 

 

        if not p.GetNumberOfPoints(): 

            return 

 

        cameraToLocalFused = vtk.vtkTransform() 

        self.queue.getTransform('KINECT_RGB', 'local', utime, cameraToLocalFused) 

        p = filterUtils.transformPolyData(p, cameraToLocalFused) 

        self.polyDataObj.setPolyData(p) 

 

        if not self.polyDataObj.initialized: 

            self.polyDataObj.setProperty('Color By', 'rgb_colors') 

            self.polyDataObj.initialized = True 

 

 

def init(view): 

    global KinectQueue, _kinectItem, _kinectSource 

    KinectQueue = PythonQt.dd.ddKinectLCM(lcmUtils.getGlobalLCMThread()) 

    KinectQueue.init(lcmUtils.getGlobalLCMThread(), drcargs.args().config_file) 

 

    _kinectSource = KinectSource(view, KinectQueue) 

    _kinectSource.start() 

 

    sensorsFolder = om.getOrCreateContainer('sensors') 

 

    _kinectItem = KinectItem(_kinectSource) 

    om.addToObjectModel(_kinectItem, sensorsFolder) 

 

 

# Hasn't been used - currently deactivated 

#def renderLastKinectPointCloud(): 

#    # view = view or app.getCurrentRenderView() 

#    # if view is None: 

#    #     return 

#    p = vtk.vtkPolyData() 

#    print("will grab the last point cloud in python \n") 

#    KinectQueue.getPointCloudFromKinect(p) 

#    print("grabbed the last point cloud in python, will #render now \n") 

#    obj = vis.showPolyData (p, 'kinect cloud') 

#    print("director rendered last point cloud \n") 

 

 

def startButton(): 

    view = app.getCurrentRenderView() 

    init(view) 

    _kinectSource.start() 

 

app.addToolbarMacro('start live kinect', startButton)