Commit 0389c0c0 authored by Matt Clarkson's avatar Matt Clarkson

Merge branch '96-control-camera-reset'

parents 75b7da22 d6742fa4
Pipeline #4534 canceled with stages
in 2 minutes and 5 seconds
......@@ -143,12 +143,22 @@ def compute_viewport(window_width,
return x_min, y_min, x_max, y_max
def set_camera_pose(vtk_camera, vtk_matrix):
def set_camera_pose(vtk_camera, vtk_matrix, opencv_style=True):
"""
Sets the camera position and orientation from a camera to world matrix.
If opencv_style is False, the camera defaults to the origin,
facing along the -z axis, with +y being up.
If opencv_style is True (default for legacy compatibility), the camera
defaults to the origin, facing along the +z axis, with +y being down.
This is more in-line with Opencv. So, if you are calibrating with OpenCV,
and want to use those extrinsic matrices to set the pose,
then you want this option.
:param vtk_camera: a vtkCamera
:param vtk_matrix: a vtkMatrix4x4 representing the camera to world.
:param opencv_style: If True uses OpenCV (+z), otherwise OpenGL (-z)
"""
if not isinstance(vtk_camera, vtk.vtkCamera):
raise TypeError('Invalid camera object passed')
......@@ -156,12 +166,17 @@ def set_camera_pose(vtk_camera, vtk_matrix):
if not isinstance(vtk_matrix, vtk.vtkMatrix4x4):
raise TypeError('Invalid matrix object passed')
# This implies a right handed coordinate system.
# By default, assume camera position is at origin,
# looking down the world +ve z-axis.
# All OpenCV/OpenGL/VTK use a right handed coordinate system.
# Start by placing at origin.
origin = [0, 0, 0, 1]
focal_point = [0, 0, 1000, 1]
view_up = [0, -1000, 0, 1]
# Then work out which way its facing.
if opencv_style:
focal_point = [0, 0, 1000, 1]
view_up = [0, -1000, 0, 1]
else:
focal_point = [0, 0, -1000, 1]
view_up = [0, 1000, 0, 1]
vtk_matrix.MultiplyPoint(origin, origin)
vtk_matrix.MultiplyPoint(focal_point, focal_point)
......
......@@ -50,13 +50,19 @@ class VTKOverlayWindow(QVTKRenderWindowInteractor):
:param clipping_range: Near/Far clipping range.
:param aspect_ratio: Relative physical size of pixels, as x/y.
:param zbuffer: if True, will only render zbuffer of main renderer.
:param opencv_style: If True, adopts OpenCV convention, otherwise OpenGL.
:param init_pose: If True, will initialise the camera pose to identity.
:param reset_camera: If True, resets camera when a new model is added.
"""
def __init__(self,
offscreen=False,
camera_matrix=None,
clipping_range=(1, 1000),
aspect_ratio=1,
zbuffer=False
zbuffer=False,
opencv_style=True,
init_pose=False,
reset_camera=True,
):
"""
Constructs a new VTKOverlayWindow.
......@@ -73,6 +79,8 @@ class VTKOverlayWindow(QVTKRenderWindowInteractor):
self.clipping_range = clipping_range
self.aspect_ratio = aspect_ratio
self.zbuffer = zbuffer
self.reset_camera = reset_camera
self.opencv_style = opencv_style
self.input = np.ones((400, 400, 3), dtype=np.uint8)
self.rgb_frame = None
......@@ -157,6 +165,11 @@ class VTKOverlayWindow(QVTKRenderWindowInteractor):
QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setSizePolicy(self.size_policy)
# Set default position to origin.
if init_pose:
default_pose = np.eye(4)
self.set_camera_pose(default_pose)
# Startup the widget fully
self.Initialize()
self.Start()
......@@ -304,7 +317,7 @@ class VTKOverlayWindow(QVTKRenderWindowInteractor):
self.camera_to_world = camera_to_world
vtk_cam = self.get_foreground_camera()
vtk_mat = mu.create_vtk_matrix_from_numpy(camera_to_world)
cm.set_camera_pose(vtk_cam, vtk_mat)
cm.set_camera_pose(vtk_cam, vtk_mat, self.opencv_style)
self.Render()
def add_vtk_models(self, models, layer=1):
......@@ -331,7 +344,9 @@ class VTKOverlayWindow(QVTKRenderWindowInteractor):
for model in models:
renderer.AddActor(model.actor)
renderer.ResetCamera()
if self.reset_camera:
renderer.ResetCamera()
def add_vtk_actor(self, actor, layer=1):
"""
......@@ -354,7 +369,9 @@ class VTKOverlayWindow(QVTKRenderWindowInteractor):
raise ValueError("Invalid layer specified")
renderer.AddActor(actor)
renderer.ResetCamera()
if self.reset_camera:
renderer.ResetCamera()
def get_foreground_renderer(self):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment