00001
00002 #ifndef __JBXL_CPP_T_VECTOR_H_
00003 #define __JBXL_CPP_T_VECTOR_H_
00004
00012 #include "Vector.h"
00013
00014
00015
00016 namespace jbxl {
00017
00018
00019 #define TVECTOR TVector
00020
00021
00027 template<typename T=double> class DllExport TVector : public Vector<T>
00028 {
00029 public:
00030 T t;
00031
00032 public:
00033 TVector(T X=0, T Y=0, T Z=0, T W=0, double N=0.0, double C=1.0, int D=0) { set(X, Y, Z, W, N, C, D);}
00034 virtual ~TVector() {}
00035
00036 void set(T X, T Y=0, T Z=0, T W=0, double N=0.0, double C=1.0, int D=0);
00037 void init() { Vector<T>::init(); t = (T)0;}
00038 };
00039
00040
00041
00047 template <typename T> void TVector<T>::set(T X, T Y, T Z, T W, double N, double C, int D)
00048 {
00049 Vector<T>::set(X, Y, Z, N, C, D);
00050 t = W;
00051
00052 if (N<=0.0) {
00053 Vector<T>::n = (T)sqrt(X*X + Y*Y + Z*Z);
00054 }
00055 }
00056
00057
00058
00065 template <typename T> double ProportionVector(TVector<T> v1, TVector<T> v2, T& t)
00066 {
00067 double cc = 0.0;
00068 T tt = (T)(Max(v2.t, Zero_Eps));
00069 if (v2.n>=tt) cc = (v1*v2)/(double)(v2.n*v2.n);
00070
00071 TVector<T> dif = v1 - cc*v2;
00072 T tolerance = (T)Max(dif.t, Zero_Eps);
00073 if (dif.n>=tolerance) {
00074 t = 0.0;
00075 return -HUGE_VALF;
00076 }
00077 t = (T)((v1.n*v2.t + v2.n*v1.t)/(v2.n*v2.n));
00078 return cc;
00079 }
00080
00081
00082
00084
00085
00086 template<typename T> inline TVector<T> operator - (const TVector<T> a)
00087 { return TVector<T>(-a.x, -a.y, -a.z, a.t, a.n); }
00088
00089
00090 template<typename T> inline TVector<T> operator + (const TVector<T> a, const TVector<T> b)
00091 {
00092 T xx = a.x + b.x;
00093 T yy = a.y + b.y;
00094 T zz = a.z + b.z;
00095 T tt = a.t + b.t;
00096 if (Xabs(xx)<tt) xx = (T)0.0;
00097 if (Xabs(yy)<tt) yy = (T)0.0;
00098 if (Xabs(zz)<tt) zz = (T)0.0;
00099 return TVector<T>(xx, yy, zz, tt);
00100 }
00101
00102
00103 template<typename T, typename R> inline TVector<T> operator + (const R d, const TVector<T> a)
00104 { return TVector<T>((T)d+a.x, (T)d+a.y, (T)d+a.z, a.t);}
00105
00106
00107 template<typename T, typename R> inline TVector<T> operator + (const TVector<T> a, const R d)
00108 { return TVector<T>(a.x+(T)d, a.y+(T)d, a.z+(T)d, a.t);}
00109
00110
00111 template<typename T> inline TVector<T> operator - (const TVector<T> a, const TVector<T> b)
00112 {
00113 T xx = a.x - b.x;
00114 T yy = a.y - b.y;
00115 T zz = a.z - b.z;
00116 T tt = a.t + b.t;
00117 if (Xabs(xx)<tt) xx = (T)0.0;
00118 if (Xabs(yy)<tt) yy = (T)0.0;
00119 if (Xabs(zz)<tt) zz = (T)0.0;
00120 return TVector<T>(xx, yy, zz, tt);
00121 }
00122
00123
00124 template<typename T, typename R> inline TVector<T> operator - (const R d, const TVector<T> a)
00125 { return TVector<T>((T)d-a.x, (T)d-a.y, (T)d-a.z, a.t);}
00126
00127
00128 template<typename T, typename R> inline TVector<T> operator - (const TVector<T> a, const R d)
00129 { return TVector<T>(a.x-(T)d, a.y-(T)d, a.z-(T)d, a.t);}
00130
00131
00132 template<typename T, typename R> inline TVector<T> operator * (const R d, const TVector<T> a)
00133 { return TVector<T>(a.x*(T)d, a.y*(T)d, a.z*(T)d, a.t*Xabs((T)d));}
00134
00135
00136 template<typename T, typename R> inline TVector<T> operator * (const TVector<T> a, const R d)
00137 { return TVector<T>(a.x*(T)d, a.y*(T)d, a.z*(T)d, a.t*Xabs((T)d));}
00138
00139
00140 template<typename T, typename R> inline TVector<T> operator / (const TVector<T> a, const R d)
00141 { return TVector<T>(a.x/(T)d, a.y/(T)d, a.z/(T)d, a.t/Xabs((T)d));}
00142
00143
00144 template<typename T, typename R> inline TVector<T> operator / (const R d, const TVector<T> a)
00145 { return TVector<T>((T)d/a.x, (T)d/a.y, (T)d/a.z, Xabs((T)d)*a.t/a.norm2());}
00146
00147
00148 template <typename T> inline bool operator == (const TVector<T> v1, const TVector<T> v2)
00149 {
00150 T dst = (v1.x-v2.x)*(v1.x-v2.x) + (v1.y-v2.y)*(v1.y-v2.y) + (v1.z-v2.z)*(v1.z-v2.z);
00151 T err = (v1.t+v2.t)*(v1.t+v2.t);
00152 if (dst<=err) return true;
00153 return false;
00154 }
00155
00156
00157 template <typename T> inline bool operator != (const TVector<T> v1, const TVector<T> v2)
00158 {
00159 T dst = (v1.x-v2.x)*(v1.x-v2.x) + (v1.y-v2.y)*(v1.y-v2.y) + (v1.z-v2.z)*(v1.z-v2.z);
00160 T err = (v1.t+v2.t)*(v1.t+v2.t);
00161 if (dst>err) return true;
00162 return false;
00163 }
00164
00165
00167 template<typename T> inline TVector<T> operator ^ (const TVector<T> a, const TVector<T> b)
00168 {
00169 T xx = a.y*b.z - a.z*b.y;
00170 T yy = a.z*b.x - a.x*b.z;
00171 T zz = a.x*b.y - a.y*b.x;
00172 T tt = (T)(a.n*b.t + a.t*b.n);
00173 if (Xabs(xx)<tt) xx = (T)0.0;
00174 if (Xabs(yy)<tt) yy = (T)0.0;
00175 if (Xabs(zz)<tt) zz = (T)0.0;
00176 return TVector<T>(xx, yy, zz, tt);
00177 }
00178
00179
00181 template<typename T> inline T operator * (const TVector<T> a, const TVector<T> b)
00182 { return (T)(a.x*b.x + a.y*b.y + a.z*b.z);}
00183
00184
00186 template<typename T> inline T TVectorMultiTolerance(TVector<T> a, TVector<T> b)
00187 { return (T)(a.n*b.t + a.t*b.n); }
00188
00189
00190
00191 }
00192
00193
00194 #endif
00195