svg3d.view

Overview

Define OpenGL-style views and viewports for scene rendering.

class svg3d.view.View(look_at, projection, scene, viewport=None)[source]

Bases: object

Parameters:
DEFAULT_OBJECT_POSITION = array([0., 0., 0.])

Classmethods for this object center their view on the origin by default.

ISOMETRIC_VIEW_MATRIX = array([[   0.70710678,   -0.40824829,    0.57735027,    0.        ],        [   0.        ,    0.81649658,    0.57735027,    0.        ],        [  -0.70710678,   -0.40824829,    0.57735027,    0.        ],        [   0.        ,    0.        , -100.        ,    1.        ]])
classmethod dimetric(scene, fov=1.0, distance=100.0)[source]

Create a View based on a dimetric projection.

In a dimetric projection, the scale along two out of three axes is identical. This strikes a balance between the simplicity and interpretability of isometric projections and the improved sense of realism afforded by trimetric projections.

This is a parallel projection method, meaning that objects remain the same size regardless of their position from the camera. This is useful in diagrams and technical renderings but may be undesirable for realistic scenes.

Parameters:
  • scene (list[Mesh]) – An iterable of mesh objects to view.

  • fov (float) – Field of view, in degrees. Should be in the open range (0.0, 180.0). Default value: 1.0

  • distance (float) – Distance of the viewer from the origin. Default value: 100.0

classmethod from_look_at_and_projection(look_at, projection, scene)[source]

Create a new View from a lookAt and projection matrix.

Parameters:
classmethod isometric(scene, fov=1.0, distance=100.0)[source]

Create a View based on an isometric projection.

In an isometric projection, the scale along each coordinate axis is identical. This is a parallel projection method, meaning that objects remain the same size regardless of their position from the camera. This is useful in diagrams and technical renderings but may be undesirable for realistic scenes.

Parameters:
  • scene (list[Mesh]) – An iterable of mesh objects to view.

  • fov (float) – Field of view, in degrees. Should be in the open range (0.0, 180.0). Default value: 1.0

  • distance (float) – Distance of the viewer from the origin. Default value: 100.0

property look_at

The openGL-style lookAt matrix.

Type:

\((4,4)\) numpy.ndarray

property projection

The openGL-style projection matrix.

Type:

\((4,4)\) numpy.ndarray

property scene

Get or set the list of Mesh objects to render.

Type:

Iterable[Mesh]

classmethod trimetric(scene, fov=1.0, distance=100.0)[source]

Create a View based on a trimetric projection.

In a trimetric projection, each axis is scaled independently. This results in a more “natural” scene than isometric and trimetric views, as the foreshortening of each axis provides a sense of depth to the scene.

This is a parallel projection method, meaning that objects remain the same size regardless of their position from the camera. This is useful in diagrams and technical renderings but may be undesirable for realistic scenes.

Parameters:
  • scene (list[Mesh]) – An iterable of mesh objects to view.

  • fov (float) – Field of view, in degrees. Should be in the open range (0.0, 180.0). Default value: 1.0

  • distance (float) – Distance of the viewer from the origin. Default value: 100.0

property viewport

Get or set the system’s Viewport.

Type:

Viewport

class svg3d.view.Viewport(minx=-0.5, miny=-0.5, width=1.0, height=1.0)[source]

Bases: NamedTuple

A Viewport controls the visible area in a rendered SVG.

This is a convience wrapper around the svgwrite Viewbox classes with a simplified interface.

Parameters:
classmethod from_aspect(aspect_ratio)[source]

Create a Viewport with the given aspect ratio.

Parameters:

aspect_ratio (float)

classmethod from_string(string_to_parse)[source]

Create a Viewport from a space-delimited string of floats.

Parameters:

string_to_parse (str)

height: float

Height of the viewport.

minx: float

Left border of the viewport.

miny: float

Right border of the viewport.

width: float

Width of the viewport.

svg3d.view.get_lookat_matrix(pos_object, pos_camera, vec_up=(0.0, 1.0, 0.0))[source]

Get the “look at” or view matrix for our system.

This matrix moves the world such that the camera is at the origin and rotates the world such that the z-axis of the camera is the mathematical z axis.

Parameters:
  • pos_object (\((3,)\) numpy.ndarray) – Position of the object we are looking at. “at” in openGL vernacular.

  • pos_camera (\((3,)\) numpy.ndarray) – Position of the camera. “eye” in openGL vernacular.

  • vec_up (\((3,)\) numpy.ndarray: | tuple, optional) – Vector describing the height of the camera. “up” in openGL vernacular. Default value: (0.0, 1.0, 0.0)

svg3d.view.get_projection_matrix(z_near, z_far, fov_y, aspect=1.0)[source]

Get a projection matrix from parameters of the provided view frustum.

z_near and z_far are the distances to the tip and base of the frustum, respectively. fov_y describes the opening angle of the base, and aspect describes the relationship between the y opening angle and the x. Objects that lie outside the view frustum are culled and wil not be rendered into the scene.

Parameters:
  • z_near (float) – Distance to the near clipping plane. Must be greater than zero.

  • z_far (float) – Distance to the far clipping plane. Must be greater than z_near.

  • fov_y (float) – Field of view angle along the y direction, in degrees.

  • aspect (float, optional) – Ratio of field of view angle in the y direction to field of view angle in x. Default value: 1.0

See also

Understanding Projection Matrices:

http://www.songho.ca/opengl/gl_projectionmatrix.html