ML Reference
MeVis/Foundation/Sources/MLLinearAlgebra/mlVector3.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //=====================================================================================
00004 
00009 //=====================================================================================
00010 #ifndef __mlVector3_H
00011 #define __mlVector3_H
00012 
00013 // Include system independency file and project settings.
00014 #ifndef __mlLinearAlgebraSystem_H
00015 #include "mlLinearAlgebraSystem.h"
00016 #endif
00017 #ifndef __mlLinearAlgebraDefs_H
00018 #include "mlLinearAlgebraDefs.h"
00019 #endif
00020 #ifndef __mlFloatingPointVector_H
00021 #include "mlFloatingPointVector.h"
00022 #endif
00023 #ifndef __mlVector2_H
00024 #include "mlVector2.h"
00025 #endif
00026 
00027 
00028 // All declarations of this header will be in the ML_LA_NAMESPACE namespace.
00029 ML_LA_START_NAMESPACE
00030 
00032 template <class DT>
00033 class Vector3DataContainer
00034 {
00035 public:
00036   union {
00037     struct {
00039       DT x;
00041       DT y;
00043       DT z;
00044     };
00046     DT _buffer[3];
00047   };
00048 };
00049 
00050 //--------------------------------------------------------------------
00052 // This is necessary because we do not known whether vector or
00053 // matrix header is included first and we cannot move template
00054 // code into C++ file.
00055 //--------------------------------------------------------------------
00056 template <class DT> class Tvec4;
00057 template <class DT> class Tmat3;
00058 template <class DT> class Tmat4;
00060 
00061 //--------------------------------------------------------------------
00063 //--------------------------------------------------------------------
00064 template <class DT>
00065 class Tvec3 : public FloatingPointVector<DT, 3, Vector3DataContainer<DT> >
00066 {
00067 
00068 public:
00069 
00071   typedef FloatingPointVector<DT, 3, Vector3DataContainer<DT> > Superclass;
00072 
00074   typedef DT ComponentType;
00075 
00076   //--------------------------------------------------------------------
00078 
00079   //--------------------------------------------------------------------
00082   inline explicit Tvec3(const DT value=0) : FloatingPointVector<DT, 3, Vector3DataContainer<DT> >(value)
00083   {
00084     ML_TRACE_IN_TIME_CRITICAL("Tvec3::Tvec3(const DT value=0)");
00085   }
00086 
00090   inline Tvec3(const Superclass &v) : Superclass(v)
00091   {
00092     ML_TRACE_IN_TIME_CRITICAL("Tvec3::Tvec3(const FloatingPointVector<DT, 3, Vector3DataContainer<DT> >& v)");
00093   }
00094 
00097   inline Tvec3(const DT px, const DT py, const DT pz)
00098   {
00099     ML_TRACE_IN_TIME_CRITICAL("Tvec3::Tvec3(const DT px, const DT py, const DT pz)");
00100 
00101     Superclass::_buffer[0] = px;
00102     Superclass::_buffer[1] = py;
00103     Superclass::_buffer[2] = pz;
00104   };
00105 
00107   inline Tvec3(const Tvec2<DT>& v, const DT pz)
00108   {
00109     ML_TRACE_IN_TIME_CRITICAL("Tvec3::Tvec3(const Tvec2<DT>& v, const DT z)");
00110 
00111     Superclass::_buffer[0] = v[0];
00112     Superclass::_buffer[1] = v[1];
00113     Superclass::_buffer[2] = pz;
00114   };
00115 
00121   Tvec3(const Tvec4<DT>& v, const bool normalizeV)
00122   {
00123     ML_TRACE_IN("Tvec3::Tvec3(const Tvec4<DT>& v, const bool normalizeV)");
00124 
00125     ML_TRY
00126     {
00127       if (normalizeV){
00128         // Homogeneous cast.
00129         if (MLValuesDifferWOM(v[3], static_cast<DT>(0))){
00130           DT divi = 1.0/v[3];
00131           Superclass::_buffer[0] = v[0]*divi;
00132           Superclass::_buffer[1] = v[1]*divi;
00133           Superclass::_buffer[2] = v[2]*divi;
00134         }
00135         else{
00136           // This must/should not occur.
00137           ML_PRINT_ERROR("Tvec3::Tvec3(const Tvec4<DT>& v, const bool normalizeV)",
00138                          ML_BAD_PARAMETER,
00139                          "Normalization with zero fourth component of homogeneous vector "
00140                          "performed. Leaving vector unchanged.");
00141         }
00142       }
00143       else{
00144         // Normal cast.
00145         Superclass::_buffer[0] = v[0];
00146         Superclass::_buffer[1] = v[1];
00147         Superclass::_buffer[2] = v[2];
00148       }
00149     }
00150     ML_CATCH_RETHROW;
00151   }
00152 
00156   Tvec3(const Tvec4<DT>& v, const int axis)
00157   {
00158     ML_TRACE_IN_TIME_CRITICAL("Tvec3::Tvec3(const Tvec4<DT>& v, const int axis)");
00159 
00160     switch (axis) {
00161       case  0:
00162         Superclass::_buffer[0] = v[1];
00163         Superclass::_buffer[1] = v[2];
00164         Superclass::_buffer[2] = v[3];
00165         break;
00166       case  1:
00167         Superclass::_buffer[0] = v[0];
00168         Superclass::_buffer[1] = v[2];
00169         Superclass::_buffer[2] = v[3];
00170         break;
00171       case  2:
00172         Superclass::_buffer[0] = v[0];
00173         Superclass::_buffer[1] = v[1];
00174         Superclass::_buffer[2] = v[3];
00175         break;
00176       default:
00177         Superclass::_buffer[0] = v[0];
00178         Superclass::_buffer[1] = v[1];
00179         Superclass::_buffer[2] = v[2];
00180         break;
00181     }
00182   }
00184 
00186   inline void assign(const DT px, const DT py, const DT pz)
00187   {
00188     ML_TRACE_IN_TIME_CRITICAL("Tvec3::assign(const DT px, const DT py, const DT pz)");
00189 
00190     Superclass::_buffer[0] = px;
00191     Superclass::_buffer[1] = py;
00192     Superclass::_buffer[2] = pz;
00193   }
00194 
00199   inline Tvec3<DT> divideByLastComp() const
00200   {
00201     ML_TRACE_IN( "Tvec3<DT>::divideByLastComp() const( )" );
00202 
00203     Tvec3<DT> retVal;
00204     ML_TRY
00205     {
00206 
00207       const DT lastComp = Superclass::_buffer[2];
00208       ML_CHECK_FLOAT(lastComp);
00209 
00210       const DT div = static_cast<DT>(1) / lastComp;
00211 
00212       retVal = Tvec3(Superclass::_buffer[0] * div,
00213                      Superclass::_buffer[1] * div,
00214                      1);
00215     }
00216     ML_CATCH_RETHROW;
00217     return retVal;
00218   }
00219 
00222   inline Tvec4<DT> affineVec() const
00223   {
00224     ML_TRACE_IN_TIME_CRITICAL("Tvec3::affineVec() const");
00225 
00226     return Tvec4<DT>(*this, 0);
00227   }
00228 
00231   inline Tvec4<DT> affinePoint() const
00232   {
00233     ML_TRACE_IN_TIME_CRITICAL("Tvec3::affinePoint() const");
00234 
00235     return Tvec4<DT>(*this, 1);
00236   }
00237 
00238 #ifdef ML_DEPRECATED
00239 
00240 
00241 
00242 
00243   inline ML_DEPRECATED void Set(const DT px, const DT py, const DT pz)
00244   {
00245     ML_TRACE_IN_TIME_CRITICAL("Set(const DT px, const DT py, const DT pz)");
00246 
00247     assign(px, py, pz);
00248   }
00250 #endif
00251 
00252 
00253 }; // end of class *Tvec3*
00254 
00255 
00256 //--------------------------------------------------------------------
00259 //--------------------------------------------------------------------
00260 template <class DT, class DT2>
00261 inline Tvec3<DT> operator*(const Tmat3<DT>& a, const Tvec3<DT2>& v)
00262 {
00263   ML_TRACE_IN_TIME_CRITICAL("mlVector3.h: operator*(const Tmat3<DT>& a, const Tvec3<DT2>& v)");
00264 
00265   return Tvec3<DT>(a[0][0]*v[0] + a[0][1]*v[1] + a[0][2]*v[2],
00266                    a[1][0]*v[0] + a[1][1]*v[1] + a[1][2]*v[2],
00267                    a[2][0]*v[0] + a[2][1]*v[1] + a[2][2]*v[2]);
00268 }
00269 
00270 //--------------------------------------------------------------------
00273 //--------------------------------------------------------------------
00274 template <class DT, class DT2>
00275 inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat3<DT2>& a)
00276 {
00277   ML_TRACE_IN_TIME_CRITICAL("mlVector3.h: operator*(const Tvec3<DT>& v, const Tmat3<DT2>& a)");
00278 
00279   return a.transpose() * v;
00280 }
00281 
00282 //--------------------------------------------------------------------
00289 //--------------------------------------------------------------------
00290 template <class DT, class DT2>
00291 inline Tvec3<DT> operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)
00292 {
00293   ML_TRACE_IN_TIME_CRITICAL("mlVector3.h: operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)");
00294 
00295   // Normalize homogeneous 4d result to Tvec3 by dividing by the fourth component.
00296   return Tvec3<DT>(a * v.affinePoint(), true);
00297 }
00298 
00299 //--------------------------------------------------------------------
00306 //--------------------------------------------------------------------
00307 template <class DT, class DT2>
00308 inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat4<DT2>& a)
00309 {
00310   ML_TRACE_IN_TIME_CRITICAL("mlVector3.h: operator*(const Tvec3<DT>& v, const Tmat4<DT2>& a)");
00311 
00312   // Note: This uses homogeneous "operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)"
00313   // implemented above.
00314   return a.transpose() * v;
00315 }
00316 
00317 //--------------------------------------------------------------------
00319 //--------------------------------------------------------------------
00320 template <class DT, class DT2>
00321 inline Tvec3<DT> operator^(const Tvec3<DT>& a, const Tvec3<DT2>& b)
00322 {
00323   ML_TRACE_IN_TIME_CRITICAL("mlVector3.h: operator^(const Tvec3<DT> &a, const Tvec3<DT2> &b)");
00324 
00325   return Tvec3<DT>(a[1]*b[2] - a[2]*b[1],
00326                    a[2]*b[0] - a[0]*b[2],
00327                    a[0]*b[1] - a[1]*b[0]);
00328 }
00329 
00330 //-----------------------------------------------------------------------------------
00332 
00333 //-----------------------------------------------------------------------------------
00335 typedef Tvec3<MLfloat>   Vector3f;
00337 typedef Tvec3<MLdouble>  Vector3d;
00339 typedef Tvec3<MLldouble> Vector3ld;
00341 typedef Tvec3<MLdouble>  Vector3;
00343 
00344 
00345 #ifdef ML_DEPRECATED
00346 
00347 
00348 
00349 
00350 ML_DEPRECATED typedef Tvec3<MLfloat>   vecf3;
00353 ML_DEPRECATED typedef Tvec3<MLdouble>  vecd3;
00356 ML_DEPRECATED typedef Tvec3<MLldouble> vecld3;
00359 ML_DEPRECATED typedef Tvec3<MLdouble> vec3;
00361 
00362 #endif // ML_DEPRECATED
00363 
00364 
00365 ML_LA_END_NAMESPACE
00366 
00367 #endif //of __mlVector3_H
00368 
00369