Skip to content

TextureCutQuadric

Repository source: TextureCutQuadric

Description

Clip geometry using a 2D texture map and two implicit functions. The technique is described in : Geometric Clipping Using Boolean Textures.

Other languages

See (Cxx), (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

TextureCutQuadric.py

#!/usr/bin/env python

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonDataModel import vtkQuadric
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkFiltersTexture import vtkImplicitTextureCoords
from vtkmodules.vtkImagingHybrid import vtkBooleanTexture
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkDataSetMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
    vtkTexture
)

positions = [
    [-4, 4, 0], [-2, 4, 0], [0, 4, 0], [2, 4, 0],
    [-4, 2, 0], [-2, 2, 0], [0, 2, 0], [2, 2, 0],
    [-4, 0, 0], [-2, 0, 0], [0, 0, 0], [2, 0, 0],
    [-4, -2, 0], [-2, -2, 0], [0, -2, 0], [2, -2, 0]
]

solid = [255, 255]
clear = [255, 0]
edge = [0, 255]


def main():
    colors = vtkNamedColors()

    renWin = vtkRenderWindow()

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    aren = vtkRenderer()

    # define two elliptical cylinders
    quadric1 = vtkQuadric()
    quadric1.SetCoefficients(1, 2, 0, 0, 0, 0, 0, 0, 0, -.07)

    quadric2 = vtkQuadric()
    quadric2.SetCoefficients(2, 1, 0, 0, 0, 0, 0, 0, 0, -.07)

    # Create a sphere for all to use.
    aSphere = vtkSphereSource()
    aSphere.SetPhiResolution(21)
    aSphere.SetThetaResolution(21)

    # Create texture coordinates for all.
    tcoords = vtkImplicitTextureCoords()
    tcoords.SetInputConnection(aSphere.GetOutputPort())
    tcoords.SetRFunction(quadric1)
    tcoords.SetSFunction(quadric2)

    aMapper = vtkDataSetMapper()
    aMapper.SetInputConnection(tcoords.GetOutputPort())

    # Create a mapper, sphere and texture map for each case.
    for i in range(0, 16):
        aBoolean = MakeBooleanTexture(i, 64, 0)

        aTexture2 = vtkTexture()
        aTexture2.SetInputConnection(aBoolean.GetOutputPort())
        aTexture2.InterpolateOff()
        aTexture2.RepeatOff()

        anActor2 = vtkActor()

        anActor2.SetMapper(aMapper)
        anActor2.SetTexture(aTexture2)
        anActor2.SetPosition(positions[i])
        anActor2.SetScale(2.0, 2.0, 2.0)
        anActor2.GetProperty().SetColor(colors.GetColor3d('MistyRose'))
        aren.AddActor(anActor2)

    aren.SetBackground(colors.GetColor3d('SlateGray'))
    renWin.SetSize(500, 500)
    renWin.AddRenderer(aren)
    renWin.SetWindowName('TextureCutQuadric')

    # Interact with the data.
    renWin.Render()

    iren.Start()


def MakeBooleanTexture(caseNumber, resolution, thickness):
    booleanTexture = vtkBooleanTexture()

    booleanTexture.SetXSize(resolution)
    booleanTexture.SetYSize(resolution)
    booleanTexture.SetThickness(thickness)

    if caseNumber == 0:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(solid)
        booleanTexture.SetOnIn(solid)
        booleanTexture.SetOnOut(solid)
        booleanTexture.SetInOn(solid)
        booleanTexture.SetOutOn(solid)
    elif caseNumber == 1:  # cut inside 1
        booleanTexture.SetInIn(clear)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(solid)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(solid)
    elif caseNumber == 2:  # cut outside 1
        booleanTexture.SetInIn(solid)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(solid)
        booleanTexture.SetInOn(solid)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 3:  # cut all 1
        booleanTexture.SetInIn(clear)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(clear)
        booleanTexture.SetOnOut(solid)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 4:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(solid)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(solid)
    elif caseNumber == 5:
        booleanTexture.SetInIn(clear)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(clear)
        booleanTexture.SetOutOn(solid)
    elif caseNumber == 6:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 7:
        booleanTexture.SetInIn(clear)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutOut(solid)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(clear)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(clear)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 8:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(solid)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(solid)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 9:
        booleanTexture.SetInIn(clear)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 10:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(solid)
        booleanTexture.SetOutOn(clear)
    elif caseNumber == 11:
        booleanTexture.SetInIn(clear)
        booleanTexture.SetInOut(solid)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(clear)
        booleanTexture.SetOnOut(edge)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(clear)
    elif caseNumber == 12:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(solid)
        booleanTexture.SetOnOut(clear)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 13:
        booleanTexture.SetInIn(clear)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutIn(solid)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(clear)
        booleanTexture.SetInOn(clear)
        booleanTexture.SetOutOn(edge)
    elif caseNumber == 14:
        booleanTexture.SetInIn(solid)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(edge)
        booleanTexture.SetOnIn(edge)
        booleanTexture.SetOnOut(clear)
        booleanTexture.SetInOn(edge)
        booleanTexture.SetOutOn(clear)
    else:  # caseNumber ==  15:
        booleanTexture.SetInIn(clear)
        booleanTexture.SetInOut(clear)
        booleanTexture.SetOutIn(clear)
        booleanTexture.SetOutOut(clear)
        booleanTexture.SetOnOn(clear)
        booleanTexture.SetOnIn(clear)
        booleanTexture.SetOnOut(clear)
        booleanTexture.SetInOn(clear)
        booleanTexture.SetOutOn(clear)

    return booleanTexture


if __name__ == '__main__':
    main()