\section{Sampling Camera}\label{sec:sampCam}


Since the purpose of the sampling camera is to capture only the geographic
area that could affect the final image, the sampling camera is crucial
to the efficiency and visual fidelity of our approach.  Similar to the
viewing camera, the sampling camera is defined by a view matrix and a
projection matrix.  These two matrices are required for projecting the
grid and rasterizing the resulting mesh.

\paragraph*{Pertinent Rays.}

To ensure that the sampling camera
minimizes rays that miss the sphere and maximizes pertinent rays, we
intersect the set of eye rays defined by the viewing camera and the entire
set of rays that intersect the sphere from the viewer's current position.

The former is determined by the viewing frustum. The latter is determined
by an infinite cone emanating from the viewpoint and containing the sphere
entirely. To simplify intersecting these two regions, we approximate the
planet cone with a frustum, called the planet frustum, which contains
the planet. Every side of this frustum is tangent to the sphere.

A third area that needs to be included is the region below the viewing
frustum between the nadir and where the frustum intersects the planet.
To generate the sampling camera, we intersect the viewing frustum with
the planet frustum and include the nadir.

\paragraph*{Frustum-Frustum Intersection.}

The algorithm for intersecting frusta is simplified by the observation
that the viewing frustum and the planet frustum share an apex, the
viewpoint.  The algorithm represents each frustum with respect to the
corners where its sides meet, and defines each corner using a ray. The
viewing frustum's corner rays can be obtained from the attributes used to
define the frustum, \ie the left, right, bottom, and top values which are
in eye space.  The planet frustum corner rays are defined by generating
a frustum that is oriented so that its up direction is aligned to the
plane of the viewing camera's up and forward directions.

Let frustum \entity{A} be the viewing frustum, \entity{B}
be the planet frustum, and \entity{C} be the intersection
of \entity{A} and \entity{B}. Given the corner rays of \entity{A} and
\entity{B}, the corner rays of \entity{C} can be determined by applying
two rules. First, any corner ray of \entity{A} that is in \entity{B} or of
\entity{B} that is in \entity{A} is a corner ray of \entity{C}.  Second,
possible corner rays of \entity{C} result from the intersection of the
planes of \entity{A} with the planes of \entity{B}. Any such ray that is
bounded by both \entity{A} and \entity{B} is a corner ray of \entity{C}.

The planes of a frustum are the planes that contain each side of the frustum.
These planes are defined by their normals, which are obtained from the cross
product of the corner rays.

Using these two rules, our algorithm is as follows:

\begin{enumerate}
	\item Find the rays of \entity{A} that exist within \entity{B}.
	\item Find the rays of \entity{B} that exist within \entity{A}.'
	\item Calculate the normals of the planes of \entity{A} and \entity{B}.
	\item Intersect the planes of A with the planes of B.
	\item For each resulting ray, if the ray is bounded by \entity{A} and \entity{B} it is a corner ray of \entity{C}.
\end{enumerate}

This algorithm requires the ability to determine whether a ray is bounded
by a frustum and the ability to intersect planes. To determine whether
a ray is bounded by a frustum, we test the ray's angle to each plane
of the frustum. Given the normal \unit{n} to a plane and a ray direction
\unit{r}, the ray is on the positive side of the plane if $\dot{n}{r}
> 0$. When generating plane normals for the frusta, we ensure that each
plane normal points inward to the frustum. The second requirement is to
intersect to the planes. The ray that lies in the intersection of two
planes can be determined by the cross product of the planes normals.

In the case that there is no intersection between the frusta, as when
the viewer looks away from the planet, we use the corner rays of the
planet frustum.

We carry out the intersection in a special coordinate system based on
the viewing camera. This coordinate system is a rotated version of the
viewing camera such that the up direction lies in the plane of the view
direction and the nadir. The purpose of this is to generate a sampling
camera that is oriented such that the top edge of the sampling frustum is
aligned with the horizon. This minimizes the number of rays that miss the
sphere, especially when the viewer is close to the surface of the planet.

\paragraph*{View Matrix.}

The output of frustum-frustum intersection is a set of rays that describes
the corners of the intersection frustum.

To build the view matrix of the sampling camera, we first need to choose
a view direction based on the intersection rays. The view direction
should take into account all of the intersection rays, since a skewed
view direction leads to nonuniform distribution of grid points when
the projective grid is stretched across the viewport of the sampling
camera. To obtain the view direction, we first set its $x$-component
to zero, since the previously described rotated space already aligns the top
of the camera to the horizon. We then take the average of the $y$- and
$z$-components of all intersection rays. We include the nadir in this
average in order to include the area below the viewing camera on the
planet, as described earlier. 

The view direction and the nadir are then used in a series of cross
products to build a basis which defines the coordinate system of the
sampling camera. The three vectors of this basis are used to create the
sampling camera's view matrix.

\paragraph*{Projection Matrix.}

The frustum of the sampling camera can be described using the standard
left, right, top, bottom, near, and far values used to create a
projection matrix.  These values relate to the corners of the frustum
on the user-chosen near plane.

We obtain these values by intersecting all of the intersection rays,
along with the nadir, with an arbitrary near plane. This is carried out
in coordinates relative to the sampling camera, which were defined in
the previous section. The user-chosen near plane is perpendicular to the
view direction within that coordinate system. We then find the minimum
and maximum $x-$ and $y-$components of the resulting intersections on
the near plane. The output is the left, right, bottom and top values.
The distance to the far plane, like the near plane, is chosen arbitrarily.
Thus, the near and far values can be the same values used to generate
the viewing frustum's projection matrix.

