D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/cl_vectors_template.h

00001 /* LICENSE_BEGIN
00002 SutoiTools library supply support for debugging, testing and 
00003 setting good bases for you application. It is part of the 
00004 SugoiEngine project.
00005 Copyright (C) 2006 Allusse Yannick (yannick dot allusse at laposte dot net)
00006 
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 LICENSE_END
00022 */
00023 
00024 /* \file SG_TLS_cl_vectors_template.h
00025  * \brief Basic template vector class of any dimension and any type.
00026  * \author Yannick Allusse
00027 */
00028 #ifndef CL_VECTORS_h
00029 #define CL_VECTORS_h
00030 
00031 
00032 #include "SugoiTools\config.h"
00033 #include <math.h>
00034 #include "SugoiTools\XML.h"
00035 #include "SugoiTools\exceptions.h"
00036 #include "SugoiTools\logger_main.h"
00037 #include "SugoiTools\tools.h"
00038 
00039 #if SG_TLS_MEMORY_MANAGER
00040           #include "SugoiTools\debug_new.h"
00041 #endif 
00042 
00043 
00044 
00045 namespace SGE{
00046 #define VECTOR_OP_EQUAL(_THIS, _SND_OBJ, _OP, _NUMBER){\
00047           for (int i=0; i< _NUMBER; i++)\
00048                     _THIS->vec[i] _OP= _SND_OBJ;\
00049           return *this;\
00050 }
00051 #define VECTOR_OP_EQUAL_EXT(_FRST_OBJ, _SND_OBJ, _OP, _NUMBER){\
00052           for (int i=0; i< _NUMBER; i++)\
00053                     _FRST_OBJ.vec[i] _OP= _SND_OBJ;\
00054           return _FRST_OBJ;\
00055 }
00056 
00057 #define VECTOR_OP(_THIS, _SND_OBJ, _OP, _TTYPE){\
00058           CLT_Vector<_TTYPE> Temp((*_THIS));\
00059           Temp _OP= _SND_OBJ;\
00060           return Temp;\
00061 }
00062 #define VECTOR_OP_EXT(_FRST_OBJ, _SND_OBJ, _OP, _TTYPE, _NUMBER){\
00063           CLT_Vector<_TTYPE> Temp(_FRST_OBJ);\
00064           for (int i=0; i< _NUMBER; i++)\
00065                     Temp.vec[i] _OP= _SND_OBJ;\
00066           return Temp;\
00067 }
00068 
00069 #define VECTOR_COMPARE(_VEC1, _SND_OBJ, _OP, _NUMBER){\
00070           for (int i=0; i< _NUMBER; i++)\
00071           {         if (!(_VEC1.vec[i] _OP _SND_OBJ))\
00072                               return false;\
00073           }\
00074           return true;\
00075 }
00076 
00077 #define VECTOR_COMPARE_INTERNAL(_VEC1, _SND_OBJ, _OP, _NUMBER){\
00078           for (int i=0; i< _NUMBER; i++)\
00079           {         if (!(_VEC1->vec[i] _OP _SND_OBJ))\
00080                               return false;\
00081           }\
00082           return true;\
00083 }
00084 
00085 #define VECTOR_CHECK_INDEX(index){\
00086           SG_Assert(index>=0, "Index out of bound");\
00087           SG_Assert(index<VectorDim, "Index out of bound");\
00088 }
00089 
00090 #define VECTOR_CHECK_BOTH_SIZE(_size1, size2){SG_Assert(_size1 == size2, "Both vector don't have the same size");}
00091 
00092 
00099 template <typename TType> 
00100           class CLT_Vector
00101                     //: public CL_XML_OBJ????????
00102           {
00103 public :
00104           typedef int         VectorDimType;
00105 
00106 private:
00107           VectorDimType VectorDim;
00108 //        TType               m_defaultValue;
00109 public :
00110           TType *vec; //value table
00111 
00112 //Contructors / Destructors   =================================
00113           _SG_TLS_INLINE      CLT_Vector(const CLT_Vector<TType> &    _Vec);
00114           template <typename TType2> CLT_Vector(const CLT_Vector<TType2> &      _Vec);
00115           _SG_TLS_INLINE      CLT_Vector(const int _VectorDim, const TType *    _Val);
00116           _SG_TLS_INLINE      CLT_Vector(const int _VectorDim, const TType                _Val);
00117           _SG_TLS_INLINE      ~CLT_Vector();
00118 //===========================================================
00119 
00120           _SG_TLS_INLINE void           Clear(int _Dim = -1);
00121           _SG_TLS_INLINE float          Length(); 
00122           _SG_TLS_INLINE float          Normalize(TType _unit = 1);   
00123 
00124           _SG_TLS_INLINE TType          GetVal(const int _index);
00125           _SG_TLS_INLINE TType *        GetPtr(const int _index);
00126           _SG_TLS_INLINE int            GetDim() const;
00127 //        _SG_TLS_INLINE void           SetDefaultValue(TType _val);
00128 //        _SG_TLS_INLINE TType          GetDefaultValue()const;
00129 
00130 
00131 
00132 //Operators         =================================================
00133           _SG_TLS_INLINE TType& operator[]	(const int _index);
00134           _SG_TLS_INLINE TType  operator[]	(const int _index)const;
00135 
00136           //We do the operators with CLT_Vector and TType, cause the float val enter in conflict with the int val of the basic constructor CLT_Vector(const int _VectorDim)
00137           _SG_TLS_INLINE CLT_Vector<TType> &operator+=		(const CLT_Vector<TType> &_Vec);
00138           _SG_TLS_INLINE CLT_Vector<TType> operator+		(const CLT_Vector<TType> &_Vec)     const;
00139           _SG_TLS_INLINE CLT_Vector<TType> &operator+=		(const TType _Val);
00140           _SG_TLS_INLINE CLT_Vector<TType> operator+		(const TType _Val)        const;
00141 
00142           _SG_TLS_INLINE CLT_Vector<TType> &operator-=                (const CLT_Vector &_Vec);
00143           _SG_TLS_INLINE CLT_Vector<TType> operator-		(const CLT_Vector &_Vec)  const;
00144           _SG_TLS_INLINE CLT_Vector<TType> operator-		()                                                                const;
00145           _SG_TLS_INLINE CLT_Vector<TType> &operator-=                (const TType _Val);
00146           _SG_TLS_INLINE CLT_Vector<TType> operator-		(const TType _Val)        const;
00147 
00148           _SG_TLS_INLINE CLT_Vector<TType> &operator/=		(const CLT_Vector<TType> &_Vec);
00149           _SG_TLS_INLINE CLT_Vector<TType> operator/                  (const CLT_Vector<TType> &_Vec);
00150           _SG_TLS_INLINE CLT_Vector<TType> &operator/=		(const TType _Val);
00151           _SG_TLS_INLINE CLT_Vector<TType> operator/                  (const TType _Val)  const;
00152 
00153           _SG_TLS_INLINE CLT_Vector<TType> &operator*=		(const CLT_Vector<TType> &_Vec);
00154           _SG_TLS_INLINE CLT_Vector<TType> operator*		(const CLT_Vector<TType> &_Vec)     const;
00155           _SG_TLS_INLINE CLT_Vector<TType> &operator*=		(const TType _Val);
00156           _SG_TLS_INLINE CLT_Vector<TType> operator*		(const TType _Val)        const;
00157 
00158           _SG_TLS_INLINE CLT_Vector<TType> &operator=		(const CLT_Vector<TType> &_Vec);
00159           _SG_TLS_INLINE CLT_Vector<TType> &operator=		(const TType   _f);
00160           _SG_TLS_INLINE CLT_Vector<TType> &operator=		(const TType * _f);
00161           _SG_TLS_INLINE bool operator==                                                  (const CLT_Vector<TType> &_Vec2);
00162           _SG_TLS_INLINE bool operator!=                                                  (const CLT_Vector<TType> &_Vec2);
00163           //===========================================================
00164 
00165           _SG_TLS_INLINE bool           operator< (const CLT_Vector<TType> &_Vec2);
00166           _SG_TLS_INLINE bool           operator< (const TType        _f);
00167           _SG_TLS_INLINE bool           operator< (const TType *      _f);
00168           _SG_TLS_INLINE bool           operator> (const CLT_Vector<TType> &_Vec2);
00169           _SG_TLS_INLINE bool           operator> (const TType        _f);
00170           _SG_TLS_INLINE bool           operator> (const TType *      _f);
00171 
00172 
00173           template <typename TType2> CLT_Vector<TType>& operator=	(CLT_Vector<TType2> &_Vec2)
00174           {
00175                     VECTOR_OP_EQUAL(this,  _Vec2.vec[i], , this.GetDim());
00176           }
00177 
00178 #if _SG_TLS_XML
00179           
00188           _SG_TLS_INLINE 
00189                     TiXmlElement*
00190                     XMLLoad(TiXmlElement* XML_Elem, const std::string _name, TType _defVal = 0);
00191           
00220           _SG_TLS_INLINE 
00221                     TiXmlElement* 
00222                     XMLLoad(TiXmlElement* XML_Elem, const std::string _name, std::string  _type, TType _defVal = 0);
00223 
00224 
00225           _SG_TLS_INLINE bool XMLSave(TiXmlElement* XML_Elem, const std::string _name, char * _type, TType _defVal=0);
00226           _SG_TLS_INLINE bool XMLSave(TiXmlElement* XML_Elem, const std::string _Name, TType _defVal=0);
00227 #endif
00228 
00229 };
00230 
00231 _SG_TLS_INLINE std::ostream& operator<< (std::ostream& s, const CLT_Vector<typename>& _v);
00232 _SG_TLS_INLINE std::ostream& operator<< (const CLT_Vector<typename>& _v, std::ostream& s);
00233 
00234 
00235 
00236 template <typename Type>
00237 void Swap(Type& _v1, Type& _v2)
00238 {
00239           Type TempVal(_v1);
00240           _v1 = _v2;
00241           _v2 = TempVal;
00242 }
00243 
00244 
00245 template <typename TType1, typename TType2> CLT_Vector<TType1> operator*        (const CLT_Vector<TType1> &_Vec1, CLT_Vector<TType2> &_Vec2)
00246 {
00247           VECTOR_OP_EXT(_Vec1,  _Vec2.vec[i], *, TType1, _Vec1.GetDim());
00248 }
00249 
00250 template <typename TType1, typename TType2> CLT_Vector<TType1> operator+        (const CLT_Vector<TType1> &_Vec1, CLT_Vector<TType2> &_Vec2)
00251 {
00252           VECTOR_OP_EXT(_Vec1,  _Vec2.vec[i], +, TType1, _Vec1.GetDim());
00253 }
00254 template <typename TType1, typename TType2> CLT_Vector<TType1> operator-        (const CLT_Vector<TType1> &_Vec1, CLT_Vector<TType2> &_Vec2)
00255 {
00256           VECTOR_OP_EXT(_Vec1,  _Vec2.vec[i], -, TType1, _Vec1.GetDim());
00257 }
00258 template <typename TType1, typename TType2> CLT_Vector<TType1> operator/        (const CLT_Vector<TType1> &_Vec1, CLT_Vector<TType2> &_Vec2)
00259 {
00260           VECTOR_OP_EXT(_Vec1,  _Vec2.vec[i], /, TType1, _Vec1.GetDim());
00261 }
00262 
00263 
00264 
00265 
00266 
00267 //CODE of template is HERE !!!!!!!!!
00268 
00269 
00270 //============================================================================
00271 //CLT_Vector START===========================================================
00272 //============================================================================
00278 template <typename TType> CLT_Vector<TType>::CLT_Vector(const CLT_Vector<TType> &_Vec)
00279 {
00280           Clear(_Vec.GetDim());
00281           (*this) = _Vec;
00282 };
00283 
00289 template <typename TType>template <typename TType2> CLT_Vector<TType>::CLT_Vector(const CLT_Vector<TType2> &_Vec)
00290 {
00291           Clear(_Vec.GetDim());
00292           for (int i=0; i< _Vec.GetDim(); i++)\
00293                     vec[i] = _Vec.vec[i];\
00294 };
00295 
00301 template <typename TType>  CLT_Vector<TType>::CLT_Vector(const int _VectorDim, const TType * _Val)
00302 {
00303           Clear(_VectorDim);
00304           SG_Assert(_Val, "Given Value is NULL");
00305           (*this) = _Val;
00306 }
00307 
00313 template <typename TType>  CLT_Vector<TType>::CLT_Vector(const int _VectorDim, const TType _Val)
00314 {
00315           Clear(_VectorDim);  
00316           (*this) = _Val;
00317 }
00318 
00323 template <typename TType>  CLT_Vector<TType>::~CLT_Vector()
00324 {         delete[](vec); }
00325 
00332 template <typename TType>void   CLT_Vector<TType>::Clear(int _Dim/* = -1*/)
00333 {         
00334           //create it if required
00335           if (_Dim != -1)
00336           {
00337                     VectorDim = _Dim;
00338                     vec = new TType[_Dim];
00339           }
00340           
00341           SG_Assert(VectorDim>0, "CLT_Vector has dimension : " << VectorDim << "and should be > 0!");
00342 
00343           //clear it
00344           (*this) = 0.;       
00345 }
00346 //==================================================================
00347 //==================================================================
00348  template <typename TType> TType   CLT_Vector<TType>::GetVal(const int _index)
00349 {
00350           VECTOR_CHECK_INDEX(_index);
00351           return vec[_index];
00352  }
00353 
00354  
00355 template <typename TType> TType *  CLT_Vector<TType>::GetPtr(const int _index)
00356 {
00357           VECTOR_CHECK_INDEX(_index);
00358           return &vec[_index];
00359 }
00360 
00364 template <typename TType> int             CLT_Vector<TType>::GetDim() const
00365 {  return VectorDim; }
00366 /*
00367 template <typename TType>void           
00368 CLT_Vector<TType>::SetDefaultValue(TType _val)
00369 {         m_defaultValue = _val; }
00370 
00371 template <typename TType>TType          
00372 CLT_Vector<TType>::GetDefaultValue()const
00373 {return m_defaultValue ;}
00374 */
00375 //==================================================================
00376 //Arithmetic operators START =====================================
00377 //==================================================================
00382 template <typename TType> TType &  CLT_Vector<TType>::operator[]	(const int _index)
00383 {
00384           VECTOR_CHECK_INDEX(_index);
00385           return vec[_index]; 
00386 }
00387 
00388 template <typename TType> TType   CLT_Vector<TType>::operator[]	(const int _index)const
00389 {
00390           VECTOR_CHECK_INDEX(_index);
00391           return vec[_index];
00392 }
00393 
00394 //        => ++++
00400 template <typename TType>  CLT_Vector<TType>&   CLT_Vector<TType>::operator+=	(const CLT_Vector<TType> &_Vec)
00401 {
00402           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00403           VECTOR_OP_EQUAL(this, _Vec.vec[i], + , VectorDim);
00404 }
00405 
00411 template <typename TType> CLT_Vector<TType> CLT_Vector<TType>::operator+	(const CLT_Vector<TType> &_Vec)const
00412 { 
00413           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00414           VECTOR_OP(this, _Vec, +, TType);
00415 }
00416 
00422 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator+=	(const TType _Val)
00423 {         
00424           VECTOR_OP_EQUAL(this, _Val, + , VectorDim);
00425 }
00426 
00432 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator+	(const TType _Val)const
00433 { 
00434           VECTOR_OP(this, _Val, +, TType);
00435 }
00436 
00437 //  => ----
00438 
00442 template <typename TType> 
00443 CLT_Vector<TType> &  
00444 CLT_Vector<TType>::operator-=	(const CLT_Vector<TType> &_Vec)
00445 {
00446           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00447           VECTOR_OP_EQUAL(this, _Vec.vec[i], - , VectorDim);
00448 }
00449 
00453 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator-      (const CLT_Vector<TType> &_Vec) const
00454 { 
00455           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00456           VECTOR_OP(this, _Vec, -, TType);        
00457 }
00458 
00463 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator-	()const
00464 { 
00465           return (*this) * -1;          
00466 }
00467 
00471 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator-=	(const TType _Val)
00472 {         
00473           VECTOR_OP_EQUAL(this, _Val, - , VectorDim);
00474 }
00475 
00479 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator-	(const TType _Val)const
00480 { 
00481           VECTOR_OP(this, _Val, -, TType);
00482 }
00483 
00484 //  => //////
00485 
00489 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator/=	(const CLT_Vector<TType> &_Vec)
00490 {
00491           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00492           VECTOR_OP_EQUAL(this, _Vec.vec[i], / , VectorDim);
00493 }
00494 
00498 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator/	(const CLT_Vector<TType> &_Vec)
00499 { 
00500           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00501           VECTOR_OP(this, _Vec, /, TType);
00502 }
00503 
00507 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator/=	(const TType _Val)
00508 {         
00509           VECTOR_OP_EQUAL(this, _Val, / , VectorDim);
00510 }
00514 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator/	(const TType _Val)const
00515 { 
00516           VECTOR_OP(this, _Val, /, TType);
00517 }
00518 
00519 //  => *****
00520 
00524 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator*=	(const CLT_Vector<TType> &_Vec)
00525 {
00526           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00527           VECTOR_OP_EQUAL(this, _Vec.vec[i], * , VectorDim);
00528 }
00529 
00533 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator*	(const CLT_Vector<TType> &_Vec)const
00534 { 
00535           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00536           VECTOR_OP(this, _Vec, *, TType);
00537 }
00538 
00542 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator*=	(const TType _Val)
00543 {         
00544           VECTOR_OP_EQUAL(this, _Val, * , VectorDim);
00545 }
00546 
00550 template <typename TType> CLT_Vector<TType>   CLT_Vector<TType>::operator*	(const TType _Val)const
00551 { 
00552           VECTOR_OP(this, _Val, *, TType);
00553 }
00554 //        => ====
00558 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator=	(const CLT_Vector<TType> &_Vec)
00559 {
00560           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec.VectorDim);
00561           VECTOR_OP_EQUAL(this, _Vec.vec[i],  , VectorDim);
00562 }
00563 
00567 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator=	(const TType _f)
00568 {
00569           VECTOR_OP_EQUAL(this, _f,  , VectorDim);
00570 }
00571 
00576 template <typename TType> CLT_Vector<TType> &  CLT_Vector<TType>::operator=	(const TType *_f)
00577 {
00578           VECTOR_OP_EQUAL(this, _f[i],  , VectorDim);
00579 }
00580 
00581 
00586 template <typename TType> float  CLT_Vector<TType>::Length()
00587 {
00588           TType Rslt = 0;
00589           for (VectorDimType i=0; i< VectorDim; i++)
00590                     Rslt += vec[i]*vec[i];
00591 
00592           if(typeid(TType) == typeid(float))
00593                     return (float)sqrt((float)Rslt);
00594           else if(typeid(TType) == typeid(double))
00595                     return (double)sqrt((double)Rslt);
00596           else if(typeid(TType) == typeid(int))
00597                     return (float)sqrt((float)Rslt);
00598           
00599           SG_Assert(0, "The template Type TType is unknown, we can not process the sqrt() function!");
00600           return 0;
00601 }
00606 /*
00607 template <typename TType> void CL_Vector3D<TType>::Normalize(TType _unit)
00608 {
00609           float len = Length();
00610           if (len != 0.0f)
00611           *this/= len;
00612           *this*=_unit;
00613           return len;
00614 }
00615 */
00616 std::ostream& operator<< (std::ostream& s, const CLT_Vector<typename>& _v)
00617 { 
00618           int Dim = _v.GetDim();
00619           s << "{";
00620           for (int i=0; i< Dim; i++)
00621                     s << _v.vec[i] << ", ";
00622           s << "}";;
00623           
00624           return s ;
00625 }
00626 
00627 std::ostream& operator<< (const CLT_Vector<typename>& _v, std::ostream& s)
00628 { 
00629           int Dim = _v.GetDim();
00630           s << "{";
00631           for (int i=0; i< Dim; i++)
00632                     s << _v.vec[i] << ", ";
00633           s << "}";;
00634           
00635           return s ;
00636 }
00637 
00638 template <typename TType> bool  CLT_Vector<TType>::operator==(const CLT_Vector<TType> &_Vec2)
00639 {
00640           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec2.VectorDim);
00641           VECTOR_COMPARE((*this), _Vec2.vec[i], ==, 4);
00642 }
00643 
00644 template <typename TType> bool  CLT_Vector<TType>::operator!=(const CLT_Vector<TType> &_Vec2)
00645 {
00646           VECTOR_CHECK_BOTH_SIZE(VectorDim, _Vec2.VectorDim);
00647           if ((*this)==_Vec2)
00648                     return true;
00649           else
00650                     return false;
00651 }
00652 
00653 
00654 
00655 template <typename TType> bool  CLT_Vector<TType>::operator<          (const CLT_Vector<TType> &_Vec2)
00656 {
00657           VECTOR_COMPARE_INTERNAL(this, _Vec2.vec[i], < , GetDim());
00658 }
00659 
00660 template <typename TType> bool  CLT_Vector<TType>::operator<          (const TType _f)
00661 {
00662           VECTOR_COMPARE_INTERNAL(this, _f, < , GetDim());
00663 }
00664 
00665 template <typename TType> bool  CLT_Vector<TType>::operator<          (const TType *_f)
00666 {
00667           VECTOR_COMPARE_INTERNAL(this, _f[i], < , GetDim());
00668 }
00669 
00670 
00671 template <typename TType> bool  CLT_Vector<TType>::operator>          (const CLT_Vector<TType> &_Vec2)
00672 {
00673           VECTOR_COMPARE_INTERNAL(this, _Vec2.vec[i], > , GetDim());
00674 }
00675 
00676 template <typename TType> bool  CLT_Vector<TType>::operator>          (const TType _f)
00677 {
00678           VECTOR_COMPARE_INTERNAL(this, _f, > , GetDim());
00679 }
00680 
00681 template <typename TType> bool  CLT_Vector<TType>::operator>          (const TType *_f)
00682 {
00683           VECTOR_COMPARE_INTERNAL(this, _f[i], > , GetDim());
00684 }
00685 
00686 
00687 
00688 #if _SG_TLS_XML     
00689 
00690 
00691 template <typename TType>
00692 TiXmlElement*  
00693 CLT_Vector<TType>::XMLLoad(TiXmlElement* XML_Elem, const std::string _name, TType _defVal/* = 0 */)
00694 {
00695           if (VectorDim == 4)
00696                     return XMLLoad(XML_Elem, _name, "rgba", _defVal);
00697           else if (VectorDim == 3)
00698                     return XMLLoad(XML_Elem, _name, "xyz", _defVal);
00699           else if (VectorDim == 2)
00700                     return XMLLoad(XML_Elem, _name, "xy", _defVal);
00701           else
00702                     return XMLLoad(XML_Elem, _name, "", _defVal);
00703           //SG_Assert(0, " CLT_Vector<TType>XMLLoad(), this vector dimension is not managed");
00704 }
00705 
00706 
00707 template <typename TType> 
00708 TiXmlElement*  
00709 CLT_Vector<TType>::XMLLoad(TiXmlElement* _XML_Elem, 
00710                                                             const std::string _name,
00711                                                             const std::string  _type, 
00712                                                             TType _defVal/* = 0 */)
00713 {
00714           SG_Assert(_name!="", "XML tag name is empty.");
00715 
00716           TiXmlElement*       XML_ELM             = NULL;
00717           
00718           //set default values
00719           (*this) = _defVal;
00720 
00721           XML_ELM = XMLGetElement(_XML_Elem, _name);
00722           if (!XML_ELM)
00723                     return NULL;
00724           
00725           char buff[2];
00726           buff[1] = '\0';
00727           int result = 0;
00728           if (_type != "")
00729           {
00730                     for (VectorDimType i =0; i< VectorDim; i++)
00731                     {
00732                               if ((VectorDimType)_type.length() > i)
00733                               {
00734                                         buff[0] = _type[i];
00735                                         XMLReadVal(XML_ELM, buff, vec[i]);
00736                               }
00737                               //else//we don't want to read all the dimension from the XML input
00738                     }
00739           }
00740           else
00741           {
00742                     std::string ValueName = "";                       
00743                     for (VectorDimType i =0; i< VectorDim; i++)
00744                     {
00745                               ValueName = "v" + ToCharStr(i);
00746                               XMLReadVal(XML_ELM, ValueName, vec[i]);
00747                     }
00748           }
00749 
00750           return XML_ELM;
00751 }
00752 
00753 template <typename TType> 
00754 bool  
00755 CLT_Vector<TType>::XMLSave(TiXmlElement* XML_Elem,
00756                                                                const std::string _name,
00757                                                                TType _defVal/* = 0 */)
00758 {
00759           if (VectorDim == 4)
00760                     return XMLSave(XML_Elem, _name, "rgba", _defVal);
00761           else if (VectorDim == 3)
00762                     return XMLSave(XML_Elem, _name, "xyz", _defVal);
00763           else if (VectorDim == 2)
00764                     return XMLSave(XML_Elem, _name, "xy", _defVal);
00765           else
00766                     return XMLSave(XML_Elem, _name, NULL, _defVal);
00767           //SG_Assert(0, " CLT_Vector<TType>XMLSave(), this vector dimension is not managed");
00768 }
00769 
00770 
00771 template <typename TType>
00772 bool  
00773 CLT_Vector<TType>::XMLSave(TiXmlElement* _XML_Elem, const std::string _name, char * _type, TType _defVal/* = 0 */)
00774 {
00775           SG_Assert(_name!="", "XML tag name is empty.");
00776 
00777           bool SaveIt=false;
00778           
00779           TiXmlElement * XML_SubElem = new TiXmlElement( _name.data() );
00780           
00781           
00782 
00783           if (_type!=NULL)
00784           {//we use to given labels
00785                     char buff[2];
00786                     buff[1] = '\0';
00787                     for (VectorDimType i =0; i< VectorDim; i++)
00788                     {
00789                               buff[0] = _type[i];
00790                               //SG_Assert(
00791                               //We don't save default value.
00792                               if(_defVal != vec[i])
00793                               {
00794                                         XMLWriteVal(XML_SubElem, buff, vec[i]);
00795                               //, "Some field are missing into an XML vector tag");
00796                                         SaveIt = true;
00797                               }
00798                     }
00799           }
00800           else
00801           {//we create generic label like v0, v1....
00802                     std::string ValueName = "";                       
00803                     for (int i = 0 ;i < VectorDim; i++)
00804                     {         
00805                               ValueName = "v" + ToCharStr(i);
00806                               //SG_Assert(
00807                               //We don't save default value.
00808                               if(_defVal != vec[i])
00809                               {
00810                                         XMLWriteVal(XML_SubElem, ValueName, vec[i]);
00811                               //, "Some field are missing into an XML vector tag");
00812                                         SaveIt = true;
00813                               }
00814                     }
00815           }
00816 
00817 
00818           if (SaveIt)
00819                     _XML_Elem->InsertEndChild(*XML_SubElem);
00820           else
00821                     delete(XML_SubElem);
00822 
00823           return SaveIt;
00824 }
00825 
00826 
00827 #endif
00828 
00829 //============================================================================
00830 //CLT_Vector END================================================================
00831 //============================================================================
00832 
00833 #if SG_TLS_MEMORY_MANAGER
00834           #include "SugoiTools\debug_new_off.h"//Must be the last....
00835 #endif 
00836 
00837 };//namespace SGE
00838 #endif

Generated on Mon Mar 19 23:15:10 2007 for SugoiTools by  doxygen 1.4.6-NO