Lesson of 21 July 2009 (morning)¶
Simple vector/matrix library for 3d vector using templates
File lib3d.hh
1/*
2 File: lib3d.hh
3 Simple vector/matrix library for 3d vector and 3x3 matrix.
4 Improved version by using template
5*/
6
7/* first trick, avoid double inclusion using conditional compilation */
8
9/* check if LIB3D_H is defined, if NOT the lines up to #endif are expanded */
10#ifndef LIB3D_HH
11#define LIB3D_HH // define an empty macro, only to define the symbol LIB3D_HH
12
13#include <iostream>
14
15#ifdef DEBUG
16 #define DEBUG_MSG(A) std::cout << A
17#else
18 #define DEBUG_MSG(A)
19#endif
20
21// A vector is a CLASS with 3 field, x y z
22template <typename T>
23class point3d {
24public:
25 // make an alias of the type T
26 typedef T valueType ;
27
28private:
29 // the data of the vector
30 valueType v[3] ;
31
32public:
33 // constructor
34 inline
35 point3d() {
36 DEBUG_MSG("point3d contructor\n") ;
37 } // do nothing
38
39 inline
40 point3d( point3d const & a ) {
41 DEBUG_MSG("point3d copy contructor\n") ;
42 v[0] = a . v[0] ; // accessing the pribate part of a!.
43 v[1] = a . v[1] ;
44 v[2] = a . v[2] ;
45 }
46
47 inline
48 point3d( valueType x, valueType y, valueType z ) {
49 DEBUG_MSG("point3d(x,y,z) constructor \n") ;
50 v[0] = x ;
51 v[1] = y ;
52 v[2] = z ;
53 }
54
55 // destructor
56 ~point3d() {
57 DEBUG_MSG("point3d destructor\n") ;
58 } // do nothing
59
60 /* comment produced using figlet.
61 _ _ _ _
62 (_)_ __ | |_ ___ _ __ _ __ __ _| | ___ _ __ ___ _ __ __ _| |_ ___ _ __
63 | | '_ \| __/ _ \ '__| '_ \ / _` | | / _ \| '_ \ / _ \ '__/ _` | __/ _ \| '__|
64 | | | | | || __/ | | | | | (_| | | | (_) | |_) | __/ | | (_| | || (_) | |
65 |_|_| |_|\__\___|_| |_| |_|\__,_|_| \___/| .__/ \___|_| \__,_|\__\___/|_|
66 |_|
67 */
68
69 // assigment operator
70 point3d const &
71 operator = ( point3d const & a ) {
72 DEBUG_MSG("point3d assign (=) operator\n") ;
73 v[0] = a . v[0] ; // accessing the pribate part of a!.
74 v[1] = a . v[1] ;
75 v[2] = a . v[2] ;
76 return *this ;
77 }
78
79 // add operator
80 point3d const &
81 operator += ( point3d const & a ) {
82 DEBUG_MSG("point3d assign (=) operator\n") ;
83 v[0] += a . v[0] ; // accessing the pribate part of a!.
84 v[1] += a . v[1] ;
85 v[2] += a . v[2] ;
86 return *this ;
87 }
88
89 // sub operator
90 point3d const &
91 operator -= ( point3d const & a ) {
92 DEBUG_MSG("point3d assign (=) operator\n") ;
93 v[0] -= a . v[0] ; // accessing the pribate part of a!.
94 v[1] -= a . v[1] ;
95 v[2] -= a . v[2] ;
96 return *this ;
97 }
98
99 // multiply element by element operator
100 point3d const &
101 operator *= ( point3d const & a ) {
102 DEBUG_MSG("point3d assign (=) operator\n") ;
103 v[0] *= a . v[0] ; // accessing the pribate part of a!.
104 v[1] *= a . v[1] ;
105 v[2] *= a . v[2] ;
106 return *this ;
107 }
108
109 // division element by element operator
110 point3d const &
111 operator /= ( point3d const & a ) {
112 DEBUG_MSG("point3d assign (=) operator\n") ;
113 v[0] /= a . v[0] ; // accessing the pribate part of a!.
114 v[1] /= a . v[1] ;
115 v[2] /= a . v[2] ;
116 return *this ;
117 }
118
119 // accessor
120 valueType const & x() const { return v[0] ; }
121 valueType const & y() const { return v[1] ; }
122 valueType const & z() const { return v[2] ; }
123
124 // initialier
125 point3d &
126 setup( valueType x, valueType y, valueType z ) {
127 DEBUG_MSG("execut point3d::setup\n") ;
128 this -> v[0] = x ;
129 this -> v[1] = y ;
130 this -> v[2] = z ;
131 return *this ;
132 }
133
134} ;
135
136/*
137 _ _
138 _____ _| |_ ___ _ __ _ __ __ _| |
139 / _ \ \/ / __/ _ \ '__| '_ \ / _` | |
140| __/> <| || __/ | | | | | (_| | |
141 \___/_/\_\\__\___|_| |_| |_|\__,_|_|
142
143 _
144 ___ _ __ ___ _ __ __ _| |_ ___ _ __
145 / _ \| '_ \ / _ \ '__/ _` | __/ _ \| '__|
146| (_) | |_) | __/ | | (_| | || (_) | |
147 \___/| .__/ \___|_| \__,_|\__\___/|_|
148 |_|
149*/
150
151// printing of a vector
152template <typename T>
153std::ostream &
154operator << ( std::ostream & s, point3d<T> const & a ) {
155 s << "[ x= " << a.x()
156 << ", y= " << a.y()
157 << ", z= " << a.z()
158 << " ]" ;
159 return s ;
160}
161
162// addition of two vectors
163template <typename T>
164point3d<T>
165operator +
166( point3d<T> const & a, point3d<T> const & b ) {
167 DEBUG_MSG("point3d operator +\n") ;
168 return point3d<T>( a.x()+b.x(),
169 a.y()+b.y(),
170 a.z()+b.z() ) ;
171}
172
173// subtraction of two vectors
174template <typename T>
175point3d<T>
176operator -
177( point3d<T> const & a, point3d<T> const & b ) {
178 DEBUG_MSG("point3d operator -\n") ;
179 return point3d<T>( a.x()-b.x(),
180 a.y()-b.y(),
181 a.z()-b.z() ) ;
182}
183
184// multiplication (element by element) of two vectors
185template <typename T>
186point3d<T>
187operator *
188( point3d<T> const & a, point3d<T> const & b ) {
189 DEBUG_MSG("point3d operator *\n") ;
190 return point3d<T>( a.x()*b.x(),
191 a.y()*b.y(),
192 a.z()*b.z() ) ;
193}
194
195// division (element by element) of two vectors
196template <typename T>
197point3d<T>
198operator /
199( point3d<T> const & a, point3d<T> const & b ) {
200 DEBUG_MSG("point3d operator /\n") ;
201 return point3d<T>( a.x()/b.x(),
202 a.y()/b.y(),
203 a.z()/b.z() ) ;
204}
205
206// vector product two vectors
207template <typename T>
208point3d<T>
209operator ^
210( point3d<T> const & a, point3d<T> const & b ) {
211 DEBUG_MSG("point3d operator ^\n") ;
212 return point3d<T>( a . y() * b . z() - a . z() * b . y(),
213 a . z() * b . x() - a . x() * b . z(),
214 a . x() * b . y() - a . y() * b . x() ) ;
215}
216
217// scalar product two vectors
218template <typename T>
219T // return type
220dot
221( point3d<T> const & a, point3d<T> const & b ) {
222 DEBUG_MSG("dot(point3d,point3d)\n") ;
223 return a.x()*b.x()+a.y()*b.y()+a.z()*b.z() ;
224}
225
226
227#endif /* close LIB3D_HH */
File test.cc
1#include "lib3d.hh"
2#include <cmath>
3
4using namespace std ;
5
6//
7// in a large library it is a good habit to
8// make alias of the used parameterized objects
9typedef point3d<double> vec3 ;
10
11int
12main() {
13
14 // define the vector a,b,c
15 vec3 a, b(1,2,3), c ;
16
17 c = a . setup(2,3,4) / b ;
18
19 // equivalent operation
20 // a . setup(2,3,4) ;
21 // c = operator / ( a , b ) ;
22
23 cout << a << '\n' ;
24 cout << b << '\n' ;
25 cout << c << '\n' ;
26
27 cout << "dot(a,b^c) = " << dot(a,b^c) << '\n' ;
28 cout << "end of program\n" ;
29
30 return 0 ;
31}