17 |
|
|
18 |
// $Id$ |
// $Id$ |
19 |
|
|
|
// This class provides tools to manipulate vectors of R^3. |
|
|
|
|
20 |
#ifndef TINY_VECTOR_HPP |
#ifndef TINY_VECTOR_HPP |
21 |
#define TINY_VECTOR_HPP |
#define TINY_VECTOR_HPP |
22 |
|
|
27 |
#include <Types.hpp> |
#include <Types.hpp> |
28 |
#include <StreamCenter.hpp> |
#include <StreamCenter.hpp> |
29 |
|
|
30 |
#include <system.h> |
/** |
31 |
|
* @file TinyVector.hpp |
32 |
class Vertex; |
* @author Stéphane Del Pino |
33 |
|
* @date Wed Aug 6 17:36:14 2003 |
34 |
/*! |
* |
35 |
\class TinyVector |
* @brief this class defines small vectors of predefined size (ie: |
36 |
|
* known at compilation time) |
37 |
The aim of this template class if to provide a generic TinyVector type whose template |
* |
38 |
argument is the dimension. |
* @todo use more meta compution |
|
|
|
|
\author Stéphane Del Pino |
|
|
\todo use more meta-computing. |
|
|
\todo remove Vertex contructor (should provide automatic cast instead). |
|
39 |
*/ |
*/ |
40 |
template <int N, typename T=real_t> |
|
41 |
|
template <size_t N, typename T=real_t> |
42 |
class TinyVector |
class TinyVector |
43 |
{ |
{ |
44 |
public: |
public: |
45 |
//! The type of values stored in the TinyVector. |
/// The type of values stored in the TinyVector. |
46 |
typedef T ValueType; |
typedef T ValueType; |
47 |
private: |
|
48 |
//! TinyVector coordinates. |
enum { |
49 |
T x[N]; |
Dimension = N /**< The dimension of the vector */ |
50 |
|
}; |
51 |
|
|
52 |
|
protected: |
53 |
|
/// stored data |
54 |
|
T __x[N]; |
55 |
|
|
56 |
public: |
public: |
57 |
//! Read-only access to the dimension of the TinyVector. |
|
58 |
|
/** |
59 |
|
* Read-only access to the dimension of the TinyVector. |
60 |
|
* |
61 |
|
* @return N |
62 |
|
*/ |
63 |
inline const size_t size() const |
inline const size_t size() const |
64 |
{ |
{ |
65 |
return N; |
return N; |
66 |
} |
} |
67 |
|
|
68 |
//! Access to the \a i th coordinate. |
/** |
69 |
inline T& operator[](const int& i) |
* Access to the \a i th coordinate. |
70 |
|
* |
71 |
|
* @param i the component number |
72 |
|
* |
73 |
|
* @return ith component |
74 |
|
*/ |
75 |
|
inline T& operator[](const size_t& i) |
76 |
{ |
{ |
77 |
assert ((i>=0) && (i<N)); |
assert (i<N); |
78 |
return x[i]; |
return __x[i]; |
79 |
} |
} |
80 |
|
|
81 |
//! Read-only access to the \a i th coordinate. |
/** |
82 |
inline const T& operator[](const int& i) const |
* Read-only access to the \a i th component. |
83 |
|
* |
84 |
|
* @param i the component number |
85 |
|
* |
86 |
|
* @return ith component |
87 |
|
*/ |
88 |
|
inline const T& operator[](const size_t& i) const |
89 |
{ |
{ |
90 |
assert ((i>=0) && (i<N)); |
assert (i<N); |
91 |
return x[i]; |
return __x[i]; |
92 |
} |
} |
93 |
|
|
94 |
//! Operator = overloading. |
/** |
95 |
|
* Makes the current vector equal to a given one |
96 |
|
* |
97 |
|
* @param V the vector to copy |
98 |
|
* |
99 |
|
* @return the modified vector |
100 |
|
*/ |
101 |
inline const TinyVector<N, T>& operator = (const TinyVector<N, T>& V) |
inline const TinyVector<N, T>& operator = (const TinyVector<N, T>& V) |
102 |
{ |
{ |
103 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
104 |
x[i] = V.x[i]; |
__x[i] = V.__x[i]; |
105 |
return (*this); |
return (*this); |
106 |
} |
} |
107 |
|
|
108 |
/*! Operator = overloading from a T. Makes all components be 't'. |
/** |
109 |
*/ |
* Copies the value t to every component of the vector |
110 |
|
* |
111 |
|
* @param t the value to copy |
112 |
|
* |
113 |
|
* @return the result vector |
114 |
|
*/ |
115 |
inline const TinyVector<N, T>& operator = (const T& t) |
inline const TinyVector<N, T>& operator = (const T& t) |
116 |
{ |
{ |
117 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
118 |
x[i] = t; |
__x[i] = t; |
119 |
return (*this); |
return (*this); |
120 |
} |
} |
121 |
|
|
122 |
//! Returns multiplication by a T2 from the right ... |
/** |
123 |
template <typename T2> |
* Returns multiplication by a T from the right ... |
124 |
inline TinyVector<N, T> operator*(const T2& d) const |
* |
125 |
|
* @param t the value |
126 |
|
* |
127 |
|
* @return \f$ t U \f$ where \f$ U\f$ is the current vector |
128 |
|
*/ |
129 |
|
inline TinyVector<N, T> operator*(const T& t) const |
130 |
{ |
{ |
131 |
TinyVector<N, T> W; |
TinyVector<N, T> W; |
132 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
133 |
W.x[i] = x[i] * d; |
W.__x[i] = __x[i] * t; |
134 |
return W; |
return W; |
135 |
} |
} |
136 |
|
|
137 |
//! Scalar product between 2 TinyVectors. |
/** |
138 |
template <typename T2> |
* Computes the scalar product with another vector |
139 |
inline real_t operator*(const TinyVector<N,T2>& V) const |
* |
140 |
|
* @param V the other vector |
141 |
|
* |
142 |
|
* @return \f$ U\cdot V\f$ |
143 |
|
*/ |
144 |
|
inline real_t operator*(const TinyVector<N,T>& V) const |
145 |
{ |
{ |
146 |
assert(typeid(T) == typeid(real_t)); |
assert(typeid(T) == typeid(real_t)); |
147 |
real_t s=0; |
real_t s=0; |
148 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
149 |
s += x[i] * V.x[i]; |
s += __x[i] * V.__x[i]; |
150 |
return s; |
return s; |
151 |
} |
} |
152 |
|
|
153 |
//! Multiplies the TinyVector by a T on the right |
/** |
154 |
inline TinyVector<N, T>& operator*=(const T& d) |
* Multiplies the TinyVector by a T |
155 |
|
* |
156 |
|
* @param t the given value |
157 |
|
* |
158 |
|
* @return \f$ U := t U\f$ |
159 |
|
*/ |
160 |
|
inline const TinyVector<N, T>& operator*=(const T& t) |
161 |
{ |
{ |
162 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
163 |
x[i] *= d; |
__x[i] *= t; |
164 |
return (*this); |
return (*this); |
165 |
} |
} |
166 |
|
|
167 |
//! Multiplies the TinyVector by the inverse of a T on the right |
/** |
168 |
inline TinyVector<N, T>& operator/=(const T& d) |
* Multiplies the TinyVector by the inverse of a T |
169 |
|
* |
170 |
|
* @param t the given value |
171 |
|
* |
172 |
|
* @return \f$ U := t^{-1} U\f$ |
173 |
|
*/ |
174 |
|
inline const TinyVector<N, T>& operator/=(const T& t) |
175 |
{ |
{ |
176 |
for (int i=0; i<N; i++) |
const T temp = 1./t; |
177 |
x[i] /= d; |
this->operator*=(temp); |
178 |
return (*this); |
return (*this); |
179 |
} |
} |
180 |
|
|
181 |
//! Returns the difference with an other vector |
/** |
182 |
|
* Computes the difference with an other vector |
183 |
|
* |
184 |
|
* @param V the other vector |
185 |
|
* |
186 |
|
* @return \f$ U - V \f$ |
187 |
|
*/ |
188 |
inline TinyVector<N, T> operator-(const TinyVector<N, T>& V) const |
inline TinyVector<N, T> operator-(const TinyVector<N, T>& V) const |
189 |
{ |
{ |
190 |
TinyVector<N, T> v=0; |
TinyVector<N, T> v = 0; |
191 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
192 |
v[i] = x[i] - V.x[i]; |
v[i] = __x[i] - V.__x[i]; |
193 |
return v; |
return v; |
194 |
} |
} |
195 |
|
|
196 |
//! Substracts a TinyVector. |
/** |
197 |
inline TinyVector<N, T>& operator-=(const TinyVector<N, T>& V) |
* Substracts a vector. |
198 |
|
* |
199 |
|
* @param V the vector to remove |
200 |
|
* |
201 |
|
* @return \f$ U := U - V\f$ |
202 |
|
*/ |
203 |
|
inline const TinyVector<N, T>& operator-=(const TinyVector<N, T>& V) |
204 |
{ |
{ |
205 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
206 |
x[i] -= V.x[i]; |
__x[i] -= V.__x[i]; |
207 |
return (*this); |
return (*this); |
208 |
} |
} |
209 |
|
|
210 |
//! TinyVector vectorial product. |
/** |
211 |
|
* Computes the vectorial product with a vector |
212 |
|
* |
213 |
|
* @param V the given vector |
214 |
|
* |
215 |
|
* @return \f$ U \land V \f$ |
216 |
|
*/ |
217 |
inline TinyVector<N, T> operator^(const TinyVector<N, T>& V) const |
inline TinyVector<N, T> operator^(const TinyVector<N, T>& V) const |
218 |
{ |
{ |
219 |
TinyVector<N, T> W=0; |
TinyVector<N, T> W=0; |
220 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
221 |
W[i] += x[(i+1)%N] * V.x[(i+2)%N] - x[(i+2)%N] * V.x[(i+1)%N]; |
W[i] += __x[(i+1)%N] * V.__x[(i+2)%N] - __x[(i+2)%N] * V.__x[(i+1)%N]; |
222 |
return W; |
return W; |
223 |
} |
} |
224 |
|
|
225 |
//! Returns the sum with another TinyVector \a V. |
/** |
226 |
|
* Computes the sum with another vector |
227 |
|
* |
228 |
|
* @param V the other vector |
229 |
|
* |
230 |
|
* @return \f$ U+V \f$ |
231 |
|
*/ |
232 |
inline TinyVector<N, T> operator + (const TinyVector<N, T>& V) const |
inline TinyVector<N, T> operator + (const TinyVector<N, T>& V) const |
233 |
{ |
{ |
234 |
TinyVector<N, T> W(*this); |
TinyVector<N, T> W(*this); |
235 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
236 |
W.x[i] += V.x[i]; |
W.__x[i] += V.__x[i]; |
237 |
return W; |
return W; |
238 |
} |
} |
239 |
|
|
240 |
//! Adds the TinyVector \a V to current. |
/** |
241 |
|
* Adds a vector |
242 |
|
* |
243 |
|
* @param V the vector to add |
244 |
|
* |
245 |
|
* @return \f$ U := U+V\f$ |
246 |
|
*/ |
247 |
inline TinyVector<N, T>& operator += (const TinyVector<N, T>& V) { |
inline TinyVector<N, T>& operator += (const TinyVector<N, T>& V) { |
248 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
249 |
x[i] += V.x[i]; |
__x[i] += V.__x[i]; |
250 |
return (*this); |
return (*this); |
251 |
} |
} |
252 |
|
|
253 |
//! Compares 2 vectors. true if they are \a exactly the same |
/** |
254 |
inline const bool operator==(const TinyVector<N, T>& V) const |
* Compares two vectors |
255 |
|
* |
256 |
|
* @param V the vector to compare with |
257 |
|
* |
258 |
|
* @return true if vector matches |
259 |
|
* |
260 |
|
* @note Use this function only for special purpose when vectors can |
261 |
|
* really match (not if results of numerical computations). |
262 |
|
*/ |
263 |
|
inline bool operator==(const TinyVector<N, T>& V) const |
264 |
|
{ |
265 |
|
for (size_t i=0; i<N; i++) { |
266 |
|
if (__x[i] != V.__x[i]) { |
267 |
|
return false; |
268 |
|
} |
269 |
|
} |
270 |
|
return true; |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
|
* Compares 2 vectors. true if they are not \b exactly the same |
275 |
|
* |
276 |
|
* @param V the vector to compare with |
277 |
|
* |
278 |
|
* @return true if vector do not match |
279 |
|
*/ |
280 |
|
inline const bool operator!=(const TinyVector<N, T>& V) const |
281 |
{ |
{ |
282 |
bool b = true; |
for (size_t i=0; i<N; i++) { |
283 |
for (int i=0; i<N; i++) |
if (__x[i] != V.__x[i]) { |
284 |
b = (b && (x[i] == V.x[i])); |
return true; |
285 |
return b; |
} |
286 |
|
} |
287 |
|
return false; |
288 |
} |
} |
289 |
|
|
290 |
/*! Returns the square root of the TinyVector \a V. |
/** |
291 |
(ie: it returns the vector whoose componnents are square roots of arg) |
* Returns the square root of a vector. |
292 |
*/ |
* (ie: it returns the vector whoose componnents are square roots of arg) |
293 |
|
* |
294 |
|
* @param V the given vector |
295 |
|
* |
296 |
|
* @return \f$ \left( V_0^{\frac{1}{2}}, \ldot , V_N^{\frac{1}{2} \right) \f$ |
297 |
|
*/ |
298 |
inline friend TinyVector<N, T> sqrt(const TinyVector<N, T>& V) |
inline friend TinyVector<N, T> sqrt(const TinyVector<N, T>& V) |
299 |
{ |
{ |
300 |
using namespace std; |
using namespace std; |
301 |
TinyVector<N, T> W; |
TinyVector<N, T> W; |
302 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
303 |
W.x[i] = sqrt(V.x[i]); |
W.__x[i] = sqrt(V.__x[i]); |
304 |
return W; |
return W; |
305 |
} |
} |
306 |
|
|
307 |
//! Compares 2 vectors. true if they are not \b exactly the same |
/** |
308 |
inline const bool operator!=(const TinyVector<N, T>& V) const |
* Returns the Euclidean norm of the vector. |
309 |
|
* |
310 |
|
* @param V the vector which norm is to compute |
311 |
|
* |
312 |
|
* @return \f$ ||V||_2 \f$ |
313 |
|
*/ |
314 |
|
inline friend T Norm(const TinyVector<N, T>& V) |
315 |
{ |
{ |
316 |
bool b = true; |
T t = 0; |
317 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) { |
318 |
b = (b && (x[i] != V.x[i])); |
t += V.__x[i]*V.__x[i]; |
319 |
return b; |
} |
|
} |
|
320 |
|
|
321 |
|
using namespace std; |
322 |
//! Returns the Euclidean norm of the vector. |
return sqrt(t); |
|
inline friend real_t Norm(const TinyVector<N, T>& V) |
|
|
{ |
|
|
assert(typeid(T) == typeid(real_t)); |
|
|
real_t d = 0; |
|
|
for (int i=0; i<N; i++) |
|
|
d += V.x[i]*V.x[i]; |
|
|
return std::sqrt(d); |
|
323 |
} |
} |
324 |
|
|
325 |
//! Returns the multiplication of a TinyVector by a T on the left. |
/** |
326 |
inline friend TinyVector<N, T> operator*(const T a, const TinyVector<N, T>& V) |
* Returns the multiplication of a TinyVector by a T on the left. |
327 |
|
* |
328 |
|
* @param a the multiplicative constant |
329 |
|
* @param V the vector to multiply |
330 |
|
* |
331 |
|
* @return \$f a V \f$ |
332 |
|
*/ |
333 |
|
inline friend TinyVector<N, T> operator*(const T& a, const TinyVector<N, T>& V) |
334 |
{ |
{ |
335 |
return (V*a); |
return (V*a); |
336 |
} |
} |
337 |
|
|
338 |
//! Prints out the transposed TinyVector of \a V into the \p std::ostream \a os. |
/** |
339 |
|
* Prints out the transposed of a vector in a stream |
340 |
|
* |
341 |
|
* @param os the given stream |
342 |
|
* @param V the vector |
343 |
|
* |
344 |
|
* @return the modified stream |
345 |
|
*/ |
346 |
inline friend std::ostream& operator<<(std::ostream& os, |
inline friend std::ostream& operator<<(std::ostream& os, |
347 |
const TinyVector<N, T>& V) |
const TinyVector<N, T>& V) |
348 |
{ |
{ |
349 |
assert (N>0); |
assert (N>0); |
350 |
os << '('; |
os << '('; |
351 |
for (int i=0; i<N-1; i++) |
for (size_t i=0; i<N-1; i++) |
352 |
os << V[i] << ", "; |
os << V[i] << ", "; |
353 |
os << V[N-1] << ')'; |
os << V[N-1] << ')'; |
354 |
return os; |
return os; |
355 |
} |
} |
356 |
|
|
357 |
//! Default constructor does nothing. |
/** |
358 |
|
* Default constructor. |
359 |
|
* |
360 |
|
* @note data are not initialized |
361 |
|
*/ |
362 |
TinyVector() |
TinyVector() |
363 |
{ |
{ |
364 |
; |
; |
365 |
} |
} |
366 |
|
|
367 |
//! Construct the TinyVector setting its coordinates to \a d. |
/** |
368 |
TinyVector(const T& d) |
* Construct the vector setting its coordinates to \a t. |
369 |
|
* |
370 |
|
* @param t the given value |
371 |
|
*/ |
372 |
|
TinyVector(const T& t) |
373 |
{ |
{ |
374 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) { |
375 |
x[i] = d; |
__x[i] = t; |
376 |
|
} |
377 |
} |
} |
378 |
|
|
379 |
//! Copy constructor. |
/** |
380 |
|
* Copy constructor. |
381 |
|
* |
382 |
|
* @param V the original vector |
383 |
|
*/ |
384 |
TinyVector(const TinyVector<N, T>& V) |
TinyVector(const TinyVector<N, T>& V) |
385 |
{ |
{ |
386 |
for (int i=0; i<N; i++) |
for (size_t i=0; i<N; i++) |
387 |
x[i] = V.x[i]; |
__x[i] = V.__x[i]; |
388 |
} |
} |
389 |
|
|
390 |
//! Destructor. |
/** |
391 |
|
* Destructor |
392 |
|
* |
393 |
|
*/ |
394 |
~TinyVector() |
~TinyVector() |
395 |
{ |
{ |
396 |
; |
; |
397 |
} |
} |
398 |
}; |
}; |
399 |
|
|
400 |
/*! |
/** |
401 |
\class TinyVector<3> |
* @class TinyVector<3, T> |
402 |
|
* @author Stéphane Del Pino |
403 |
This class is the specialization to the case N=3. |
* @date Wed Aug 6 18:52:20 2003 |
404 |
|
* |
405 |
\author Stéphane Del Pino |
* @brief specialization for three component vectors |
406 |
\todo use more meta-computing. |
* |
407 |
|
* |
408 |
*/ |
*/ |
409 |
template <class T> |
template <class T> |
410 |
class TinyVector<3, T> |
class TinyVector<3, T> |
411 |
{ |
{ |
412 |
//! The type of values stored in the TinyVector. |
/// The type of values stored in the TinyVector. |
413 |
typedef T ValueType; |
typedef T ValueType; |
414 |
private: |
|
415 |
//! TinyVector coordinates. |
enum { |
416 |
T x[3]; |
Dimension = 3 /**< The dimension of the vector */ |
417 |
|
}; |
418 |
|
|
419 |
|
protected: |
420 |
|
/// stored data |
421 |
|
T __x[3]; |
422 |
|
|
423 |
public: |
public: |
424 |
//! Read-only access to the dimension of the TinyVector. |
|
425 |
|
/** |
426 |
|
* Read-only access to the dimension of the TinyVector. |
427 |
|
* |
428 |
|
* @return 3 |
429 |
|
*/ |
430 |
inline const size_t size() const |
inline const size_t size() const |
431 |
{ |
{ |
432 |
return 3; |
return 3; |
433 |
} |
} |
434 |
|
|
435 |
//! Access to the \a i th coordinate. |
/** |
436 |
inline T& operator[](const int& i) |
* Access to the \a i th coordinate. |
437 |
|
* |
438 |
|
* @param i the component number |
439 |
|
* |
440 |
|
* @return ith component |
441 |
|
*/ |
442 |
|
inline T& operator[](const size_t& i) |
443 |
{ |
{ |
444 |
assert ((i>=0) && (i<3)); |
assert (i<3); |
445 |
return x[i]; |
return __x[i]; |
446 |
} |
} |
447 |
|
|
448 |
//! Read-only access to the \a i th coordinate. |
/** |
449 |
inline const T& operator[](const int& i) const |
* Read-only access to the \a i th component. |
450 |
|
* |
451 |
|
* @param i the component number |
452 |
|
* |
453 |
|
* @return ith component |
454 |
|
*/ |
455 |
|
inline const T& operator[](const size_t& i) const |
456 |
{ |
{ |
457 |
assert ((i>=0) && (i<3)); |
assert (i<3); |
458 |
return x[i]; |
return __x[i]; |
459 |
} |
} |
460 |
|
|
461 |
//! Operator = overloading. |
/** |
462 |
|
* Makes the current vector equal to a given one |
463 |
|
* |
464 |
|
* @param V the vector to copy |
465 |
|
* |
466 |
|
* @return the modified vector |
467 |
|
*/ |
468 |
inline const TinyVector<3, T>& operator = (const TinyVector<3, T>& V) |
inline const TinyVector<3, T>& operator = (const TinyVector<3, T>& V) |
469 |
{ |
{ |
470 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
471 |
x[i] = V.x[i]; |
__x[i] = V.__x[i]; |
472 |
return (*this); |
return (*this); |
473 |
} |
} |
474 |
|
|
475 |
/*! Operator = overloading from a T. Makes all components be 't'. |
/** |
476 |
*/ |
* Copies the value t to every component of the vector |
477 |
template <typename T2> |
* |
478 |
inline const TinyVector<3, T>& operator = (const T2& t) |
* @param t the value to copy |
479 |
|
* |
480 |
|
* @return the result vector |
481 |
|
*/ |
482 |
|
inline const TinyVector<3, T>& operator = (const T& t) |
483 |
{ |
{ |
484 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
485 |
x[i] = t; |
__x[i] = t; |
486 |
return (*this); |
return (*this); |
487 |
} |
} |
488 |
|
|
489 |
//! Scalar product between 2 TinyVectors. |
/** |
490 |
template <typename T2> |
* Returns multiplication by a T from the right ... |
491 |
inline real_t operator*(const TinyVector<3,T2>& V) const |
* |
492 |
|
* @param t the value |
493 |
|
* |
494 |
|
* @return \f$ t U \f$ where \f$ U\f$ is the current vector |
495 |
|
*/ |
496 |
|
inline TinyVector<3, T> operator*(const T& t) const |
497 |
{ |
{ |
498 |
assert(typeid(T) == typeid(real_t)); |
TinyVector<3,T> W; |
499 |
real_t s=0; |
for (size_t i=0; i<3; i++) { |
500 |
for (int i=0; i<3; i++) |
W.__x[i] = __x[i] * t; |
501 |
s += x[i] * V.x[i]; |
} |
502 |
return s; |
return W; |
503 |
} |
} |
504 |
|
|
505 |
//! Returns multiplication by a T from the right ... |
/** |
506 |
template <typename T2> |
* Computes the scalar product with another vector |
507 |
inline TinyVector<3, T> operator*(const T2& d) const |
* |
508 |
|
* @param V the other vector |
509 |
|
* |
510 |
|
* @return \f$ U\cdot V\f$ |
511 |
|
*/ |
512 |
|
inline real_t operator*(const TinyVector<3,T>& V) const |
513 |
{ |
{ |
514 |
TinyVector<3,T> W; |
assert(typeid(T) == typeid(real_t)); |
515 |
for (int i=0; i<3; i++) |
real_t s=0; |
516 |
W.x[i] = x[i] * d; |
for (size_t i=0; i<3; i++) |
517 |
return W; |
s += __x[i] * V.__x[i]; |
518 |
|
return s; |
519 |
} |
} |
520 |
|
|
521 |
//! Multiplies the TinyVector by a T on the right |
/** |
522 |
inline TinyVector<3, T>& operator*=(const T& d) |
* Multiplies the TinyVector by a T |
523 |
|
* |
524 |
|
* @param t the given value |
525 |
|
* |
526 |
|
* @return \f$ U := t U\f$ |
527 |
|
*/ |
528 |
|
inline TinyVector<3, T>& operator*=(const T& t) |
529 |
{ |
{ |
530 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
531 |
x[i] *= d; |
__x[i] *= t; |
532 |
return (*this); |
return (*this); |
533 |
} |
} |
534 |
|
|
535 |
//! Multiplies the TinyVector by the inverse of a T on the right |
/** |
536 |
inline TinyVector<3, T>& operator/=(const T& d) |
* Multiplies the TinyVector by the inverse of a T |
537 |
|
* |
538 |
|
* @param t the given value |
539 |
|
* |
540 |
|
* @return \f$ U := t^{-1} U\f$ |
541 |
|
*/ |
542 |
|
inline TinyVector<3, T>& operator/=(const T& t) |
543 |
{ |
{ |
544 |
for (int i=0; i<3; i++) |
const T temp = 1./t; |
545 |
x[i] /= d; |
this->operator*=(temp); |
546 |
return (*this); |
return (*this); |
547 |
} |
} |
548 |
|
|
549 |
//! Returns the difference with an other vector |
/** |
550 |
|
* Computes the difference with an other vector |
551 |
|
* |
552 |
|
* @param V the other vector |
553 |
|
* |
554 |
|
* @return \f$ U - V \f$ |
555 |
|
*/ |
556 |
inline TinyVector<3, T> operator-(const TinyVector<3, T>& V) const |
inline TinyVector<3, T> operator-(const TinyVector<3, T>& V) const |
557 |
{ |
{ |
558 |
TinyVector<3, T> v(0,0,0); |
TinyVector<3, T> v = 0; |
559 |
for (size_t i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
560 |
v[i] = x[i] - V.x[i]; |
v[i] = __x[i] - V.__x[i]; |
561 |
return v; |
return v; |
562 |
} |
} |
563 |
|
|
564 |
//! Returns the opposed TinyVector. |
/** |
565 |
|
* Returns the opposed TinyVector. |
566 |
|
* |
567 |
|
* |
568 |
|
* @return \$f -X \f$ |
569 |
|
*/ |
570 |
inline TinyVector<3, T> operator-() const |
inline TinyVector<3, T> operator-() const |
571 |
{ |
{ |
572 |
return (TinyVector<3, T> (-x[0],-x[1],-x[2])); |
return (TinyVector<3, T> (-__x[0],-__x[1],-__x[2])); |
573 |
} |
} |
574 |
|
|
575 |
//! Substracts a TinyVector. |
/** |
576 |
inline TinyVector<3, T>& operator-=(const TinyVector<3, T>& V) |
* Substracts a vector. |
577 |
|
* |
578 |
|
* @param V the vector to remove |
579 |
|
* |
580 |
|
* @return \f$ U := U - V\f$ |
581 |
|
*/ |
582 |
|
inline const TinyVector<3, T>& operator-=(const TinyVector<3, T>& V) |
583 |
{ |
{ |
584 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
585 |
x[i] -= V.x[i]; |
__x[i] -= V.__x[i]; |
586 |
return (*this); |
return (*this); |
587 |
} |
} |
588 |
|
|
589 |
//! TinyVector vectorial product. |
/** |
590 |
|
* Computes the vectorial product with a vector |
591 |
|
* |
592 |
|
* @param V the given vector |
593 |
|
* |
594 |
|
* @return \f$ U \land V \f$ |
595 |
|
*/ |
596 |
inline TinyVector<3, T> operator^(const TinyVector<3, T>& V) const |
inline TinyVector<3, T> operator^(const TinyVector<3, T>& V) const |
597 |
{ |
{ |
598 |
TinyVector<3, T> W(0); |
TinyVector<3, T> W(0); |
599 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
600 |
W[i] += x[(i+1)%3] * V.x[(i+2)%3] - x[(i+2)%3] * V.x[(i+1)%3]; |
W[i] += __x[(i+1)%3] * V.__x[(i+2)%3] - __x[(i+2)%3] * V.__x[(i+1)%3]; |
601 |
return W; |
return W; |
602 |
} |
} |
603 |
|
|
604 |
//! Returns the sum with another TinyVector \a V. |
/** |
605 |
|
* Computes the sum with another vector |
606 |
|
* |
607 |
|
* @param V the other vector |
608 |
|
* |
609 |
|
* @return \f$ U+V \f$ |
610 |
|
*/ |
611 |
inline TinyVector<3, T> operator + (const TinyVector<3, T>& V) const |
inline TinyVector<3, T> operator + (const TinyVector<3, T>& V) const |
612 |
{ |
{ |
613 |
TinyVector<3, T> W(*this); |
TinyVector<3, T> W(*this); |
614 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
615 |
W.x[i] += V.x[i]; |
W.__x[i] += V.__x[i]; |
616 |
return W; |
return W; |
617 |
} |
} |
618 |
|
|
619 |
//! Adds the TinyVector \a V to current. |
/** |
620 |
inline TinyVector<3, T>& operator += (const TinyVector<3, T>& V) |
* Adds a vector |
621 |
|
* |
622 |
|
* @param V the vector to add |
623 |
|
* |
624 |
|
* @return \f$ U := U+V\f$ |
625 |
|
*/ |
626 |
|
inline const TinyVector<3, T>& operator += (const TinyVector<3, T>& V) |
627 |
{ |
{ |
628 |
for (size_t i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
629 |
x[i] += V.x[i]; |
__x[i] += V.__x[i]; |
630 |
return (*this); |
return (*this); |
631 |
} |
} |
632 |
|
|
633 |
//! Compares 2 vectors. true if they are \a exactly the same |
/** |
634 |
|
* Compares two vectors |
635 |
|
* |
636 |
|
* @param V the vector to compare with |
637 |
|
* |
638 |
|
* @return true if vector matches |
639 |
|
* |
640 |
|
* @note Use this function only for special purpose when vectors can |
641 |
|
* really match (not if results of numerical computations). |
642 |
|
*/ |
643 |
inline bool operator==(const TinyVector<3, T>& V) const |
inline bool operator==(const TinyVector<3, T>& V) const |
644 |
{ |
{ |
645 |
return ((x[0]==V.x[0])&&(x[1]==V.x[1])&&(x[2]==V.x[2])); |
return ((__x[0]==V.__x[0])&&(__x[1]==V.__x[1])&&(__x[2]==V.__x[2])); |
646 |
} |
} |
647 |
|
|
648 |
//! Compares 2 vectors. true if they are not \b exactly the same |
/** |
649 |
|
* Compares 2 vectors. true if they are not \b exactly the same |
650 |
|
* |
651 |
|
* @param V the vector to compare with |
652 |
|
* |
653 |
|
* @return true if vector do not match |
654 |
|
*/ |
655 |
inline bool operator!=(const TinyVector<3, T>& V) const |
inline bool operator!=(const TinyVector<3, T>& V) const |
656 |
{ |
{ |
657 |
return ((x[0]!=V.x[0])||(x[1]!=V.x[1])||(x[2]!=V.x[2])); |
return ((__x[0]!=V.__x[0])||(__x[1]!=V.__x[1])||(__x[2]!=V.__x[2])); |
658 |
} |
} |
659 |
|
|
660 |
/*! Returns the square root of the TinyVector \a V. |
/** |
661 |
(ie: it returns the vector whoose componnents are square roots of arg) |
* Returns the square root of a vector. |
662 |
*/ |
* (ie: it returns the vector whoose componnents are square roots of arg) |
663 |
|
* |
664 |
|
* @param V the given vector |
665 |
|
* |
666 |
|
* @return \f$ \left( V_0^{\frac{1}{2}}, \ldot , V_N^{\frac{1}{2} \right) \f$ |
667 |
|
*/ |
668 |
inline friend TinyVector<3, T> sqrt(const TinyVector<3, T>& V) |
inline friend TinyVector<3, T> sqrt(const TinyVector<3, T>& V) |
669 |
{ |
{ |
670 |
using namespace std; |
using namespace std; |
671 |
TinyVector<3, T> W; |
TinyVector<3, T> W; |
672 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
673 |
W.x[i] = sqrt(V.x[i]); |
W.__x[i] = sqrt(V.__x[i]); |
674 |
return W; |
return W; |
675 |
} |
} |
676 |
|
|
677 |
//! Returns the Euclidean norm of the vector. |
/** |
678 |
|
* Returns the Euclidean norm of the vector. |
679 |
|
* |
680 |
|
* @param V the vector which norm is to compute |
681 |
|
* |
682 |
|
* @return \f$ ||V||_2 \f$ |
683 |
|
*/ |
684 |
inline friend real_t Norm(const TinyVector<3, T>& V) |
inline friend real_t Norm(const TinyVector<3, T>& V) |
685 |
{ |
{ |
686 |
assert(typeid(T) == typeid(real_t)); |
assert(typeid(T) == typeid(real_t)); |
687 |
real_t d = 0; |
real_t d = 0; |
688 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
689 |
d += V.x[i]*V.x[i]; |
d += V.__x[i]*V.__x[i]; |
690 |
return std::sqrt(d); |
using namespace std; |
691 |
|
return sqrt(d); |
692 |
} |
} |
693 |
|
|
694 |
/// Return the multiplication of a TinyVector by a real_t on the left. |
/** |
695 |
inline friend TinyVector<3, T> operator*(const real_t a, const TinyVector<3, T>& V) { |
* Returns the multiplication of a TinyVector by a T on the left. |
696 |
|
* |
697 |
|
* @param a the multiplicative constant |
698 |
|
* @param V the vector to multiply |
699 |
|
* |
700 |
|
* @return \$f a V \f$ |
701 |
|
*/ |
702 |
|
inline friend TinyVector<3, T> operator*(const real_t& a, |
703 |
|
const TinyVector<3, T>& V) |
704 |
|
{ |
705 |
return (V*a); |
return (V*a); |
706 |
} |
} |
707 |
|
|
708 |
//! Prints out the transposed TinyVector of \a V into the \p std::ostream \a os. |
/** |
709 |
|
* Prints out the transposed of a vector in a stream |
710 |
|
* |
711 |
|
* @param os the given stream |
712 |
|
* @param V the vector |
713 |
|
* |
714 |
|
* @return the modified stream |
715 |
|
*/ |
716 |
inline friend std::ostream& operator<<(std::ostream& os, |
inline friend std::ostream& operator<<(std::ostream& os, |
717 |
const TinyVector<3, T>& V) |
const TinyVector<3, T>& V) |
718 |
{ |
{ |
|
assert (3>0); |
|
719 |
os << '('; |
os << '('; |
720 |
for (int i=0; i<3-1; i++) |
for (size_t i=0; i<3-1; i++) |
721 |
os << V[i] << ", "; |
os << V[i] << ", "; |
722 |
os << V[3-1] << ')'; |
os << V[3-1] << ')'; |
723 |
return os; |
return os; |
724 |
} |
} |
725 |
|
|
726 |
/*! Constructs the vector given its coordinates. |
/** |
727 |
\remark This is only available for 3D. |
* Constructs the vector using three values |
728 |
*/ |
* |
729 |
|
* @param x1 first component |
730 |
|
* @param x2 second component |
731 |
|
* @param x3 third component |
732 |
|
*/ |
733 |
TinyVector (const real_t& x1, const real_t& x2, const real_t& x3) |
TinyVector (const real_t& x1, const real_t& x2, const real_t& x3) |
734 |
{ |
{ |
735 |
x[0] = x1; |
__x[0] = x1; |
736 |
x[1] = x2; |
__x[1] = x2; |
737 |
x[2] = x3; |
__x[2] = x3; |
738 |
} |
} |
739 |
|
|
740 |
//! Default constructor does nothing. |
/** |
741 |
|
* Default constructor. |
742 |
|
* |
743 |
|
* @note data are not initialized |
744 |
|
*/ |
745 |
TinyVector() |
TinyVector() |
746 |
{ |
{ |
747 |
; |
; |
748 |
} |
} |
749 |
|
|
750 |
//! Construct the TinyVector setting its coordinates to d |
/** |
751 |
TinyVector(const T& d) |
* Construct the vector setting its coordinates to \a t. |
752 |
|
* |
753 |
|
* @param t the given value |
754 |
|
*/ |
755 |
|
TinyVector(const T& t) |
756 |
{ |
{ |
757 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
758 |
x[i] = d; |
__x[i] = t; |
759 |
} |
} |
760 |
|
|
761 |
//! Copy constructor |
/** |
762 |
|
* Copy constructor. |
763 |
|
* |
764 |
|
* @param V the original vector |
765 |
|
*/ |
766 |
TinyVector(const TinyVector<3, T>& V) |
TinyVector(const TinyVector<3, T>& V) |
767 |
{ |
{ |
768 |
for (int i=0; i<3; i++) |
for (size_t i=0; i<3; i++) |
769 |
x[i] = V.x[i]; |
__x[i] = V.__x[i]; |
770 |
} |
} |
771 |
|
|
772 |
/*! Cast from a Vertex |
/** |
773 |
\remark Only available for N==3. |
* Destructor |
774 |
|
* |
775 |
*/ |
*/ |
|
TinyVector(const Vertex& V) |
|
|
{ |
|
|
x[0] = V.X(); |
|
|
x[1] = V.Y(); |
|
|
x[2] = V.Z(); |
|
|
} |
|
|
|
|
|
//! Default destructor. |
|
776 |
~TinyVector() |
~TinyVector() |
777 |
{ |
{ |
778 |
; |
; |
779 |
} |
} |
780 |
}; |
}; |
781 |
|
|
782 |
|
/** |
783 |
/*! |
* @class TinyVector<1,real_t> |
784 |
\class TinyVector<1,real_t> |
* @author Stéphane Del Pino |
785 |
|
* @date Wed Aug 6 21:41:47 2003 |
786 |
This is the specialization of TinyVector in case N==1. This is done to |
* |
787 |
provide the same interface for scalar and vectorial problems. |
* @brief special vector of size 1 (ie lonely values) |
788 |
|
* |
789 |
\author Stéphane Del Pino |
* |
790 |
*/ |
*/ |
791 |
template<> |
template<typename T> |
792 |
class TinyVector<1, real_t> |
class TinyVector<1, T> |
793 |
{ |
{ |
794 |
//! The type of values stored in the TinyVector. |
/// The type of values stored in the TinyVector. |
795 |
typedef real_t ValueType; |
typedef T ValueType; |
796 |
private: |
|
797 |
|
enum { |
798 |
|
Dimension =1 /**< The dimension of the vector */ |
799 |
|
}; |
800 |
|
protected: |
801 |
//! The value of the |
//! The value of the |
802 |
real_t x; |
T __x; |
803 |
public: |
public: |
804 |
|
|
805 |
/*! Cast operators to use TinyVector on right side. |
/*! |
|
So, TinyVector<1,real_t> can be used as an argument for \b all 'real_t' |
|
|
functions. |
|
806 |
*/ |
*/ |
|
inline operator real_t() |
|
|
{ |
|
|
return x; |
|
|
} |
|
807 |
|
|
808 |
/*! Cast operators to use TinyVector on right side. |
/** |
809 |
So, TinyVector<1,real_t> can be used as an argument for \b all 'const real_t' |
* Cast operators to use TinyVector<1, T> as r_values. |
810 |
functions. |
* So, TinyVector<1,T> can be used as an argument for \b all 'T' |
811 |
*/ |
* functions. |
812 |
inline operator real_t() const |
* |
813 |
{ |
* @return the value |
814 |
return x; |
*/ |
815 |
} |
inline operator T&() |
|
|
|
|
//! \todo comment this |
|
|
inline real_t operator*(const real_t y) const |
|
|
{ |
|
|
return x*y; |
|
|
} |
|
|
|
|
|
//! Adds a \p real_t \a d. |
|
|
inline real_t& operator+=(const real_t& d) |
|
|
{ |
|
|
x+=d; |
|
|
return x; |
|
|
} |
|
|
|
|
|
//! Substracts the \p real_t \a d. |
|
|
inline real_t& operator-=(const real_t& d) |
|
|
{ |
|
|
x-=d; |
|
|
return x; |
|
|
} |
|
|
|
|
|
//! Multiplies by the \p real_t \a d. |
|
|
inline real_t& operator*=(const real_t d) |
|
|
{ |
|
|
x*=d; |
|
|
return x; |
|
|
} |
|
|
|
|
|
//! Divides by the \p real_t \a d. |
|
|
inline real_t& operator/=(const real_t d) |
|
816 |
{ |
{ |
817 |
x/=d; |
return __x; |
|
return x; |
|
818 |
} |
} |
819 |
|
|
820 |
//! Affectes the \p real_t \a d. |
/** |
821 |
inline real_t& operator=(const real_t d) |
* Cast operators to use TinyVector<1, T> as r_values. |
822 |
|
* So, TinyVector<1,T> can be used as an argument for \b all 'T' |
823 |
|
* functions. |
824 |
|
* |
825 |
|
* @return the value |
826 |
|
*/ |
827 |
|
inline operator const T&() const |
828 |
{ |
{ |
829 |
x=d; |
return __x; |
|
return x; |
|
830 |
} |
} |
831 |
|
|
832 |
//! This is just a compatibility acces operator[]. |
/** |
833 |
inline real_t& operator[](const int& i) |
* Compatibility access function |
834 |
|
* |
835 |
|
* @param i the component |
836 |
|
* |
837 |
|
* @return the ith value |
838 |
|
*/ |
839 |
|
inline T& operator[](const size_t& i) |
840 |
{ |
{ |
841 |
assert(i==0); |
assert(i==0); |
842 |
return x; |
return __x; |
843 |
} |
} |
844 |
|
|
845 |
//! This is just a compatibility acces operator[] const. |
/** |
846 |
inline const real_t& operator[](const int& i) const |
* Compatibility access function |
847 |
|
* |
848 |
|
* @param i the component |
849 |
|
* |
850 |
|
* @return the ith value |
851 |
|
*/ |
852 |
|
inline const T& operator[](const size_t& i) const |
853 |
{ |
{ |
854 |
assert(i==0); |
assert(i==0); |
855 |
return x; |
return __x; |
856 |
} |
} |
857 |
|
|
858 |
/*! Compares 2 TinyVector<1, real_t> |
/** |
859 |
\return true if they are \b exactly the same. |
* Read-only access to the dimension of the TinyVector. |
860 |
*/ |
* |
861 |
inline bool operator==(const TinyVector<1, real_t>& V) const |
* @return 1 |
862 |
{ |
*/ |
|
return (x==V.x); |
|
|
} |
|
|
|
|
|
/*! Compares 2 TinyVector<1, real_t>. |
|
|
\returns true if they are different. |
|
|
*/ |
|
|
inline bool operator!=(const TinyVector<1, real_t>& V) const |
|
|
{ |
|
|
return (x!=V.x); |
|
|
} |
|
|
|
|
|
//! Returns the dimension of the vector. |
|
863 |
inline const size_t size() const |
inline const size_t size() const |
864 |
{ |
{ |
865 |
return 1; |
return 1; |
866 |
} |
} |
867 |
|
|
868 |
//! Default constructor does nothing. |
/** |
869 |
TinyVector() |
* Constructs a TinyVector using a T |
870 |
|
* |
871 |
|
* @param y the given value |
872 |
|
*/ |
873 |
|
TinyVector(const T& y) |
874 |
|
: __x(y) |
875 |
{ |
{ |
876 |
; |
; |
877 |
} |
} |
878 |
|
|
879 |
//! Constructs using a real_t. |
/** |
880 |
TinyVector(const real_t& y) |
* Default constructor. |
881 |
: x(y) |
* |
882 |
|
* @note data are not initialized |
883 |
|
*/ |
884 |
|
TinyVector() |
885 |
{ |
{ |
886 |
; |
; |
887 |
} |
} |
888 |
|
|
889 |
//! Copy constructor. |
//! Copy constructor. |
890 |
TinyVector(const TinyVector<1, real_t>& y) |
/** |
891 |
: x(y.x) |
* Copy constructor. |
892 |
|
* |
893 |
|
* @param V the original vector |
894 |
|
*/ |
895 |
|
TinyVector(const TinyVector<1, real_t>& V) |
896 |
|
: __x(V.__x) |
897 |
{ |
{ |
898 |
; |
; |
899 |
} |
} |
900 |
|
|
901 |
//! Destructor. |
/** |
902 |
|
* Destructor |
903 |
|
* |
904 |
|
*/ |
905 |
~TinyVector() |
~TinyVector() |
906 |
{ |
{ |
907 |
; |
; |
908 |
} |
} |
909 |
}; |
}; |
910 |
|
|
|
/*! |
|
|
Class is defined in TinyVectorUF |
|
|
\warning Bad design shoud move this elsewhere ... |
|
|
*/ |
|
|
class UserFunction; |
|
|
template <> |
|
|
class TinyVector<3, UserFunction*>; |
|
|
|
|
911 |
#endif // TINY_VECTOR_HPP |
#endif // TINY_VECTOR_HPP |
912 |
|
|