VTK学习笔记(二十九)Python vtkClipClosedSurface示例

1、Python vtkClipClosedSurface示例

Python vtkClipClosedSurface - 已找到6个示例。这些是从开源项目中提取的最受好评的vtk.vtkClipClosedSurface现实Python示例。

1.1、文件: vtkOperations.py 项目: avontd2868/OU_Final_Year

 def opImplement(self):
     clipper = vtk.vtkClipClosedSurface();
     clipper.TriangulationErrorDisplayOn();
     clipper.SetInputData(self.data);
     capPlanes = vtk.vtkPlaneCollection()
     firstclipPlane = vtk.vtkPlane();
     #We should change following 2 lines only
     #print("Plane normal : {}".format(self.normal))
     firstclipPlane.SetNormal(*self.normal);
     firstclipPlane.SetOrigin(*self.origin);
     #End possible changes
     capPlanes.AddItem(firstclipPlane)
     clipper.SetClippingPlanes(capPlanes);
     clipper.Update();
     resultOne = clipper.GetOutput();
     return resultOne;

1.2、文件: TestClipClosedSurface.py 项目: 0004c/VTK

iso.SetValue(0,1150)
iso.SetInputMemoryLimit(1000)
topPlane = vtk.vtkPlane()
topPlane.SetNormal(0,0,1)
topPlane.SetOrigin(0,0,0.5)
botPlane = vtk.vtkPlane()
botPlane.SetNormal(0,0,-1)
botPlane.SetOrigin(0,0,137.0)
sagPlane = vtk.vtkPlane()
sagPlane.SetNormal(1,0,0)
sagPlane.SetOrigin(100.8,0,0)
capPlanes = vtk.vtkPlaneCollection()
capPlanes.AddItem(topPlane)
capPlanes.AddItem(botPlane)
capPlanes.AddItem(sagPlane)
clip = vtk.vtkClipClosedSurface()
clip.SetClippingPlanes(capPlanes)
clip.SetInputConnection(iso.GetOutputPort())
clip.SetBaseColor(0.9804,0.9216,0.8431)
clip.SetClipColor(1.0,1.0,1.0)
clip.SetActivePlaneColor(1.0,1.0,0.8)
clip.SetActivePlaneId(2)
clip.SetScalarModeToColors()
clip.GenerateOutlineOn()
clip.GenerateFacesOn()
isoMapper = vtk.vtkPolyDataMapper()
isoMapper.SetInputConnection(clip.GetOutputPort())
isoMapper.ScalarVisibilityOn()
isoMapper.ImmediateModeRenderingOn()
isoActor = vtk.vtkActor()
isoActor.SetMapper(isoMapper)

1.3、

volumeMapper1.SetClippingPlanes(clippingPlanes)

volume1 = vtk.vtkVolume()
volume1.SetMapper(volumeMapper1)
volume1.SetProperty(volumeProperty)

outline1 = vtk.vtkVolumeOutlineSource()
outline1.SetVolumeMapper(volumeMapper1)
outline1.GenerateFacesOn()
outline1.GenerateScalarsOn()

preTrans1 = vtk.vtkTransformPolyDataFilter()
preTrans1.SetInputConnection(outline1.GetOutputPort())
preTrans1.SetTransform(userTrans)

outlineClip1 = vtk.vtkClipClosedSurface()
outlineClip1.SetInputConnection(preTrans1.GetOutputPort())
outlineClip1.SetClippingPlanes(clippingPlanes)
outlineClip1.GenerateFacesOff()
outlineClip1.GenerateOutlineOn()
outlineClip1.SetScalarModeToColors()
outlineClip1.SetClipColor(1, 1, 0)
outlineClip1.SetActivePlaneId(2)
outlineClip1.SetActivePlaneColor(0, 1, 0)

postTrans1 = vtk.vtkTransformPolyDataFilter()
postTrans1.SetInputConnection(outlineClip1.GetOutputPort())
postTrans1.SetTransform(userTrans.GetInverse())

outlineMapper1 = vtk.vtkDataSetMapper()
outlineMapper1.SetInputConnection(postTrans1.GetOutputPort())

1.4、文件: TestMNIObjects.py 项目: timkrentz/SunTracker

renWin.SetSize(600, 200)
    renWin.AddRenderer(ren1)
    renWin.AddRenderer(ren2)
    renWin.AddRenderer(ren3)
    renWin.SetMultiSamples(0)
    property0 = vtk.vtkProperty()
    property0.SetDiffuseColor(0.95, 0.90, 0.70)
    filename = VTK_DATA_ROOT + "/Data/mni-surface-mesh.obj"
    asciiReader = vtk.vtkMNIObjectReader()
    property1 = asciiReader.GetProperty()
    if (asciiReader.CanReadFile(str(filename)) != 0):
        asciiReader.SetFileName(str(filename))

        # this is just to remove the normals, to increase coverage,
        # i.e. by forcing the writer to generate normals
        removeNormals = vtk.vtkClipClosedSurface()
        removeNormals.SetInputConnection(asciiReader.GetOutputPort())

        # this is to make triangle strips, also to increase coverage,
        # because it forces the writer to decompose the strips
        stripper = vtk.vtkStripper()
        stripper.SetInputConnection(removeNormals.GetOutputPort())

        # test binary writing and reading for polygons
        binaryWriter = vtk.vtkMNIObjectWriter()
        binaryWriter.SetInputConnection(stripper.GetOutputPort())
        binaryWriter.SetFileName("mni-surface-mesh-binary.obj")
        binaryWriter.SetProperty(property0)
        binaryWriter.SetFileTypeToBinary()
        binaryWriter.Write()

1.5、文件: TestClipOutline.py 项目: ElsevierSoftwareX/SOFTX-D-15-00004

sphere.SetRadius(1)
sphere.SetPhiResolution(10)
sphere.SetThetaResolution(10)
plane1 = vtk.vtkPlane()
plane1.SetOrigin(0.3,0.3,0.3)
plane1.SetNormal(-1,-1,-1)
plane2 = vtk.vtkPlane()
plane2.SetOrigin(0.5,0,0)
plane2.SetNormal(-1,0,0)
planes = vtk.vtkPlaneCollection()
planes.AddItem(plane1)
planes.AddItem(plane2)
# stripper just increases coverage
stripper = vtk.vtkStripper()
stripper.SetInputConnection(sphere.GetOutputPort())
clipper = vtk.vtkClipClosedSurface()
clipper.SetInputConnection(stripper.GetOutputPort())
clipper.SetClippingPlanes(planes)
clipperOutline = vtk.vtkClipClosedSurface()
clipperOutline.SetInputConnection(stripper.GetOutputPort())
clipperOutline.SetClippingPlanes(planes)
clipperOutline.GenerateFacesOff()
clipperOutline.GenerateOutlineOn()
sphereMapper = vtk.vtkPolyDataMapper()
sphereMapper.SetInputConnection(clipper.GetOutputPort())
clipperOutlineMapper = vtk.vtkPolyDataMapper()
clipperOutlineMapper.SetInputConnection(clipperOutline.GetOutputPort())
clipActor = vtk.vtkActor()
clipActor.SetMapper(sphereMapper)
clipActor.GetProperty().SetColor(0.8,0.05,0.2)
clipOutlineActor = vtk.vtkActor()

1.6、文件: preprocessor.py 项目: go-smart/goosefoot

def __init__(self, runname, input_file, output_file, radius, x, y, z):
        self.runname = runname

        reader = vtk.vtkPolyDataReader()
        reader.SetFileName(input_file)
        reader.Update()

        planes = vtk.vtkPlaneCollection()

        top = vtk.vtkPlane()
        top.SetNormal(0, 0, -1)
        top.SetOrigin(x, y, z + radius)
        planes.AddItem(top)

        bottom = vtk.vtkPlane()
        bottom.SetNormal(0, 0, 1)
        bottom.SetOrigin(x, y, z - radius)
        planes.AddItem(bottom)

        front = vtk.vtkPlane()
        front.SetNormal(0, -1, 0)
        front.SetOrigin(x, y + radius, z)
        planes.AddItem(front)

        back = vtk.vtkPlane()
        back.SetNormal(0, 1, 0)
        back.SetOrigin(x, y - radius, z)
        planes.AddItem(back)

        right = vtk.vtkPlane()
        right.SetNormal(1, 0, 0)
        right.SetOrigin(x - radius, y, z)
        planes.AddItem(right)

        left = vtk.vtkPlane()
        left.SetNormal(-1, 0, 0)
        left.SetOrigin(x + radius, y, z)
        planes.AddItem(left)

        clipper = vtk.vtkClipClosedSurface()
        clipper.SetClippingPlanes(planes)
        clipper.SetInputConnection(reader.GetOutputPort())

        connectivity = vtk.vtkPolyDataConnectivityFilter()
        connectivity.SetExtractionModeToAllRegions()
        connectivity.SetInputConnection(clipper.GetOutputPort())
        connectivity.Update()

        specified = vtk.vtkPolyDataConnectivityFilter()
        specified.SetExtractionModeToSpecifiedRegions()
        specified.SetInputConnection(clipper.GetOutputPort())
        tester = vtk.vtkPolyDataConnectivityFilter()
        tester.SetExtractionModeToSpecifiedRegions()
        tester.SetInputConnection(clipper.GetOutputPort())

        dims = {
    
    }
        for i in range(connectivity.GetNumberOfExtractedRegions()):
            tester.AddSpecifiedRegion(i)
            tester.Update()
            bounds = tester.GetOutput().GetBounds()

            if bounds[0] + radius - delta < x or bounds[1] - radius + delta > x or \
               bounds[2] + radius - delta < y or bounds[3] - radius + delta > y or \
               bounds[4] + radius - delta < z or bounds[5] - radius + delta > z:
                    mindim = min(bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4])
                    dims[i] = mindim

            tester.DeleteSpecifiedRegion(i)

        for i in sorted(dims.iteritems(), key=lambda i: i[1], reverse=True)[0:5]:
            print i
            specified.AddSpecifiedRegion(i[0])

        #cleaner = vtk.vtkCleanPolyData()
        #cleaner.SetInputConnection(specified.GetOutputPort())
        #cleaner.SetTolerance(1e-5)

        #orientator = vtk.vtkPolyDataNormals()
        #orientator.SetInputConnection(clipper.GetOutputPort())
        #orientator.ConsistencyOn()
        #orientator.AutoOrientNormalsOn()

        writer = vtk.vtkSTLWriter()
        writer.SetFileName(output_file + 'tmp1.stl')
        writer.SetInputConnection(clipper.GetOutputPort())
        writer.Write()

        with open("/tmp/test.mlx", "w") as f:
            f.write("""
<FilterScript>
 <filter name="Remove Zero Area Faces"/>
 <filter name="Re-Orient all faces coherentely"/>
 <filter name="Quadric Edge Collapse Decimation">
  <Param type="RichInt" value="30000" name="TargetFaceNum"/>
  <Param type="RichFloat" value="0" name="TargetPerc"/>
  <Param type="RichFloat" value="0.3" name="QualityThr"/>
  <Param type="RichBool" value="false" name="PreserveBoundary"/>
  <Param type="RichBool" value="false" name="PreserveNormal"/>
  <Param type="RichBool" value="true" name="PreserveTopology"/>
  <Param type="RichBool" value="true" name="OptimalPlacement"/>
  <Param type="RichBool" value="false" name="PlanarQuadric"/>
  <Param type="RichBool" value="false" name="QualityWeight"/>
  <Param type="RichBool" value="true" name="AutoClean"/>
  <Param type="RichBool" value="false" name="Selected"/>
 </filter>
</FilterScript>
           """)
        subprocess.call(["meshlabserver", "-i", output_file + 'tmp1.stl', "-o", output_file + '.off', "-s", "/tmp/test.mlx"])

        #reader = vtk.vtkSTLReader()
        #reader.SetFileName(output_file + 'tmp2.stl')
        #reader.Update()

        writer = vtk.vtkSTLWriter()
        writer.SetFileName(output_file)
        writer.SetInputConnection(specified.GetOutputPort())
        writer.Write()

        vtk_writer = vtk.vtkXMLPolyDataWriter()
        vtk_writer.SetFileName(output_file + ".vtu")
        vtk_writer.SetInputConnection(specified.GetOutputPort())
        vtk_writer.Write()

猜你喜欢

转载自blog.csdn.net/juluwangriyue/article/details/124007010