Subversion Repositories gelsvn

Rev

Rev 115 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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