The SKIRT project
advanced radiative transfer for astrophysics
The perspective instrument

Introduction

The PerspectiveInstrument class in SKIRT implements a full perspective view of the simulated model, with arbitrary placement of the viewport outside or inside the model. Only photon packets arriving from the front are recorded; light emitted behind the viewport is ignored. For each wavelength the instrument outputs the detected surface brightness in every pixel of a given frame as a data cube in a FITS file. The instrument does not support recording of spatially integrated flux densities.

The perspective instrument is intended mostly for making movies. Each movie frame is generated by a separate perspective instrument with the appropriate parameters.

Parameters and coordinate systems

The following table lists all parameters for the perspective instrument other than the options common to all instrument, which obviously have no effect on the perspective transformation. Positions and sizes are specified in the right-handed world coordinate system, i.e. the coordinate system in which the SKIRT model is defined, using units of length such as parsec. Usually the model is centered around the origin O of the world coordinate system.

Parameter(s) Description
Nx,Ny The number of pixels in the viewport in the local x and y directions
Sx,Sy The size (twice the extent) of the viewport in the local x and y directions
Vx,Vy,Vz The position of the viewport origin V
Cx,Cy,Cz The crosshair position C that determines the orientation of the viewport's z axis
Ux,Uy,Uz The upwards position U that determines the orientation of the viewport's y axis
Fe The focal length that determines the position of the eye

The aspect ratio of the viewport is assumed to be the same when calculated in pixels ( Ny/Nx) or in world size ( Sy/Sx). If not the perspective transformation will introduce a distortion.

The viewport is placed in the xy plane of the left-handed viewport coordinate system (left-handed so that depth increases along the z-axis). Both the viewport and the coordinate system are centered around the viewport origin V. The z axis is placed along the crosshair line VC¯, i.e. the line connecting the viewport origin with the crosshair position, in the direction of C. Consequently the viewport is perpendicular to VC¯. The y axis is placed such that it is parallel with the upwards line OU¯, i.e. the line connecting the world origin with the upwards position, and in the same direction.

The eye and the origin of the left-handed eye coordinate system are placed on the crosshair line VC¯ at a distance Fe from the viewport origin V, at the opposite side from C. In other words, the eye is behind the viewport and looks towards the crosshair position through the viewport. The axes of the eye coordinate system have the same orientation as those of the viewport coordinate system. The perspective transformation occurs from the eye to the viewport system.

Derived positions and directions

We denote the norm of an arbitrary point or vector A as

A=Ax2+Ay2+Az2

A unit vector in the direction from the crosshair position to the viewport origin can then be written as

G=VCVC

and the eye position E becomes

E=V+FeG

When a peel-off photon packet is launched towards the instrument from position P, a unit vector with the appropriate direction is given by

D=EPEP

Homogeneous coordinates

We use homogeneous coordinates (x,y,z,w) with arbitrary scale factor w. An arbitary point P=(Px,Py,Pz) can be represented in homogeneous coordinates as P=(Px,Py,Pz,1). An arbitrary direction D=(Dx,Dy,Dz) is represented as a point at infinity D=(Dx,Dy,Dz,0).

Vice versa, given the homogeneous coordinates Q=(Qx,Qy,Qz,Qw), the corresponding regular coordinates for the point can be retrieved as Q=(Qx/Qw,Qy/Qw,Qz/Qw) provided Qw0.

A homogeneous coordinate transformation can be written as P=PT where T is a 4×4 transformation matrix which can represent a scaling, rotation, translation, perspective transformation or any combination thereof; or more explicitly

[xyzw]=[xyzw][t11t12t13t14t21t22t23t24t31t32t33t34t41t42t43t44]

Consecutive transformations can be combined into a single transformation matrix through regular matrix multiplication.

The instrument transformation

When a peel-off photon packet launched from position P arrives at the perspective instrument, a single transformation is applied to find the homogeneous coordinates of the pixel receiving the photon packet's luminosity

J=PTtot

where P=(Px,Py,Pz,1) and Ttot represents the composite transformation from world coordinates to pixel coordinates which will be developed below.

The incoming photon packet's luminosity is taken into account only if the packet originated in front of the viewport and if the pixel indices are both within range (from zero up to the number of pixels in the relevant direction minus one). The pixel indices can be retrieved using

i=floor(Jx/Jw)j=floor(Jy/Jw)

based on the standard mechanism for converting homogeneous to regular coordinates. The perspective transformation developed below is such that the distance d from the launching position P to the the viewport plane is given by

d=|Jz|

and P is in front of the viewport if and only if Jz>0. In this case there is no division by Jw to avoid distortion of the distance scale by the perspective transformation.

Developing the transformation

At the highest level we compose the instrument transformation as follows

Ttot=TweTevTvp

where

  • Twe transforms from world coordinates to eye coordinates
  • Tev transforms from eye coordinates to viewport coordinates
  • Tvp transforms from viewport coordinates to pixel coordinates

From world to eye coordinates

The transformation from world to eye coordinates can be composed as follows

Twe=MeRxyRzFz

with from left to right:

  • translate the origin to the eye position;
  • rotate around the x and y axes to align the z axis with the crosshair line CV¯, pointing in the direction of the eye; the xy plane is now parallel with the viewport;
  • rotate around the z axis to align the y axis so that it is parallel with and in the same direction as the projection on the xy plane of the upwards line OU¯; the y axis now points up and the x axis now points to the right when looking down from the positive z axis;
  • flip the direction of the z axis so that depth increases away from the eye.

It is easy to see that

Me=[100001000010ExEyEz1]andFz=[1000010000100001]

To determine Rxy let (a,b,c) be the direction cosines of the desired z axis alignment, i.e. the direction G from the crosshair position to the eye position

(a,b,c)=(Gx,Gy,Gz)

and define v=b2+c2 as the hypothenuse of the direction vector's projection in the yz plane. For the time being, assume that v0.

Alignment of the z axis with (a,b,c) can then be accomplished by a rotation over α about the x axis (figure (a)) followed by a rotation over β about the y axis (figure (b)) – the angles are directed clockwise when looking down from the positive rotation axis. The projection of vector (a,b,c) in the yz plane has components b along the y axis and c along the z axis, so that cosα=c/v and sinα=b/v. The situation after the first rotation is shown in figure (b). The length of the hypothenuse in this figure is 1 since the direction cosines (a,b,c) are normalized. It is then immediately clear that cosβ=v and sinβ=a. Thus we find

Rxy=[10000c/vb/v00b/vc/v00001][v0a00100a0v00001]

To determine Rz, let (k,l,m) be the direction cosines of the desired y axis alignment, i.e. the direction from the world origin to the upwards position, after partial transformation to eye coordinates

[klm1]=[UxUyUz1]Rxy

so that the projection of the upwards direction on the eye xy plane has components k along the x axis and l along the y axis. Using the previously derived value for Rxy simple matrix algebra leads to

k=(b2+c2)UxabUyacUzandl=cUybUz

where we dropped a proportionality factor of 1/v in both values, since k and l are not normalized.

We need a rotation over γ about the z axis (figure (c)). Defining u=k2+l2, a reasoning similar to the one before gives cosγ=l/u and sinγ=k/u, and thus

Rz=[l/uk/u00k/ul/u0000100001]

In case v0 the matrices Rxy and Rz as derived above are not well defined and the rotations to align the z axis should be performed in reverse order, i.e. first about the y axis and then about the x axis. A reasoning similar to the above leads to the alternative

v=a2+c2

Rxy=[c/v0a/v00100a/v0c/v00001][10000vb00bv00001]

k=cUxaUz

l=(a2+c2)UyabUxbcUz

u=k2+l2

Rz=[l/uk/u00k/ul/u0000100001]

From eye to viewport coordinates

The figure shows the yz plane of the eye and viewport coordinate systems. To develop the perspective transformation, note that the triangles EPV and EPQ are similar so that yvFe=yeze. With similar reasoning in the xz plane, we get

xv=Fexezeyv=Feyeze

In the z direction we wish to preserve the distance scale and simply perform the translation

zv=zeFe

This can be accomplished through the homogeneous transformation matrix

Tev=[100001000011/Fe00Fe0]

keeping in mind that the xv and yv coordinates will be divided by wv=ze/Fe when converting from homogeneous to regular coordinates, and assuming that the zv coordinate will not be divided by this factor. This `‘trick’' for the z coordinate works only because we have set w=1 when converting the original point to homogeneous coordinates, and none of the other transformation matrices change the w coordinate.

From viewport to pixel coordinates

The viewport is centered on the origin and is scaled in world distances, while pixel indices range from zero to the number of pixels in each direction minus one. Thus we need to apply the following scaling and translation

Tvp=[Nx/Sx0000Ny/Sy0000100001][100001000010Nx/2Ny/201]

We leave the depth coordinate unchanged, which means it is still scaled in world distances, zero depth corresponds with the viewport plane, and positive depth indicates positions in front of the viewport.

Flux calibration

Each photon packet arriving at the instrument carries a certain fraction of luminosity Lpp at a particular wavelength λ. The contribution of the photon packet to the surface brightness fλ,px of the target instrument pixel can be written as (also see the FluxRecorder class),

fλ,px=1Δλ14πdpp21ΩpxLpx

where Δλ is the width of the wavelength bin containing the wavelength λ, dpp is the distance from the photon packet's origin to the instrument, and Ωpx is the solid angle subtended by the target pixel as seen from the eye position.

This solid angle depends on the location of the pixel in the viewport. Pixels at the edges or in the corners of the viewport subtend a smaller solid angle than central pixels. The current implementation of the instrument, though, uses the solid angle of the central pixel for all pixels in the viewport. Let us examine the nature of the error introduced by this approximation. For simplicity, we assume a square viewport of size S×S with N×N square pixels and a distance from the viewport to the eye position (focal length) of Fe. Further assuming that N is uneven, the solid angle Ω0 of the central viewport pixel is given by

Ω0=(2arctan(S2NFe))2.

It is straightforward to also calculate the solid angle ΩN of a pixel in of the corners of the viewport. The relative error (Ω0ΩN)/Ω0 is plotted in the figure below (as a percentage) as a function of the ratio S/Fe of the viewport size and the focal length:

It is thus recommended to place the eye at a distance of at least twice the size of the viewpoint. In that case, the error in the flux calibration at the edges of the viewport is limited to about 10%. Conversely, if the focal distance becomes smaller than the viewport size, the error quickly rises to an unacceptable level.

Lastly, to avoid overly large contributions from extremely nearby objects, the instrument ignores any photon packets that originated at a distance to the viewport d<S10N. (Photon packets originating behind the viewport are ignored in any case).