Crystal Space: csgeom/transfrm.h Source File (Crystal Space Public API Reference)

| ## 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 () const
    00308   {
    00309     return csVector3 (m_o2t.m31, m_o2t.m32, m_o2t.m33);
    00310   }
    00311
    00317   csVector3 GetUp () const
    00318   {
    00319     return csVector3 (m_o2t.m21, m_o2t.m22, m_o2t.m23);
    00320   }
    00321
    00327   csVector3 GetRight () const
    00328   {
    00329     return csVector3 (m_o2t.m11, m_o2t.m12, m_o2t.m13);
    00330   }
    00331 };
    00332
    00344 class CS_CRYSTALSPACE_EXPORT csReversibleTransform : public csTransform
    00345 {
    00346 protected:
    00348   csMatrix3 m_t2o;
    00349
    00353   csReversibleTransform (const csMatrix3& o2t, const csMatrix3& t2o,
    00354     const csVector3& pos) : csTransform (o2t,pos), m_t2o (t2o) {}
    00355
    00356 public:
    00360   csReversibleTransform () : csTransform (), m_t2o () {}
    00361
    00369   csReversibleTransform (const csMatrix3& o2t, const csVector3& pos) :
    00370     csTransform (o2t,pos) { m_t2o = m_o2t.GetInverse (); }
    00371
    00375   csReversibleTransform (const csTransform& t) :
    00376     csTransform (t) { m_t2o = m_o2t.GetInverse (); }
    00377
    00381   csReversibleTransform (const csReversibleTransform& t) :
    00382     csTransform (t) { m_t2o = t.m_t2o; }
    00383
    00388   inline const csMatrix3& GetT2O () const { return m_t2o; }
    00389
    00394   inline csVector3 GetT2OTranslation () const { return -m_o2t*v_o2t; }
    00395
    00399   csReversibleTransform GetInverse () const
    00400   { return csReversibleTransform (m_t2o, m_o2t, -m_o2t*v_o2t); }
    00401
    00406   virtual void SetO2T (const csMatrix3& m)
    00407   { m_o2t = m;  m_t2o = m_o2t.GetInverse (); }
    00408
    00414   virtual void SetT2O (const csMatrix3& m)
    00415   { m_t2o = m;  m_o2t = m_t2o.GetInverse (); }
    00416
    00422   inline csVector3 This2Other (const csVector3& v) const
    00423   { return v_o2t + m_t2o * v; }
    00424
    00430   inline csVector3 This2OtherRelative (const csVector3& v) const
    00431   { return m_t2o * v; }
    00432
    00439   csPlane3 This2Other (const csPlane3& p) const;
    00440
    00447   csPlane3 This2OtherRelative (const csPlane3& p) const;
    00448
    00457   void This2Other (const csPlane3& p, const csVector3& point,
    00458         csPlane3& result) const;
    00459
    00463   csSphere This2Other (const csSphere& s) const;
    00464
    00470   void RotateOther (const csVector3& v, float angle);
    00471
    00477   void RotateThis (const csVector3& v, float angle);
    00478
    00486   void RotateOther (const csMatrix3& m) { SetT2O (m * m_t2o); }
    00487
    00495   void RotateThis (const csMatrix3& m) { SetT2O (m_t2o * m); }
    00496
    00505   void LookAt (const csVector3& v, const csVector3& up);
    00506
    00511   friend CS_CRYSTALSPACE_EXPORT csVector3 operator/ (const csVector3& v,
    00512         const csReversibleTransform& t);
    00513
    00518   friend CS_CRYSTALSPACE_EXPORT csVector3& operator/= (csVector3& v,
    00519     const csReversibleTransform& t);
    00520
    00525   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator/ (const csPlane3& p,
    00526     const csReversibleTransform& t);
    00527
    00532   friend CS_CRYSTALSPACE_EXPORT csPlane3& operator/= (csPlane3& p,
    00533     const csReversibleTransform& t);
    00534
    00539   friend CS_CRYSTALSPACE_EXPORT csSphere operator/ (const csSphere& p,
    00540     const csReversibleTransform& t);
    00541
    00553   friend csReversibleTransform& operator*= (csReversibleTransform& t1,
    00554                                           const csReversibleTransform& t2)
    00555   {
    00556     t1.v_o2t = t2.m_t2o*t1.v_o2t;
    00557     t1.v_o2t += t2.v_o2t;
    00558     t1.m_o2t *= t2.m_o2t;
    00559     t1.m_t2o *= t1.m_t2o;
    00560     return t1;
    00561   }
    00562
    00574   friend csReversibleTransform operator* (const csReversibleTransform& t1,
    00575                                         const csReversibleTransform& t2)
    00576   {
    00577     return csReversibleTransform (t1.m_o2t*t2.m_o2t, t2.m_t2o*t1.m_t2o,
    00578                              t2.v_o2t + t2.m_t2o*t1.v_o2t);
    00579   }
    00580
    00581 #if !defined(SWIG) /* Otherwise Swig 1.3.22 thinks this is multiply declared */
    00582
    00593   friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1,
    00594                               const csReversibleTransform& t2);
    00595 #endif
    00596
    00608   friend CS_CRYSTALSPACE_EXPORT csReversibleTransform& operator/= (
    00609     csReversibleTransform& t1, const csReversibleTransform& t2);
    00610
    00622   friend CS_CRYSTALSPACE_EXPORT csReversibleTransform operator/ (
    00623     const csReversibleTransform& t1, const csReversibleTransform& t2);
    00624 };
    00625
    00632 class csOrthoTransform : public csReversibleTransform
    00633 {
    00634 public:
    00638   csOrthoTransform () : csReversibleTransform () {}
    00639
    00643   csOrthoTransform (const csMatrix3& o2t, const csVector3& pos) :
    00644     csReversibleTransform (o2t, o2t.GetTranspose (), pos) { }
    00645
    00649   csOrthoTransform (const csTransform& t) :
    00650     csReversibleTransform (t.GetO2T (), t.GetO2T ().GetTranspose (),
    00651         t.GetO2TTranslation ())
    00652   { }
    00653
    00658   virtual void SetO2T (const csMatrix3& m)
    00659   { m_o2t = m;  m_t2o = m_o2t.GetTranspose (); }
    00660
    00666   virtual void SetT2O (const csMatrix3& m)
    00667   { m_t2o = m;  m_o2t = m_t2o.GetTranspose (); }
    00668 };
    00669
    00672 #endif // __CS_TRANSFORM_H__
    00673