def GenerateMould(self, facePolydata, distanceFromMask, ROI, tubePolydata, clippedFace):
        ROIExtents = self.utility.GetROIExtents(ROI)
        outerFaceImplicitFunction = vtk.vtkImplicitModeller()
        outerFaceImplicitFunction.SetInputConnection(facePolydata.GetOutputPort())
        outerFaceImplicitFunction.SetSampleDimensions(70, 70, 70)
        outerFaceImplicitFunction.SetMaximumDistance(distanceFromMask)
        outerFaceImplicitFunction.SetModelBounds(ROIExtents)
        outerFaceImplicitFunction.SetProcessModeToPerVoxel()
        outerMaskContourFunction = vtk.vtkContourFilter()
        # TODO Find out the the allowed thinkness of the plastic
        THICKNESS = 3
        outerMaskContourFunction.SetValue(0, THICKNESS)
        outerMaskContourFunction.SetInputConnection(outerFaceImplicitFunction.GetOutputPort())

        outerFaceNormalsFunction = vtk.vtkPolyDataNormals()
        outerFaceNormalsFunction.AutoOrientNormalsOn()
        outerFaceNormalsFunction.AddInputConnection(outerMaskContourFunction.GetOutputPort())
        # Subtract the Tubes from the mask
        subtractionFilter1 = vtk.vtkBooleanOperationPolyDataFilter()
        subtractionFilter1.SetOperationToDifference()
        subtractionFilter1.SetInputConnection(0, outerFaceNormalsFunction.GetOutputPort())
        subtractionFilter1.SetInputConnection(1, tubePolydata.GetOutputPort())
        # Subtract the inner face from the outer face to make the mask
        subtractionFilter2 = vtk.vtkBooleanOperationPolyDataFilter()
        subtractionFilter2.SetOperationToDifference()
        subtractionFilter2.SetInputConnection(0, subtractionFilter1.GetOutputPort())
        subtractionFilter2.SetInputConnection(1, clippedFace.GetOutputPort())
        x = self.utility.DisplayPolyData("Mask", subtractionFilter2.GetOutput())
        x.SetColor(0.8, 0.8, 0.8)
        x.SetOpacity(1)
    def polydataBoolean(self,
                        polyData1,
                        polyData2,
                        operation,
                        triangleFilter=False,
                        loop=False,
                        clean=True):
        # Subtract/add polyData2 from polyData1
        if (not polyData1) or (not polyData2):
            return polyData1

        booleanFilter = vtk.vtkBooleanOperationPolyDataFilter()
        if loop:
            booleanFilter = vtk.vtkLoopBooleanPolyDataFilter()

        if operation == "difference" or operation == "subtract":
            booleanFilter.SetOperationToDifference()
        elif operation == "union" or operation == "addition":
            booleanFilter.SetOperationToUnion()
        elif operation == "intersection":
            booleanFilter.SetOperationToIntersection()
        else:
            return None

        if triangleFilter:
            triangleFilter1 = vtk.vtkTriangleFilter()
            triangleFilter1.SetInputData(polyData1)
            triangleFilter1.Update()

            triangleFilter2 = vtk.vtkTriangleFilter()
            triangleFilter2.SetInputData(polyData2)
            triangleFilter2.Update()

            booleanFilter.SetInputData(0, triangleFilter1.GetOutput())
            booleanFilter.SetInputData(1, triangleFilter2.GetOutput())
        else:
            booleanFilter.SetInputData(0, polyData1)
            booleanFilter.SetInputData(1, polyData2)

        booleanFilter.Update()

        if clean:
            cleanFilter = vtk.vtkCleanPolyData()
            cleanFilter.SetInputData(booleanFilter.GetOutput())
            cleanFilter.PointMergingOn()
            cleanFilter.Update()
            print("blah")
            return cleanFilter.GetOutput()
        else:
            return booleanFilter.GetOutput()
    def GenerateMould(self, facePolydata, distanceFromMask, ROI, tubePolydata,
                      clippedFace):
        ROIExtents = self.utility.GetROIExtents(ROI)
        outerFaceImplicitFunction = vtk.vtkImplicitModeller()
        outerFaceImplicitFunction.SetInputConnection(
            facePolydata.GetOutputPort())
        outerFaceImplicitFunction.SetSampleDimensions(70, 70, 70)
        outerFaceImplicitFunction.SetMaximumDistance(distanceFromMask)
        outerFaceImplicitFunction.SetModelBounds(ROIExtents)
        outerFaceImplicitFunction.SetProcessModeToPerVoxel()
        outerMaskContourFunction = vtk.vtkContourFilter()
        #TODO Find out the the allowed thinkness of the plastic
        THICKNESS = 3
        outerMaskContourFunction.SetValue(0, THICKNESS)
        outerMaskContourFunction.SetInputConnection(
            outerFaceImplicitFunction.GetOutputPort())

        outerFaceNormalsFunction = vtk.vtkPolyDataNormals()
        outerFaceNormalsFunction.AutoOrientNormalsOn()
        outerFaceNormalsFunction.AddInputConnection(
            outerMaskContourFunction.GetOutputPort())
        # Subtract the Tubes from the mask
        subtractionFilter1 = vtk.vtkBooleanOperationPolyDataFilter()
        subtractionFilter1.SetOperationToDifference()
        subtractionFilter1.SetInputConnection(
            0, outerFaceNormalsFunction.GetOutputPort())
        subtractionFilter1.SetInputConnection(1, tubePolydata.GetOutputPort())
        # Subtract the inner face from the outer face to make the mask
        subtractionFilter2 = vtk.vtkBooleanOperationPolyDataFilter()
        subtractionFilter2.SetOperationToDifference()
        subtractionFilter2.SetInputConnection(
            0, subtractionFilter1.GetOutputPort())
        subtractionFilter2.SetInputConnection(1, clippedFace.GetOutputPort())
        x = self.utility.DisplayPolyData("Mask",
                                         subtractionFilter2.GetOutput())
        x.SetColor(0.8, 0.8, 0.8)
        x.SetOpacity(1)
    def AddTubeToMask(self, maskPolydata, tube, ROI, radius, ruler):
        ROIExtents = self.utility.GetROIExtents(ROI)
        implicitFilter = vtk.vtkImplicitModeller()
        implicitFilter.SetInput(tube)
        implicitFilter.SetSampleDimensions(70, 70, 70)
        implicitFilter.SetMaximumDistance(radius)
        implicitFilter.SetModelBounds(ROIExtents)
        implicitFilter.SetProcessModeToPerVoxel()
        contourFilter = vtk.vtkContourFilter()
        contourFilter.SetValue(0, radius)
        implicitFilter.AdjustBoundsOn()
        implicitFilter.SetAdjustBounds(1)  # Removes the boundary effects
        implicitFilter.CappingOff()
        contourFilter.SetInputConnection(implicitFilter.GetOutputPort())
        normalsFunction = vtk.vtkPolyDataNormals()
        normalsFunction.AutoOrientNormalsOn()
        normalsFunction.AddInputConnection(contourFilter.GetOutputPort())

        unionFilter = vtk.vtkBooleanOperationPolyDataFilter()
        unionFilter.SetOperationToUnion()
        unionFilter.SetInput(0, normalsFunction.GetOutput())
        unionFilter.SetInput(1, maskPolydata)
        return unionFilter.GetOutput()
    def AddTubeToMask(self, maskPolydata, tube, ROI, radius, ruler):
        ROIExtents = self.utility.GetROIExtents(ROI)
        implicitFilter = vtk.vtkImplicitModeller()
        implicitFilter.SetInput(tube)
        implicitFilter.SetSampleDimensions(70, 70, 70)
        implicitFilter.SetMaximumDistance(radius)
        implicitFilter.SetModelBounds(ROIExtents)
        implicitFilter.SetProcessModeToPerVoxel()
        contourFilter = vtk.vtkContourFilter()
        contourFilter.SetValue(0, radius)
        implicitFilter.AdjustBoundsOn()
        implicitFilter.SetAdjustBounds(1)  # Removes the boundary effects
        implicitFilter.CappingOff()
        contourFilter.SetInputConnection(implicitFilter.GetOutputPort())
        normalsFunction = vtk.vtkPolyDataNormals()
        normalsFunction.AutoOrientNormalsOn()
        normalsFunction.AddInputConnection(contourFilter.GetOutputPort())

        unionFilter = vtk.vtkBooleanOperationPolyDataFilter()
        unionFilter.SetOperationToUnion()
        unionFilter.SetInput(0, normalsFunction.GetOutput())
        unionFilter.SetInput(1, maskPolydata)
        return unionFilter.GetOutput()