Skip to content

CameraModel2

vtk-examples/PythonicAPI/Visualization/CameraModel2

Description

This example illustrates the camera movement centered at the camera position.

Note

For camera movement around the focal point see CameraModel1.

Note

This is an adaptation of the code written by Chung Kai Lun Pete.

Other languages

See (Cxx), (Python)

Question

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

Code

CameraModel2.py

#!/usr/bin/env python3

from dataclasses import dataclass

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingFreeType
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import (
    vtkCellArray,
    vtkPolyData
)
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkFiltersCore import (
    vtkAppendPolyData,
    vtkContourFilter
)
from vtkmodules.vtkFiltersGeneral import (
    vtkTransformFilter,
    vtkTransformPolyDataFilter,
    vtkWarpTo
)
from vtkmodules.vtkFiltersHybrid import vtkImplicitModeller
from vtkmodules.vtkFiltersModeling import vtkRotationalExtrusionFilter
from vtkmodules.vtkFiltersSources import (
    vtkConeSource,
    vtkCubeSource,
    vtkSphereSource
)
from vtkmodules.vtkRenderingCore import (
    vtkDataSetMapper,
    vtkProperty,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
    vtkTextActor,
    vtkTextProperty
)
from vtkmodules.vtkRenderingLOD import vtkLODActor


def main():
    colors = vtkNamedColors()

    # Set the colors.
    colors.SetColor("AzimuthArrowColor", 255, 77, 77, 255)
    colors.SetColor("YawArrowColor", 255, 77, 77, 255)
    colors.SetColor("ElevationArrowColor", 77, 255, 77, 255)
    colors.SetColor("PitchArrowColor", 77, 255, 77, 255)
    colors.SetColor("RollArrowColor", 255, 255, 77, 255)
    colors.SetColor("SpikeColor", 255, 77, 255, 255)
    colors.SetColor("UpSpikeColor", 77, 255, 255, 255)

    # Create a rendering window, renderer and interactor.
    ren = vtkRenderer(background=colors.GetColor3d('SlateGray'))
    ren_win = vtkRenderWindow(size=(640, 480), window_name='CameraModel2')
    ren_win.AddRenderer(ren)
    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(ren_win)

    # Create a camera model.
    cam_cs = vtkConeSource(height=1.5, resolution=12, radius=0.4)
    cam_cbs = vtkCubeSource(center=(0.4, 0, 0), x_length=1.5, z_length=0.8)
    cam_apd = vtkAppendPolyData()
    cam_mapper = vtkPolyDataMapper()
    (cam_cbs, cam_cs) >> cam_apd >> cam_mapper
    cam_actor = vtkLODActor(mapper=cam_mapper, scale=(2, 2, 2))

    # Draw the arrows.
    pd = vtkPolyData()
    ca = vtkCellArray()
    pts = vtkPoints()
    pts.InsertNextPoint(0, 1, 0)
    pts.InsertNextPoint(8, 1, 0)
    pts.InsertNextPoint(8, 2, 0)
    pts.InsertNextPoint(10, 0, 0)
    pts.InsertNextPoint(8, -2, 0)
    pts.InsertNextPoint(8, -1, 0)
    pts.InsertNextPoint(0, -1, 0)
    ca.InsertNextCell(7)
    ca.InsertCellPoint(0)
    ca.InsertCellPoint(1)
    ca.InsertCellPoint(2)
    ca.InsertCellPoint(3)
    ca.InsertCellPoint(4)
    ca.InsertCellPoint(5)
    ca.InsertCellPoint(6)
    pd.SetPoints(pts)
    pd.SetPolys(ca)

    pd2 = vtkPolyData()
    ca2 = vtkCellArray()
    pts2 = vtkPoints()
    pts2.InsertNextPoint(0, 1, 0)
    pts2.InsertNextPoint(8, 1, 0)
    pts2.InsertNextPoint(8, 2, 0)
    pts2.InsertNextPoint(10, 0.01, 0)
    ca2.InsertNextCell(4)
    ca2.InsertCellPoint(0)
    ca2.InsertCellPoint(1)
    ca2.InsertCellPoint(2)
    ca2.InsertCellPoint(3)
    pd2.SetPoints(pts2)
    pd2.SetLines(ca2)

    arrow_im = vtkImplicitModeller(input_data=pd, sample_dimensions=(50, 20, 8))
    arrow_cf = vtkContourFilter()
    arrow_cf.SetValue(0, 0.2)
    arrow_wt = vtkWarpTo(position=(5, 0, 5), scale_factor=0.85, absolute=True)

    arrow_t = vtkTransform()
    arrow_t.RotateY(60)
    arrow_t.Translate(-1.33198, 0, -1.479)
    arrow_t.Scale(1, 0.5, 1)

    arrow_tf = vtkTransformFilter(transform=arrow_t)

    arrow_mapper = vtkDataSetMapper(scalar_visibility=False)
    arrow_im >> arrow_cf >> arrow_wt >> arrow_tf >> arrow_mapper

    # Draw the yaw arrows.
    arrow1_property = vtkProperty(color=colors.GetColor3d('YawArrowColor'),
                                  specular_color=colors.GetColor3d('White'),
                                  specular=0.3, specular_power=20, ambient=0.2, diffuse=0.8)
    a1_actor = vtkLODActor(mapper=arrow_mapper, position=(-9, 0, -1), property=arrow1_property)
    a2_actor = vtkLODActor(mapper=arrow_mapper, position=(-9, 0, 1), property=arrow1_property)
    a2_actor.RotateX(180)

    # Draw the pitch arrows.
    arrow2_property = vtkProperty(color=colors.GetColor3d('PitchArrowColor'),
                                  specular_color=colors.GetColor3d('White'),
                                  specular=0.3, specular_power=20, ambient=0.2, diffuse=0.8)
    a3_actor = vtkLODActor(mapper=arrow_mapper, position=(-9, -1, 0), property=arrow2_property)
    a3_actor.RotateX(-90)
    a4_actor = vtkLODActor(mapper=arrow_mapper, position=(-9, 1, 0), property=arrow2_property)
    a4_actor.RotateX(90)

    # Draw the DOP.
    arrow_t2 = vtkTransform()
    arrow_t2.Scale(1, 0.6, 1)
    arrow_t2.RotateY(90)
    arrow_tf2 = vtkTransformPolyDataFilter(transform=arrow_t2)
    arrow_ref = vtkRotationalExtrusionFilter(capping=False, resolution=30)
    spike_mapper = vtkPolyDataMapper()
    pd2 >> arrow_tf2 >> arrow_ref >> spike_mapper
    spike_property = vtkProperty(color=colors.GetColor3d('SpikeColor'),
                                 specular_color=colors.GetColor3d('White'),
                                 specular=0.3, specular_power=20, ambient=0.2, diffuse=0.8)
    a5_actor = vtkLODActor(mapper=spike_mapper, scale=(0.3, 0.3, 0.6), position=(-2, 0, 0), property=spike_property)
    a5_actor.RotateY(90)
    a5_actor.SetPosition(-2, 0, 0)

    up_spike_property = vtkProperty(color=colors.GetColor3d('UpSpikeColor'),
                                    specular_color=colors.GetColor3d('White'),
                                    specular=0.3, specular_power=20, ambient=0.2, diffuse=0.8)
    a7_actor = vtkLODActor(mapper=spike_mapper, scale=(0.2, 0.2, 0.7), position=(-9, 0, 0), property=up_spike_property)
    a7_actor.RotateZ(90)
    a7_actor.RotateY(-90)

    # Focal point.
    fps = vtkSphereSource(radius=0.5)
    fps.SetRadius(0.5)
    fp_mapper = vtkPolyDataMapper()
    fps >> fp_mapper
    fp_property = vtkProperty(color=colors.GetColor3d('White'),
                              specular_color=colors.GetColor3d('White'),
                              specular=0.3, specular_power=20, ambient=0.2, diffuse=0.8)
    fp_actor = vtkLODActor(mapper=fp_mapper, scale=(1, 1, 1), position=(-9, 0, 0), property=fp_property)

    # Create the roll arrow.
    arrow_wt2 = vtkWarpTo(position=(5, 0, 2.5), scale_factor=0.95, absolute=True)

    arrow_t3 = vtkTransform()
    arrow_t3.Translate(-2.50358, 0, -1.70408)
    arrow_t3.Scale(0.5, 0.3, 1)

    arrow_tf3 = vtkTransformFilter(transform=arrow_t3)

    arrow_mapper2 = vtkDataSetMapper(scalar_visibility=False)
    arrow_im >> arrow_cf >> arrow_wt2 >> arrow_tf3 >> arrow_mapper2

    # Draw the roll arrow.
    arrow6_property = vtkProperty(color=colors.GetColor3d('RollArrowColor'),
                                  specular_color=colors.GetColor3d('White'),
                                  specular=0.3, specular_power=20, ambient=0.2, diffuse=0.8)
    a6_actor = vtkLODActor(mapper=arrow_mapper2, scale=(1.5, 1.5, 1.5), position=(-4, 0, 0), property=arrow6_property)
    a6_actor.RotateZ(90)

    # Add the actors to the renderer, set the background and size.
    ren.AddActor(cam_actor)
    ren.AddActor(a1_actor)
    ren.AddActor(a2_actor)
    ren.AddActor(a3_actor)
    ren.AddActor(a4_actor)
    ren.AddActor(a5_actor)
    ren.AddActor(a6_actor)
    ren.AddActor(a7_actor)
    ren.AddActor(fp_actor)
    ren.SetBackground(colors.GetColor3d("SlateGray"))

    # Render the image.

    cam1 = (ren.active_camera)
    ren.ResetCamera()
    cam1.Azimuth(150)
    cam1.Elevation(30)
    cam1.Dolly(1.5)
    ren.ResetCameraClippingRange()

    # Create a TextActor for Yaw  (a1 and a2 actor's color).
    text = get_text_actor('Yaw', (20, 50), a1_actor.property.color)
    ren.AddActor2D(text)

    # Create a TextActor for Pitch  (a3 and a4 actor's color).
    text2 = get_text_actor('Pitch', (20, 100), a3_actor.property.color)
    ren.AddActor2D(text2)

    # Create a TextActor for roll (a6 actor's color).
    text3 = get_text_actor('Roll', (20, 150), a6_actor.property.color)
    ren.AddActor2D(text3)

    iren.Initialize()
    iren.Start()


def get_text_actor(text_str, position, color):
    tprop = vtkTextProperty(font_family=TextProperty.FontFamily.VTK_ARIAL, shadow=False, line_spacing=1.0, font_size=36,
                            color=color)
    return vtkTextActor(input=text_str, display_position=position, text_property=tprop)


@dataclass(frozen=True)
class TextProperty:
    @dataclass(frozen=True)
    class FontFamily:
        VTK_ARIAL: int = 0
        VTK_COURIER: int = 1
        VTK_TIMES: int = 2
        VTK_UNKNOWN_FONT: int = 3

    @dataclass(frozen=True)
    class Justification:
        VTK_TEXT_LEFT: int = 0
        VTK_TEXT_CENTERED: int = 1
        VTK_TEXT_RIGHT: int = 2

    @dataclass(frozen=True)
    class VerticalJustification:
        VTK_TEXT_BOTTOM: int = 0
        VTK_TEXT_CENTERED: int = 1
        VTK_TEXT_TOP: int = 2


if __name__ == "__main__":
    main()