<commit_before>import ddapp
import ddapp.objectmodel as om
from ddapp import affordance
from ddapp import visualization as vis
from ddapp.visualization import PolyDataItem
from ddapp import filterUtils
from ddapp import ioUtils
from ddapp import meshmanager
from ddapp import transformUtils
from ddapp.uuidutil import newUUID
from ddapp.debugVis import DebugData
from ddapp import vtkAll as vtk
import numpy as np

import os
import uuid
from collections import OrderedDict


class AffordanceItem(PolyDataItem):

    def __init__(self, name, polyData, view):
        PolyDataItem.__init__(self, name, polyData, view)
        self.params = {}
        self.addProperty('uuid', newUUID(), attributes=om.PropertyAttributes(hidden=True))
        self.addProperty('Collision Enabled', True)
        self.properties.setPropertyIndex('Collision Enabled', 0)
        self.setProperty('Icon', om.Icons.Hammer)

    def publish(self):
        pass

    def getActionNames(self):
        actions = ['Publish affordance']
        return PolyDataItem.getActionNames(self) + actions

    def onAction(self, action):
        if action == 'Publish affordance':
            self.publish()
        else:
            PolyDataItem.onAction(self, action)

    def getDescription(self):
        d = OrderedDict()
        d['classname'] = type(self).__name__
        d.update(self.properties._properties)
        pose = transformUtils.poseFromTransform(self.getChildFrame().transform)
        d['pose'] = pose
        return d

    def repositionFromDescription(self, desc):
        position, quat = desc['pose']
        t = transformUtils.transformFromPose(position, quat)
        self.getChildFrame().copyFrame(t)

    def loadDescription(self, desc):
        self.syncProperties(desc)
        self.repositionFromDescription(desc)
        self._renderAllViews()

    def syncProperties(self, desc):
        for propertyName, propertyValue in desc.iteritems():
            if self.hasProperty(propertyName) and (self.getProperty(propertyName) != propertyValue):
                #print 'syncing property %s: %r' % (propertyName, propertyValue)
                self.setProperty(propertyName, propertyValue)

    def onRemoveFromObjectModel(self):
        PolyDataItem.onRemoveFromObjectModel(self)



class BoxAffordanceItem(AffordanceItem):

    def __init__(self, name, view):
        AffordanceItem.__init__(self, name, vtk.vtkPolyData(), view)
        self.addProperty('Dimensions', [0.25, 0.25, 0.25], attributes=om.PropertyAttributes(decimals=3, singleStep=0.01, minimum=0.0, maximum=1e4))
        self.properties.setPropertyIndex('Dimensions', 0)
        self.updateGeometryFromProperties()

    def loadDescription(self, desc):
        AffordanceItem.loadDescription(self, desc)

    def updateGeometryFromProperties(self):
        d = DebugData()
        d.addCube(self.getProperty('Dimensions'), (0,0,0))
        self.setPolyData(d.getPolyData())

    def _onPropertyChanged(self, propertySet, propertyName):
        AffordanceItem._onPropertyChanged(self, propertySet, propertyName)

        if propertyName == 'Dimensions':
            self.updateGeometryFromProperties()


class SphereAffordanceItem(AffordanceItem):

    def __init__(self, name, view):
        AffordanceItem.__init__(self, name, vtk.vtkPolyData(), view)
        self.addProperty('Radius', 0.15, attributes=om.PropertyAttributes(decimals=3, singleStep=0.01, minimum=0.0, maximum=1e4))
        self.properties.setPropertyIndex('Radius', 0)
        self.updateGeometryFromProperties()

    def loadDescription(self, desc):
        AffordanceItem.loadDescription(self, desc)

    def updateGeometryFromProperties(self):
        d = DebugData()
        d.addSphere((0,0,0), self.getProperty('Radius'))
        self.setPolyData(d.getPolyData())<commit_msg>add subdivisions property to BoxAffordanceItem<commit_after>import ddapp
import ddapp.objectmodel as om
from ddapp import affordance
from ddapp import visualization as vis
from ddapp.visualization import PolyDataItem
from ddapp import filterUtils
from ddapp import ioUtils
from ddapp import meshmanager
from ddapp import transformUtils
from ddapp.uuidutil import newUUID
from ddapp.debugVis import DebugData
from ddapp import vtkAll as vtk
import numpy as np

import os
import uuid
from collections import OrderedDict


class AffordanceItem(PolyDataItem):

    def __init__(self, name, polyData, view):
        PolyDataItem.__init__(self, name, polyData, view)
        self.params = {}
        self.addProperty('uuid', newUUID(), attributes=om.PropertyAttributes(hidden=True))
        self.addProperty('Collision Enabled', True)
        self.properties.setPropertyIndex('Collision Enabled', 0)
        self.setProperty('Icon', om.Icons.Hammer)

    def publish(self):
        pass

    def getActionNames(self):
        actions = ['Publish affordance']
        return PolyDataItem.getActionNames(self) + actions

    def onAction(self, action):
        if action == 'Publish affordance':
            self.publish()
        else:
            PolyDataItem.onAction(self, action)

    def getDescription(self):
        d = OrderedDict()
        d['classname'] = type(self).__name__
        d.update(self.properties._properties)
        pose = transformUtils.poseFromTransform(self.getChildFrame().transform)
        d['pose'] = pose
        return d

    def repositionFromDescription(self, desc):
        position, quat = desc['pose']
        t = transformUtils.transformFromPose(position, quat)
        self.getChildFrame().copyFrame(t)

    def loadDescription(self, desc):
        self.syncProperties(desc)
        self.repositionFromDescription(desc)
        self._renderAllViews()

    def syncProperties(self, desc):
        for propertyName, propertyValue in desc.iteritems():
            if self.hasProperty(propertyName) and (self.getProperty(propertyName) != propertyValue):
                #print 'syncing property %s: %r' % (propertyName, propertyValue)
                self.setProperty(propertyName, propertyValue)

    def onRemoveFromObjectModel(self):
        PolyDataItem.onRemoveFromObjectModel(self)



class BoxAffordanceItem(AffordanceItem):

    def __init__(self, name, view):
        AffordanceItem.__init__(self, name, vtk.vtkPolyData(), view)
        self.addProperty('Dimensions', [0.25, 0.25, 0.25], attributes=om.PropertyAttributes(decimals=3, singleStep=0.01, minimum=0.0, maximum=1e4))
        self.addProperty('Subdivisions', 0, attributes=om.PropertyAttributes(minimum=0, maximum=1000))
        self.properties.setPropertyIndex('Dimensions', 0)
        self.properties.setPropertyIndex('Subdivisions', 1)
        self.updateGeometryFromProperties()

    def loadDescription(self, desc):
        AffordanceItem.loadDescription(self, desc)

    def updateGeometryFromProperties(self):
        d = DebugData()
        d.addCube(self.getProperty('Dimensions'), (0,0,0), subdivisions=self.getProperty('Subdivisions'))
        self.setPolyData(d.getPolyData())

    def _onPropertyChanged(self, propertySet, propertyName):
        AffordanceItem._onPropertyChanged(self, propertySet, propertyName)

        if propertyName in ('Dimensions', 'Subdivisions'):
            self.updateGeometryFromProperties()


class SphereAffordanceItem(AffordanceItem):

    def __init__(self, name, view):
        AffordanceItem.__init__(self, name, vtk.vtkPolyData(), view)
        self.addProperty('Radius', 0.15, attributes=om.PropertyAttributes(decimals=3, singleStep=0.01, minimum=0.0, maximum=1e4))
        self.properties.setPropertyIndex('Radius', 0)
        self.updateGeometryFromProperties()

    def loadDescription(self, desc):
        AffordanceItem.loadDescription(self, desc)

    def updateGeometryFromProperties(self):
        d = DebugData()
        d.addSphere((0,0,0), self.getProperty('Radius'))
        self.setPolyData(d.getPolyData())
{'fraction_non_alphanumeric': 0.10174012447605742, 'fraction_numerical': 0.010288327194208052, 'mean_word_length': 3.631764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2119864', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2196, 'n_words': 484}