| 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 | 
| 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]; | 
| 91 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 92 | 
> | 
                    data_[i] = v[i]; | 
| 93 | 
  | 
            } | 
| 94 | 
  | 
 | 
| 95 | 
  | 
            /**  | 
| 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]; | 
| 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]; | 
| 182 | 
> | 
            for (unsigned int i = 0; i < Dim; i++) | 
| 183 | 
> | 
                data_[i] += v1.data_[i]; | 
| 184 | 
  | 
                } | 
| 185 | 
  | 
 | 
| 186 | 
  | 
            /** | 
| 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]; | 
| 192 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 193 | 
> | 
                    data_[i] = v1.data_[i] + v2.data_[i]; | 
| 194 | 
  | 
            } | 
| 195 | 
  | 
 | 
| 196 | 
  | 
            /** | 
| 217 | 
  | 
            * @param s the scalar value | 
| 218 | 
  | 
            */ | 
| 219 | 
  | 
            inline void mul( double s ) { | 
| 220 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 220 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 221 | 
  | 
                   data_[i] *= s; | 
| 222 | 
  | 
            } | 
| 223 | 
  | 
 | 
| 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++) | 
| 231 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 232 | 
  | 
                    data_[i] = s * v1.data_[i]; | 
| 233 | 
  | 
            } | 
| 234 | 
  | 
 | 
| 237 | 
  | 
            * @param s the scalar value | 
| 238 | 
  | 
            */              | 
| 239 | 
  | 
            inline void div( double s) { | 
| 240 | 
< | 
                for (unsigned int i = 0; i < Dim; i++)             | 
| 240 | 
> | 
                for (unsigned int i = 0; i < Dim; i++)             | 
| 241 | 
  | 
                    data_[i] /= s; | 
| 242 | 
  | 
            } | 
| 243 | 
  | 
 | 
| 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++) | 
| 250 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 251 | 
  | 
                    data_[i] = v1.data_[i] / s; | 
| 252 | 
  | 
            } | 
| 253 | 
  | 
 | 
| 287 | 
  | 
             * Returns the squared length of this vector. | 
| 288 | 
  | 
             * @return the squared length of this vector | 
| 289 | 
  | 
             */ | 
| 290 | 
< | 
             inline double lengthSquared() { | 
| 290 | 
> | 
             inline double lengthSquare() { | 
| 291 | 
  | 
                return dot(*this, *this); | 
| 292 | 
  | 
            } | 
| 293 | 
  | 
             | 
| 298 | 
  | 
                len = length(); | 
| 299 | 
  | 
                *this /= len; | 
| 300 | 
  | 
            } | 
| 301 | 
+ | 
 | 
| 302 | 
+ | 
            /** | 
| 303 | 
+ | 
             * Tests if this vector is normalized | 
| 304 | 
+ | 
             * @return true if this vector is normalized, otherwise return false | 
| 305 | 
+ | 
             */ | 
| 306 | 
+ | 
            inline bool isNormalized() const | 
| 307 | 
+ | 
            { | 
| 308 | 
+ | 
                return lengthSquare() == 1.0; | 
| 309 | 
+ | 
            }            | 
| 310 | 
  | 
             | 
| 311 | 
  | 
        protected: | 
| 312 | 
  | 
            double data_[3]; | 
| 414 | 
  | 
     */ | 
| 415 | 
  | 
    template<typename Real, unsigned int Dim>     | 
| 416 | 
  | 
    inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 417 | 
< | 
                Real tmp; | 
| 418 | 
< | 
                tmp = 0; | 
| 417 | 
> | 
        Real tmp; | 
| 418 | 
> | 
        tmp = 0; | 
| 419 | 
  | 
 | 
| 420 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 421 | 
< | 
                        tmp += v1[i] + v2[i]; | 
| 422 | 
< | 
                 | 
| 423 | 
< | 
                return tmp; | 
| 420 | 
> | 
        for (unsigned int i = 0; i < Dim; i++) | 
| 421 | 
> | 
            tmp += v1[i] + v2[i]; | 
| 422 | 
> | 
 | 
| 423 | 
> | 
        return tmp; | 
| 424 | 
  | 
    } | 
| 425 | 
  | 
 | 
| 426 | 
  | 
    /** | 
| 451 | 
  | 
     * Write to an output stream | 
| 452 | 
  | 
     */ | 
| 453 | 
  | 
    template<typename Real, unsigned int Dim> | 
| 454 | 
< | 
    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { | 
| 455 | 
< | 
         | 
| 454 | 
> | 
    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { | 
| 455 | 
> | 
 | 
| 456 | 
> | 
        o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; | 
| 457 | 
  | 
        return o;         | 
| 458 | 
  | 
    } | 
| 459 | 
  | 
     |