vedo icon indicating copy to clipboard operation
vedo copied to clipboard

The function moveCamera do not work with resetcam=self.resetcam

Open XushanLu opened this issue 2 years ago • 1 comments

Hi Marco,

I am still trying to create short videos with the Animation class. This time I am trying to use function moveCamera to create a video by smoothly moving the camera from one point to another.

However, I find the camera is not doing what I wanted to do because it is called with resetcam=self.resetcam which seems to be getting the initial value True.

Here is the code I used to create a toy video and the expected final result would be something more sophisticated than this:

#!/usr/bin/env python3

import numpy as np
from vedo import TetMesh, show, screenshot, settings, Picture, buildLUT, Box, \
    Plotter, Axes
from vedo.applications import Animation

import vtk
import time as tm

# Do some settings
settings.useDepthPeeling=False  # Useful to show the axes grid
font_name = 'Theemim'
settings.defaultFont = font_name
settings.multiSamples=8
# settings.useParallelProjection = True # avoid perspective parallax

# Create a TetMesh object form the vtk file
ovoid_tet = TetMesh('final_mesh.1.vtk')
host_tet = ovoid_tet.clone()

# This will get rid of the background Earth unit and air unit in the model
# which leaves us with the central part of the model
ovoid_tet.threshold(name='cell_scalars', above=1, below=1)
host_tet.threshold(name='cell_scalars', above=2, below=2)

# Crop the entire mesh using a Box object (which is considered to be a mesh
# object in vedo)
# First build a Box object with its centers and dimensions
cent = [555700, 6243165, 100]
box = Box(pos=cent, size=(4000, 4000, 3000))
# So, we now cut the TetMesh object with a mesh (that Box object)
# host_tet.cutWithMesh(box, wholeCells=True)
host_tet.cutWithMesh(box, wholeCells=False)

# And we need to convert it to a mesh object for later plotting
ovoid = ovoid_tet.tomesh().lineWidth(1).lineColor('w')
host = host_tet.tomesh().lineWidth(1).lineColor('w')

# We need to build a look up table for our color bar, and now it supports
# using category names as labels instead of the numerical values
# This was implemented upon my request
lut_table = [
    # Value, color, alpha, category
    (1.0, 'indianred', 1, 'Ovoid'),
    (1.5, 'lightgray', 1, 'Host'),
]
lut = buildLUT(lut_table)

host.cmap(lut, 'cell_scalars', on='cells')
ovoid.cmap(lut, 'cell_scalars', on='cells')
ovoid.addScalarBar3D(
    categories=lut_table,
    # pos=(508000, 6416500, -1830),
    title='Units',
    titleSize=1.5,
    # sx=100,
    # sy=4000,
    # titleXOffset=-2,
)

# zlabels = [(500, '500'), (0, '0'), (-500, '-500'), (-1000, '-1000'),
#            (-1500, '-1500')]
axes = Axes(host,
            xtitle='Easting (m)',
            ytitle='Northing (m)',
            ztitle='Elevation (m)',
            xLabelSize=0.02,
            xTitlePosition=0.55,
            yTitlePosition=0.65,
            yTitleOffset=-0.18,
            yLabelRotation=90,
            yLabelOffset=-1.5,
            yLabelSize=0.02,
            yShiftAlongX=1,
            zTitlePosition=0.85,
            zTitleOffset=0.04,
            zLabelSize=0.02,
            zLabelRotation=90,
            # zValuesAndLabels=zlabels,
            # zShiftAlongX=-1,
            axesLineWidth=3,
            yrange=host.ybounds(),
            # xTitleOffset=0.02,
            # yzShift=1,
            tipSize=0.,
            yzGrid=True,
            xyGrid=True,
            gridLineWidth=5,
            )
# Set the camera position
plt = Animation()

# Define the start camera
cam_stop = vtk.vtkCamera()
cam_stop.SetPosition( [556629.071, 6238901.528, 3960.485] )
cam_stop.SetFocalPoint( [555689.855, 6243058.282, -204.657] )
cam_stop.SetViewUp( [-0.226, 0.663, 0.713] )
cam_stop.SetDistance( 5958.954 )
cam_stop.SetClippingRange( [3505.518, 9061.698] )

# Define the stop camera
cam_start = vtk.vtkCamera()
cam_start.SetPosition( [558856.126, 6232517.129, 17699.124] )
cam_start.SetFocalPoint( [555708.33, 6242942.712, -600.042] )
cam_start.SetViewUp( [-0.272, 0.815, 0.511] )
cam_start.SetDistance( 21294.621 )
cam_start.SetClippingRange( [16952.201, 26812.198] )

# size = [3940, 2160]
size = [2560, 1600]
plt.size = size

# # Trying to play with the Animation class
plt.showProgressBar = True
plt.timeResolution = 0.05  # secs
plt.fadeIn(host, t=0, duration=2)
# Try to move the camera
plt.moveCamera(cam_start, cam_stop, t=2, duration=5)
plt.totalDuration = 9 # can shrink/expand total duration
plt.play()

Here is the only vtk file you would need to create the video: final_mesh.1.vtk.zip

If you 'play' the video, you can see that the camera does not move in to the mesh object (i.e., getting closer to the mesh) while the mesh does seem to be rotating (which means that the camera is still changing but not entirely following cam_start and cam_stop). I think the issue is with cam.SetDistance() because if you print(self.cam.GetDistance()) after the call to self.show() in the play function, you get a constant value rather than a changing value as what should be s1 in function moveCamera. And if you print this before the call to self.show(), you will get the correct value. Then, I changed self.show(interactive=False, resetcam=self.resetcam) to self.show(interactive=False, resetcam=False), I got what I wanted.

Is this a bug? Thanks very much!

XushanLu avatar Nov 05 '21 12:11 XushanLu

Thanks for reporting - it's not completely trivial to see what's going on... I'll check and get back to you!

marcomusy avatar Nov 07 '21 22:11 marcomusy