Skip to content

Commit 73a8970

Browse files
committed
ENH: Add vtkCamera support for the camera trait
1 parent 8681486 commit 73a8970

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

examples/CameraParameters.ipynb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"metadata": {},
7-
"outputs": [
8-
{
9-
"name": "stdout",
10-
"output_type": "stream",
11-
"text": [
12-
"Requirement already satisfied: itk-io in /home/matt/bin/miniconda3/envs/itkwidgets/lib/python3.7/site-packages (5.1.0.post3)\r\n",
13-
"Requirement already satisfied: vtk in /home/matt/bin/miniconda3/envs/itkwidgets/lib/python3.7/site-packages (8.1.2)\r\n",
14-
"Requirement already satisfied: itk-core==5.1.0.post3 in /home/matt/bin/miniconda3/envs/itkwidgets/lib/python3.7/site-packages (from itk-io) (5.1.0.post3)\r\n"
15-
]
16-
}
17-
],
7+
"outputs": [],
188
"source": [
199
"# Install dependencies for this example\n",
2010
"# Note: This does not include itkwidgets, itself\n",
2111
"import sys\n",
22-
"!{sys.executable} -m pip install itk-io vtk"
12+
"!{sys.executable} -m pip install itk vtk"
2313
]
2414
},
2515
{
@@ -59,7 +49,7 @@
5949
{
6050
"data": {
6151
"application/vnd.jupyter.widget-view+json": {
62-
"model_id": "6e4bf6b5d5524c27ab58325155a4b859",
52+
"model_id": "4acaff0c96954105bd3a0264cfe837bf",
6353
"version_major": 2,
6454
"version_minor": 0
6555
},
@@ -123,15 +113,24 @@
123113
}
124114
],
125115
"source": [
116+
"# Get camera parameters\n",
126117
"viewer.camera"
127118
]
128119
},
129120
{
130121
"cell_type": "code",
131-
"execution_count": null,
122+
"execution_count": 7,
132123
"metadata": {},
133124
"outputs": [],
134-
"source": []
125+
"source": [
126+
"# Use a vtkCamera instance\n",
127+
"vtk_camera = vtk.vtkCamera()\n",
128+
"vtk_camera.SetPosition([3.6606824e+02, 1.4895028e+00, 1.0029479e+02])\n",
129+
"vtk_camera.SetFocalPoint([ 1.5009012e+02, 1.2883676e+02, -1.1309737e+01])\n",
130+
"vtk_camera.SetViewUp([-4.5256451e-01, 1.3861613e-02, 8.9162391e-01])\n",
131+
"\n",
132+
"viewer.camera = vtk_camera"
133+
]
135134
}
136135
],
137136
"metadata": {

itkwidgets/trait_types.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
from functools import reduce
1616
except ImportError:
1717
pass
18+
have_vtk = False
19+
try:
20+
import vtk
21+
have_vtk = True
22+
except ImportError:
23+
pass
1824

1925
from ._transform_types import to_itk_image, to_point_set, to_geometry
20-
from ipydatawidgets import array_serialization
26+
from ipydatawidgets import array_serialization, NDArray
2127

2228
# from IPython.core.debugger import set_trace
2329

@@ -721,3 +727,19 @@ def validate(self, obj, value):
721727
'Custom'):
722728
raise self.error('Invalid lookup table')
723729
return super(LookupTable, self).validate(obj, value)
730+
731+
class Camera(NDArray):
732+
"""A trait type holding visualization camera parameters."""
733+
734+
info_text = 'Camera parameters: [[position_x, position_y, position_z], '\
735+
'[focal_point_x, focal_point_y, focal_point_z], '\
736+
'[view_up_x, view_up_y, view_up_z]]'
737+
738+
def validate(self, obj, value):
739+
result = value
740+
if have_vtk and isinstance(value, vtk.vtkCamera):
741+
result = np.zeros((3,3), dtype=np.float32)
742+
result[0,:] = value.GetPosition()
743+
result[1,:] = value.GetFocalPoint()
744+
result[2,:] = value.GetViewUp()
745+
return super(Camera, self).validate(obj, result)

itkwidgets/widget_viewer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import ipywidgets as widgets
1717
from traitlets import CBool, CFloat, CInt, Unicode, CaselessStrEnum, List, validate, TraitError, Tuple
1818
from ipydatawidgets import NDArray, array_serialization, shape_constraints
19-
from .trait_types import ITKImage, ImagePointTrait, ImagePoint, PointSetList, PolyDataList, itkimage_serialization, image_point_serialization, polydata_list_serialization, Colormap, LookupTable
19+
from .trait_types import ITKImage, ImagePointTrait, ImagePoint, PointSetList, PolyDataList, itkimage_serialization, image_point_serialization, polydata_list_serialization, Colormap, LookupTable, Camera
2020

2121
try:
2222
import ipywebrtc
@@ -313,7 +313,7 @@ class Viewer(ViewerParent):
313313
default_value='v',
314314
help="View mode: x: x plane, y: y plane, z: z plane, v: volume rendering").tag(
315315
sync=True)
316-
camera = NDArray(dtype=np.float32, default_value=np.zeros((3, 3), dtype=np.float32),
316+
camera = Camera(dtype=np.float32, default_value=np.zeros((3, 3), dtype=np.float32),
317317
help="Camera parameters: [[position_x, position_y, position_z], "
318318
"[focal_point_x, focal_point_y, focal_point_z], "
319319
"[view_up_x, view_up_y, view_up_z]]")\
@@ -833,7 +833,7 @@ def view(image=None, # noqa: C901
833833
'z': z-plane
834834
'v': volume rendering
835835
836-
camera: 3x3 numpy float32 array
836+
camera: 3x3 numpy float32 array, or vtk.vtkCamera
837837
Camera parameters:
838838
[[position_x, position_y, position_z],
839839
[focal_point_x, focal_point_y, focal_point_z],

0 commit comments

Comments
 (0)