• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

vector3.h

Go to the documentation of this file.
00001 // -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
00002 
00003 // Copyright 2010 Michael Smith, all rights reserved.
00004 
00005 //      This library is free software; you can redistribute it and / or
00006 //      modify it under the terms of the GNU Lesser General Public
00007 //      License as published by the Free Software Foundation; either
00008 //      version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Derived closely from:
00011 /****************************************
00012  * 3D Vector Classes
00013  * By Bill Perone (billperone@yahoo.com)
00014  * Original: 9-16-2002
00015  * Revised: 19-11-2003
00016  *          11-12-2003
00017  *          18-12-2003
00018  *          06-06-2004
00019  *
00020  * © 2003, This code is provided "as is" and you can use it freely as long as
00021  * credit is given to Bill Perone in the application it is used in
00022  *
00023  * Notes:
00024  * if a*b = 0 then a & b are orthogonal
00025  * a%b = -b%a
00026  * a*(b%c) = (a%b)*c
00027  * a%b = a(cast to matrix)*b
00028  * (a%b).length() = area of parallelogram formed by a & b
00029  * (a%b).length() = a.length()*b.length() * sin(angle between a & b)
00030  * (a%b).length() = 0 if angle between a & b = 0 or a.length() = 0 or b.length() = 0
00031  * a * (b%c) = volume of parallelpiped formed by a, b, c
00032  * vector triple product: a%(b%c) = b*(a*c) - c*(a*b)
00033  * scalar triple product: a*(b%c) = c*(a%b) = b*(c%a)
00034  * vector quadruple product: (a%b)*(c%d) = (a*c)*(b*d) - (a*d)*(b*c)
00035  * if a is unit vector along b then a%b = -b%a = -b(cast to matrix)*a = 0
00036  * vectors a1...an are linearly dependant if there exists a vector of scalars (b) where a1*b1 + ... + an*bn = 0
00037  *           or if the matrix (A) * b = 0
00038  *
00039  ****************************************/
00040 
00041 #ifndef VECTOR3_H
00042 #define VECTOR3_H
00043 
00044 #include <math.h>
00045 #include <string.h>
00046 
00047 template <typename T>
00048 class Vector3
00049 {
00050 public:
00051         T x, y, z;
00052 
00053         // trivial ctor
00054         Vector3<T>() { x = y = x = 0; }
00055 
00056         // setting ctor
00057         Vector3<T>(const T x0, const T y0, const T z0): x(x0), y(y0), z(z0) {}
00058 
00059         // function call operator
00060         void operator ()(const T x0, const T y0, const T z0)
00061         {       x= x0; y= y0; z= z0;  }
00062 
00063         // test for equality
00064         bool operator==(const Vector3<T> &v)
00065         {       return (x==v.x && y==v.y && z==v.z);    }
00066 
00067         // test for inequality
00068         bool operator!=(const Vector3<T> &v)
00069         {       return (x!=v.x || y!=v.y || z!=v.z);    }
00070 
00071         // negation
00072         Vector3<T> operator -(void) const
00073         {       return Vector3<T>(-x,-y,-z);    }
00074 
00075         // addition
00076         Vector3<T> operator +(const Vector3<T> &v) const
00077         {   return Vector3<T>(x+v.x, y+v.y, z+v.z);      }
00078 
00079         // subtraction
00080         Vector3<T> operator -(const Vector3<T> &v) const
00081         {   return Vector3<T>(x-v.x, y-v.y, z-v.z);      }
00082 
00083         // uniform scaling
00084         Vector3<T> operator *(const T num) const
00085         {
00086                 Vector3<T> temp(*this);
00087                 return temp*=num;
00088         }
00089 
00090         // uniform scaling
00091         Vector3<T> operator /(const T num) const
00092         {
00093                 Vector3<T> temp(*this);
00094                 return temp/=num;
00095         }
00096 
00097         // addition
00098         Vector3<T> &operator +=(const Vector3<T> &v)
00099         {
00100                 x+=v.x; y+=v.y; z+=v.z;
00101                 return *this;
00102         }
00103 
00104         // subtraction
00105         Vector3<T> &operator -=(const Vector3<T> &v)
00106         {
00107                 x-=v.x; y-=v.y; z-=v.z;
00108                 return *this;
00109         }
00110 
00111         // uniform scaling
00112         Vector3<T> &operator *=(const T num)
00113         {
00114                 x*=num; y*=num; z*=num;
00115                 return *this;
00116         }
00117 
00118         // uniform scaling
00119         Vector3<T> &operator /=(const T num)
00120         {
00121                 x/=num; y/=num; z/=num;
00122                 return *this;
00123         }
00124 
00125         // dot product
00126         T operator *(const Vector3<T> &v) const
00127         {       return x*v.x + y*v.y + z*v.z;   }
00128 
00129         // cross product
00130         Vector3<T> operator %(const Vector3<T> &v) const
00131         {
00132                 Vector3<T> temp(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
00133                 return temp;
00134         }
00135 
00136         // gets the length of this vector squared
00137         T length_squared() const
00138         {       return (T)(*this * *this);   }
00139 
00140         // gets the length of this vector
00141         float length() const
00142         {       return (T)sqrt(*this * *this);   }
00143 
00144         // normalizes this vector
00145         void normalize()
00146         {       *this/=length();        }
00147 
00148         // returns the normalized version of this vector
00149         Vector3<T> normalized() const
00150         {   return  *this/length();  }
00151 
00152         // reflects this vector about n
00153         void reflect(const Vector3<T> &n)
00154         {
00155                 Vector3<T> orig(*this);
00156                 project(n);
00157                 *this= *this*2 - orig;
00158         }
00159 
00160         // projects this vector onto v
00161         void project(const Vector3<T> &v)
00162         {       *this= v * (*this * v)/(v*v);   }
00163 
00164         // returns this vector projected onto v
00165         Vector3<T> projected(const Vector3<T> &v)
00166         {   return v * (*this * v)/(v*v);       }
00167 
00168         // computes the angle between 2 arbitrary vectors
00169         T angle(const Vector3<T> &v1, const Vector3<T> &v2)
00170         {   return (T)acosf((v1*v2) / (v1.length()*v2.length()));  }
00171 
00172         // computes the angle between 2 arbitrary normalized vectors
00173         T angle_normalized(const Vector3<T> &v1, const Vector3<T> &v2)
00174         {   return (T)acosf(v1*v2);  }
00175 
00176 };
00177 
00178 typedef Vector3<int>                    Vector3i;
00179 typedef Vector3<unsigned int>   Vector3ui;
00180 typedef Vector3<long>                   Vector3l;
00181 typedef Vector3<unsigned long>  Vector3ul;
00182 typedef Vector3<float>                  Vector3f;
00183 
00184 #endif // VECTOR3_H

Generated for ArduPilot Libraries by doxygen