| 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. |
| 2 |
> |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
| 3 |
|
* |
| 4 |
+ |
* The University of Notre Dame grants you ("Licensee") a |
| 5 |
+ |
* non-exclusive, royalty free, license to use, modify and |
| 6 |
+ |
* redistribute this software in source and binary code form, provided |
| 7 |
+ |
* that the following conditions are met: |
| 8 |
+ |
* |
| 9 |
+ |
* 1. Acknowledgement of the program authors must be made in any |
| 10 |
+ |
* publication of scientific results based in part on use of the |
| 11 |
+ |
* program. An acceptable form of acknowledgement is citation of |
| 12 |
+ |
* the article in which the program was described (Matthew |
| 13 |
+ |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
| 14 |
+ |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
| 15 |
+ |
* Parallel Simulation Engine for Molecular Dynamics," |
| 16 |
+ |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
| 17 |
+ |
* |
| 18 |
+ |
* 2. Redistributions of source code must retain the above copyright |
| 19 |
+ |
* notice, this list of conditions and the following disclaimer. |
| 20 |
+ |
* |
| 21 |
+ |
* 3. Redistributions in binary form must reproduce the above copyright |
| 22 |
+ |
* notice, this list of conditions and the following disclaimer in the |
| 23 |
+ |
* documentation and/or other materials provided with the |
| 24 |
+ |
* distribution. |
| 25 |
+ |
* |
| 26 |
+ |
* This software is provided "AS IS," without a warranty of any |
| 27 |
+ |
* kind. All express or implied conditions, representations and |
| 28 |
+ |
* warranties, including any implied warranty of merchantability, |
| 29 |
+ |
* fitness for a particular purpose or non-infringement, are hereby |
| 30 |
+ |
* excluded. The University of Notre Dame and its licensors shall not |
| 31 |
+ |
* be liable for any damages suffered by licensee as a result of |
| 32 |
+ |
* using, modifying or distributing the software or its |
| 33 |
+ |
* derivatives. In no event will the University of Notre Dame or its |
| 34 |
+ |
* licensors be liable for any lost revenue, profit or data, or for |
| 35 |
+ |
* direct, indirect, special, consequential, incidental or punitive |
| 36 |
+ |
* damages, however caused and regardless of the theory of liability, |
| 37 |
+ |
* arising out of the use of or inability to use software, even if the |
| 38 |
+ |
* University of Notre Dame has been advised of the possibility of |
| 39 |
+ |
* such damages. |
| 40 |
|
*/ |
| 41 |
< |
|
| 41 |
> |
|
| 42 |
|
/** |
| 43 |
|
* @file Vector.hpp |
| 44 |
|
* @author Teng Lin |
| 51 |
|
|
| 52 |
|
#include <cassert> |
| 53 |
|
#include <cmath> |
| 54 |
< |
|
| 54 |
> |
#include <iostream> |
| 55 |
> |
#include <math.h> |
| 56 |
> |
#include "config.h" |
| 57 |
|
namespace oopse { |
| 58 |
|
|
| 59 |
< |
/** |
| 42 |
< |
* @class Vector Vector.hpp "math/Vector.hpp" |
| 43 |
< |
* @brief Fix length vector class |
| 44 |
< |
*/ |
| 45 |
< |
template<typename Real, int Dim> |
| 46 |
< |
class Vector{ |
| 47 |
< |
public: |
| 59 |
> |
static const RealType epsilon = 0.000001; |
| 60 |
|
|
| 61 |
< |
/** default constructor */ |
| 62 |
< |
inline Vector(){ |
| 63 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 64 |
< |
data_[i] = 0.0; |
| 53 |
< |
} |
| 61 |
> |
template<typename T> |
| 62 |
> |
inline bool equal(T e1, T e2) { |
| 63 |
> |
return e1 == e2; |
| 64 |
> |
} |
| 65 |
|
|
| 66 |
< |
/** Constructs and initializes a Vector from a vector */ |
| 67 |
< |
inline Vector(const Vector<Real, Dim>& v) { |
| 68 |
< |
*this = v; |
| 69 |
< |
} |
| 66 |
> |
//template<> |
| 67 |
> |
//inline bool equal(float e1, float e2) { |
| 68 |
> |
// return fabs(e1 - e2) < epsilon; |
| 69 |
> |
//} |
| 70 |
|
|
| 71 |
< |
/** copy assignment operator */ |
| 72 |
< |
inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) { |
| 73 |
< |
if (this == &v) |
| 74 |
< |
return *this; |
| 64 |
< |
|
| 65 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 66 |
< |
data_[i] = v[i]; |
| 67 |
< |
|
| 68 |
< |
return *this; |
| 69 |
< |
} |
| 70 |
< |
|
| 71 |
< |
/** Constructs and initializes a Vector from an array */ |
| 72 |
< |
inline Vector( double* v) { |
| 73 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 74 |
< |
data_[i] = v[i]; |
| 75 |
< |
} |
| 71 |
> |
template<> |
| 72 |
> |
inline bool equal(RealType e1, RealType e2) { |
| 73 |
> |
return fabs(e1 - e2) < epsilon; |
| 74 |
> |
} |
| 75 |
|
|
| 76 |
< |
/** |
| 77 |
< |
* Returns reference of ith element. |
| 78 |
< |
* @return reference of ith element |
| 79 |
< |
* @param i index |
| 80 |
< |
*/ |
| 81 |
< |
inline double& operator[](unsigned int i) { |
| 82 |
< |
assert( i < Dim); |
| 83 |
< |
return data_[i]; |
| 85 |
< |
} |
| 76 |
> |
|
| 77 |
> |
/** |
| 78 |
> |
* @class Vector Vector.hpp "math/Vector.hpp" |
| 79 |
> |
* @brief Fix length vector class |
| 80 |
> |
*/ |
| 81 |
> |
template<typename Real, unsigned int Dim> |
| 82 |
> |
class Vector{ |
| 83 |
> |
public: |
| 84 |
|
|
| 85 |
< |
/** |
| 86 |
< |
* Returns reference of ith element. |
| 89 |
< |
* @return reference of ith element |
| 90 |
< |
* @param i index |
| 91 |
< |
*/ |
| 92 |
< |
inline double& operator()(unsigned int i) { |
| 93 |
< |
assert( i < Dim); |
| 94 |
< |
return data_[i]; |
| 95 |
< |
} |
| 85 |
> |
typedef Real ElemType; |
| 86 |
> |
typedef Real* ElemPoinerType; |
| 87 |
|
|
| 88 |
< |
/** |
| 89 |
< |
* Returns constant reference of ith element. |
| 90 |
< |
* @return reference of ith element |
| 91 |
< |
* @param i index |
| 92 |
< |
*/ |
| 102 |
< |
inline const double& operator[](unsigned int i) const { |
| 103 |
< |
assert( i < Dim); |
| 104 |
< |
return data_[i]; |
| 105 |
< |
} |
| 88 |
> |
/** default constructor */ |
| 89 |
> |
inline Vector(){ |
| 90 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 91 |
> |
this->data_[i] = 0; |
| 92 |
> |
} |
| 93 |
|
|
| 94 |
< |
/** |
| 95 |
< |
* Returns constant reference of ith element. |
| 96 |
< |
* @return reference of ith element |
| 97 |
< |
* @param i index |
| 111 |
< |
*/ |
| 112 |
< |
inline const double& operator()(unsigned int i) const { |
| 113 |
< |
assert( i < Dim); |
| 114 |
< |
return data_[i]; |
| 115 |
< |
} |
| 94 |
> |
/** Constructs and initializes a Vector from a vector */ |
| 95 |
> |
inline Vector(const Vector<Real, Dim>& v) { |
| 96 |
> |
*this = v; |
| 97 |
> |
} |
| 98 |
|
|
| 99 |
< |
/** Negates the value of this vector in place. */ |
| 100 |
< |
inline void negate() { |
| 101 |
< |
data_[0] = -data_[0]; |
| 102 |
< |
data_[1] = -data_[1]; |
| 103 |
< |
data_[2] = -data_[2]; |
| 104 |
< |
} |
| 99 |
> |
/** copy assignment operator */ |
| 100 |
> |
inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) { |
| 101 |
> |
if (this == &v) |
| 102 |
> |
return *this; |
| 103 |
> |
|
| 104 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 105 |
> |
this->data_[i] = v[i]; |
| 106 |
> |
|
| 107 |
> |
return *this; |
| 108 |
> |
} |
| 109 |
|
|
| 110 |
< |
/** |
| 111 |
< |
* Sets the value of this vector to the negation of vector v1. |
| 112 |
< |
* @param v1 the source vector |
| 113 |
< |
*/ |
| 114 |
< |
inline void negate(const Vector<Real, Dim>& v1) { |
| 129 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 130 |
< |
data_[i] = -v1.data_[i]; |
| 131 |
< |
|
| 132 |
< |
} |
| 110 |
> |
template<typename T> |
| 111 |
> |
inline Vector(const T& s){ |
| 112 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 113 |
> |
this->data_[i] = s; |
| 114 |
> |
} |
| 115 |
|
|
| 116 |
< |
/** |
| 117 |
< |
* Sets the value of this vector to the sum of itself and v1 (*this += v1). |
| 118 |
< |
* @param v1 the other vector |
| 119 |
< |
*/ |
| 120 |
< |
inline void add( const Vector<Real, Dim>& v1 ) { |
| 139 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 140 |
< |
data_[i] += v1.data_[i]; |
| 141 |
< |
} |
| 116 |
> |
/** Constructs and initializes a Vector from an array */ |
| 117 |
> |
inline Vector( Real* v) { |
| 118 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 119 |
> |
this->data_[i] = v[i]; |
| 120 |
> |
} |
| 121 |
|
|
| 122 |
< |
/** |
| 123 |
< |
* Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). |
| 124 |
< |
* @param v1 the first vector |
| 125 |
< |
* @param v2 the second vector |
| 126 |
< |
*/ |
| 127 |
< |
inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 128 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 129 |
< |
data_[i] = v1.data_[i] + v2.data_[i]; |
| 130 |
< |
} |
| 122 |
> |
/** |
| 123 |
> |
* Returns reference of ith element. |
| 124 |
> |
* @return reference of ith element |
| 125 |
> |
* @param i index |
| 126 |
> |
*/ |
| 127 |
> |
inline Real& operator[](unsigned int i) { |
| 128 |
> |
assert( i < Dim); |
| 129 |
> |
return this->data_[i]; |
| 130 |
> |
} |
| 131 |
|
|
| 132 |
< |
/** |
| 133 |
< |
* Sets the value of this vector to the difference of itself and v1 (*this -= v1). |
| 134 |
< |
* @param v1 the other vector |
| 135 |
< |
*/ |
| 136 |
< |
inline void sub( const Vector<Real, Dim>& v1 ) { |
| 137 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 138 |
< |
data_[i] -= v1.data_[i]; |
| 139 |
< |
} |
| 132 |
> |
/** |
| 133 |
> |
* Returns reference of ith element. |
| 134 |
> |
* @return reference of ith element |
| 135 |
> |
* @param i index |
| 136 |
> |
*/ |
| 137 |
> |
inline Real& operator()(unsigned int i) { |
| 138 |
> |
assert( i < Dim); |
| 139 |
> |
return this->data_[i]; |
| 140 |
> |
} |
| 141 |
|
|
| 142 |
< |
/** |
| 143 |
< |
* Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2). |
| 144 |
< |
* @param v1 the first vector |
| 145 |
< |
* @param v2 the second vector |
| 146 |
< |
*/ |
| 147 |
< |
inline void sub( const Vector<Real, Dim>& v1, const Vector &v2 ){ |
| 148 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 149 |
< |
data_[i] = v1.data_[i] - v2.data_[i]; |
| 150 |
< |
} |
| 142 |
> |
/** |
| 143 |
> |
* Returns constant reference of ith element. |
| 144 |
> |
* @return reference of ith element |
| 145 |
> |
* @param i index |
| 146 |
> |
*/ |
| 147 |
> |
inline const Real& operator[](unsigned int i) const { |
| 148 |
> |
assert( i < Dim); |
| 149 |
> |
return this->data_[i]; |
| 150 |
> |
} |
| 151 |
|
|
| 152 |
< |
/** |
| 153 |
< |
* Sets the value of this vector to the scalar multiplication of itself (*this *= s). |
| 154 |
< |
* @param s the scalar value |
| 155 |
< |
*/ |
| 156 |
< |
inline void mul( double s ) { |
| 157 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 158 |
< |
data_[i] *= s; |
| 159 |
< |
} |
| 152 |
> |
/** |
| 153 |
> |
* Returns constant reference of ith element. |
| 154 |
> |
* @return reference of ith element |
| 155 |
> |
* @param i index |
| 156 |
> |
*/ |
| 157 |
> |
inline const Real& operator()(unsigned int i) const { |
| 158 |
> |
assert( i < Dim); |
| 159 |
> |
return this->data_[i]; |
| 160 |
> |
} |
| 161 |
|
|
| 162 |
< |
/** |
| 163 |
< |
* Sets the value of this vector to the scalar multiplication of vector v1 |
| 164 |
< |
* (*this = s * v1). |
| 165 |
< |
* @param s the scalar value |
| 166 |
< |
* @param v1 the vector |
| 167 |
< |
*/ |
| 187 |
< |
inline void mul( double s, const Vector<Real, Dim>& v1 ) { |
| 188 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 189 |
< |
data_[i] = s * v1.data_[i]; |
| 190 |
< |
} |
| 162 |
> |
/** Copy the internal data to an array*/ |
| 163 |
> |
void getArray(Real* array) { |
| 164 |
> |
for (unsigned int i = 0; i < Dim; i ++) { |
| 165 |
> |
array[i] = this->data_[i]; |
| 166 |
> |
} |
| 167 |
> |
} |
| 168 |
|
|
| 169 |
< |
/** |
| 170 |
< |
* Sets the value of this vector to the scalar division of itself (*this /= s ). |
| 171 |
< |
* @param s the scalar value |
| 172 |
< |
*/ |
| 173 |
< |
inline void div( double s) { |
| 174 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 175 |
< |
data_[i] /= s; |
| 176 |
< |
} |
| 169 |
> |
/** Returns the pointer of internal array */ |
| 170 |
> |
Real* getArrayPointer() { |
| 171 |
> |
return this->data_; |
| 172 |
> |
} |
| 173 |
> |
|
| 174 |
> |
/** |
| 175 |
> |
* Tests if this vetor is equal to other vector |
| 176 |
> |
* @return true if equal, otherwise return false |
| 177 |
> |
* @param v vector to be compared |
| 178 |
> |
*/ |
| 179 |
> |
inline bool operator ==(const Vector<Real, Dim>& v) { |
| 180 |
|
|
| 181 |
< |
/** |
| 182 |
< |
* Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ). |
| 183 |
< |
* @param v1 the source vector |
| 184 |
< |
* @param s the scalar value |
| 185 |
< |
*/ |
| 186 |
< |
inline void div( const Vector<Real, Dim>& v1, double s ) { |
| 187 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 188 |
< |
data_[i] = v1.data_[i] / s; |
| 209 |
< |
} |
| 181 |
> |
for (unsigned int i = 0; i < Dim; i ++) { |
| 182 |
> |
if (!equal(this->data_[i], v[i])) { |
| 183 |
> |
return false; |
| 184 |
> |
} |
| 185 |
> |
} |
| 186 |
> |
|
| 187 |
> |
return true; |
| 188 |
> |
} |
| 189 |
|
|
| 190 |
< |
/** @see #add */ |
| 191 |
< |
inline Vector<Real, Dim> operator +=( const Vector<Real, Dim>& v1 ) { |
| 192 |
< |
add(v1); |
| 193 |
< |
return *this; |
| 194 |
< |
} |
| 190 |
> |
/** |
| 191 |
> |
* Tests if this vetor is not equal to other vector |
| 192 |
> |
* @return true if equal, otherwise return false |
| 193 |
> |
* @param v vector to be compared |
| 194 |
> |
*/ |
| 195 |
> |
inline bool operator !=(const Vector<Real, Dim>& v) { |
| 196 |
> |
return !(*this == v); |
| 197 |
> |
} |
| 198 |
> |
|
| 199 |
> |
/** Negates the value of this vector in place. */ |
| 200 |
> |
inline void negate() { |
| 201 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 202 |
> |
this->data_[i] = -this->data_[i]; |
| 203 |
> |
} |
| 204 |
|
|
| 205 |
< |
/** @see #sub */ |
| 206 |
< |
inline Vector<Real, Dim> operator -=( const Vector<Real, Dim>& v1 ) { |
| 207 |
< |
sub(v1); |
| 208 |
< |
return *this; |
| 209 |
< |
} |
| 205 |
> |
/** |
| 206 |
> |
* Sets the value of this vector to the negation of vector v1. |
| 207 |
> |
* @param v1 the source vector |
| 208 |
> |
*/ |
| 209 |
> |
inline void negate(const Vector<Real, Dim>& v1) { |
| 210 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 211 |
> |
this->data_[i] = -v1.data_[i]; |
| 212 |
|
|
| 213 |
< |
/** @see #mul */ |
| 224 |
< |
inline Vector<Real, Dim> operator *=( double s) { |
| 225 |
< |
mul(s); |
| 226 |
< |
return *this; |
| 227 |
< |
} |
| 228 |
< |
|
| 229 |
< |
/** @see #div */ |
| 230 |
< |
inline Vector<Real, Dim> operator /=( double s ) { |
| 231 |
< |
div(s); |
| 232 |
< |
return *this; |
| 233 |
< |
} |
| 234 |
< |
|
| 235 |
< |
/** |
| 236 |
< |
* Returns the length of this vector. |
| 237 |
< |
* @return the length of this vector |
| 238 |
< |
*/ |
| 239 |
< |
inline double length() { |
| 240 |
< |
return sqrt(lengthSquared()); |
| 241 |
< |
} |
| 242 |
< |
|
| 243 |
< |
/** |
| 244 |
< |
* Returns the squared length of this vector. |
| 245 |
< |
* @return the squared length of this vector |
| 246 |
< |
*/ |
| 247 |
< |
inline double lengthSquared() { |
| 248 |
< |
return dot(*this, *this); |
| 249 |
< |
} |
| 250 |
< |
|
| 251 |
< |
/** Normalizes this vector in place */ |
| 252 |
< |
inline void normalize() { |
| 253 |
< |
double len; |
| 254 |
< |
|
| 255 |
< |
len = length(); |
| 256 |
< |
*this /= len; |
| 257 |
< |
} |
| 213 |
> |
} |
| 214 |
|
|
| 215 |
< |
protected: |
| 216 |
< |
double data_[3]; |
| 217 |
< |
|
| 218 |
< |
}; |
| 219 |
< |
|
| 220 |
< |
/** unary minus*/ |
| 221 |
< |
template<typename Real, int Dim> |
| 266 |
< |
inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ |
| 267 |
< |
Vector tmp(v1); |
| 268 |
< |
return tmp.negate(); |
| 215 |
> |
/** |
| 216 |
> |
* Sets the value of this vector to the sum of itself and v1 (*this += v1). |
| 217 |
> |
* @param v1 the other vector |
| 218 |
> |
*/ |
| 219 |
> |
inline void add( const Vector<Real, Dim>& v1 ) { |
| 220 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 221 |
> |
this->data_[i] += v1.data_[i]; |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
/** |
| 225 |
< |
* Return the sum of two vectors (v1 - v2). |
| 273 |
< |
* @return the sum of two vectors |
| 225 |
> |
* Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). |
| 226 |
|
* @param v1 the first vector |
| 227 |
|
* @param v2 the second vector |
| 228 |
< |
*/ |
| 229 |
< |
template<typename Real, int Dim> |
| 230 |
< |
inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { |
| 231 |
< |
Vector<Real, Dim> result; |
| 280 |
< |
|
| 281 |
< |
result.add(v1, v2); |
| 282 |
< |
return result; |
| 228 |
> |
*/ |
| 229 |
> |
inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 230 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 231 |
> |
this->data_[i] = v1.data_[i] + v2.data_[i]; |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
/** |
| 235 |
< |
* Return the difference of two vectors (v1 - v2). |
| 236 |
< |
* @return the difference of two vectors |
| 235 |
> |
* Sets the value of this vector to the difference of itself and v1 (*this -= v1). |
| 236 |
> |
* @param v1 the other vector |
| 237 |
> |
*/ |
| 238 |
> |
inline void sub( const Vector<Real, Dim>& v1 ) { |
| 239 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 240 |
> |
this->data_[i] -= v1.data_[i]; |
| 241 |
> |
} |
| 242 |
> |
|
| 243 |
> |
/** |
| 244 |
> |
* Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2). |
| 245 |
|
* @param v1 the first vector |
| 246 |
|
* @param v2 the second vector |
| 247 |
< |
*/ |
| 248 |
< |
template<typename Real, int Dim> |
| 249 |
< |
Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { |
| 250 |
< |
Vector<Real, Dim> result; |
| 294 |
< |
result.sub(v1, v2); |
| 295 |
< |
return result; |
| 247 |
> |
*/ |
| 248 |
> |
inline void sub( const Vector<Real, Dim>& v1, const Vector &v2 ){ |
| 249 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 250 |
> |
this->data_[i] = v1.data_[i] - v2.data_[i]; |
| 251 |
|
} |
| 252 |
< |
|
| 252 |
> |
|
| 253 |
|
/** |
| 254 |
< |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
| 300 |
< |
* @return the vaule of scalar multiplication of this vector |
| 301 |
< |
* @param v1 the source vector |
| 254 |
> |
* Sets the value of this vector to the scalar multiplication of itself (*this *= s). |
| 255 |
|
* @param s the scalar value |
| 256 |
< |
*/ |
| 257 |
< |
template<typename Real, int Dim> |
| 258 |
< |
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { |
| 259 |
< |
Vector<Real, Dim> result; |
| 307 |
< |
result.mul(s, v1); |
| 308 |
< |
return result; |
| 256 |
> |
*/ |
| 257 |
> |
inline void mul( Real s ) { |
| 258 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 259 |
> |
this->data_[i] *= s; |
| 260 |
|
} |
| 261 |
< |
|
| 261 |
> |
|
| 262 |
|
/** |
| 263 |
< |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
| 264 |
< |
* @return the vaule of scalar multiplication of this vector |
| 263 |
> |
* Sets the value of this vector to the scalar multiplication of vector v1 |
| 264 |
> |
* (*this = s * v1). |
| 265 |
> |
* @param v1 the vector |
| 266 |
|
* @param s the scalar value |
| 267 |
< |
* @param v1 the source vector |
| 268 |
< |
*/ |
| 269 |
< |
template<typename Real, int Dim> |
| 270 |
< |
Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { |
| 319 |
< |
Vector<Real, Dim> result; |
| 320 |
< |
result.mul(s, v1); |
| 321 |
< |
return result; |
| 267 |
> |
*/ |
| 268 |
> |
inline void mul( const Vector<Real, Dim>& v1, Real s) { |
| 269 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 270 |
> |
this->data_[i] = s * v1.data_[i]; |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
/** |
| 274 |
< |
* Returns the value of division of a vector by a scalar. |
| 326 |
< |
* @return the vaule of scalar division of this vector |
| 327 |
< |
* @param v1 the source vector |
| 274 |
> |
* Sets the value of this vector to the scalar division of itself (*this /= s ). |
| 275 |
|
* @param s the scalar value |
| 276 |
< |
*/ |
| 277 |
< |
template<typename Real, int Dim> |
| 278 |
< |
Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) { |
| 279 |
< |
Vector<Real, Dim> result; |
| 333 |
< |
result.div( v1,s); |
| 334 |
< |
return result; |
| 276 |
> |
*/ |
| 277 |
> |
inline void div( Real s) { |
| 278 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 279 |
> |
this->data_[i] /= s; |
| 280 |
|
} |
| 281 |
< |
|
| 281 |
> |
|
| 282 |
|
/** |
| 283 |
< |
* Returns the value of division of a vector by a scalar. |
| 339 |
< |
* @return the vaule of scalar division of this vector |
| 340 |
< |
* @param s the scalar value |
| 283 |
> |
* Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ). |
| 284 |
|
* @param v1 the source vector |
| 285 |
< |
*/ |
| 286 |
< |
template<typename Real, int Dim> |
| 287 |
< |
inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) { |
| 288 |
< |
Vector<Real, Dim> result; |
| 289 |
< |
result.div( v1,s); |
| 347 |
< |
return result; |
| 285 |
> |
* @param s the scalar value |
| 286 |
> |
*/ |
| 287 |
> |
inline void div( const Vector<Real, Dim>& v1, Real s ) { |
| 288 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 289 |
> |
this->data_[i] = v1.data_[i] / s; |
| 290 |
|
} |
| 291 |
|
|
| 292 |
< |
/** fuzzy comparson */ |
| 293 |
< |
template<typename Real, int Dim> |
| 294 |
< |
inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 292 |
> |
/** @see #add */ |
| 293 |
> |
inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) { |
| 294 |
> |
add(v1); |
| 295 |
> |
return *this; |
| 296 |
> |
} |
| 297 |
|
|
| 298 |
+ |
/** @see #sub */ |
| 299 |
+ |
inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) { |
| 300 |
+ |
sub(v1); |
| 301 |
+ |
return *this; |
| 302 |
|
} |
| 303 |
|
|
| 304 |
< |
|
| 305 |
< |
/** |
| 306 |
< |
* Returns the dot product of two Vectors |
| 307 |
< |
* @param v1 first vector |
| 308 |
< |
* @param v2 second vector |
| 361 |
< |
* @return the dot product of v1 and v2 |
| 362 |
< |
*/ |
| 363 |
< |
template<typename Real, int Dim> |
| 364 |
< |
inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 365 |
< |
Real tmp; |
| 366 |
< |
tmp = 0; |
| 304 |
> |
/** @see #mul */ |
| 305 |
> |
inline Vector<Real, Dim>& operator *=( Real s) { |
| 306 |
> |
mul(s); |
| 307 |
> |
return *this; |
| 308 |
> |
} |
| 309 |
|
|
| 310 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 311 |
< |
tmp += v1[i] + v2[i]; |
| 312 |
< |
|
| 313 |
< |
return tmp; |
| 310 |
> |
/** @see #div */ |
| 311 |
> |
inline Vector<Real, Dim>& operator /=( Real s ) { |
| 312 |
> |
div(s); |
| 313 |
> |
return *this; |
| 314 |
|
} |
| 315 |
|
|
| 316 |
|
/** |
| 317 |
< |
* Returns the distance between two Vectors |
| 318 |
< |
* @param v1 first vector |
| 319 |
< |
* @param v2 second vector |
| 320 |
< |
* @return the distance between v1 and v2 |
| 321 |
< |
*/ |
| 322 |
< |
template<typename Real, int Dim> |
| 323 |
< |
inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 324 |
< |
Vector<Real, Dim> tempVector = v1 - v2; |
| 325 |
< |
return tempVector.length(); |
| 317 |
> |
* Returns the sum of all elements of this vector. |
| 318 |
> |
* @return the sum of all elements of this vector |
| 319 |
> |
*/ |
| 320 |
> |
inline Real sum() { |
| 321 |
> |
Real tmp; |
| 322 |
> |
tmp = 0; |
| 323 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 324 |
> |
tmp += this->data_[i]; |
| 325 |
> |
return tmp; |
| 326 |
|
} |
| 327 |
< |
|
| 327 |
> |
|
| 328 |
|
/** |
| 329 |
< |
* Returns the squared distance between two Vectors |
| 330 |
< |
* @param v1 first vector |
| 389 |
< |
* @param v2 second vector |
| 390 |
< |
* @return the squared distance between v1 and v2 |
| 329 |
> |
* Returns the length of this vector. |
| 330 |
> |
* @return the length of this vector |
| 331 |
|
*/ |
| 332 |
< |
template<typename Real, int Dim> |
| 333 |
< |
inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 394 |
< |
Vector<Real, Dim> tempVector = v1 - v2; |
| 395 |
< |
return tempVector.lengthSquare(); |
| 332 |
> |
inline Real length() { |
| 333 |
> |
return sqrt(lengthSquare()); |
| 334 |
|
} |
| 335 |
+ |
|
| 336 |
+ |
/** |
| 337 |
+ |
* Returns the squared length of this vector. |
| 338 |
+ |
* @return the squared length of this vector |
| 339 |
+ |
*/ |
| 340 |
+ |
inline Real lengthSquare() { |
| 341 |
+ |
return dot(*this, *this); |
| 342 |
+ |
} |
| 343 |
+ |
|
| 344 |
+ |
/** Normalizes this vector in place */ |
| 345 |
+ |
inline void normalize() { |
| 346 |
+ |
Real len; |
| 347 |
|
|
| 348 |
+ |
len = length(); |
| 349 |
+ |
|
| 350 |
+ |
//if (len < oopse:epsilon) |
| 351 |
+ |
// throw(); |
| 352 |
+ |
|
| 353 |
+ |
*this /= len; |
| 354 |
+ |
} |
| 355 |
+ |
|
| 356 |
|
/** |
| 357 |
< |
* Write to an output stream |
| 357 |
> |
* Tests if this vector is normalized |
| 358 |
> |
* @return true if this vector is normalized, otherwise return false |
| 359 |
|
*/ |
| 360 |
< |
template<typename Real, int Dim> |
| 361 |
< |
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { |
| 360 |
> |
inline bool isNormalized() { |
| 361 |
> |
return equal(lengthSquare(), (RealType)1); |
| 362 |
> |
} |
| 363 |
> |
|
| 364 |
> |
unsigned int size() {return Dim;} |
| 365 |
> |
protected: |
| 366 |
> |
Real data_[Dim]; |
| 367 |
|
|
| 368 |
< |
return o; |
| 368 |
> |
}; |
| 369 |
> |
|
| 370 |
> |
/** unary minus*/ |
| 371 |
> |
template<typename Real, unsigned int Dim> |
| 372 |
> |
inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ |
| 373 |
> |
Vector<Real, Dim> tmp(v1); |
| 374 |
> |
tmp.negate(); |
| 375 |
> |
return tmp; |
| 376 |
> |
} |
| 377 |
> |
|
| 378 |
> |
/** |
| 379 |
> |
* Return the sum of two vectors (v1 - v2). |
| 380 |
> |
* @return the sum of two vectors |
| 381 |
> |
* @param v1 the first vector |
| 382 |
> |
* @param v2 the second vector |
| 383 |
> |
*/ |
| 384 |
> |
template<typename Real, unsigned int Dim> |
| 385 |
> |
inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { |
| 386 |
> |
Vector<Real, Dim> result; |
| 387 |
> |
|
| 388 |
> |
result.add(v1, v2); |
| 389 |
> |
return result; |
| 390 |
> |
} |
| 391 |
> |
|
| 392 |
> |
/** |
| 393 |
> |
* Return the difference of two vectors (v1 - v2). |
| 394 |
> |
* @return the difference of two vectors |
| 395 |
> |
* @param v1 the first vector |
| 396 |
> |
* @param v2 the second vector |
| 397 |
> |
*/ |
| 398 |
> |
template<typename Real, unsigned int Dim> |
| 399 |
> |
Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) { |
| 400 |
> |
Vector<Real, Dim> result; |
| 401 |
> |
result.sub(v1, v2); |
| 402 |
> |
return result; |
| 403 |
> |
} |
| 404 |
> |
|
| 405 |
> |
/** |
| 406 |
> |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
| 407 |
> |
* @return the vaule of scalar multiplication of this vector |
| 408 |
> |
* @param v1 the source vector |
| 409 |
> |
* @param s the scalar value |
| 410 |
> |
*/ |
| 411 |
> |
template<typename Real, unsigned int Dim> |
| 412 |
> |
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) { |
| 413 |
> |
Vector<Real, Dim> result; |
| 414 |
> |
result.mul(v1,s); |
| 415 |
> |
return result; |
| 416 |
> |
} |
| 417 |
> |
|
| 418 |
> |
/** |
| 419 |
> |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
| 420 |
> |
* @return the vaule of scalar multiplication of this vector |
| 421 |
> |
* @param s the scalar value |
| 422 |
> |
* @param v1 the source vector |
| 423 |
> |
*/ |
| 424 |
> |
template<typename Real, unsigned int Dim> |
| 425 |
> |
Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) { |
| 426 |
> |
Vector<Real, Dim> result; |
| 427 |
> |
result.mul(v1, s); |
| 428 |
> |
return result; |
| 429 |
> |
} |
| 430 |
> |
|
| 431 |
> |
/** |
| 432 |
> |
* Returns the value of division of a vector by a scalar. |
| 433 |
> |
* @return the vaule of scalar division of this vector |
| 434 |
> |
* @param v1 the source vector |
| 435 |
> |
* @param s the scalar value |
| 436 |
> |
*/ |
| 437 |
> |
template<typename Real, unsigned int Dim> |
| 438 |
> |
Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) { |
| 439 |
> |
Vector<Real, Dim> result; |
| 440 |
> |
result.div( v1,s); |
| 441 |
> |
return result; |
| 442 |
> |
} |
| 443 |
> |
|
| 444 |
> |
/** |
| 445 |
> |
* Returns the dot product of two Vectors |
| 446 |
> |
* @param v1 first vector |
| 447 |
> |
* @param v2 second vector |
| 448 |
> |
* @return the dot product of v1 and v2 |
| 449 |
> |
*/ |
| 450 |
> |
template<typename Real, unsigned int Dim> |
| 451 |
> |
inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 452 |
> |
Real tmp; |
| 453 |
> |
tmp = 0; |
| 454 |
> |
|
| 455 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 456 |
> |
tmp += v1[i] * v2[i]; |
| 457 |
> |
|
| 458 |
> |
return tmp; |
| 459 |
> |
} |
| 460 |
> |
|
| 461 |
> |
/** |
| 462 |
> |
* Returns the distance between two Vectors |
| 463 |
> |
* @param v1 first vector |
| 464 |
> |
* @param v2 second vector |
| 465 |
> |
* @return the distance between v1 and v2 |
| 466 |
> |
*/ |
| 467 |
> |
template<typename Real, unsigned int Dim> |
| 468 |
> |
inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 469 |
> |
Vector<Real, Dim> tempVector = v1 - v2; |
| 470 |
> |
return tempVector.length(); |
| 471 |
> |
} |
| 472 |
> |
|
| 473 |
> |
/** |
| 474 |
> |
* Returns the squared distance between two Vectors |
| 475 |
> |
* @param v1 first vector |
| 476 |
> |
* @param v2 second vector |
| 477 |
> |
* @return the squared distance between v1 and v2 |
| 478 |
> |
*/ |
| 479 |
> |
template<typename Real, unsigned int Dim> |
| 480 |
> |
inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 481 |
> |
Vector<Real, Dim> tempVector = v1 - v2; |
| 482 |
> |
return tempVector.lengthSquare(); |
| 483 |
> |
} |
| 484 |
> |
|
| 485 |
> |
/** |
| 486 |
> |
* Write to an output stream |
| 487 |
> |
*/ |
| 488 |
> |
template<typename Real, unsigned int Dim> |
| 489 |
> |
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { |
| 490 |
> |
|
| 491 |
> |
o << "[ "; |
| 492 |
> |
|
| 493 |
> |
for (unsigned int i = 0 ; i< Dim; i++) { |
| 494 |
> |
o << v[i]; |
| 495 |
> |
|
| 496 |
> |
if (i != Dim -1) { |
| 497 |
> |
o<< ", "; |
| 498 |
> |
} |
| 499 |
|
} |
| 500 |
+ |
|
| 501 |
+ |
o << " ]"; |
| 502 |
+ |
return o; |
| 503 |
+ |
} |
| 504 |
|
|
| 505 |
|
} |
| 506 |
|
#endif |