Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 jab 1
\documentclass[a4paper,landscape,twocolumn]{article}
2
\usepackage{listings}
3
\usepackage[isolatin]{inputenc}
4
\usepackage{makeidx,times}
5
\usepackage{fancyhdr}
6
\usepackage{graphicx}
7
\usepackage{float}
8
\usepackage{alltt}
9
%%\usepackage{doxygen}
10
\usepackage[pdftex,
11
            pagebackref=true,
12
            colorlinks=true,
13
            linkcolor=blue
14
           ]{hyperref}
15
\makeindex
16
\lstset{tabsize=1,language=C++,basicstyle=\small}
17
\author{J. Andreas Bærentzen}
18
\title{CGLA usage}
19
\setcounter{tocdepth}{2}
20
\begin{document}
21
\maketitle
22
\tableofcontents
23
\section{Introduction}
24
 
25
CGLA is a set of numerical C++ vector and matrix classes and class
26
templates designed with computer graphics in mind. CGLA stands for
27
``Computer Graphics Linear Algebra''.
28
 
29
Let us get right down to the obvious question: Why create another
30
linear algebra package?
31
Well, CGLA evolved from a few matrix and vector classes because I
32
didn't have anything better. Also, I created CGLA to experiment with
33
some template programming techniques. This led to the most important
34
feature of CGLA, namely the fact that all vector types are derived
35
from the same template. 
36
 
37
This makes it easy to ensure identical semantics: Since all vectors
38
have inherited, say, the * operator from a common ancestor, it works
39
the same for all of them.
40
 
41
It is important to note that CGLA was designed for Computer Graphics 
42
(not numerical computations) and this had a number of
43
implications. Since, in computer graphics we mainly need small vectors
44
of dimension 2,3, or 4 CGLA was designed for vectors of low
45
dimensionality. Moreover, the amount of memory allocated for a vector
46
is decided by its type at compile time. CGLA does not use dynamic
47
memory. CGLA also does not use virtual functions, and most functions
48
are inline. These features all help making CGLA relatively fast. 
49
 
50
Of course, other libraries of vector templates for computer graphics
51
exist, but to my knowledge none where the fundamental templates are
52
parametrized w.r.t. dimension as well as type. In other words, we have
53
a template (\texttt{ArithVec})that gets both type
54
(e.g. \texttt{float}) and dimension 
55
(e.g. 3) as arguments. the intended use of this template is as
56
ancestor of concrete types such as \texttt{Vec3f} - a 3D floating
57
point type. 
58
 
59
The use of just one template as basis is very important, I believe,
60
since it makes it extremely simple to add new types of
61
vectors. Another very generic template is \texttt{ArithMat} which is a
62
template for matrix classes. (and not necessarily NxN matrices). 
63
 
64
From a users perspective CGLA contains a number of vector and matrix
65
classes, a quaternion and some utility classes. In summary, the most
66
important features are
67
\begin{itemize}
68
\item A number of 2, 3 and 4 d vector classes.
69
\item A number of Matrix classes.
70
\item A Quaternion class.
71
\item Some test programs.
72
\item Works well with OpenGL.
73
\end{itemize}
74
 
75
 
76
\subsection{Naming conventions}
77
 
78
Vectors in CGLA are named \texttt{VecDT} where \texttt{D} stands for
79
dimension at  \texttt{T} 
80
for type. For instance a 3D floating point vector is named
81
\texttt{Vec3f}. Other types are d (double), s (short), i (int), uc
82
(unsigned char), and usi (unsigned shourt int).
83
 
84
Matrices are similiarly named \texttt{MatDxDT}. For instance a 4D
85
\texttt{double} matrix is called \texttt{Mat4x4d}.
86
 
87
\subsection{How to use CGLA}
88
 
89
If you need a given CGLA class you can find the header file that
90
contains it in this document. Simply include the header file and use
91
the class. Remember also that all CGLA functions and classes live in
92
the CGLA namespace! Lastly, look at the example programs that came
93
with the code.
94
 
95
An important point is that you should never use the Arith... classes
96
directly. Classes whose names begin with Arith are templates used for
97
deriving concrete types. It is simpler, cleaner, and the intended
98
thing to do to only use the derived types.
99
 
100
In some cases, you may find that you need a vector or matrix class
101
that I haven't defined. If so, it is fortunate that CGLA is easy to
102
extend. Just look at, say, \texttt{Vec4f} if you need a \texttt{Vec5d}
103
class.
104
 
105
For some more specific help look at the next section where some of the
106
commonly used operations are shown.
107
 
108
\subsection{How to get CGLA}
109
 
110
If you have this document but not the code, look at\\
111
\href{http://www.imm.dtu.dk/~jab/software.html#cgla}
112
{http://www.imm.dtu.dk/~jab/software.html\#cgla}
113
 
114
\subsection{How to use this document}
115
 
116
This document is mostly autogenerated from Doxygen tags in the source
117
code. While this is the only way of creating documentation that
118
stands a reasonable chance of being updated every time the code is,
119
the method does have some drawbacks.
120
 
121
If you want to know whether a given class contains a given function,
122
first look at the class. If you don't find the function, look at its
123
ancestors. For instance, the class Vec3f certainly has a += operator
124
function but it is defined in the template class ArithVec. 
125
 
126
Another problem is that since templates are used extensively in CGLA,
127
template syntax clutters this document. Unfortunately, that cannot be
128
helped. 
129
 
130
\subsection{Help, bugs, contributions}
131
 
132
CGLA was written (mostly) by Andreas Bærentzen (jab{@}imm.dtu.dk), and
133
any bug fixes, contributions, or questions should be addressed to me.
134
 
135
\section{CGLA: Examples of Usage}
136
 
137
While this is mostly a reference manual some examples are probably
138
helpful. The examples below are by no means complete. Many things are
139
possible but not covered below. However, most of the common usage is
140
shown, so this should be enough to get you started. Note that in the
141
following we assume that you are \texttt{using namespace CGLA} and
142
hence don't prefix with \texttt{CGLA::}.
143
 
144
In short, to compile the examples below you would need the following
145
in the top of your file
146
 
147
\begin{lstlisting}{}
148
#include <iostream> // For input output
149
#include "CGLA/Vec3f.h"
150
#include "CGLA/Quaternion.h"
151
#include "CGLA/Mat4x4f.h"
152
 
153
using namespace std; // For input output
154
using namespace CGLA;
155
\end{lstlisting}
156
 
157
To begin with let us create 3 3D vectors. This is done as follows:
158
\begin{lstlisting}{}
159
  Vec3f p0(10,10,10);
160
  Vec3f p1(20,10,10);
161
  Vec3f p2(10,20,10);
162
\end{lstlisting}
163
if we had left out the arguments the three vectors would have been
164
uninitialized, at least in release mode. If we compile in debug mode
165
they would have been initialized to ``Not a Number'' to aid
166
debugging. However, the $[10 10 10]^T$ vector could also have been
167
created differently, using
168
\begin{lstlisting}{}
169
  Vec3f p0(10);
170
\end{}
171
 
172
A very common operation is to compute the normal of a triangle from
173
the position of its vertices. Assuming the three vectors represent the
174
vertices of a triangle, we can compute the normal by finding the
175
vector from the first vertex to the two other vertices, taking the
176
cross product and normalizing the result. This is a one-liner:
177
 
178
\begin{lstlisting}{}
179
  Vec3f n = normalize(cross(p1-p0,p2-p0));
180
\end{lstlisting}
181
 
182
Quite a lot goes on in the snippet above. Observe that the - operator
183
also works for vectors. In fact almost all the arithmetic operators
184
work for vectors. You can also use assignment operators (i.e
185
\texttt{+=}) which is often faster. Then there is the function
186
\texttt{cross} which simply computes the cross product of its
187
arguments. Another frequently used function is \texttt{dot} which
188
takes the dot product. Finally the vector is normalized using the
189
function normalize.
190
 
191
Of course, we can print all or at least most CGLA entities. For
192
example 
193
 
194
\begin{lstlisting}{}
195
  cout << n << endl;
196
\end{lstlisting}
197
 
198
will print the normal vector just computed. We can also treat a vector
199
as an array as shown below
200
 
201
\begin{lstlisting}{}
202
  float x = n[0];
203
\end{lstlisting}
204
 
205
here, of course, we just extracted the first coordinate of the
206
vector. 
207
 
208
CGLA contains a number of features that are not used very frequently,
209
but which are used frequently enough to warrent inclusion. A good
210
example is assigning to a vector using spherical coordinates:
211
 
212
\begin{lstlisting}{}
213
  Vec3f p;
214
  p.set_spherical(0.955317, 3.1415926f/4.0f , 1);
215
\end{lstlisting}
216
 
217
CGLA also includes a quaternion class. Here it is used to construct a
218
quaternion which will rotate the x axis into the y axis.
219
 
220
\begin{lstlisting}{}
221
  Quaternion q;
222
  q.make_rot(Vec3f(1,0,0), Vec3f(0,1,0));
223
\end{lstlisting}
224
 
225
Next, we construct a $4\times 4$ matrix \texttt{m} and assign a translation
226
matrix to the newly constructed matrix. After that we ask the
227
quaternion to return a $4\times 4$ matrix corresponding to its
228
rotation. This rotation matrix is then multiplied onto \texttt{m}.
229
 
230
\begin{lstlisting}{} 
231
  Mat4x4f m = translation_Mat4x4f(Vec3f(1,2,3));
232
  m *= q.get_mat4x4f();
233
\end{lstlisting}
234
 
235
Just like for vectors, the subscript operator works on
236
matrices. However, in this case there are two indices. Just using one
237
index will return the ith row as a vector as shown on the first line
238
below. On the second line we see that using two indices will get us an
239
element in the matrix. 
240
 
241
\begin{lstlisting}{} 
242
  Vec4f v4 = m[0];
243
  float c = m[0][3];
244
\end{lstlisting}
245
 
246
There is a number of constructors for vectors. The default constructor
247
will create a null vector as we have already seen. We can also specify
248
all the coordinates. Finally, we can pass just a single number
249
$a$. This will create the $[a\:a\:a]^T$ vector. For instance, below we
250
create the $[1\: 1\: 1]^T$ vector. Subsequently, this vector is
251
multiplied onto \texttt{m}. 
252
 
253
\begin{lstlisting}{}
254
  Vec3f p(1);
255
  Vec3f p2 = m.mul_3D_point(p);
256
\end{lstlisting}
257
 
258
Note though that \texttt{m} is a $4\times
259
4$ matrix so ... how is that possible? Well, we use the function
260
\texttt{mul\_3D\_point} which, effectively, adds a $w=1$ coordinate to p
261
making it a 4D vector. This $w$ coordinate is stripped afterwards. In
262
practice, this means that the translation part of the matrix is also
263
applied. There is a similar function \texttt{mul\_3D\_vector} if we want
264
to transform vectors without having the translation. This function,
265
effectively, sets $w=0$.
266
 
267
Finally, CGLA is often used together with OpenGL although there is no
268
explicit tie to the OpenGL library. However, we can call the
269
\texttt{get} function of most CGLA entities to get a pointer to the
270
contents. E.g. \texttt{p.get()} will return a pointer to the first
271
float in the 3D vector \texttt{p}. This can be used with OpenGL's
272
``v'' functions as shown below. 
273
 
274
\begin{lstlisting}{}
275
  glVertex3fv(p.get());
276
\end{lstlisting}
277
\end{document}