| 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 | 
| 85 | 
  | 
                 | 
| 86 | 
  | 
                return *this; | 
| 87 | 
  | 
            } | 
| 88 | 
+ | 
 | 
| 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 | 
  | 
             | 
| 95 | 
  | 
            /** Constructs and initializes a Vector from an array */             | 
| 96 | 
  | 
            inline Vector( double* v) { | 
| 97 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 98 | 
< | 
                    data_[i] = v[i]; | 
| 97 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 98 | 
> | 
                    data_[i] = v[i]; | 
| 99 | 
  | 
            } | 
| 100 | 
  | 
 | 
| 101 | 
  | 
            /**  | 
| 138 | 
  | 
                return data_[i]; | 
| 139 | 
  | 
            } | 
| 140 | 
  | 
 | 
| 141 | 
+ | 
            /** | 
| 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 | 
  | 
            /** Negates the value of this vector in place. */            | 
| 167 | 
  | 
            inline void negate() { | 
| 168 | 
< | 
                data_[0] = -data_[0]; | 
| 169 | 
< | 
                data_[1] = -data_[1]; | 
| 122 | 
< | 
                data_[2] = -data_[2]; | 
| 168 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 169 | 
> | 
                    data_[i] = -data_[i]; | 
| 170 | 
  | 
            } | 
| 171 | 
  | 
 | 
| 172 | 
  | 
            /** | 
| 184 | 
  | 
            * @param v1 the other vector | 
| 185 | 
  | 
            */ | 
| 186 | 
  | 
            inline void add( const Vector<Real, Dim>& v1 ) { | 
| 187 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 188 | 
< | 
                    data_[i] += v1.data_[i]; | 
| 187 | 
> | 
            for (unsigned int i = 0; i < Dim; i++) | 
| 188 | 
> | 
                data_[i] += v1.data_[i]; | 
| 189 | 
  | 
                } | 
| 190 | 
  | 
 | 
| 191 | 
  | 
            /** | 
| 194 | 
  | 
            * @param v2 the second vector | 
| 195 | 
  | 
            */ | 
| 196 | 
  | 
            inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 197 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 198 | 
< | 
                    data_[i] = v1.data_[i] + v2.data_[i]; | 
| 197 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 198 | 
> | 
                    data_[i] = v1.data_[i] + v2.data_[i]; | 
| 199 | 
  | 
            } | 
| 200 | 
  | 
 | 
| 201 | 
  | 
            /** | 
| 222 | 
  | 
            * @param s the scalar value | 
| 223 | 
  | 
            */ | 
| 224 | 
  | 
            inline void mul( double s ) { | 
| 225 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 225 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 226 | 
  | 
                   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 | 
+ | 
            * @param v1 the vector             | 
| 233 | 
  | 
            * @param s the scalar value | 
| 186 | 
– | 
            * @param v1 the vector | 
| 234 | 
  | 
            */ | 
| 235 | 
< | 
            inline void mul( double s, const Vector<Real, Dim>& v1 ) { | 
| 236 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 235 | 
> | 
            inline void mul( const Vector<Real, Dim>& v1, double s) { | 
| 236 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 237 | 
  | 
                    data_[i] = s * v1.data_[i]; | 
| 238 | 
  | 
            } | 
| 239 | 
  | 
 | 
| 242 | 
  | 
            * @param s the scalar value | 
| 243 | 
  | 
            */              | 
| 244 | 
  | 
            inline void div( double s) { | 
| 245 | 
< | 
                for (unsigned int i = 0; i < Dim; i++)             | 
| 245 | 
> | 
                for (unsigned int i = 0; i < Dim; i++)             | 
| 246 | 
  | 
                    data_[i] /= s; | 
| 247 | 
  | 
            } | 
| 248 | 
  | 
 | 
| 252 | 
  | 
            * @param s the scalar value | 
| 253 | 
  | 
            */                          | 
| 254 | 
  | 
            inline void div( const Vector<Real, Dim>& v1, double s ) { | 
| 255 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 255 | 
> | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 256 | 
  | 
                    data_[i] = v1.data_[i] / s; | 
| 257 | 
  | 
            } | 
| 258 | 
  | 
 | 
| 285 | 
  | 
             * @return the length of this vector | 
| 286 | 
  | 
             */ | 
| 287 | 
  | 
             inline double length() { | 
| 288 | 
< | 
                return sqrt(lengthSquared());   | 
| 288 | 
> | 
                return sqrt(lengthSquare());   | 
| 289 | 
  | 
            } | 
| 290 | 
  | 
             | 
| 291 | 
  | 
            /** | 
| 292 | 
  | 
             * Returns the squared length of this vector. | 
| 293 | 
  | 
             * @return the squared length of this vector | 
| 294 | 
  | 
             */ | 
| 295 | 
< | 
             inline double lengthSquared() { | 
| 295 | 
> | 
             inline double lengthSquare() { | 
| 296 | 
  | 
                return dot(*this, *this); | 
| 297 | 
  | 
            } | 
| 298 | 
  | 
             | 
| 301 | 
  | 
                double len; | 
| 302 | 
  | 
 | 
| 303 | 
  | 
                len = length(); | 
| 304 | 
+ | 
                 | 
| 305 | 
+ | 
                //if (len < oopse:epsilon) | 
| 306 | 
+ | 
                //  throw(); | 
| 307 | 
+ | 
                 | 
| 308 | 
  | 
                *this /= len; | 
| 309 | 
  | 
            } | 
| 310 | 
+ | 
 | 
| 311 | 
+ | 
            /** | 
| 312 | 
+ | 
             * Tests if this vector is normalized | 
| 313 | 
+ | 
             * @return true if this vector is normalized, otherwise return false | 
| 314 | 
+ | 
             */ | 
| 315 | 
+ | 
            inline bool isNormalized() { | 
| 316 | 
+ | 
                return equal(lengthSquare(), 1.0); | 
| 317 | 
+ | 
            }            | 
| 318 | 
  | 
             | 
| 319 | 
  | 
        protected: | 
| 320 | 
< | 
            double data_[3]; | 
| 320 | 
> | 
            double data_[Dim]; | 
| 321 | 
  | 
         | 
| 322 | 
  | 
    }; | 
| 323 | 
  | 
 | 
| 324 | 
  | 
    /** unary minus*/ | 
| 325 | 
  | 
    template<typename Real, unsigned int Dim>     | 
| 326 | 
  | 
    inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ | 
| 327 | 
< | 
        Vector tmp(v1); | 
| 328 | 
< | 
        return tmp.negate(); | 
| 327 | 
> | 
        Vector<Real, Dim> tmp(v1); | 
| 328 | 
> | 
        tmp.negate(); | 
| 329 | 
> | 
        return tmp; | 
| 330 | 
  | 
    } | 
| 331 | 
  | 
 | 
| 332 | 
  | 
    /** | 
| 365 | 
  | 
    template<typename Real, unsigned int Dim>                  | 
| 366 | 
  | 
    Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) {        | 
| 367 | 
  | 
        Vector<Real, Dim> result; | 
| 368 | 
< | 
        result.mul(s, v1); | 
| 368 | 
> | 
        result.mul(v1,s); | 
| 369 | 
  | 
        return result;            | 
| 370 | 
  | 
    } | 
| 371 | 
  | 
     | 
| 378 | 
  | 
    template<typename Real, unsigned int Dim> | 
| 379 | 
  | 
    Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { | 
| 380 | 
  | 
        Vector<Real, Dim> result; | 
| 381 | 
< | 
        result.mul(s, v1); | 
| 381 | 
> | 
        result.mul(v1, s); | 
| 382 | 
  | 
        return result;            | 
| 383 | 
  | 
    } | 
| 384 | 
  | 
 | 
| 396 | 
  | 
    } | 
| 397 | 
  | 
     | 
| 398 | 
  | 
    /** | 
| 339 | 
– | 
     * Returns the  value of division of a vector by a scalar.  | 
| 340 | 
– | 
     * @return  the vaule of scalar division of this vector | 
| 341 | 
– | 
     * @param s the scalar value | 
| 342 | 
– | 
     * @param v1 the source vector | 
| 343 | 
– | 
     */ | 
| 344 | 
– | 
    template<typename Real, unsigned int Dim>         | 
| 345 | 
– | 
    inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) { | 
| 346 | 
– | 
        Vector<Real, Dim> result; | 
| 347 | 
– | 
        result.div( v1,s); | 
| 348 | 
– | 
        return result;            | 
| 349 | 
– | 
    } | 
| 350 | 
– | 
 | 
| 351 | 
– | 
    /** fuzzy comparson */ | 
| 352 | 
– | 
    template<typename Real, unsigned int Dim>         | 
| 353 | 
– | 
    inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 354 | 
– | 
 | 
| 355 | 
– | 
    } | 
| 356 | 
– | 
 | 
| 357 | 
– | 
     | 
| 358 | 
– | 
    /** | 
| 399 | 
  | 
     * Returns the dot product of two Vectors | 
| 400 | 
  | 
     * @param v1 first vector | 
| 401 | 
  | 
     * @param v2 second vector | 
| 403 | 
  | 
     */ | 
| 404 | 
  | 
    template<typename Real, unsigned int Dim>     | 
| 405 | 
  | 
    inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | 
| 406 | 
< | 
                Real tmp; | 
| 407 | 
< | 
                tmp = 0; | 
| 406 | 
> | 
        Real tmp; | 
| 407 | 
> | 
        tmp = 0; | 
| 408 | 
  | 
 | 
| 409 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 410 | 
< | 
                        tmp += v1[i] + v2[i]; | 
| 411 | 
< | 
                 | 
| 412 | 
< | 
                return tmp; | 
| 409 | 
> | 
        for (unsigned int i = 0; i < Dim; i++) | 
| 410 | 
> | 
            tmp += v1[i] + v2[i]; | 
| 411 | 
> | 
 | 
| 412 | 
> | 
        return tmp; | 
| 413 | 
  | 
    } | 
| 414 | 
  | 
 | 
| 415 | 
  | 
    /** | 
| 440 | 
  | 
     * Write to an output stream | 
| 441 | 
  | 
     */ | 
| 442 | 
  | 
    template<typename Real, unsigned int Dim> | 
| 443 | 
< | 
    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { | 
| 444 | 
< | 
         | 
| 443 | 
> | 
    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { | 
| 444 | 
> | 
 | 
| 445 | 
> | 
        o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; | 
| 446 | 
  | 
        return o;         | 
| 447 | 
  | 
    } | 
| 448 | 
  | 
     |