torcs - 1.2.2

src/modules/simu/simuv2/SOLID-2.0/include/3D/Vector.h

Go to the documentation of this file.
00001 /*
00002   3D - C++ Class Library for 3D Transformations
00003   Copyright (C) 1996-1998  Gino van den Bergen
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Library General Public
00007   License as published by the Free Software Foundation; either
00008   version 2 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Library General Public License for more details.
00014 
00015   You should have received a copy of the GNU Library General Public
00016   License along with this library; if not, write to the Free
00017   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019   Please send remarks, questions and bug reports to gino@win.tue.nl,
00020   or write to:
00021                   Gino van den Bergen
00022                   Department of Mathematics and Computing Science
00023                   Eindhoven University of Technology
00024                   P.O. Box 513, 5600 MB Eindhoven, The Netherlands
00025 */
00026 
00027 #ifndef _VECTOR_H_
00028 #define _VECTOR_H_
00029 
00030 #include "Tuple3.h"
00031 
00032 class Vector : public Tuple3 {
00033 public:
00034   Vector() {}
00035   Vector(const float v[3]) : Tuple3(v) {}
00036   Vector(const double v[3]) : Tuple3(v) {}
00037   Vector(Scalar x, Scalar y, Scalar z) : Tuple3(x, y, z) {}
00038   
00039   Vector& operator+=(const Vector& v);
00040   Vector& operator-=(const Vector& v);
00041   Vector& operator*=(Scalar s);
00042   Vector& operator/=(Scalar s);
00043   
00044   Scalar length2() const;
00045   Scalar length() const;
00046 
00047   bool approxZero() const;
00048 
00049   void normalize();
00050   Vector normalized() const;
00051 
00052   int closestAxis() const;
00053 
00054   static Vector random();
00055 };
00056 
00057 Vector operator+(const Vector& v1, const Vector& v2);
00058 Vector operator-(const Vector& v1, const Vector& v2);
00059 Vector operator-(const Vector& v);
00060 Vector operator*(const Vector& v, Scalar s);
00061 Vector operator*(Scalar s, const Vector& v);
00062 Vector operator/(const Vector& v, Scalar s);
00063 
00064 Scalar dot(const Vector& v1, const Vector& v2);
00065 
00066 Scalar length2(const Vector& v);
00067 Scalar length(const Vector& v);
00068 
00069 bool approxZero(const Vector& v);
00070 bool approxEqual(const Vector& v1, const Vector& v2);
00071 
00072 Scalar angle(const Vector& v1, const Vector& v2);
00073 Vector cross(const Vector& v1, const Vector& v2);
00074 Scalar triple(const Vector& v1, const Vector& v2, const Vector& v3);
00075 
00076 
00077 
00078 inline Vector& Vector::operator+=(const Vector& v) {
00079   comp[X] += v[X]; comp[Y] += v[Y]; comp[Z] += v[Z];
00080   return *this;
00081 }
00082 
00083 inline Vector& Vector::operator-=(const Vector& v) {
00084   comp[X] -= v[X]; comp[Y] -= v[Y]; comp[Z] -= v[Z];
00085   return *this;
00086 }
00087  
00088 inline Vector& Vector::operator*=(Scalar s) {
00089   comp[X] *= s; comp[Y] *= s; comp[Z] *= s;
00090   return *this;
00091 }
00092 
00093 inline Vector& Vector::operator/=(Scalar s) {
00094   assert(!eqz(s));
00095   return *this *= 1 / s;
00096 }
00097 
00098 inline Vector operator+(const Vector& v1, const Vector& v2) {
00099   return Vector(v1[X] + v2[X], v1[Y] + v2[Y], v1[Z] + v2[Z]);
00100 }
00101 
00102 inline Vector operator-(const Vector& v1, const Vector& v2) {
00103   return Vector(v1[X] - v2[X], v1[Y] - v2[Y], v1[Z] - v2[Z]);
00104 }
00105 
00106 inline Vector operator-(const Vector& v) {
00107   return Vector(-v[X], -v[Y], -v[Z]);
00108 }
00109 
00110 inline Vector operator*(const Vector& v, Scalar s) {
00111   return Vector(v[X] * s, v[Y] * s, v[Z] * s);
00112 }
00113 
00114 inline Vector operator*(Scalar s, const Vector& v) { return v * s; }
00115 
00116 inline Vector operator/(const Vector& v, Scalar s) {
00117   assert(!eqz(s));
00118   return v * (1 / s);
00119 }
00120 
00121 inline Scalar dot(const Vector& v1, const Vector& v2) {
00122   return v1[X] * v2[X] + v1[Y] * v2[Y] + v1[Z] * v2[Z];
00123 }
00124 
00125 inline Scalar Vector::length2() const { return dot(*this, *this); }
00126 inline Scalar Vector::length() const { return sqrt(length2()); }
00127 
00128 inline bool Vector::approxZero() const { return length2() < EPSILON2; }
00129 
00130 inline void Vector::normalize() { *this /= length(); }
00131 inline Vector Vector::normalized() const { return *this / length(); }
00132 
00133 inline int Vector::closestAxis() const {
00134   Scalar a[2];
00135   int axis = (a[X] = fabs(comp[X])) < (a[Y] = fabs(comp[Y])) ? Y : X;
00136   return a[axis] < fabs(comp[Z]) ? Z : axis;
00137 }
00138 
00139 inline Vector Vector::random() {
00140   Scalar z = 2 * rnd() - 1;
00141   Scalar r = sqrt(1 - z * z);
00142   Scalar t = TWO_PI * rnd();
00143   return Vector(r * cos(t), r * sin(t), z);
00144 }
00145 
00146 inline Scalar length2(const Vector& v) { return v.length2(); }
00147 inline Scalar length(const Vector& v) { return v.length(); }
00148 
00149 inline bool approxZero(const Vector& v) { return v.approxZero(); }
00150 inline bool approxEqual(const Vector& v1, const Vector& v2) { 
00151   return approxZero(v1 - v2); 
00152 }
00153 
00154 inline Scalar angle(const Vector& v1, const Vector& v2) {
00155   Scalar s = sqrt(v1.length2() * v2.length2());
00156   assert(!eqz(s));
00157   return acos(dot(v1, v2) / s);
00158 }
00159 
00160 inline Vector cross(const Vector& v1, const Vector& v2) {
00161   return Vector(v1[Y] * v2[Z] - v1[Z] * v2[Y],
00162                 v1[Z] * v2[X] - v1[X] * v2[Z],
00163                 v1[X] * v2[Y] - v1[Y] * v2[X]);
00164 }
00165 
00166 inline Scalar triple(const Vector& v1, const Vector& v2, const Vector& v3) {
00167   return v1[X] * (v2[Y] * v3[Z] - v2[Z] * v3[Y]) + 
00168          v1[Y] * (v2[Z] * v3[X] - v2[X] * v3[Z]) + 
00169          v1[Z] * (v2[X] * v3[Y] - v2[Y] * v3[X]);
00170 }
00171 
00172 #endif
00173 

Generated at Thu Feb 26 21:52:31 2004 for torcs by doxygen 1.3.3 written by Dimitri van Heesch, © 1997-1999
TORCS © Eric Espié 1999, 2002.