Skip to content

Commit

Permalink
Merge eed1163 into 30983d3
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Nov 20, 2018
2 parents 30983d3 + eed1163 commit c0f310b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
4 changes: 3 additions & 1 deletion trimesh/scene/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .camera import Camera

from .scene import Scene, split_scene

from .viewerJS import in_notebook

# add to __all__ as per pep8
__all__ = [Scene, split_scene, in_notebook]
__all__ = [Camera, Scene, split_scene, in_notebook]
75 changes: 75 additions & 0 deletions trimesh/scene/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import numpy as np


class Camera(object):

def __init__(
self,
width=None,
height=None,
fx=None,
fy=None,
fovx=None,
fovy=None,
matrix=None,
):
# TODO(unknown): skew is not supported
# TODO(unknown): cx and cy that are not half of width and height
self.width = width
self.height = height
self._fx = fx
self._fy = fy
self._fovx = fovx
self._fovy = fovy
self.matrix = matrix

@property
def fx(self):
if self._fx is None:
assert self.width is not None
assert self._fovx is not None
self._fx = (
(self.width / 2.) /
np.tan(np.deg2rad(self._fovx / 2.))
)
return self._fx

@property
def fy(self):
if self._fy is None:
assert self._height is not None
assert self._fovy is not None
self._fy = (
(self._height / 2.) /
np.tan(np.deg2rad(self._fovy / 2.))
)
return self._fy

@property
def intrinsics(self):
K = np.eye(3, dtype=np.float64)
K[0, 0] = self.fx
K[1, 1] = self.fy
K[0, 2] = self.width / 2.
K[1, 2] = self._height / 2.
return K

@property
def fovx(self):
if self._fovx is None:
assert self.width is not None
assert self._fx is not None
self._fovx = 2 * np.rad2deg(
np.arctan(self.width / 2. / self._fx)
)
return self._fovx

@property
def fovy(self):
if self._fovy is None:
assert self._height is not None
assert self._fy is not None
self._fovy = 2 * np.rad2deg(
np.arctan(self._height / 2. / self._fy)
)
return self._fovy
16 changes: 15 additions & 1 deletion trimesh/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self,
# if we've been passed a graph override the default
self.graph = graph

self.camera = None

def add_geometry(self,
geometry,
node_name=None,
Expand Down Expand Up @@ -379,7 +381,8 @@ def duplicate_nodes(self):
def set_camera(self,
angles=None,
distance=None,
center=None):
center=None,
camera=None):
"""
Add a transform to self.graph for 'camera'
Expand All @@ -393,7 +396,18 @@ def set_camera(self,
Distance from centroid
center: (3,) float
Point camera should be center on
camera: Camera object
Object that stores camera parameters
"""
if camera is not None:
if not (angles is None and distance is None and center is None):
raise ValueError(
'If camera is given, angles, distance '
'and center must be None'
)

self.camera = camera

if len(self.geometry) == 0:
return

Expand Down
14 changes: 13 additions & 1 deletion trimesh/scene/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .. import rendering
from ..transformations import Arcball
from ..util import log

# smooth only when fewer faces than this
_SMOOTH_MAX_FACES = 100000
Expand Down Expand Up @@ -38,6 +39,16 @@ def __init__(self,
self.vertex_list_hash = {}
self.vertex_list_mode = {}

if scene.camera is not None:
if resolution is not None:
resolution_from_cam = (scene.camera.width, scene.camera.height)
if resolution != resolution_from_cam:
log.warning(
'resolution is overwritten by Camera: '
'{} -> {}'.format(resolution, resolution_from_cam)
)
resolution = resolution_from_cam

try:
# try enabling antialiasing
# if you have a graphics card this will probably work
Expand Down Expand Up @@ -212,7 +223,8 @@ def on_resize(self, width, height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.gluPerspective(60.,
fovy = self.scene.camera.fovy if self.scene.camera else 60
gl.gluPerspective(fovy,
width / float(height),
.01,
self.scene.scale * 5.0)
Expand Down

0 comments on commit c0f310b

Please sign in to comment.