Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OSGVector.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000,2001 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifndef _OSGVECTOR_H_
00040 #define _OSGVECTOR_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044 
00045 #include <OSGConfig.h>
00046 #include <OSGBase.h>
00047 #include <OSGBaseTypes.h>
00048 #include <OSGBaseFunctions.h>
00049 
00050 #include <iostream>
00051 
00052 OSG_BEGIN_NAMESPACE
00053 
00054 template<class ValueTypeT, class StorageInterfaceT>
00055 class VectorInterface;
00056 
00057 template<class ValueTypeT, class StorageInterfaceT>
00058 class PointInterface;
00059 
00060 //---------------------------------------------------------------------------
00061 //  Class
00062 //---------------------------------------------------------------------------
00063 
00067 
00068 template <class ValueTypeT>
00069 class VecStorage2
00070 {
00071     /*==========================  PUBLIC  =================================*/
00072   public:
00073 
00074     static const UInt32 _iSize = 2;
00075 
00076     /*---------------------------------------------------------------------*/
00080     VecStorage2(void);
00081 
00083     /*---------------------------------------------------------------------*/
00087     ~VecStorage2(void);
00088 
00090     /*---------------------------------------------------------------------*/
00094     void       setValues        (const ValueTypeT  rVal1, 
00095                                  const ValueTypeT  rVal2);
00096 
00097     void       getSeparateValues(      ValueTypeT &rVal1, 
00098                                        ValueTypeT &rVal2) const;
00099 
00100     ValueTypeT x                (void                   ) const;
00101     ValueTypeT y                (void                   ) const;
00102 
00104     /*=========================  PROTECTED  ===============================*/
00105   protected:
00106 
00107     ValueTypeT _values[_iSize];
00108 
00109     /*==========================  PRIVATE  ================================*/
00110   private:
00111 
00113     VecStorage2(const VecStorage2 &source);
00115     void operator =(const VecStorage2 &source);
00116 };
00117 
00118 
00119 
00120 
00121 //---------------------------------------------------------------------------
00122 //  Class
00123 //---------------------------------------------------------------------------
00124 
00128 
00129 template <class ValueTypeT>
00130 class VecStorage3
00131 {
00132     /*==========================  PUBLIC  =================================*/
00133   public:
00134 
00135     static const UInt32 _iSize = 3;
00136 
00137     /*---------------------------------------------------------------------*/
00141     VecStorage3(void);
00142 
00144     /*---------------------------------------------------------------------*/
00148     ~VecStorage3(void);
00149 
00151     /*---------------------------------------------------------------------*/
00155     void       setValues        (const ValueTypeT  rVal1,
00156                                  const ValueTypeT  rVal2,
00157                                  const ValueTypeT  rVal3);
00158 
00159     void       getSeparateValues(      ValueTypeT &rVal1,
00160                                        ValueTypeT &rVal2,
00161                                        ValueTypeT &rVal3) const;
00162 
00163     ValueTypeT x                (void                   ) const;
00164     ValueTypeT y                (void                   ) const;
00165     ValueTypeT z                (void                   ) const;
00166 
00168     /*=========================  PROTECTED  ===============================*/
00169   protected:
00170 
00171     ValueTypeT _values[_iSize];
00172 
00173     /*==========================  PRIVATE  ================================*/
00174   private:
00175 
00177     VecStorage3(const VecStorage3 &source);
00179     void operator =(const VecStorage3 &source);
00180 };
00181 
00182 
00183 
00184 
00185 //---------------------------------------------------------------------------
00186 //  Class
00187 //---------------------------------------------------------------------------
00188 
00192 
00193 template <class ValueTypeT>
00194 class VecStorage4
00195 {
00196     /*==========================  PUBLIC  =================================*/
00197   public:
00198 
00199     static const UInt32 _iSize = 4;
00200 
00201     /*---------------------------------------------------------------------*/
00205     VecStorage4(void);
00206 
00208     /*---------------------------------------------------------------------*/
00212     ~VecStorage4(void);
00213 
00215     /*---------------------------------------------------------------------*/
00219     void       setValues        (const ValueTypeT  rVal1,
00220                                  const ValueTypeT  rVal2,
00221                                  const ValueTypeT  rVal3,
00222                                  const ValueTypeT  rVal4);
00223 
00224     void       getSeparateValues(      ValueTypeT &rVal1,
00225                                        ValueTypeT &rVal2,
00226                                        ValueTypeT &rVal3,
00227                                        ValueTypeT &rVal4) const;
00228 
00229     ValueTypeT x                (void                   ) const;
00230     ValueTypeT y                (void                   ) const;
00231     ValueTypeT z                (void                   ) const;
00232     ValueTypeT w                (void                   ) const;
00233 
00235     /*=========================  PROTECTED  ===============================*/
00236   protected:
00237 
00238     ValueTypeT _values[_iSize];
00239 
00240     /*==========================  PRIVATE  ================================*/
00241   private:
00242 
00244     VecStorage4(const VecStorage4 &source);
00246     void operator =(const VecStorage4 &source);
00247 };
00248 
00249 
00250 
00251 
00252 //---------------------------------------------------------------------------
00253 //  Class
00254 //---------------------------------------------------------------------------
00255 
00259 
00260 #ifdef __sgi
00261 #pragma set woff 1375
00262 #endif
00263 
00264 template<class ValueTypeT, class StorageInterfaceT>
00265 class PointInterface : public StorageInterfaceT
00266 {
00267     /*==========================  PUBLIC  =================================*/
00268   public:
00269 
00270     typedef          StorageInterfaceT                         Inherited;
00271 
00272     typedef typename TypeConstants<ValueTypeT>::RealReturnType RealReturnType;
00273 
00274     typedef          ValueTypeT                                ValueType;
00275 
00276     typedef          VectorInterface<ValueTypeT, 
00277                                      StorageInterfaceT>        VecInterface;
00278 
00279 
00280     typedef          PointInterface <ValueTypeT, 
00281                                      StorageInterfaceT>        Self;
00282 
00283     static  const    PointInterface                            Null;
00284 
00285     /*---------------------------------------------------------------------*/
00289     PointInterface(void);
00290 
00291     explicit PointInterface(const ValueTypeT *pVals);
00292     explicit PointInterface(      ValueTypeT *pVals);
00293 
00318 #ifdef __sgi
00319 #pragma set woff 1209
00320 #endif
00321 
00322     template<class VectorT> 
00323 #ifdef OSG_MICROSOFT_COMPILER_HACKS
00324              PointInterface(const VectorT &vec) : 
00325 #else
00326     explicit PointInterface(const VectorT &vec) : 
00327 #endif
00328         Inherited()
00329     {
00330         UInt32 i;
00331         
00332         if(Self::_iSize <= VectorT::_iSize)
00333         {
00334             for(i = 0; i < Self::_iSize; i++)
00335             {
00336                 Self::_values[i] = vec.getValues()[i];
00337             }
00338         }
00339         else
00340         {
00341             for(i = 0; i < VectorT::_iSize; i++)
00342             {
00343                 Self::_values[i] = vec.getValues()[i];
00344             }
00345             for(i = VectorT::_iSize; i < Self::_iSize; i++)
00346             {
00347                 Self::_values[i] = TypeConstants<ValueTypeT>::getZeroElement();
00348             }
00349         }
00350     }
00351 
00352 #ifdef __sgi
00353 #pragma reset woff 1209
00354 #endif
00355 
00356     PointInterface(const PointInterface &source);
00357 
00358 #if 0
00359     /* Constructor which takes one value, remaining entries will be zero;
00360        Actually this one collides with the template constuctor above,
00361        so I do not implement this one, any problems with that (GV)
00362     */
00363 
00364     PointInterface(const ValueTypeT rVal1);
00365 #endif
00366 
00367     /* Found so far no way to move these constructors to the memory
00368        interface :-(. I still find them a little bit wrong placed (GV)
00369     */
00370 
00371     PointInterface(const ValueTypeT rVal1, const ValueTypeT rVal2);
00372 
00373     PointInterface(const ValueTypeT rVal1, const ValueTypeT rVal2,
00374                    const ValueTypeT rVal3);
00375 
00376     PointInterface(const ValueTypeT rVal1, const ValueTypeT rVal2,
00377                    const ValueTypeT rVal3, const ValueTypeT rVal4);
00378 
00380     /*---------------------------------------------------------------------*/
00384     ~PointInterface(void);
00385 
00387     /*---------------------------------------------------------------------*/
00391     void setNull (void);
00392 
00393 #if 0
00394     /* set first values, remaining entries will not change;
00395        Actually this one collides with the template set function below,
00396        so I do not implement this one, any problems with that (GV)
00397     */
00398 
00399     void setValue(const ValueTypeT &rVal1);
00400 #endif
00401 
00402     void setValue(const PointInterface &vec);
00403 
00430 #ifdef __sgi
00431 #pragma set woff 1209
00432 #endif
00433 
00434     template<class VectorT>
00435     void setValue(const VectorT &vec)
00436     {
00437         UInt32 i;
00438         
00439         for(i = 0;
00440             i < (Self::_iSize < VectorT::_iSize ? 
00441                                               Self::_iSize : VectorT::_iSize);
00442             i++)
00443         {
00444             Self::_values[i] = vec.getValues()[i];
00445         }
00446     }
00447 
00448 #ifdef __sgi
00449 #pragma reset woff 1209
00450 #endif
00451 
00452     void setValue(const ValueTypeT *pVals);
00453 
00454     void setValue(const Char8      *szString);
00455     void setValue(      Char8      *szString);
00456 
00458     /*---------------------------------------------------------------------*/
00462           ValueTypeT *getValues(void);
00463     const ValueTypeT *getValues(void) const;
00464 
00466     /*---------------------------------------------------------------------*/
00470           bool            isZero  (void                           ) const;
00471 
00472           void            negate  (void                           );
00473 
00474           bool            equals  (const PointInterface &vec,
00475                                    const ValueTypeT      tolerance) const;
00476 
00477           VecInterface   &subZero (void                           );
00478     const VecInterface   &subZero (void                           ) const;
00479 
00480           RealReturnType  dist    (const PointInterface &vec      ) const;
00481           RealReturnType  dist2   (const PointInterface &vec      ) const;
00482 
00483           RealReturnType  maxValue(void                           ) const;
00484 
00486     /*---------------------------------------------------------------------*/
00490     void            operator *=(const ValueTypeT      val);
00491     void            operator /=(const ValueTypeT      val);
00492 
00493     VecInterface    operator - (const PointInterface &vec ) const;
00494 
00495     PointInterface  operator + (const VecInterface   &vec ) const;
00496 
00497     PointInterface  operator - (const VecInterface   &vec ) const;
00498 
00499     PointInterface  operator * (const ValueTypeT      rVal) const;
00500 
00501     PointInterface  operator - (void);
00502 
00503 
00504     void            operator +=(const VecInterface   &vec );
00505     void            operator -=(const VecInterface   &vec );
00506 
00508     /*---------------------------------------------------------------------*/
00512            ValueTypeT &operator[](const UInt32 uiVal);
00513     const  ValueTypeT &operator[](const UInt32 uiVal) const;
00514 
00515     /* Cast operator, cast VecBase to ValueTypeT *
00516        Bad Bad idea, in my opion to buggy to be used, use getValues
00517        Const cast operator, cast VecBase to ValueTypeT *
00518 
00519        operator       ValueTypeT *(void);
00520        operator const ValueTypeT *(void);
00521     */
00522 
00524     /*---------------------------------------------------------------------*/
00528     PointInterface &operator =(const PointInterface &source);
00529 
00531     /*---------------------------------------------------------------------*/
00535     bool operator <  (const PointInterface &other)  const;
00536 
00537     bool operator == (const PointInterface &other) const;
00538     bool operator != (const PointInterface &other) const;
00539 
00541     /*=========================  PROTECTED  ===============================*/
00542   protected:
00543 
00544     /*==========================  PRIVATE  ================================*/
00545   private:
00546 };
00547 
00548 
00549 template <class ValueTypeT,
00550           class StorageInterfaceT> inline
00551 PointInterface<ValueTypeT, StorageInterfaceT>
00552     operator *(const ValueTypeT                         val,
00553                const PointInterface<ValueTypeT,
00554                                     StorageInterfaceT> &vec);
00555 
00556 template <class ValueTypeT,
00557           class StorageInterfaceT> inline
00558 ostream &operator <<(ostream                                   &os,
00559                      const   PointInterface<ValueTypeT,
00560                                             StorageInterfaceT> &obj);
00561 
00562 //---------------------------------------------------------------------------
00563 //  Class
00564 //---------------------------------------------------------------------------
00565 
00569 
00570 template<class ValueTypeT, class StorageInterfaceT>
00571 class VectorInterface :
00572     public PointInterface<ValueTypeT, StorageInterfaceT>
00573 {
00574     /*==========================  PUBLIC  =================================*/
00575   public:
00576 
00577     typedef          PointInterface<ValueTypeT,
00578                                     StorageInterfaceT>         Inherited;
00579 
00580     typedef typename TypeConstants<ValueTypeT>::RealReturnType RealReturnType;
00581 
00582     typedef                        ValueTypeT                  ValueType;
00583 
00584     typedef          Inherited                                 PntInterface;
00585 
00586     typedef          VectorInterface<ValueTypeT, 
00587                                      StorageInterfaceT>        Self;
00588 
00589     static  const    VectorInterface                           Null;
00590 
00591     /*---------------------------------------------------------------------*/
00595     VectorInterface(void);
00596 
00597     explicit VectorInterface(const ValueTypeT *pVals);
00598     explicit VectorInterface(      ValueTypeT *pVals);
00599 
00624 #ifdef __sgi
00625 #pragma set woff 1209
00626 #endif
00627 
00628     template<class VectorT> 
00629 #ifdef OSG_MICROSOFT_COMPILER_HACKS
00630              VectorInterface(const VectorT &vec) : Inherited()
00631 #else
00632     explicit VectorInterface(const VectorT &vec) : Inherited()
00633 #endif
00634     {
00635         UInt32 i;
00636         
00637         if(Self::_iSize <= VectorT::_iSize)
00638         {
00639             for(i = 0; i < Self::_iSize; i++)
00640             {
00641                 Self::_values[i] = vec.getValues()[i];
00642             }
00643         }
00644         else
00645         {
00646             for(i = 0; i < VectorT::_iSize; i++)
00647             {
00648                 Self::_values[i] = vec.getValues()[i];
00649             }
00650             for(i = VectorT::_iSize; i < Self::_iSize; i++)
00651             {
00652                 Self::_values[i] = TypeConstants<ValueTypeT>::getZeroElement();
00653             }
00654         }
00655     }
00656 
00657 #ifdef __sgi
00658 #pragma reset woff 1209
00659 #endif
00660 
00661     VectorInterface(const VectorInterface &source);
00662 
00663 #if 0
00664     /* Constructor which takes one value, remaining entries will be zero;
00665        Actually this one collides with the template constuctor above,
00666        so I do not implement this one, any problems with that (GV)
00667     */
00668 
00669     VectorInterface(const ValueTypeT rVal1);
00670 #endif
00671 
00672     /* Found so far no way to move these constructors to the memory
00673        interface :-(. I still find them a little bit wrong placed (GV)
00674     */
00675 
00676     VectorInterface(const ValueTypeT rVal1, const ValueTypeT rVal2);
00677 
00678     VectorInterface(const ValueTypeT rVal1, const ValueTypeT rVal2,
00679                     const ValueTypeT rVal3);
00680 
00681     VectorInterface(const ValueTypeT rVal1, const ValueTypeT rVal2,
00682                     const ValueTypeT rVal3, const ValueTypeT rVal4);
00683 
00685     /*---------------------------------------------------------------------*/
00689     ~VectorInterface(void);
00690 
00692     /*---------------------------------------------------------------------*/
00696     RealReturnType  length       (void)                         const;
00697 
00698     void            normalize    (void);
00699 
00700     VectorInterface cross        (const VectorInterface &vec  ) const;
00701     VectorInterface operator %   (const VectorInterface &vec  ) const;
00702 
00703     void            crossThis    (const VectorInterface &vec  );
00704 
00705     ValueTypeT      dot          (const VectorInterface &vec  ) const;
00706     ValueTypeT      operator *   (const VectorInterface &vec  ) const;
00707     ValueTypeT      dot          (const PntInterface    &pnt  ) const;
00708     ValueTypeT      operator *   (const PntInterface    &pnt  ) const;
00709 
00710     RealReturnType  enclosedAngle(const VectorInterface &vec  ) const;
00711 
00712     RealReturnType  projectTo    (const VectorInterface &toVec);
00713 
00715     /*---------------------------------------------------------------------*/
00719     VectorInterface operator - (const VectorInterface &vec ) const;
00720     VectorInterface operator + (const VectorInterface &vec ) const;
00721 
00722     VectorInterface operator * (const ValueTypeT       rVal) const;
00723 
00724     VectorInterface operator - (void)                        const;
00725 
00727     /*---------------------------------------------------------------------*/
00731     VectorInterface &operator =(const VectorInterface &source);
00732 
00734     /*---------------------------------------------------------------------*/
00738     bool operator <  (const VectorInterface &other) const;
00739 
00740     bool operator == (const VectorInterface &other) const;
00741     bool operator != (const VectorInterface &other) const;
00742 
00744     /*=========================  PROTECTED  ===============================*/
00745   protected:
00746 
00747     /*==========================  PRIVATE  ================================*/
00748   private:
00749 };
00750 
00751 #ifdef __sgi
00752 #pragma reset woff 1375
00753 #endif
00754 
00755 template <class ValueTypeT,
00756           class StorageInterfaceT> inline
00757 VectorInterface<ValueTypeT, StorageInterfaceT>
00758     operator *(const ValueTypeT                          val,
00759                const VectorInterface<ValueTypeT,
00760                                      StorageInterfaceT> &vec);
00761 
00762 template <class ValueTypeT,
00763           class StorageInterfaceT> inline
00764 ostream &operator <<(        ostream                            &os,
00765                      const   VectorInterface<ValueTypeT,
00766                                              StorageInterfaceT> &obj);
00767 
00768 
00774 typedef VectorInterface<Real32, VecStorage2<Real32> > Vec2f;
00775 
00781 typedef Vec2f *Vec2fP;
00782 
00788 typedef VectorInterface<Real32, VecStorage3<Real32> > Vec3f;
00789 
00794 typedef Vec3f *Vec3fP;
00795 
00801 typedef VectorInterface<Real32, VecStorage4<Real32> > Vec4f;
00802 
00808 typedef Vec4f *Vec4fP;
00809 
00815 typedef VectorInterface<UInt16, VecStorage2<UInt16> > Vec2s;
00816 
00822 typedef Vec2s *Vec2sP;
00823 
00829 typedef VectorInterface<UInt8, VecStorage4<UInt8> > Vec4ub;
00830 
00835 typedef Vec4ub *Vec4ubP;
00836 
00837 
00843 typedef PointInterface<Real32, VecStorage2<Real32> > Pnt2f;
00844 
00850 typedef Pnt2f *Pnt2fP;
00851 
00852 
00858 typedef PointInterface<Real64, VecStorage2<Real64> > Pnt2d;
00859 
00865 typedef Pnt2d *Pnt2dP;
00866 
00867 
00873 typedef PointInterface<Real32, VecStorage3<Real32> > Pnt3f;
00874 
00880 typedef Pnt3f *Pnt3fP;
00881 
00887 typedef PointInterface<Real64, VecStorage3<Real64> > Pnt3d;
00888 
00894 typedef Pnt3d *Pnt3dP;
00895 
00901 typedef PointInterface<Real32, VecStorage4<Real32> > Pnt4f;
00902 
00908 typedef Pnt4f *Pnt4fP;
00909 
00915 typedef PointInterface<Real64, VecStorage4<Real64> > Pnt4d;
00916 
00922 typedef Pnt4d *Pnt4dP;
00923 
00929 typedef PointInterface<UInt16, VecStorage2<UInt16> > Pnt2s;
00930 
00936 typedef Vec2s *Vec2sP;
00937 
00938 OSG_END_NAMESPACE
00939 
00940 #include <OSGVector.inl>
00941 
00942 #define OSGVECTOR_HEADER_CVSID "@(#)$Id: $"
00943 
00944 #endif /* OSGVECTOR_H */
00945 
00946 

Generated on Wed Jul 3 14:39:46 2002 for OpenSG by doxygen1.2.16