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

from director import lcmobjectcollection 

from director import geometryencoder 

from director import ioUtils 

from director.uuidutil import newUUID 

import os 

 

 

class MeshManager(object): 

 

    def __init__(self): 

        self.meshes = {} 

        self.cacheDirectory = '/tmp' 

        self.cacheDataType = 'stl' 

        self.collection = lcmobjectcollection.LCMObjectCollection(channel='MESH_COLLECTION_COMMAND') 

        self.collection.connectDescriptionUpdated(self._onDescriptionUpdated) 

 

    def add(self, polyData, publish=True): 

        meshId = newUUID() 

        self.meshes[meshId] = polyData 

        if publish and self.collection: 

            self._publishMesh(meshId) 

        return meshId 

 

    def get(self, meshId): 

        return self.meshes.get(meshId) 

 

    def getFilesystemFilename(self, meshId): 

        if meshId in self.meshes: 

            filename = os.path.join(self.cacheDirectory, '%s.%s' % (meshId, self.cacheDataType)) 

            if not os.path.isfile(filename): 

                ioUtils.writePolyData(self.get(meshId), filename) 

            return filename 

        return None 

 

    def _publishMesh(self, meshId): 

        polyData = self.meshes[meshId] 

        self.collection.updateDescription(dict(uuid=meshId, data=geometryencoder.encodePolyData(polyData)), notify=False) 

 

    def _onDescriptionUpdated(self, collection, descriptionId): 

        desc = collection.getDescription(descriptionId) 

        meshId = desc['uuid'] 

        if meshId not in self.meshes: 

            polyData = geometryencoder.decodePolyData(desc['data']) 

            self.meshes[meshId] = polyData 

            #print 'decoded polydata with %d points' % polyData.GetNumberOfPoints()