def updateBlockFit(affordanceObj, polyData=None):

    affordanceObj.updateParamsFromActorTransform()

    name = affordanceObj.getProperty('Name') + ' refit'
    origin = affordanceObj.params['origin']
    normal = affordanceObj.params['yaxis']
    edgePerpAxis = affordanceObj.params['xaxis']
    blockDimensions = [affordanceObj.params['xwidth'], affordanceObj.params['ywidth']]

    if polyData is None:
        inputObj = om.findObjectByName('pointcloud snapshot')
        polyData = shallowCopy(inputObj.polyData)

    cropThreshold = 0.1
    cropped = polyData
    cropped, _ = cropToPlane(cropped, origin, normal, [-cropThreshold, cropThreshold])
    cropped, _ = cropToPlane(cropped, origin, edgePerpAxis, [-cropThreshold, cropThreshold])

    updatePolyData(cropped, 'refit search region', parent=getDebugFolder(), visible=False)

    cropped = extractLargestCluster(cropped)

    planePoints, planeNormal = applyPlaneFit(cropped, distanceThreshold=0.005, perpendicularAxis=normal, angleEpsilon=math.radians(10))
    planePoints = thresholdPoints(planePoints, 'dist_to_plane', [-0.005, 0.005])
    updatePolyData(planePoints, 'refit board segmentation', parent=getDebugFolder(), visible=False)

    refitObj = segmentBlockByTopPlane(planePoints, blockDimensions, expectedNormal=normal, expectedXAxis=edgePerpAxis, edgeSign=-1, name=name)

    refitOrigin = np.array(refitObj.params['origin'])
    refitLength = refitObj.params['zwidth']
    refitZAxis = refitObj.params['zaxis']
    refitEndPoint1 = refitOrigin + refitZAxis*refitLength/2.0

    originalLength = affordanceObj.params['zwidth']
    correctedOrigin = refitEndPoint1 - refitZAxis*originalLength/2.0
    originDelta = correctedOrigin - refitOrigin

    refitObj.params['zwidth'] = originalLength
    refitObj.polyData.DeepCopy(affordanceObj.polyData)
    refitObj.actor.GetUserTransform().Translate(originDelta)
    refitObj.updateParamsFromActorTransform()
dataset/ETHPy150Open RobotLocomotion/director/src/python/director/segmentation.py/updateBlockFit/original