- Main Page
- Classes
- Files

- File List
- File Members

# src/vectorMath/tvector.h

Go to the documentation of this file.

    00001 #ifndef tvector_h
    00002 #define tvector_h
    00003
    00004 #include <iostream>
    00005 #include <math.h>
    00006 #include "mathex.h"
    00007
    00008 using std::istream;
    00009 using std::ostream;
    00010
    00011 class TRay;
    00012
    00013 class TVector {
    00014     public:
    00015         enum TStatus { INVALID, DEFAULT, UNIT };
    00016
    00017     private:
    00018         double _x, _y, _z;
    00019         TStatus _Status;
    00020
    00021         // Constructors
    00022         TVector(double x, double y, double z, TStatus s) : _x(x), _y(y), _z(z), _Status(s) {}
    00023
    00024         // Input and output
    00025         ostream &write(ostream &out) const;
    00026         istream &read(istream &in);
    00027
    00028     public:
    00029         TVector() : _x(0.0), _y(0.0), _z(0.0), _Status(INVALID) {}
    00030         TVector(double x, double y, double z) : _x(x), _y(y), _z(z), _Status(DEFAULT) {}
    00031
    00032         // Mid point between two lines
    00033         TVector(const TRay &line1, const TRay &line2);
    00034
    00035         // Selectors
    00036         double X() const { return _x; }
    00037         double Y() const { return _y; }
    00038         double Z() const { return _z; }
    00039         int isUnit() const { return _Status==UNIT; }
    00040         int isDefault() const { return _Status==DEFAULT; }
    00041         int isValid() const { return _Status!=INVALID; }
    00042
    00043         // Change the status of a vector
    00044         TVector &unit();
    00045         static TVector &unit(const TVector &v, TVector &result) { result = v; return result.unit(); }
    00046         static TVector unit(const TVector &v) { return TVector(v).unit(); }
    00047
    00048         TVector &Default();
    00049         static TVector Default(const TVector &v, TVector &result) { result = v; return result.Default(); }
    00050         static TVector Default(const TVector &v) { return TVector(v).Default(); }
    00051
    00052         // Magnitude
    00053         double mag() const { return (isValid() ? (isUnit() ? 1.0 : sqrt(sqr(X()) + sqr(Y()) + sqr(Z()))) : 0.0); }
    00054         double magSqr() const { return (isValid() ? (isUnit() ? 1.0 : sqr(X()) + sqr(Y()) + sqr(Z())) : 0.0); }
    00055
    00056         // Dot or scalar product
    00057         double dot(const TVector &v) const { return ((isValid() && v.isValid()) ? (X()*v.X() + Y()*v.Y() + Z()*v.Z()) : 0.0); }
    00058         static double dot(const TVector &v1, const TVector &v2) { return v1.dot(v2); }
    00059
    00060         // Distance between two vectors
    00061         double dist(const TVector &v) const { return (*this-v).mag(); }
    00062         double distSqr(const TVector &v) const { return (*this-v).magSqr(); }
    00063
    00064         // Optimised arithmetic methods
    00065         //TVector &TVector::add(const TVector &v1, const TVector &v2, TVector &result)
    00066         static TVector &add(const TVector &v1, const TVector &v2, TVector &result);
    00067         static TVector &subtract(const TVector &v1, const TVector &v2, TVector &result);
    00068         static TVector &cross(const TVector &v1, const TVector &v2, TVector &result);
    00069         static TVector &invert(const TVector &v1, TVector &result);
    00070         static TVector &multiply(const TVector &v1, const double &scale, TVector &result);
    00071         static TVector getVectorAngleMoved(const TVector &v1, double GammaAngle);
    00072         static TVector getVectorAngleMoved2(const TVector &v1, double GammaAngle);
    00073         //static double Arcsin(double x);
    00074
    00075         // Vector arithmetic, addition, subtraction and vector product
    00076         TVector operator-() const { TVector tmp; return invert(*this, tmp); }
    00077         TVector &operator+=(const TVector &v) { return add(*this, v, *this); }
    00078         TVector &operator-=(const TVector &v) { return subtract(*this, v, *this); }
    00079         TVector &operator*=(const TVector &v) { TVector tv(*this); return cross(tv, v, *this); }
    00080         TVector &operator*=(const double &scale) { return multiply(*this, scale, *this); }
    00081         TVector operator+(const TVector &v) const { TVector tv; return add(*this, v, tv); }
    00082         TVector operator-(const TVector &v) const { TVector tv; return subtract(*this, v, tv); }
    00083         TVector operator*(const TVector &v) const { TVector tv; return cross(*this, v, tv); }
    00084         TVector operator*(const double &scale) const { TVector tv; return multiply(*this, scale, tv); }
    00085
    00086         // Streaming
    00087         friend ostream &operator<<(ostream &out, const TVector &o) { return o.write(out); }
    00088         friend istream &operator>>(istream &in, TVector &o) { return o.read(in); }
    00089 };
    00090
    00091 #endif
    00092