<s> import director <EOL> import director . objectmodel as om <EOL> from director import visualization as vis <EOL> from director . visualization import PolyDataItem <EOL> from director import filterUtils <EOL> from director import ioUtils <EOL> from director import meshmanager <EOL> from director import transformUtils <EOL> from director . uuidutil import newUUID <EOL> from director . debugVis import DebugData <EOL> from director import vtkAll as vtk <EOL> import numpy as np <EOL> import os <EOL> import uuid <EOL> from collections import OrderedDict <EOL> class AffordanceItem ( PolyDataItem ) : <EOL> COPY_MODE_ALL = <NUM_LIT:0> <EOL> COPY_MODE_SKIP_LOCAL = <NUM_LIT:1> <EOL> LOCAL_PROPERTY_NAMES = ( '<STR_LIT>' ) <EOL> def __init__ ( self , name , polyData , view ) : <EOL> PolyDataItem . __init__ ( self , name , polyData , view ) <EOL> self . params = { } <EOL> self . addProperty ( '<STR_LIT>' , newUUID ( ) , attributes = om . PropertyAttributes ( hidden = True ) ) <EOL> self . addProperty ( '<STR_LIT>' , True ) <EOL> self . addProperty ( '<STR_LIT>' , [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , attributes = om . PropertyAttributes ( hidden = True ) ) <EOL> self . addProperty ( '<STR_LIT>' , False ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . setProperty ( '<STR_LIT>' , om . Icons . Hammer ) <EOL> def getPose ( self ) : <EOL> childFrame = self . getChildFrame ( ) <EOL> t = childFrame . transform if childFrame else vtk . vtkTransform ( ) <EOL> return transformUtils . poseFromTransform ( t ) <EOL> def getDescription ( self ) : <EOL> d = OrderedDict ( ) <EOL> d [ '<STR_LIT>' ] = type ( self ) . __name__ <EOL> d . update ( self . properties . _properties ) <EOL> d [ '<STR_LIT>' ] = self . getPose ( ) <EOL> return d <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> PolyDataItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName == '<STR_LIT>' : <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( ) : <EOL> pass <EOL> def setPolyData ( self , polyData ) : <EOL> if polyData . GetNumberOfPoints ( ) : <EOL> originPose = self . getProperty ( '<STR_LIT>' ) <EOL> pos , quat = originPose [ : <NUM_LIT:3> ] , originPose [ <NUM_LIT:3> : ] <EOL> t = transformUtils . transformFromPose ( pos , quat ) <EOL> polyData = filterUtils . transformPolyData ( polyData , t . GetLinearInverse ( ) ) <EOL> PolyDataItem . setPolyData ( self , polyData ) <EOL> def repositionFromDescription ( self , desc ) : <EOL> position , quat = desc [ '<STR_LIT>' ] <EOL> t = transformUtils . transformFromPose ( position , quat ) <EOL> self . getChildFrame ( ) . copyFrame ( t ) <EOL> def loadDescription ( self , desc , copyMode = COPY_MODE_ALL ) : <EOL> self . syncProperties ( desc , copyMode ) <EOL> self . repositionFromDescription ( desc ) <EOL> self . _renderAllViews ( ) <EOL> def syncProperties ( self , desc , copyMode = COPY_MODE_ALL ) : <EOL> for propertyName , propertyValue in desc . iteritems ( ) : <EOL> if copyMode == self . COPY_MODE_SKIP_LOCAL : <EOL> if propertyName in self . LOCAL_PROPERTY_NAMES : <EOL> continue <EOL> if self . hasProperty ( propertyName ) and ( self . getProperty ( propertyName ) != propertyValue ) : <EOL> self . setProperty ( propertyName , propertyValue ) <EOL> def onRemoveFromObjectModel ( self ) : <EOL> PolyDataItem . onRemoveFromObjectModel ( self ) <EOL> class BoxAffordanceItem ( AffordanceItem ) : <EOL> def __init__ ( self , name , view ) : <EOL> AffordanceItem . __init__ ( self , name , vtk . vtkPolyData ( ) , view ) <EOL> self . addProperty ( '<STR_LIT>' , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT:0> , attributes = om . PropertyAttributes ( minimum = <NUM_LIT:0> , maximum = <NUM_LIT:1000> ) ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( self ) : <EOL> d = DebugData ( ) <EOL> d . addCube ( self . getProperty ( '<STR_LIT>' ) , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , subdivisions = self . getProperty ( '<STR_LIT>' ) ) <EOL> self . setPolyData ( d . getPolyData ( ) ) <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> AffordanceItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . updateGeometryFromProperties ( ) <EOL> class SphereAffordanceItem ( AffordanceItem ) : <EOL> def __init__ ( self , name , view ) : <EOL> AffordanceItem . __init__ ( self , name , vtk . vtkPolyData ( ) , view ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( self ) : <EOL> d = DebugData ( ) <EOL> d . addSphere ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , self . getProperty ( '<STR_LIT>' ) ) <EOL> self . setPolyData ( d . getPolyData ( ) ) <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> AffordanceItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName == '<STR_LIT>' : <EOL> self . updateGeometryFromProperties ( ) <EOL> class CylinderAffordanceItem ( AffordanceItem ) : <EOL> def __init__ ( self , name , view ) : <EOL> AffordanceItem . __init__ ( self , name , vtk . vtkPolyData ( ) , view ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT:0.5> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( self ) : <EOL> d = DebugData ( ) <EOL> length = self . getProperty ( '<STR_LIT>' ) <EOL> d . addCylinder ( center = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , axis = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , length = self . getProperty ( '<STR_LIT>' ) , radius = self . getProperty ( '<STR_LIT>' ) ) <EOL> self . setPolyData ( d . getPolyData ( ) ) <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> AffordanceItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . updateGeometryFromProperties ( ) <EOL> class CapsuleAffordanceItem ( AffordanceItem ) : <EOL> def __init__ ( self , name , view ) : <EOL> AffordanceItem . __init__ ( self , name , vtk . vtkPolyData ( ) , view ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT:0.5> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( self ) : <EOL> d = DebugData ( ) <EOL> length = self . getProperty ( '<STR_LIT>' ) <EOL> d . addCapsule ( center = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , axis = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , length = self . getProperty ( '<STR_LIT>' ) , radius = self . getProperty ( '<STR_LIT>' ) ) <EOL> self . setPolyData ( d . getPolyData ( ) ) <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> AffordanceItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . updateGeometryFromProperties ( ) <EOL> class CapsuleRingAffordanceItem ( AffordanceItem ) : <EOL> def __init__ ( self , name , view ) : <EOL> AffordanceItem . __init__ ( self , name , vtk . vtkPolyData ( ) , view ) <EOL> self . setProperty ( '<STR_LIT>' , False ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT> , minimum = <NUM_LIT:0.0> , maximum = <NUM_LIT> ) ) <EOL> self . addProperty ( '<STR_LIT>' , <NUM_LIT:8> , attributes = om . PropertyAttributes ( decimals = <NUM_LIT:3> , singleStep = <NUM_LIT:1> , minimum = <NUM_LIT:3> , maximum = <NUM_LIT:100> ) ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( self ) : <EOL> radius = self . getProperty ( '<STR_LIT>' ) <EOL> circlePoints = np . linspace ( <NUM_LIT:0> , <NUM_LIT:2> * np . pi , self . getProperty ( '<STR_LIT>' ) + <NUM_LIT:1> ) <EOL> spokes = [ ( <NUM_LIT:0.0> , np . sin ( x ) , np . cos ( x ) ) for x in circlePoints ] <EOL> spokes = [ radius * np . array ( x ) / np . linalg . norm ( x ) for x in spokes ] <EOL> d = DebugData ( ) <EOL> for a , b in zip ( spokes , spokes [ <NUM_LIT:1> : ] ) : <EOL> d . addCapsule ( center = ( a + b ) / <NUM_LIT> , axis = ( b - a ) , length = np . linalg . norm ( b - a ) , radius = self . getProperty ( '<STR_LIT>' ) ) <EOL> self . setPolyData ( d . getPolyData ( ) ) <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> AffordanceItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . updateGeometryFromProperties ( ) <EOL> class MeshAffordanceItem ( AffordanceItem ) : <EOL> _meshManager = None <EOL> def __init__ ( self , name , view ) : <EOL> AffordanceItem . __init__ ( self , name , vtk . vtkPolyData ( ) , view ) <EOL> self . setProperty ( '<STR_LIT>' , False ) <EOL> self . addProperty ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . properties . setPropertyIndex ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if self . getProperty ( '<STR_LIT>' ) and not self . polyData . GetNumberOfPoints ( ) : <EOL> self . updateGeometryFromProperties ( ) <EOL> def updateGeometryFromProperties ( self ) : <EOL> filename = self . getProperty ( '<STR_LIT>' ) <EOL> if not filename : <EOL> polyData = vtk . vtkPolyData ( ) <EOL> else : <EOL> polyData = self . getMeshManager ( ) . get ( filename ) <EOL> if not polyData : <EOL> if not os . path . isabs ( filename ) : <EOL> filename = os . path . join ( director . getDRCBaseDir ( ) , filename ) <EOL> if os . path . isfile ( filename ) : <EOL> polyData = ioUtils . readPolyData ( filename ) <EOL> else : <EOL> d = DebugData ( ) <EOL> d . addFrame ( vtk . vtkTransform ( ) , scale = <NUM_LIT:0.1> , tubeRadius = <NUM_LIT> ) <EOL> polyData = d . getPolyData ( ) <EOL> self . setPolyData ( polyData ) <EOL> @ classmethod <EOL> def getMeshManager ( cls ) : <EOL> if cls . _meshManager is None : <EOL> cls . _meshManager = meshmanager . MeshManager ( ) <EOL> return cls . _meshManager <EOL> @ classmethod <EOL> def promotePolyDataItem ( cls , obj ) : <EOL> parent = obj . parent ( ) <EOL> view = obj . views [ <NUM_LIT:0> ] <EOL> name = obj . getProperty ( '<STR_LIT:Name>' ) <EOL> polyData = obj . polyData <EOL> props = obj . properties . _properties <EOL> childFrame = obj . getChildFrame ( ) <EOL> if childFrame : <EOL> t = transformUtils . copyFrame ( childFrame . transform ) <EOL> else : <EOL> t = vtk . vtkTransform ( ) <EOL> t . PostMultiply ( ) <EOL> t . Translate ( filterUtils . computeCentroid ( polyData ) ) <EOL> polyData = filterUtils . transformPolyData ( polyData , t . GetLinearInverse ( ) ) <EOL> children = [ c for c in obj . children ( ) if c is not childFrame ] <EOL> meshId = cls . getMeshManager ( ) . add ( polyData ) <EOL> om . removeFromObjectModel ( obj ) <EOL> obj = MeshAffordanceItem ( name , view ) <EOL> obj . setProperty ( '<STR_LIT>' , meshId ) <EOL> om . addToObjectModel ( obj , parentObj = parent ) <EOL> frame = vis . addChildFrame ( obj ) <EOL> frame . copyFrame ( t ) <EOL> for child in children : <EOL> om . addToObjectModel ( child , parentObj = obj ) <EOL> obj . syncProperties ( props ) <EOL> return obj <EOL> def _onPropertyChanged ( self , propertySet , propertyName ) : <EOL> AffordanceItem . _onPropertyChanged ( self , propertySet , propertyName ) <EOL> if propertyName == '<STR_LIT>' : <EOL> self . updateGeometryFromProperties ( ) <EOL> class FrameAffordanceItem ( AffordanceItem ) : <EOL> def setAffordanceParams ( self , params ) : <EOL> self . params = params <EOL> def updateParamsFromActorTransform ( self ) : <EOL> t = self . actor . GetUserTransform ( ) <EOL> xaxis = np . array ( t . TransformVector ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> yaxis = np . array ( t . TransformVector ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) ) <EOL> zaxis = np . array ( t . TransformVector ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) <EOL> self . params [ '<STR_LIT>' ] = xaxis <EOL> self . params [ '<STR_LIT>' ] = yaxis <EOL> self . params [ '<STR_LIT>' ] = zaxis <EOL> self . params [ '<STR_LIT>' ] = t . GetPosition ( ) </s>
