| 1 | tim | 70 | /* | 
| 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 | tim | 71 | #include <iostream> | 
| 39 | tim | 70 |  | 
| 40 |  |  | namespace oopse { | 
| 41 |  |  |  | 
| 42 | tim | 76 | 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 | tim | 70 | /** | 
| 60 |  |  | * @class Vector Vector.hpp "math/Vector.hpp" | 
| 61 |  |  | * @brief Fix length vector class | 
| 62 |  |  | */ | 
| 63 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 64 | tim | 70 | 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 | tim | 101 |  | 
| 89 |  |  | template<typename T> | 
| 90 |  |  | inline Vector(const T& s){ | 
| 91 |  |  | for (unsigned int i = 0; i < Dim; i++) | 
| 92 |  |  | data_[i] = s; | 
| 93 |  |  | } | 
| 94 | tim | 70 |  | 
| 95 |  |  | /** Constructs and initializes a Vector from an array */ | 
| 96 |  |  | inline Vector( double* v) { | 
| 97 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 98 |  |  | data_[i] = v[i]; | 
| 99 | tim | 70 | } | 
| 100 |  |  |  | 
| 101 |  |  | /** | 
| 102 |  |  | * Returns reference of ith element. | 
| 103 |  |  | * @return reference of ith element | 
| 104 |  |  | * @param i index | 
| 105 |  |  | */ | 
| 106 |  |  | inline double& operator[](unsigned int  i) { | 
| 107 |  |  | assert( i < Dim); | 
| 108 |  |  | return data_[i]; | 
| 109 |  |  | } | 
| 110 |  |  |  | 
| 111 |  |  | /** | 
| 112 |  |  | * Returns reference of ith element. | 
| 113 |  |  | * @return reference of ith element | 
| 114 |  |  | * @param i index | 
| 115 |  |  | */ | 
| 116 |  |  | inline double& operator()(unsigned int  i) { | 
| 117 |  |  | assert( i < Dim); | 
| 118 |  |  | return data_[i]; | 
| 119 |  |  | } | 
| 120 |  |  |  | 
| 121 |  |  | /** | 
| 122 |  |  | * Returns constant reference of ith element. | 
| 123 |  |  | * @return reference of ith element | 
| 124 |  |  | * @param i index | 
| 125 |  |  | */ | 
| 126 |  |  | inline  const double& operator[](unsigned int i) const { | 
| 127 |  |  | assert( i < Dim); | 
| 128 |  |  | return data_[i]; | 
| 129 |  |  | } | 
| 130 |  |  |  | 
| 131 |  |  | /** | 
| 132 |  |  | * Returns constant reference of ith element. | 
| 133 |  |  | * @return reference of ith element | 
| 134 |  |  | * @param i index | 
| 135 |  |  | */ | 
| 136 |  |  | inline  const double& operator()(unsigned int i) const { | 
| 137 |  |  | assert( i < Dim); | 
| 138 |  |  | return data_[i]; | 
| 139 |  |  | } | 
| 140 |  |  |  | 
| 141 | tim | 76 | /** | 
| 142 |  |  | * Tests if this vetor is equal to other vector | 
| 143 |  |  | * @return true if equal, otherwise return false | 
| 144 |  |  | * @param v vector to be compared | 
| 145 |  |  | */ | 
| 146 |  |  | inline bool operator ==(const Vector<Real, Dim>& v) { | 
| 147 |  |  |  | 
| 148 |  |  | for (unsigned int i = 0; i < Dim; i ++) { | 
| 149 |  |  | if (!equal(data_[i], v[i])) { | 
| 150 |  |  | return false; | 
| 151 |  |  | } | 
| 152 |  |  | } | 
| 153 |  |  |  | 
| 154 |  |  | return true; | 
| 155 |  |  | } | 
| 156 |  |  |  | 
| 157 |  |  | /** | 
| 158 |  |  | * Tests if this vetor is not equal to other vector | 
| 159 |  |  | * @return true if equal, otherwise return false | 
| 160 |  |  | * @param v vector to be compared | 
| 161 |  |  | */ | 
| 162 |  |  | inline bool operator !=(const Vector<Real, Dim>& v) { | 
| 163 |  |  | return !(*this == v); | 
| 164 |  |  | } | 
| 165 |  |  |  | 
| 166 | tim | 70 | /** Negates the value of this vector in place. */ | 
| 167 |  |  | inline void negate() { | 
| 168 | tim | 101 | for (unsigned int i = 0; i < Dim; i++) | 
| 169 |  |  | data_[i] = -data_[i]; | 
| 170 | tim | 70 | } | 
| 171 |  |  |  | 
| 172 |  |  | /** | 
| 173 |  |  | * Sets the value of this vector to the negation of vector v1. | 
| 174 |  |  | * @param v1 the source vector | 
| 175 |  |  | */ | 
| 176 |  |  | inline void negate(const Vector<Real, Dim>& v1) { | 
| 177 |  |  | for (unsigned int i = 0; i < Dim; i++) | 
| 178 |  |  | data_[i] = -v1.data_[i]; | 
| 179 |  |  |  | 
| 180 |  |  | } | 
| 181 |  |  |  | 
| 182 |  |  | /** | 
| 183 |  |  | * Sets the value of this vector to the sum of itself and v1 (*this += v1). | 
| 184 |  |  | * @param v1 the other vector | 
| 185 |  |  | */ | 
| 186 |  |  | inline void add( const Vector<Real, Dim>& v1 ) { | 
| 187 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 188 |  |  | data_[i] += v1.data_[i]; | 
| 189 | tim | 70 | } | 
| 190 |  |  |  | 
| 191 |  |  | /** | 
| 192 |  |  | * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). | 
| 193 |  |  | * @param v1 the first vector | 
| 194 |  |  | * @param v2 the second vector | 
| 195 |  |  | */ | 
| 196 |  |  | inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 197 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 198 |  |  | data_[i] = v1.data_[i] + v2.data_[i]; | 
| 199 | tim | 70 | } | 
| 200 |  |  |  | 
| 201 |  |  | /** | 
| 202 |  |  | * Sets the value of this vector to the difference  of itself and v1 (*this -= v1). | 
| 203 |  |  | * @param v1 the other vector | 
| 204 |  |  | */ | 
| 205 |  |  | inline void sub( const Vector<Real, Dim>& v1 ) { | 
| 206 |  |  | for (unsigned int i = 0; i < Dim; i++) | 
| 207 |  |  | data_[i] -= v1.data_[i]; | 
| 208 |  |  | } | 
| 209 |  |  |  | 
| 210 |  |  | /** | 
| 211 |  |  | * Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2). | 
| 212 |  |  | * @param v1 the first vector | 
| 213 |  |  | * @param v2 the second vector | 
| 214 |  |  | */ | 
| 215 |  |  | inline void sub( const Vector<Real, Dim>& v1, const Vector  &v2 ){ | 
| 216 |  |  | for (unsigned int i = 0; i < Dim; i++) | 
| 217 |  |  | data_[i] = v1.data_[i] - v2.data_[i]; | 
| 218 |  |  | } | 
| 219 |  |  |  | 
| 220 |  |  | /** | 
| 221 |  |  | * Sets the value of this vector to the scalar multiplication of itself (*this *= s). | 
| 222 |  |  | * @param s the scalar value | 
| 223 |  |  | */ | 
| 224 |  |  | inline void mul( double s ) { | 
| 225 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 226 | tim | 70 | data_[i] *= s; | 
| 227 |  |  | } | 
| 228 |  |  |  | 
| 229 |  |  | /** | 
| 230 |  |  | * Sets the value of this vector to the scalar multiplication of vector v1 | 
| 231 |  |  | * (*this = s * v1). | 
| 232 | tim | 101 | * @param v1 the vector | 
| 233 | tim | 70 | * @param s the scalar value | 
| 234 |  |  | */ | 
| 235 | tim | 101 | inline void mul( const Vector<Real, Dim>& v1, double s) { | 
| 236 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 237 | tim | 70 | data_[i] = s * v1.data_[i]; | 
| 238 |  |  | } | 
| 239 |  |  |  | 
| 240 |  |  | /** | 
| 241 |  |  | * Sets the value of this vector to the scalar division of itself  (*this /= s ). | 
| 242 |  |  | * @param s the scalar value | 
| 243 |  |  | */ | 
| 244 |  |  | inline void div( double s) { | 
| 245 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 246 | tim | 70 | data_[i] /= s; | 
| 247 |  |  | } | 
| 248 |  |  |  | 
| 249 |  |  | /** | 
| 250 |  |  | * Sets the value of this vector to the scalar division of vector v1  (*this = v1 / s ). | 
| 251 |  |  | * @param v1 the source vector | 
| 252 |  |  | * @param s the scalar value | 
| 253 |  |  | */ | 
| 254 |  |  | inline void div( const Vector<Real, Dim>& v1, double s ) { | 
| 255 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 256 | tim | 70 | data_[i] = v1.data_[i] / s; | 
| 257 |  |  | } | 
| 258 |  |  |  | 
| 259 |  |  | /** @see #add */ | 
| 260 | tim | 71 | inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) { | 
| 261 | tim | 70 | add(v1); | 
| 262 |  |  | return *this; | 
| 263 |  |  | } | 
| 264 |  |  |  | 
| 265 |  |  | /** @see #sub */ | 
| 266 | tim | 71 | inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) { | 
| 267 | tim | 70 | sub(v1); | 
| 268 |  |  | return *this; | 
| 269 |  |  | } | 
| 270 |  |  |  | 
| 271 |  |  | /** @see #mul */ | 
| 272 | tim | 71 | inline Vector<Real, Dim>& operator *=( double s) { | 
| 273 | tim | 70 | mul(s); | 
| 274 |  |  | return *this; | 
| 275 |  |  | } | 
| 276 |  |  |  | 
| 277 |  |  | /** @see #div */ | 
| 278 | tim | 71 | inline Vector<Real, Dim>& operator /=( double s ) { | 
| 279 | tim | 70 | div(s); | 
| 280 |  |  | return *this; | 
| 281 |  |  | } | 
| 282 |  |  |  | 
| 283 |  |  | /** | 
| 284 |  |  | * Returns the length of this vector. | 
| 285 |  |  | * @return the length of this vector | 
| 286 |  |  | */ | 
| 287 |  |  | inline double length() { | 
| 288 | tim | 104 | return sqrt(lengthSquare()); | 
| 289 | tim | 70 | } | 
| 290 |  |  |  | 
| 291 |  |  | /** | 
| 292 |  |  | * Returns the squared length of this vector. | 
| 293 |  |  | * @return the squared length of this vector | 
| 294 |  |  | */ | 
| 295 | tim | 93 | inline double lengthSquare() { | 
| 296 | tim | 70 | return dot(*this, *this); | 
| 297 |  |  | } | 
| 298 |  |  |  | 
| 299 |  |  | /** Normalizes this vector in place */ | 
| 300 |  |  | inline void normalize() { | 
| 301 |  |  | double len; | 
| 302 |  |  |  | 
| 303 |  |  | len = length(); | 
| 304 | tim | 102 |  | 
| 305 |  |  | //if (len < oopse:epsilon) | 
| 306 |  |  | //  throw(); | 
| 307 |  |  |  | 
| 308 | tim | 70 | *this /= len; | 
| 309 |  |  | } | 
| 310 | tim | 93 |  | 
| 311 |  |  | /** | 
| 312 |  |  | * Tests if this vector is normalized | 
| 313 |  |  | * @return true if this vector is normalized, otherwise return false | 
| 314 |  |  | */ | 
| 315 | tim | 104 | inline bool isNormalized() { | 
| 316 |  |  | return equal(lengthSquare(), 1.0); | 
| 317 | tim | 93 | } | 
| 318 | tim | 70 |  | 
| 319 |  |  | protected: | 
| 320 | tim | 101 | double data_[Dim]; | 
| 321 | tim | 70 |  | 
| 322 |  |  | }; | 
| 323 |  |  |  | 
| 324 |  |  | /** unary minus*/ | 
| 325 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 326 | tim | 70 | inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ | 
| 327 | tim | 101 | Vector<Real, Dim> tmp(v1); | 
| 328 |  |  | tmp.negate(); | 
| 329 |  |  | return tmp; | 
| 330 | tim | 70 | } | 
| 331 |  |  |  | 
| 332 |  |  | /** | 
| 333 |  |  | * Return the sum of two vectors  (v1 - v2). | 
| 334 |  |  | * @return the sum of two vectors | 
| 335 |  |  | * @param v1 the first vector | 
| 336 |  |  | * @param v2 the second vector | 
| 337 |  |  | */ | 
| 338 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 339 | tim | 70 | inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { | 
| 340 |  |  | Vector<Real, Dim> result; | 
| 341 |  |  |  | 
| 342 |  |  | result.add(v1, v2); | 
| 343 |  |  | return result; | 
| 344 |  |  | } | 
| 345 |  |  |  | 
| 346 |  |  | /** | 
| 347 |  |  | * Return the difference of two vectors  (v1 - v2). | 
| 348 |  |  | * @return the difference of two vectors | 
| 349 |  |  | * @param v1 the first vector | 
| 350 |  |  | * @param v2 the second vector | 
| 351 |  |  | */ | 
| 352 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 353 | tim | 70 | Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { | 
| 354 |  |  | Vector<Real, Dim> result; | 
| 355 |  |  | result.sub(v1, v2); | 
| 356 |  |  | return result; | 
| 357 |  |  | } | 
| 358 |  |  |  | 
| 359 |  |  | /** | 
| 360 |  |  | * Returns the vaule of scalar multiplication of this vector v1 (v1 * r). | 
| 361 |  |  | * @return  the vaule of scalar multiplication of this vector | 
| 362 |  |  | * @param v1 the source vector | 
| 363 |  |  | * @param s the scalar value | 
| 364 |  |  | */ | 
| 365 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 366 | tim | 70 | Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { | 
| 367 |  |  | Vector<Real, Dim> result; | 
| 368 | tim | 101 | result.mul(v1,s); | 
| 369 | tim | 70 | return result; | 
| 370 |  |  | } | 
| 371 |  |  |  | 
| 372 |  |  | /** | 
| 373 |  |  | * Returns the vaule of scalar multiplication of this vector v1 (v1 * r). | 
| 374 |  |  | * @return  the vaule of scalar multiplication of this vector | 
| 375 |  |  | * @param s the scalar value | 
| 376 |  |  | * @param v1 the source vector | 
| 377 |  |  | */ | 
| 378 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 379 | tim | 70 | Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { | 
| 380 |  |  | Vector<Real, Dim> result; | 
| 381 | tim | 101 | result.mul(v1, s); | 
| 382 | tim | 70 | return result; | 
| 383 |  |  | } | 
| 384 |  |  |  | 
| 385 |  |  | /** | 
| 386 |  |  | * Returns the  value of division of a vector by a scalar. | 
| 387 |  |  | * @return  the vaule of scalar division of this vector | 
| 388 |  |  | * @param v1 the source vector | 
| 389 |  |  | * @param s the scalar value | 
| 390 |  |  | */ | 
| 391 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 392 | tim | 70 | Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) { | 
| 393 |  |  | Vector<Real, Dim> result; | 
| 394 |  |  | result.div( v1,s); | 
| 395 |  |  | return result; | 
| 396 |  |  | } | 
| 397 |  |  |  | 
| 398 |  |  | /** | 
| 399 |  |  | * Returns the dot product of two Vectors | 
| 400 |  |  | * @param v1 first vector | 
| 401 |  |  | * @param v2 second vector | 
| 402 |  |  | * @return the dot product of v1 and v2 | 
| 403 |  |  | */ | 
| 404 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 405 | tim | 70 | inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 406 | tim | 93 | Real tmp; | 
| 407 |  |  | tmp = 0; | 
| 408 | tim | 70 |  | 
| 409 | tim | 93 | for (unsigned int i = 0; i < Dim; i++) | 
| 410 |  |  | tmp += v1[i] + v2[i]; | 
| 411 |  |  |  | 
| 412 |  |  | return tmp; | 
| 413 | tim | 70 | } | 
| 414 |  |  |  | 
| 415 |  |  | /** | 
| 416 |  |  | * Returns the distance between  two Vectors | 
| 417 |  |  | * @param v1 first vector | 
| 418 |  |  | * @param v2 second vector | 
| 419 |  |  | * @return the distance between v1 and v2 | 
| 420 |  |  | */ | 
| 421 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 422 | tim | 70 | inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 423 |  |  | Vector<Real, Dim> tempVector = v1 - v2; | 
| 424 |  |  | return tempVector.length(); | 
| 425 |  |  | } | 
| 426 |  |  |  | 
| 427 |  |  | /** | 
| 428 |  |  | * Returns the squared distance between  two Vectors | 
| 429 |  |  | * @param v1 first vector | 
| 430 |  |  | * @param v2 second vector | 
| 431 |  |  | * @return the squared distance between v1 and v2 | 
| 432 |  |  | */ | 
| 433 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 434 | tim | 70 | inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 435 |  |  | Vector<Real, Dim> tempVector = v1 - v2; | 
| 436 |  |  | return tempVector.lengthSquare(); | 
| 437 |  |  | } | 
| 438 |  |  |  | 
| 439 |  |  | /** | 
| 440 |  |  | * Write to an output stream | 
| 441 |  |  | */ | 
| 442 | tim | 71 | template<typename Real, unsigned int Dim> | 
| 443 | tim | 93 | std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { | 
| 444 |  |  |  | 
| 445 |  |  | o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; | 
| 446 | tim | 70 | return o; | 
| 447 |  |  | } | 
| 448 |  |  |  | 
| 449 |  |  | } | 
| 450 |  |  | #endif |