00001 #ifndef __VECMATH_VECTOR3_HPP
00002 #define __VECMATH_VECTOR3_HPP
00003
00004 #ifndef __VECMATH_TUPLE3_HPP
00005 #include <vecmath/_Tuple3.h>
00006 #endif
00007
00008 #ifndef __VECMATH_POINT3_HPP
00009 #include <vecmath/_Point3.h>
00010 #endif
00011
00012
00013 #define DEBUGVECTOR3(d)
00014
00015 template <class Type> class _Vector3 : public _Tuple3<Type> {
00016 public:
00017
00018 _Vector3<Type>() : _Tuple3<Type>() {}
00019
00020
00021 explicit _Vector3<Type>(Type x, Type y = 0, Type z = 0) : _Tuple3<Type>(x, y, z) {
00022 DEBUGVECTOR3(cout << "_Vector3 constructor x,y,z " << *this << endl);
00023 }
00024
00025 _Vector3<Type>(const _Tuple3<double>& v) : _Tuple3<Type>(v) {
00026 DEBUGVECTOR3(cout << "_Vector3 constructor &double " << *this << endl);
00027 }
00028
00029
00030 _Vector3<Type>(const _Point3<double>& v, const _Point3<double>& v1) {
00031 _Tuple3<Type>::sub(v, v1);
00032 DEBUGVECTOR3(cout << "_Vector3 constructor &double " << *this << endl);
00033 }
00034
00035 _Vector3<Type>(const _Tuple3<float>& v) : _Tuple3<Type>(v) {
00036 DEBUGVECTOR3(cout << "_Vector3 constructor &float " << *this << endl);
00037 }
00038
00039 _Vector3<Type>(const _Point3<float>& v, const _Point3<float>& v1) {
00040 _Tuple3<Type>::sub(v, v1);
00041 DEBUGVECTOR3(cout << "_Vector3 constructor &float " << *this << endl);
00042 }
00043
00044 _Vector3<Type>(const Type t[3]) : _Tuple3<Type>(t) {
00045 DEBUGVECTOR3(cout << "_Vector3 constructor t[3] " << *this << endl);
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055 Type angle(const _Vector3<Type>& v) const {
00056 double d = dot(v) / (length() * v.lentgh());
00057 if (d < -1)
00058 d = -1;
00059 else if (d > 1)
00060 d = 1;
00061 return (Type) acos(d);
00062 }
00063
00064
00065 void cross(const _Vector3<Type>& v, const _Vector3<Type>& v1) {
00066 Type t1 = v.y * v1.z - v1.y * v.z;
00067 Type t2 = v.z * v1.x - v1.z * v.x;
00068
00069 _Tuple3<Type>::z = v.x * v1.y - v1.x * v.y;
00070 _Tuple3<Type>::x = t1;
00071 _Tuple3<Type>::y = t2;
00072 }
00073
00074 Type dot(const _Vector3<Type>& v) const {
00075 return _Tuple3<Type>::x * v.x + _Tuple3<Type>::y * v.y + _Tuple3<Type>::z * v.z;
00076 }
00077
00078 Type lengthSquared() const {
00079 return _Tuple3<Type>::normSquared();
00080 }
00081
00082 Type length() const {
00083 return _Tuple3<Type>::norm();
00084 }
00085
00086 void normalize() {
00087 normalize(*this);
00088 }
00089
00090 void normalize(const _Vector3<Type> &v) {
00091 Type l = v.length();
00092 this->set(v.x / l, v.y / l, v.z / l);
00093 }
00094 };
00095
00096 #endif