Example #1
0
def text(txt,
         pos=(0, 0, 0),
         normal=(0, 0, 1),
         s=1,
         depth=0.1,
         justify='bottom-left',
         c='k',
         alpha=1,
         bc=None,
         texture=None,
         followcam=False,
         cam=None):
    '''
    Returns a ``vtkActor`` that shows a 3D text.

    :param pos: position in 3D space, 
                if an integer is passed [1,8], place text in one of the 4 corners.
    :type pos: list, int
    :param float s: size of text.
    :param float depth: text thickness.
    :param str justify: text justification (bottom-left, bottom-right, top-left, top-right, centered).
    :param bool followcam: if `True` the text will auto-orient itself to the cam.

    .. hint:: Example: `colorcubes.py <https://github.com/marcomusy/vtkplotter/blob/master/examples/basic/colorcubes.py>`_
    
        .. image:: https://user-images.githubusercontent.com/32848391/50738867-c0658e80-11d8-11e9-9e05-ac69b546b7ec.png
    '''
    if isinstance(pos, int):
        cornerAnnotation = vtk.vtkCornerAnnotation()
        cornerAnnotation.SetNonlinearFontScaleFactor(s / 3)
        cornerAnnotation.SetText(pos - 1, str(txt))
        cornerAnnotation.GetTextProperty().SetColor(colors.getColor(c))
        return cornerAnnotation

    tt = vtk.vtkVectorText()
    tt.SetText(str(txt))
    tt.Update()
    ttmapper = vtk.vtkPolyDataMapper()
    if followcam:
        depth = 0
        normal = (0, 0, 1)
    if depth:
        extrude = vtk.vtkLinearExtrusionFilter()
        extrude.SetInputConnection(tt.GetOutputPort())
        extrude.SetExtrusionTypeToVectorExtrusion()
        extrude.SetVector(0, 0, 1)
        extrude.SetScaleFactor(depth)
        ttmapper.SetInputConnection(extrude.GetOutputPort())
    else:
        ttmapper.SetInputConnection(tt.GetOutputPort())
    if followcam:
        ttactor = vtk.vtkFollower()
        ttactor.SetCamera(cam)
    else:
        ttactor = Actor()
    ttactor.SetMapper(ttmapper)
    ttactor.GetProperty().SetColor(colors.getColor(c))
    ttmapper.Update()

    bb = tt.GetOutput().GetBounds()
    dx, dy = (bb[1] - bb[0]) / 2 * s, (bb[3] - bb[2]) / 2 * s
    cm = np.array([(bb[1] + bb[0]) / 2, (bb[3] + bb[2]) / 2,
                   (bb[5] + bb[4]) / 2]) * s
    shift = -cm
    if 'cent' in justify:
        pass
    elif 'bottom-left' in justify:
        shift += np.array([dx, dy, 0])
    elif 'top-left' in justify:
        shift += np.array([dx, -dy, 0])
    elif 'bottom-right' in justify:
        shift += np.array([-dx, dy, 0])
    elif 'top-right' in justify:
        shift += np.array([-dx, -dy, 0])
    else:
        colors.printc("text(): Unknown justify type", justify, c=1)

    # check if color string contains a float, in this case ignore alpha
    al = colors._getAlpha(c)
    if al:
        alpha = al
    ttactor.GetProperty().SetOpacity(alpha)

    nax = np.linalg.norm(normal)
    if nax:
        normal = np.array(normal) / nax
    theta = np.arccos(normal[2])
    phi = np.arctan2(normal[1], normal[0])
    ttactor.SetScale(s, s, s)
    ttactor.RotateZ(phi * 57.3)
    ttactor.RotateY(theta * 57.3)
    ttactor.SetPosition(pos + shift)
    if bc:  # defines a specific color for the backface
        backProp = vtk.vtkProperty()
        backProp.SetDiffuseColor(colors.getColor(bc))
        backProp.SetOpacity(alpha)
        ttactor.SetBackfaceProperty(backProp)
    if texture:
        ttactor.texture(texture)
    ttactor.PickableOff()
    return ttactor
Example #2
0
def Text(
    txt,
    pos=3,
    normal=(0, 0, 1),
    s=1,
    depth=0.1,
    justify="bottom-left",
    c=None,
    alpha=1,
    bc=None,
    bg=None,
    font="courier",
    followcam=False,
):
    """
    Returns a ``vtkActor`` that shows a 3D text.

    :param pos: position in 3D space,
                if an integer is passed [1,8],
                a 2D text is placed in one of the 4 corners:

                    1, bottom-left
                    2, bottom-right
                    3, top-left
                    4, top-right
                    5, bottom-middle
                    6, middle-right
                    7, middle-left
                    8, top-middle

    :type pos: list, int
    :param float s: size of text.
    :param float depth: text thickness.
    :param str justify: text justification
        (bottom-left, bottom-right, top-left, top-right, centered).
    :param bg: background color of corner annotations. Only applies of `pos` is ``int``.
    :param str font: either `arial`, `courier` or `times`. Only applies of `pos` is ``int``.
    :param followcam: if `True` the text will auto-orient itself to the active camera.
        A ``vtkCamera`` object can also be passed.
    :type followcam: bool, vtkCamera

    .. hint:: |colorcubes.py|_ |markpoint.py|_ |annotations.py|_

        |colorcubes| |markpoint|
    """
    if c is None:  # automatic black or white
        if settings.plotter_instance and settings.plotter_instance.renderer:
            c = (0.9, 0.9, 0.9)
            if np.sum(
                    settings.plotter_instance.renderer.GetBackground()) > 1.5:
                c = (0.1, 0.1, 0.1)
        else:
            c = (0.6, 0.6, 0.6)

    if isinstance(pos, int):
        if pos > 8:
            pos = 8
        if pos < 1:
            pos = 1

        ca = vtk.vtkCornerAnnotation()
        ca.SetNonlinearFontScaleFactor(s / 2.7)
        ca.SetText(pos - 1, str(txt))

        ca.PickableOff()
        cap = ca.GetTextProperty()
        cap.SetColor(colors.getColor(c))
        if font.lower() == "courier":
            cap.SetFontFamilyToCourier()
        elif font.lower() == "times":
            cap.SetFontFamilyToTimes()
        else:
            cap.SetFontFamilyToArial()
        if bg:
            bgcol = colors.getColor(bg)
            cap.SetBackgroundColor(bgcol)
            cap.SetBackgroundOpacity(alpha * 0.5)
            cap.SetFrameColor(bgcol)
            cap.FrameOn()

        setattr(ca, 'renderedAt', set())
        settings.collectable_actors.append(ca)
        return ca

    tt = vtk.vtkVectorText()
    tt.SetText(str(txt))
    tt.Update()
    ttmapper = vtk.vtkPolyDataMapper()
    if followcam:
        depth = 0
        normal = (0, 0, 1)
    if depth:
        extrude = vtk.vtkLinearExtrusionFilter()
        extrude.SetInputConnection(tt.GetOutputPort())
        extrude.SetExtrusionTypeToVectorExtrusion()
        extrude.SetVector(0, 0, 1)
        extrude.SetScaleFactor(depth)
        ttmapper.SetInputConnection(extrude.GetOutputPort())
    else:
        ttmapper.SetInputConnection(tt.GetOutputPort())
    if followcam:
        ttactor = vtk.vtkFollower()
        if isinstance(followcam, vtk.vtkCamera):
            ttactor.SetCamera(followcam)
        else:
            ttactor.SetCamera(settings.plotter_instance.camera)
    else:
        ttactor = Actor()
    ttactor.SetMapper(ttmapper)
    ttactor.GetProperty().SetColor(colors.getColor(c))
    ttmapper.Update()

    bb = tt.GetOutput().GetBounds()
    dx, dy = (bb[1] - bb[0]) / 2 * s, (bb[3] - bb[2]) / 2 * s
    cm = np.array([(bb[1] + bb[0]) / 2, (bb[3] + bb[2]) / 2,
                   (bb[5] + bb[4]) / 2]) * s
    shift = -cm
    if "cent" in justify:
        pass
    elif "bottom-left" in justify:
        shift += np.array([dx, dy, 0])
    elif "top-left" in justify:
        shift += np.array([dx, -dy, 0])
    elif "bottom-right" in justify:
        shift += np.array([-dx, dy, 0])
    elif "top-right" in justify:
        shift += np.array([-dx, -dy, 0])
    else:
        colors.printc("~lightning Text(): Unknown justify type", justify, c=1)

    ttactor.GetProperty().SetOpacity(alpha)

    nax = np.linalg.norm(normal)
    if nax:
        normal = np.array(normal) / nax
    theta = np.arccos(normal[2])
    phi = np.arctan2(normal[1], normal[0])
    ttactor.SetScale(s, s, s)
    ttactor.RotateZ(np.rad2deg(phi))
    ttactor.RotateY(np.rad2deg(theta))
    ttactor.SetPosition(pos + shift)
    if bc:  # defines a specific color for the backface
        backProp = vtk.vtkProperty()
        backProp.SetDiffuseColor(colors.getColor(bc))
        backProp.SetOpacity(alpha)
        ttactor.SetBackfaceProperty(backProp)
    ttactor.PickableOff()
    settings.collectable_actors.append(ttactor)
    return ttactor
Example #3
0
def text(txt,
         pos=(0, 0, 0),
         normal=(0, 0, 1),
         s=1,
         depth=0.1,
         c='k',
         alpha=1,
         bc=None,
         texture=None,
         followcam=False,
         cam=None):
    '''
    Returns a vtkActor that shows a text in 3D.

    Options:

        pos = position in 3D space,
              if an integer is passed [1 -> 8], places text in one of the 4 corners

        s = size of text 

        depth = text thickness

        followcam = False, if True the text will auto-orient itself to it.

    [**Example1**](https://github.com/marcomusy/vtkplotter/blob/master/examples/basic/colorcubes.py)    
    [**Example2**](https://github.com/marcomusy/vtkplotter/blob/master/examples/basic/mesh_coloring.py)    
    '''
    if isinstance(pos, int):
        cornerAnnotation = vtk.vtkCornerAnnotation()
        cornerAnnotation.SetNonlinearFontScaleFactor(s / 3)
        cornerAnnotation.SetText(pos - 1, str(txt))
        cornerAnnotation.GetTextProperty().SetColor(colors.getColor(c))
        return cornerAnnotation

    tt = vtk.vtkVectorText()
    tt.SetText(str(txt))
    tt.Update()
    ttmapper = vtk.vtkPolyDataMapper()
    if followcam:
        depth = 0
        normal = (0, 0, 1)
    if depth:
        extrude = vtk.vtkLinearExtrusionFilter()
        extrude.SetInputConnection(tt.GetOutputPort())
        extrude.SetExtrusionTypeToVectorExtrusion()
        extrude.SetVector(0, 0, 1)
        extrude.SetScaleFactor(depth)
        ttmapper.SetInputConnection(extrude.GetOutputPort())
    else:
        ttmapper.SetInputConnection(tt.GetOutputPort())
    if followcam:
        ttactor = vtk.vtkFollower()
        ttactor.SetCamera(cam)
    else:
        ttactor = Actor()
    ttactor.SetMapper(ttmapper)
    ttactor.GetProperty().SetColor(colors.getColor(c))

    # check if color string contains a float, in this case ignore alpha
    al = colors.getAlpha(c)
    if al:
        alpha = al
    ttactor.GetProperty().SetOpacity(alpha)

    nax = np.linalg.norm(normal)
    if nax:
        normal = np.array(normal) / nax
    theta = np.arccos(normal[2])
    phi = np.arctan2(normal[1], normal[0])
    ttactor.SetScale(s, s, s)
    ttactor.RotateZ(phi * 57.3)
    ttactor.RotateY(theta * 57.3)
    ttactor.SetPosition(pos)
    if bc:  # defines a specific color for the backface
        backProp = vtk.vtkProperty()
        backProp.SetDiffuseColor(colors.getColor(bc))
        backProp.SetOpacity(alpha)
        ttactor.SetBackfaceProperty(backProp)
    if texture:
        ttactor.texture(texture)
    return ttactor