Beispiel #1
0
def fitObjectFromPolyData(polyData):

    origin, edges, wireframe = segmentation.getOrientedBoundingBox(polyData)

    edgeLengths = [float(np.linalg.norm(edge)) for edge in edges]
    axes = [edge / np.linalg.norm(edge) for edge in edges]

    boxCenter = segmentation.computeCentroid(wireframe)

    t = transformUtils.getTransformFromAxes(axes[0], axes[1], axes[2])
    zAxisIndex, zAxis, zAxisSign = transformUtils.findTransformAxis(
        t, [0, 0, 1.0])
    xAxisIndex, xAxis, xAxisSign = transformUtils.findTransformAxis(
        t, [1.0, 0, 0])

    assert zAxisIndex != xAxisIndex

    #zAxis = zAxis*zAxisSign
    zAxis = [0, 0, 1.0]
    xAxis = xAxis * xAxisSign
    yAxis = np.cross(zAxis, xAxis)
    xAxis = np.cross(yAxis, zAxis)

    zLength = edgeLengths[zAxisIndex]
    xLength = edgeLengths[xAxisIndex]
    ids = [0, 1, 2]
    ids.remove(zAxisIndex)
    ids.remove(xAxisIndex)
    yLength = edgeLengths[ids[0]]

    t = transformUtils.getTransformFromAxes(xAxis, yAxis, zAxis)
    t.PostMultiply()
    t.Translate(boxCenter)

    om.removeFromObjectModel(om.findObjectByName('box'))
    obj = makeBox()
    obj.setProperty('Dimensions', [xLength, yLength, zLength])
    obj.getChildFrame().copyFrame(t)
    #obj.getChildFrame().setProperty('Visible', True)
    obj.setProperty('Surface Mode', 'Wireframe')
    obj.setProperty('Color', [1, 0, 0])
    return obj
Beispiel #2
0
    def getOrientedBoundingBox(pd):
        mesh = pd
        origin, edges, wireframe = segmentation.getOrientedBoundingBox(mesh)

        edgeLengths = np.array([np.linalg.norm(edge) for edge in edges])
        axes = [edge / np.linalg.norm(edge) for edge in edges]

        # find axis nearest to the +/- up vector
        upVector = [0, 0, 1]
        dotProducts = [np.abs(np.dot(axe, upVector)) for axe in axes]
        zAxisIndex = np.argmax(dotProducts)

        # re-index axes and edge lengths so that the found axis is the z axis
        axesInds = [(zAxisIndex + 1) % 3, (zAxisIndex + 2) % 3, zAxisIndex]
        axes = [axes[i] for i in axesInds]
        edgeLengths = [edgeLengths[i] for i in axesInds]

        # flip if necessary so that z axis points toward up
        if np.dot(axes[2], upVector) < 0:
            axes[1] = -axes[1]
            axes[2] = -axes[2]

        boxCenter = segmentation.computeCentroid(wireframe)

        t = transformUtils.getTransformFromAxes(axes[0], axes[1], axes[2])
        t.PostMultiply()
        t.Translate(boxCenter)

        pd = filterUtils.transformPolyData(pd, t.GetLinearInverse())
        wireframe = filterUtils.transformPolyData(wireframe,
                                                  t.GetLinearInverse())

        return FieldContainer(points=pd,
                              box=wireframe,
                              frame=t,
                              dims=edgeLengths,
                              axes=axes)