\chapter{Methods and Design}


\section{Structure of Software}

A custom vector graphics viewer called IPDF\footnote{The original name ``Infinite Precision Document Format'' stuck, although the word ``Infinite'' is highly misleading} was implemented for this project in collaboration with David Gow\cite{}. We will give a brief overview of the features and limitations of this software.

\subsection{Document Structure}

IPDF is built around Objects which can be rendered either on the CPU (directly altering a bitmap and then uploading to the GPU) or the GPU (via the use of OpenGL GLSL shader programs).

Initially only very simple shapes (Rectangles and Circles) were supported, but in order to produce a meaningful demonstration of arbitrary precision viewing, Paths formed from Quadratic or Cubic B\'{e}ziers as specified by the SVG standard were added. Shading of paths is partially implemented.

\subsection{Rendering Process}

As discussed in Section \ref{} it is not clear to what extend GPUs comply with the IEEE-754 standard, and arbitrary precision arithmetic is most easily implemented on the CPU; for this reason a CPU renderer and GPU renderer were implemented.

All Objects are represented by their bounding rectangles; in the case of B\'{e}zier curves the control points are stored relative to the bounding rectangle. The rendering process involves converting the bounding rectangle coordinates from Document to Screen space as discussed in Section \ref{}. For both GPU and CPU rendering a B\'{e}zier in screen space is decomposed into straight lines; the CPU renders these lines using Bresenham's algorithm.



\subsection{Interactivity}

There are two ways to control the IPDF software; manually through use of keyboard and mouse and a Qt4\cite{} based control panel, or automatically through a script containing a sequence of commands to transform the view or insert SVGs. In both cases SVGs can be inserted scaled to the current View rectangle. 

\subsection{Version Control}

The Git version control system was used to collaborate on this project; the main repository may be viewed at \url{http://git.ucc.asn.au/?p=ipdf/code.git}

\section{Approaches to Arbitrary Precision}

\subsection{Na\"{i}ve Approach}

A na\"{i}ve approach would be to replace all floating point operations with arbitrary precision operations, and this was in fact tried in early experiments. This approach requires use of the CPU renderer, as GLSL is restricted to floating point representations. A type definition \texttt{Real} on the CPU can be selected at compile time.

Unfortunately truly arbitrary precision number representations were found to be far too inefficient for practical purposes --- for example, rendering a frame with GMP Rationals could take up to 60 seconds at the default view.


\begin{comment}
\subsubsection{Number Representations Trialed}

\begin{enumerate}
	\item IEEE-754 single, double, extended (control)
	\item Custom implementation of Rationals with 64 bit integers
	\item Custom implementation of Rationals with custom Arbitrary Precision Integers	
	\item Custom implementation of Rationals but with GMP integers
	\item GMP Rationals
	\item MPFR Arbitrary Precision Floats
	\item iRRAM ``exact'' real arithmetic\cite{}	
\end{enumerate}
\end{comment}

\subsection{Intermediate Coordinate Systems}

When an object is visible on the screen it is only necessary to render it accurately to within the nearest pixel.
As shown in the Results Section \ref{}, introducing an intermediate coordinate system for a large number of objects and applying transformations to this coordinate system instead of individual objects produces the best results both in terms of reduced rounding errors using floating point arithmetic, and reduced number of required arbitrary precision operations.

\subsection{Quadtree Document Division}

An approach identified by Gow\cite{} is to construct intermediate coordinate systems as the user manipulates the view. 



\section{Libraries Used}

The libraries used to compile the IPDF software are:
\begin{itemize}
	\item SDL2 - Simple Direct media Library
	\begin{itemize}
		\item Used for window management and to obtain an OpenGL context
		\item Provides Bitmap handling functions
	\end{itemize}
	\item Qt4 (optional) --- Open source toolkit for Dialog based applications
	\item OpenGL (4.4) --- The standard API for controlling GPUs
	\item PugiXML --- Open source XML parsing library used to implement parsing of SVGs
	\item GNU Multiple Precision (GMP)
	\begin{itemize}
		\item Implements arbitrary precision integers, floats, and rationals
		\item Highly optimised using CPU specific assembly instructions
	\end{itemize}
	\item MPFR --- built on GMP but ensures IEEE-754 consistent rounding behaviour
\end{itemize}


