## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgeom/transfrm.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
    00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_TRANSFORM_H__
    00021 #define __CS_TRANSFORM_H__
    00022
    00030 #include "csextern.h"
    00031
    00032
    00033 #include "csgeom/matrix3.h"
    00034 #include "csgeom/vector3.h"
    00035
    00036 class csPlane3;
    00037 class csSphere;
    00038
    00039 class csReversibleTransform;
    00040
    00047 class CS_CRYSTALSPACE_EXPORT csTransform
    00048 {
    00049 protected:
    00051   csMatrix3 m_o2t;
    00053   csVector3 v_o2t;
    00054
    00055 public:
    00056   // Needed for GCC4. Otherwise emits a flood of "virtual functions but
    00057   // non-virtual destructor" warnings.
    00058   virtual ~csTransform() {}
    00062   csTransform () : m_o2t (), v_o2t (0, 0, 0) {}
    00063
    00071   csTransform (const csMatrix3& other2this, const csVector3& origin_pos) :
    00072         m_o2t (other2this), v_o2t (origin_pos) {}
    00073
    00077   inline void Identity ()
    00078   {
    00079     SetO2TTranslation (csVector3 (0));
    00080     SetO2T (csMatrix3 ());
    00081   }
    00082
    00087   inline bool IsIdentity () const
    00088   {
    00089     if (ABS (v_o2t.x) >= SMALL_EPSILON) return false;
    00090     if (ABS (v_o2t.y) >= SMALL_EPSILON) return false;
    00091     if (ABS (v_o2t.z) >= SMALL_EPSILON) return false;
    00092     if (ABS (m_o2t.m11-1) >= SMALL_EPSILON) return false;
    00093     if (ABS (m_o2t.m12) >= SMALL_EPSILON) return false;
    00094     if (ABS (m_o2t.m13) >= SMALL_EPSILON) return false;
    00095     if (ABS (m_o2t.m21) >= SMALL_EPSILON) return false;
    00096     if (ABS (m_o2t.m22-1) >= SMALL_EPSILON) return false;
    00097     if (ABS (m_o2t.m23) >= SMALL_EPSILON) return false;
    00098     if (ABS (m_o2t.m31) >= SMALL_EPSILON) return false;
    00099     if (ABS (m_o2t.m32) >= SMALL_EPSILON) return false;
    00100     if (ABS (m_o2t.m33-1) >= SMALL_EPSILON) return false;
    00101     return true;
    00102   }
    00103
    00108   inline const csMatrix3& GetO2T () const { return m_o2t; }
    00109
    00115   inline const csVector3& GetO2TTranslation () const { return v_o2t; }
    00116
    00122   inline const csVector3& GetOrigin () const { return v_o2t; }
    00123
    00128   virtual void SetO2T (const csMatrix3& m) { m_o2t = m; }
    00129
    00135   virtual void SetO2TTranslation (const csVector3& v) { v_o2t = v; }
    00136
    00141   inline void SetOrigin (const csVector3& v) { SetO2TTranslation (v); }
    00142
    00148   inline void Translate (const csVector3& v) { SetO2TTranslation (v_o2t + v); }
    00149
    00155   inline csVector3 Other2This (const csVector3& v) const
    00156   {
    00157     return m_o2t * (v - v_o2t);
    00158   }
    00159
    00165   inline csVector3 Other2ThisRelative (const csVector3& v) const
    00166   { return m_o2t * v; }
    00167
    00173   csPlane3 Other2This (const csPlane3& p) const;
    00174
    00181   csPlane3 Other2ThisRelative (const csPlane3& p) const;
    00182
    00190   void Other2This (const csPlane3& p, const csVector3& point,
    00191         csPlane3& result) const;
    00192
    00196   csSphere Other2This (const csSphere& s) const;
    00197
    00202   friend CS_CRYSTALSPACE_EXPORT csVector3 operator* (const csVector3& v,
    00203     const csTransform& t);
    00204
    00209   friend CS_CRYSTALSPACE_EXPORT csVector3 operator* (const csTransform& t,
    00210     const csVector3& v);
    00211
    00216   friend CS_CRYSTALSPACE_EXPORT csVector3& operator*= (csVector3& v,
    00217     const csTransform& t);
    00218
    00223   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator* (const csPlane3& p,
    00224     const csTransform& t);
    00225
    00230   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator* (const csTransform& t,
    00231     const csPlane3& p);
    00232
    00237   friend CS_CRYSTALSPACE_EXPORT csPlane3& operator*= (csPlane3& p,
    00238     const csTransform& t);
    00239
    00244   friend CS_CRYSTALSPACE_EXPORT csSphere operator* (const csSphere& p,
    00245     const csTransform& t);
    00246
    00251   friend CS_CRYSTALSPACE_EXPORT csSphere operator* (const csTransform& t,
    00252     const csSphere& p);
    00253
    00258   friend CS_CRYSTALSPACE_EXPORT csSphere& operator*= (csSphere& p,
    00259     const csTransform& t);
    00260
    00265   friend CS_CRYSTALSPACE_EXPORT csMatrix3 operator* (const csMatrix3& m,
    00266     const csTransform& t);
    00267
    00272   friend CS_CRYSTALSPACE_EXPORT csMatrix3 operator* (const csTransform& t,
    00273     const csMatrix3& m);
    00274
    00279   friend CS_CRYSTALSPACE_EXPORT csMatrix3& operator*= (csMatrix3& m,
    00280     const csTransform& t);
    00281
    00292   friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1,
    00293                               const csReversibleTransform& t2);
    00294
    00300   static csTransform GetReflect (const csPlane3& pl);
    00301
    00307   csVector3 GetFront () { return csVector3 (m_o2t.m31, m_o2t.m32, m_o2t.m33); }
    00308
    00314   csVector3 GetUp () { return csVector3 (m_o2t.m21, m_o2t.m22, m_o2t.m23); }
    00315
    00321   csVector3 GetRight () { return csVector3 (m_o2t.m11, m_o2t.m12, m_o2t.m13); }
    00322 };
    00323
    00335 class CS_CRYSTALSPACE_EXPORT csReversibleTransform : public csTransform
    00336 {
    00337 protected:
    00339   csMatrix3 m_t2o;
    00340
    00344   csReversibleTransform (const csMatrix3& o2t, const csMatrix3& t2o,
    00345     const csVector3& pos) : csTransform (o2t,pos), m_t2o (t2o) {}
    00346
    00347 public:
    00351   csReversibleTransform () : csTransform (), m_t2o () {}
    00352
    00360   csReversibleTransform (const csMatrix3& o2t, const csVector3& pos) :
    00361     csTransform (o2t,pos) { m_t2o = m_o2t.GetInverse (); }
    00362
    00366   csReversibleTransform (const csTransform& t) :
    00367     csTransform (t) { m_t2o = m_o2t.GetInverse (); }
    00368
    00372   csReversibleTransform (const csReversibleTransform& t) :
    00373     csTransform (t) { m_t2o = t.m_t2o; }
    00374
    00379   inline const csMatrix3& GetT2O () const { return m_t2o; }
    00380
    00385   inline csVector3 GetT2OTranslation () const { return -m_o2t*v_o2t; }
    00386
    00390   csReversibleTransform GetInverse () const
    00391   { return csReversibleTransform (m_t2o, m_o2t, -m_o2t*v_o2t); }
    00392
    00397   virtual void SetO2T (const csMatrix3& m)
    00398   { m_o2t = m;  m_t2o = m_o2t.GetInverse (); }
    00399
    00405   virtual void SetT2O (const csMatrix3& m)
    00406   { m_t2o = m;  m_o2t = m_t2o.GetInverse (); }
    00407
    00413   inline csVector3 This2Other (const csVector3& v) const
    00414   { return v_o2t + m_t2o * v; }
    00415
    00421   inline csVector3 This2OtherRelative (const csVector3& v) const
    00422   { return m_t2o * v; }
    00423
    00430   csPlane3 This2Other (const csPlane3& p) const;
    00431
    00438   csPlane3 This2OtherRelative (const csPlane3& p) const;
    00439
    00448   void This2Other (const csPlane3& p, const csVector3& point,
    00449         csPlane3& result) const;
    00450
    00454   csSphere This2Other (const csSphere& s) const;
    00455
    00461   void RotateOther (const csVector3& v, float angle);
    00462
    00468   void RotateThis (const csVector3& v, float angle);
    00469
    00477   void RotateOther (const csMatrix3& m) { SetT2O (m * m_t2o); }
    00478
    00486   void RotateThis (const csMatrix3& m) { SetT2O (m_t2o * m); }
    00487
    00496   void LookAt (const csVector3& v, const csVector3& up);
    00497
    00502   friend CS_CRYSTALSPACE_EXPORT csVector3 operator/ (const csVector3& v,
    00503         const csReversibleTransform& t);
    00504
    00509   friend CS_CRYSTALSPACE_EXPORT csVector3& operator/= (csVector3& v,
    00510     const csReversibleTransform& t);
    00511
    00516   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator/ (const csPlane3& p,
    00517     const csReversibleTransform& t);
    00518
    00523   friend CS_CRYSTALSPACE_EXPORT csPlane3& operator/= (csPlane3& p,
    00524     const csReversibleTransform& t);
    00525
    00530   friend CS_CRYSTALSPACE_EXPORT csSphere operator/ (const csSphere& p,
    00531     const csReversibleTransform& t);
    00532
    00544   friend csReversibleTransform& operator*= (csReversibleTransform& t1,
    00545                                           const csReversibleTransform& t2)
    00546   {
    00547     t1.v_o2t = t2.m_t2o*t1.v_o2t;
    00548     t1.v_o2t += t2.v_o2t;
    00549     t1.m_o2t *= t2.m_o2t;
    00550     t1.m_t2o *= t1.m_t2o;
    00551     return t1;
    00552   }
    00553
    00565   friend csReversibleTransform operator* (const csReversibleTransform& t1,
    00566                                         const csReversibleTransform& t2)
    00567   {
    00568     return csReversibleTransform (t1.m_o2t*t2.m_o2t, t2.m_t2o*t1.m_t2o,
    00569                              t2.v_o2t + t2.m_t2o*t1.v_o2t);
    00570   }
    00571
    00572 #if !defined(SWIG) /* Otherwise Swig 1.3.22 thinks this is multiply declared */
    00573
    00584   friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1,
    00585                               const csReversibleTransform& t2);
    00586 #endif
    00587
    00599   friend CS_CRYSTALSPACE_EXPORT csReversibleTransform& operator/= (
    00600     csReversibleTransform& t1, const csReversibleTransform& t2);
    00601
    00613   friend CS_CRYSTALSPACE_EXPORT csReversibleTransform operator/ (
    00614     const csReversibleTransform& t1, const csReversibleTransform& t2);
    00615 };
    00616
    00623 class csOrthoTransform : public csReversibleTransform
    00624 {
    00625 public:
    00629   csOrthoTransform () : csReversibleTransform () {}
    00630
    00634   csOrthoTransform (const csMatrix3& o2t, const csVector3& pos) :
    00635     csReversibleTransform (o2t, o2t.GetTranspose (), pos) { }
    00636
    00640   csOrthoTransform (const csTransform& t) :
    00641     csReversibleTransform (t.GetO2T (), t.GetO2T ().GetTranspose (),
    00642         t.GetO2TTranslation ())
    00643   { }
    00644
    00649   virtual void SetO2T (const csMatrix3& m)
    00650   { m_o2t = m;  m_t2o = m_o2t.GetTranspose (); }
    00651
    00657   virtual void SetT2O (const csMatrix3& m)
    00658   { m_t2o = m;  m_o2t = m_t2o.GetTranspose (); }
    00659 };
    00660
    00663 #endif // __CS_TRANSFORM_H__
    00664