| 1 | /* | 
| 2 | * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 | * | 
| 4 | * Contact: oopse@oopse.org | 
| 5 | * | 
| 6 | * This program is free software; you can redistribute it and/or | 
| 7 | * modify it under the terms of the GNU Lesser General Public License | 
| 8 | * as published by the Free Software Foundation; either version 2.1 | 
| 9 | * of the License, or (at your option) any later version. | 
| 10 | * All we ask is that proper credit is given for our work, which includes | 
| 11 | * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 | * of your source code files, and to any copyright notice that you may distribute | 
| 13 | * with programs based on this work. | 
| 14 | * | 
| 15 | * This program is distributed in the hope that it will be useful, | 
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 | * GNU Lesser General Public License for more details. | 
| 19 | * | 
| 20 | * You should have received a copy of the GNU Lesser General Public License | 
| 21 | * along with this program; if not, write to the Free Software | 
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 23 | * | 
| 24 | */ | 
| 25 |  | 
| 26 | /** | 
| 27 | * @file Vector.hpp | 
| 28 | * @author Teng Lin | 
| 29 | * @date 09/14/2004 | 
| 30 | * @version 1.0 | 
| 31 | */ | 
| 32 |  | 
| 33 | #ifndef MATH_VECTOR_HPP | 
| 34 | #define MATH_VECTOR_HPP | 
| 35 |  | 
| 36 | #include <cassert> | 
| 37 | #include <cmath> | 
| 38 | #include <iostream> | 
| 39 |  | 
| 40 | namespace oopse { | 
| 41 |  | 
| 42 | const double epsilon = 0.000001; | 
| 43 |  | 
| 44 | template<typename T> | 
| 45 | inline bool equal(T e1, T e2) { | 
| 46 | return e1 == e2; | 
| 47 | } | 
| 48 |  | 
| 49 | template<> | 
| 50 | inline bool equal(float e1, float e2) { | 
| 51 | return fabs(e1 - e2) < epsilon; | 
| 52 | } | 
| 53 |  | 
| 54 | template<> | 
| 55 | inline bool equal(double e1, double e2) { | 
| 56 | return fabs(e1 - e2) < epsilon; | 
| 57 | } | 
| 58 |  | 
| 59 | /** | 
| 60 | * @class Vector Vector.hpp "math/Vector.hpp" | 
| 61 | * @brief Fix length vector class | 
| 62 | */ | 
| 63 | template<typename Real, unsigned int Dim> | 
| 64 | class Vector{ | 
| 65 | public: | 
| 66 |  | 
| 67 | /** default constructor */ | 
| 68 | inline Vector(){ | 
| 69 | for (unsigned int i = 0; i < Dim; i++) | 
| 70 | data_[i] = 0.0; | 
| 71 | } | 
| 72 |  | 
| 73 | /** Constructs and initializes a Vector from a vector */ | 
| 74 | inline Vector(const Vector<Real, Dim>& v) { | 
| 75 | *this  = v; | 
| 76 | } | 
| 77 |  | 
| 78 | /** copy assignment operator */ | 
| 79 | inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) { | 
| 80 | if (this == &v) | 
| 81 | return *this; | 
| 82 |  | 
| 83 | for (unsigned int i = 0; i < Dim; i++) | 
| 84 | data_[i] = v[i]; | 
| 85 |  | 
| 86 | return *this; | 
| 87 | } | 
| 88 |  | 
| 89 | /** Constructs and initializes a Vector from an array */ | 
| 90 | inline Vector( double* v) { | 
| 91 | for (unsigned int i = 0; i < Dim; i++) | 
| 92 | data_[i] = v[i]; | 
| 93 | } | 
| 94 |  | 
| 95 | /** | 
| 96 | * Returns reference of ith element. | 
| 97 | * @return reference of ith element | 
| 98 | * @param i index | 
| 99 | */ | 
| 100 | inline double& operator[](unsigned int  i) { | 
| 101 | assert( i < Dim); | 
| 102 | return data_[i]; | 
| 103 | } | 
| 104 |  | 
| 105 | /** | 
| 106 | * Returns reference of ith element. | 
| 107 | * @return reference of ith element | 
| 108 | * @param i index | 
| 109 | */ | 
| 110 | inline double& operator()(unsigned int  i) { | 
| 111 | assert( i < Dim); | 
| 112 | return data_[i]; | 
| 113 | } | 
| 114 |  | 
| 115 | /** | 
| 116 | * Returns constant reference of ith element. | 
| 117 | * @return reference of ith element | 
| 118 | * @param i index | 
| 119 | */ | 
| 120 | inline  const double& operator[](unsigned int i) const { | 
| 121 | assert( i < Dim); | 
| 122 | return data_[i]; | 
| 123 | } | 
| 124 |  | 
| 125 | /** | 
| 126 | * Returns constant reference of ith element. | 
| 127 | * @return reference of ith element | 
| 128 | * @param i index | 
| 129 | */ | 
| 130 | inline  const double& operator()(unsigned int i) const { | 
| 131 | assert( i < Dim); | 
| 132 | return data_[i]; | 
| 133 | } | 
| 134 |  | 
| 135 | /** | 
| 136 | * Tests if this vetor is equal to other vector | 
| 137 | * @return true if equal, otherwise return false | 
| 138 | * @param v vector to be compared | 
| 139 | */ | 
| 140 | inline bool operator ==(const Vector<Real, Dim>& v) { | 
| 141 |  | 
| 142 | for (unsigned int i = 0; i < Dim; i ++) { | 
| 143 | if (!equal(data_[i], v[i])) { | 
| 144 | return false; | 
| 145 | } | 
| 146 | } | 
| 147 |  | 
| 148 | return true; | 
| 149 | } | 
| 150 |  | 
| 151 | /** | 
| 152 | * Tests if this vetor is not equal to other vector | 
| 153 | * @return true if equal, otherwise return false | 
| 154 | * @param v vector to be compared | 
| 155 | */ | 
| 156 | inline bool operator !=(const Vector<Real, Dim>& v) { | 
| 157 | return !(*this == v); | 
| 158 | } | 
| 159 |  | 
| 160 | /** Negates the value of this vector in place. */ | 
| 161 | inline void negate() { | 
| 162 | data_[0] = -data_[0]; | 
| 163 | data_[1] = -data_[1]; | 
| 164 | data_[2] = -data_[2]; | 
| 165 | } | 
| 166 |  | 
| 167 | /** | 
| 168 | * Sets the value of this vector to the negation of vector v1. | 
| 169 | * @param v1 the source vector | 
| 170 | */ | 
| 171 | inline void negate(const Vector<Real, Dim>& v1) { | 
| 172 | for (unsigned int i = 0; i < Dim; i++) | 
| 173 | data_[i] = -v1.data_[i]; | 
| 174 |  | 
| 175 | } | 
| 176 |  | 
| 177 | /** | 
| 178 | * Sets the value of this vector to the sum of itself and v1 (*this += v1). | 
| 179 | * @param v1 the other vector | 
| 180 | */ | 
| 181 | inline void add( const Vector<Real, Dim>& v1 ) { | 
| 182 | for (unsigned int i = 0; i < Dim; i++) | 
| 183 | data_[i] += v1.data_[i]; | 
| 184 | } | 
| 185 |  | 
| 186 | /** | 
| 187 | * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). | 
| 188 | * @param v1 the first vector | 
| 189 | * @param v2 the second vector | 
| 190 | */ | 
| 191 | inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 192 | for (unsigned int i = 0; i < Dim; i++) | 
| 193 | data_[i] = v1.data_[i] + v2.data_[i]; | 
| 194 | } | 
| 195 |  | 
| 196 | /** | 
| 197 | * Sets the value of this vector to the difference  of itself and v1 (*this -= v1). | 
| 198 | * @param v1 the other vector | 
| 199 | */ | 
| 200 | inline void sub( const Vector<Real, Dim>& v1 ) { | 
| 201 | for (unsigned int i = 0; i < Dim; i++) | 
| 202 | data_[i] -= v1.data_[i]; | 
| 203 | } | 
| 204 |  | 
| 205 | /** | 
| 206 | * Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2). | 
| 207 | * @param v1 the first vector | 
| 208 | * @param v2 the second vector | 
| 209 | */ | 
| 210 | inline void sub( const Vector<Real, Dim>& v1, const Vector  &v2 ){ | 
| 211 | for (unsigned int i = 0; i < Dim; i++) | 
| 212 | data_[i] = v1.data_[i] - v2.data_[i]; | 
| 213 | } | 
| 214 |  | 
| 215 | /** | 
| 216 | * Sets the value of this vector to the scalar multiplication of itself (*this *= s). | 
| 217 | * @param s the scalar value | 
| 218 | */ | 
| 219 | inline void mul( double s ) { | 
| 220 | for (unsigned int i = 0; i < Dim; i++) | 
| 221 | data_[i] *= s; | 
| 222 | } | 
| 223 |  | 
| 224 | /** | 
| 225 | * Sets the value of this vector to the scalar multiplication of vector v1 | 
| 226 | * (*this = s * v1). | 
| 227 | * @param s the scalar value | 
| 228 | * @param v1 the vector | 
| 229 | */ | 
| 230 | inline void mul( double s, const Vector<Real, Dim>& v1 ) { | 
| 231 | for (unsigned int i = 0; i < Dim; i++) | 
| 232 | data_[i] = s * v1.data_[i]; | 
| 233 | } | 
| 234 |  | 
| 235 | /** | 
| 236 | * Sets the value of this vector to the scalar division of itself  (*this /= s ). | 
| 237 | * @param s the scalar value | 
| 238 | */ | 
| 239 | inline void div( double s) { | 
| 240 | for (unsigned int i = 0; i < Dim; i++) | 
| 241 | data_[i] /= s; | 
| 242 | } | 
| 243 |  | 
| 244 | /** | 
| 245 | * Sets the value of this vector to the scalar division of vector v1  (*this = v1 / s ). | 
| 246 | * @param v1 the source vector | 
| 247 | * @param s the scalar value | 
| 248 | */ | 
| 249 | inline void div( const Vector<Real, Dim>& v1, double s ) { | 
| 250 | for (unsigned int i = 0; i < Dim; i++) | 
| 251 | data_[i] = v1.data_[i] / s; | 
| 252 | } | 
| 253 |  | 
| 254 | /** @see #add */ | 
| 255 | inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) { | 
| 256 | add(v1); | 
| 257 | return *this; | 
| 258 | } | 
| 259 |  | 
| 260 | /** @see #sub */ | 
| 261 | inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) { | 
| 262 | sub(v1); | 
| 263 | return *this; | 
| 264 | } | 
| 265 |  | 
| 266 | /** @see #mul */ | 
| 267 | inline Vector<Real, Dim>& operator *=( double s) { | 
| 268 | mul(s); | 
| 269 | return *this; | 
| 270 | } | 
| 271 |  | 
| 272 | /** @see #div */ | 
| 273 | inline Vector<Real, Dim>& operator /=( double s ) { | 
| 274 | div(s); | 
| 275 | return *this; | 
| 276 | } | 
| 277 |  | 
| 278 | /** | 
| 279 | * Returns the length of this vector. | 
| 280 | * @return the length of this vector | 
| 281 | */ | 
| 282 | inline double length() { | 
| 283 | return sqrt(lengthSquared()); | 
| 284 | } | 
| 285 |  | 
| 286 | /** | 
| 287 | * Returns the squared length of this vector. | 
| 288 | * @return the squared length of this vector | 
| 289 | */ | 
| 290 | inline double lengthSquared() { | 
| 291 | return dot(*this, *this); | 
| 292 | } | 
| 293 |  | 
| 294 | /** Normalizes this vector in place */ | 
| 295 | inline void normalize() { | 
| 296 | double len; | 
| 297 |  | 
| 298 | len = length(); | 
| 299 | *this /= len; | 
| 300 | } | 
| 301 |  | 
| 302 | protected: | 
| 303 | double data_[3]; | 
| 304 |  | 
| 305 | }; | 
| 306 |  | 
| 307 | /** unary minus*/ | 
| 308 | template<typename Real, unsigned int Dim> | 
| 309 | inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ | 
| 310 | Vector tmp(v1); | 
| 311 | return tmp.negate(); | 
| 312 | } | 
| 313 |  | 
| 314 | /** | 
| 315 | * Return the sum of two vectors  (v1 - v2). | 
| 316 | * @return the sum of two vectors | 
| 317 | * @param v1 the first vector | 
| 318 | * @param v2 the second vector | 
| 319 | */ | 
| 320 | template<typename Real, unsigned int Dim> | 
| 321 | inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { | 
| 322 | Vector<Real, Dim> result; | 
| 323 |  | 
| 324 | result.add(v1, v2); | 
| 325 | return result; | 
| 326 | } | 
| 327 |  | 
| 328 | /** | 
| 329 | * Return the difference of two vectors  (v1 - v2). | 
| 330 | * @return the difference of two vectors | 
| 331 | * @param v1 the first vector | 
| 332 | * @param v2 the second vector | 
| 333 | */ | 
| 334 | template<typename Real, unsigned int Dim> | 
| 335 | Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { | 
| 336 | Vector<Real, Dim> result; | 
| 337 | result.sub(v1, v2); | 
| 338 | return result; | 
| 339 | } | 
| 340 |  | 
| 341 | /** | 
| 342 | * Returns the vaule of scalar multiplication of this vector v1 (v1 * r). | 
| 343 | * @return  the vaule of scalar multiplication of this vector | 
| 344 | * @param v1 the source vector | 
| 345 | * @param s the scalar value | 
| 346 | */ | 
| 347 | template<typename Real, unsigned int Dim> | 
| 348 | Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { | 
| 349 | Vector<Real, Dim> result; | 
| 350 | result.mul(s, v1); | 
| 351 | return result; | 
| 352 | } | 
| 353 |  | 
| 354 | /** | 
| 355 | * Returns the vaule of scalar multiplication of this vector v1 (v1 * r). | 
| 356 | * @return  the vaule of scalar multiplication of this vector | 
| 357 | * @param s the scalar value | 
| 358 | * @param v1 the source vector | 
| 359 | */ | 
| 360 | template<typename Real, unsigned int Dim> | 
| 361 | Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { | 
| 362 | Vector<Real, Dim> result; | 
| 363 | result.mul(s, v1); | 
| 364 | return result; | 
| 365 | } | 
| 366 |  | 
| 367 | /** | 
| 368 | * Returns the  value of division of a vector by a scalar. | 
| 369 | * @return  the vaule of scalar division of this vector | 
| 370 | * @param v1 the source vector | 
| 371 | * @param s the scalar value | 
| 372 | */ | 
| 373 | template<typename Real, unsigned int Dim> | 
| 374 | Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) { | 
| 375 | Vector<Real, Dim> result; | 
| 376 | result.div( v1,s); | 
| 377 | return result; | 
| 378 | } | 
| 379 |  | 
| 380 | /** | 
| 381 | * Returns the  value of division of a vector by a scalar. | 
| 382 | * @return  the vaule of scalar division of this vector | 
| 383 | * @param s the scalar value | 
| 384 | * @param v1 the source vector | 
| 385 | */ | 
| 386 | template<typename Real, unsigned int Dim> | 
| 387 | inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) { | 
| 388 | Vector<Real, Dim> result; | 
| 389 | result.div( v1,s); | 
| 390 | return result; | 
| 391 | } | 
| 392 |  | 
| 393 | /** fuzzy comparson */ | 
| 394 | template<typename Real, unsigned int Dim> | 
| 395 | inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 396 |  | 
| 397 | } | 
| 398 |  | 
| 399 |  | 
| 400 | /** | 
| 401 | * Returns the dot product of two Vectors | 
| 402 | * @param v1 first vector | 
| 403 | * @param v2 second vector | 
| 404 | * @return the dot product of v1 and v2 | 
| 405 | */ | 
| 406 | template<typename Real, unsigned int Dim> | 
| 407 | inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 408 | Real tmp; | 
| 409 | tmp = 0; | 
| 410 |  | 
| 411 | for (unsigned int i = 0; i < Dim; i++) | 
| 412 | tmp += v1[i] + v2[i]; | 
| 413 |  | 
| 414 | return tmp; | 
| 415 | } | 
| 416 |  | 
| 417 | /** | 
| 418 | * Returns the distance between  two Vectors | 
| 419 | * @param v1 first vector | 
| 420 | * @param v2 second vector | 
| 421 | * @return the distance between v1 and v2 | 
| 422 | */ | 
| 423 | template<typename Real, unsigned int Dim> | 
| 424 | inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 425 | Vector<Real, Dim> tempVector = v1 - v2; | 
| 426 | return tempVector.length(); | 
| 427 | } | 
| 428 |  | 
| 429 | /** | 
| 430 | * Returns the squared distance between  two Vectors | 
| 431 | * @param v1 first vector | 
| 432 | * @param v2 second vector | 
| 433 | * @return the squared distance between v1 and v2 | 
| 434 | */ | 
| 435 | template<typename Real, unsigned int Dim> | 
| 436 | inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 437 | Vector<Real, Dim> tempVector = v1 - v2; | 
| 438 | return tempVector.lengthSquare(); | 
| 439 | } | 
| 440 |  | 
| 441 | /** | 
| 442 | * Write to an output stream | 
| 443 | */ | 
| 444 | template<typename Real, unsigned int Dim> | 
| 445 | std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { | 
| 446 |  | 
| 447 | return o; | 
| 448 | } | 
| 449 |  | 
| 450 | } | 
| 451 | #endif |