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

| ## Public API Reference

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

- 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 class csBox3;
    00039
    00040 class csReversibleTransform;
    00041
    00048 class CS_CRYSTALSPACE_EXPORT csTransform
    00049 {
    00050 protected:
    00052   csMatrix3 m_o2t;
    00054   csVector3 v_o2t;
    00055
    00056 public:
    00057   // Needed for GCC4. Otherwise emits a flood of "virtual functions but
    00058   // non-virtual destructor" warnings.
    00059   virtual ~csTransform() {}
    00063   csTransform () : m_o2t (), v_o2t (0, 0, 0) {}
    00064
    00072   csTransform (const csMatrix3& other2this, const csVector3& origin_pos) :
    00073         m_o2t (other2this), v_o2t (origin_pos) {}
    00074
    00076   csString Description() const;
    00077
    00081   inline void Identity ()
    00082   {
    00083     SetO2TTranslation (csVector3 (0));
    00084     SetO2T (csMatrix3 ());
    00085   }
    00086
    00091   inline bool IsIdentity () const
    00092   {
    00093     if (ABS (v_o2t.x) >= SMALL_EPSILON) return false;
    00094     if (ABS (v_o2t.y) >= SMALL_EPSILON) return false;
    00095     if (ABS (v_o2t.z) >= SMALL_EPSILON) return false;
    00096     if (ABS (m_o2t.m11-1) >= SMALL_EPSILON) return false;
    00097     if (ABS (m_o2t.m12) >= SMALL_EPSILON) return false;
    00098     if (ABS (m_o2t.m13) >= SMALL_EPSILON) return false;
    00099     if (ABS (m_o2t.m21) >= SMALL_EPSILON) return false;
    00100     if (ABS (m_o2t.m22-1) >= SMALL_EPSILON) return false;
    00101     if (ABS (m_o2t.m23) >= SMALL_EPSILON) return false;
    00102     if (ABS (m_o2t.m31) >= SMALL_EPSILON) return false;
    00103     if (ABS (m_o2t.m32) >= SMALL_EPSILON) return false;
    00104     if (ABS (m_o2t.m33-1) >= SMALL_EPSILON) return false;
    00105     return true;
    00106   }
    00107
    00112   inline const csMatrix3& GetO2T () const { return m_o2t; }
    00113
    00119   inline const csVector3& GetO2TTranslation () const { return v_o2t; }
    00120
    00126   inline const csVector3& GetOrigin () const { return v_o2t; }
    00127
    00132   virtual void SetO2T (const csMatrix3& m) { m_o2t = m; }
    00133
    00139   virtual void SetO2TTranslation (const csVector3& v) { v_o2t = v; }
    00140
    00145   inline void SetOrigin (const csVector3& v) { SetO2TTranslation (v); }
    00146
    00152   inline void Translate (const csVector3& v) { SetO2TTranslation (v_o2t + v); }
    00153
    00159   inline csVector3 Other2This (const csVector3& v) const
    00160   {
    00161     return m_o2t * (v - v_o2t);
    00162   }
    00163
    00169   inline csVector3 Other2ThisRelative (const csVector3& v) const
    00170   { return m_o2t * v; }
    00171
    00177   csPlane3 Other2This (const csPlane3& p) const;
    00178
    00185   csPlane3 Other2ThisRelative (const csPlane3& p) const;
    00186
    00194   void Other2This (const csPlane3& p, const csVector3& point,
    00195         csPlane3& result) const;
    00196
    00200   csSphere Other2This (const csSphere& s) const;
    00201
    00205   csBox3 Other2This (const csBox3& box) const;
    00206
    00211   friend CS_CRYSTALSPACE_EXPORT csVector3 operator* (const csVector3& v,
    00212     const csTransform& t);
    00213
    00218   friend CS_CRYSTALSPACE_EXPORT csVector3 operator* (const csTransform& t,
    00219     const csVector3& v);
    00220
    00225   friend CS_CRYSTALSPACE_EXPORT csVector3& operator*= (csVector3& v,
    00226     const csTransform& t);
    00227
    00232   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator* (const csPlane3& p,
    00233     const csTransform& t);
    00234
    00239   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator* (const csTransform& t,
    00240     const csPlane3& p);
    00241
    00246   friend CS_CRYSTALSPACE_EXPORT csPlane3& operator*= (csPlane3& p,
    00247     const csTransform& t);
    00248
    00253   friend CS_CRYSTALSPACE_EXPORT csSphere operator* (const csSphere& p,
    00254     const csTransform& t);
    00255
    00260   friend CS_CRYSTALSPACE_EXPORT csSphere operator* (const csTransform& t,
    00261     const csSphere& p);
    00262
    00267   friend CS_CRYSTALSPACE_EXPORT csSphere& operator*= (csSphere& p,
    00268     const csTransform& t);
    00269
    00274   friend CS_CRYSTALSPACE_EXPORT csBox3 operator* (const csBox3& p,
    00275     const csTransform& t);
    00276
    00281   friend CS_CRYSTALSPACE_EXPORT csBox3 operator* (const csTransform& t,
    00282     const csBox3& p);
    00283
    00288   friend CS_CRYSTALSPACE_EXPORT csBox3& operator*= (csBox3& p,
    00289     const csTransform& t);
    00290
    00295   friend CS_CRYSTALSPACE_EXPORT csMatrix3 operator* (const csMatrix3& m,
    00296     const csTransform& t);
    00297
    00302   friend CS_CRYSTALSPACE_EXPORT csMatrix3 operator* (const csTransform& t,
    00303     const csMatrix3& m);
    00304
    00309   friend CS_CRYSTALSPACE_EXPORT csMatrix3& operator*= (csMatrix3& m,
    00310     const csTransform& t);
    00311
    00322   friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1,
    00323                               const csReversibleTransform& t2);
    00324
    00330   static csTransform GetReflect (const csPlane3& pl);
    00331
    00337   csVector3 GetFront () const
    00338   {
    00339     return csVector3 (m_o2t.m31, m_o2t.m32, m_o2t.m33);
    00340   }
    00341   void SetFront (const csVector3& v)
    00342   {
    00343     m_o2t.m31 = v.x;
    00344     m_o2t.m32 = v.y;
    00345     m_o2t.m33 = v.z;
    00346   }
    00347
    00353   csVector3 GetUp () const
    00354   {
    00355     return csVector3 (m_o2t.m21, m_o2t.m22, m_o2t.m23);
    00356   }
    00357   void SetUp (const csVector3& v)
    00358   {
    00359     m_o2t.m21 = v.x;
    00360     m_o2t.m22 = v.y;
    00361     m_o2t.m23 = v.z;
    00362   }
    00363
    00369   csVector3 GetRight () const
    00370   {
    00371     return csVector3 (m_o2t.m11, m_o2t.m12, m_o2t.m13);
    00372   }
    00373   void SetRight (const csVector3& v)
    00374   {
    00375     m_o2t.m11 = v.x;
    00376     m_o2t.m12 = v.y;
    00377     m_o2t.m13 = v.z;
    00378   }
    00379 };
    00380
    00392 class CS_CRYSTALSPACE_EXPORT csReversibleTransform : public csTransform
    00393 {
    00394 protected:
    00396   csMatrix3 m_t2o;
    00397
    00401   csReversibleTransform (const csMatrix3& o2t, const csMatrix3& t2o,
    00402     const csVector3& pos) : csTransform (o2t,pos), m_t2o (t2o) {}
    00403
    00404 private:
    00405   bool LookAtGeneric (const csVector3 &v, const csVector3 &upNeg,
    00406       csVector3& w1, csVector3& w2, csVector3& w3);
    00407
    00408 public:
    00412   csReversibleTransform () : csTransform (), m_t2o () {}
    00413
    00421   csReversibleTransform (const csMatrix3& o2t, const csVector3& pos) :
    00422     csTransform (o2t,pos) { m_t2o = m_o2t.GetInverse (); }
    00423
    00427   csReversibleTransform (const csTransform& t) :
    00428     csTransform (t) { m_t2o = m_o2t.GetInverse (); }
    00429
    00433   csReversibleTransform (const csReversibleTransform& t) :
    00434     csTransform (t) { m_t2o = t.m_t2o; }
    00435
    00440   inline const csMatrix3& GetT2O () const { return m_t2o; }
    00441
    00446   inline csVector3 GetT2OTranslation () const { return -m_o2t*v_o2t; }
    00447
    00451   csReversibleTransform GetInverse () const
    00452   { return csReversibleTransform (m_t2o, m_o2t, -m_o2t*v_o2t); }
    00453
    00458   virtual void SetO2T (const csMatrix3& m)
    00459   { m_o2t = m;  m_t2o = m_o2t.GetInverse (); }
    00460
    00466   virtual void SetT2O (const csMatrix3& m)
    00467   { m_t2o = m;  m_o2t = m_t2o.GetInverse (); }
    00468
    00474   inline csVector3 This2Other (const csVector3& v) const
    00475   { return v_o2t + m_t2o * v; }
    00476
    00482   inline csVector3 This2OtherRelative (const csVector3& v) const
    00483   { return m_t2o * v; }
    00484
    00491   csPlane3 This2Other (const csPlane3& p) const;
    00492
    00499   csPlane3 This2OtherRelative (const csPlane3& p) const;
    00500
    00509   void This2Other (const csPlane3& p, const csVector3& point,
    00510         csPlane3& result) const;
    00511
    00515   csSphere This2Other (const csSphere& s) const;
    00516
    00520   csBox3 This2Other (const csBox3& box) const;
    00521
    00527   void RotateOther (const csVector3& v, float angle);
    00528
    00534   void RotateThis (const csVector3& v, float angle);
    00535
    00543   void RotateOther (const csMatrix3& m) { SetT2O (m * m_t2o); }
    00544
    00552   void RotateThis (const csMatrix3& m) { SetT2O (m_t2o * m); }
    00553
    00568   bool LookAt (const csVector3& v, const csVector3& up);
    00569
    00575   bool LookAtZUpY (const csVector3& v, const csVector3& up);
    00581   bool LookAtZUpX (const csVector3& v, const csVector3& up);
    00587   bool LookAtYUpZ (const csVector3& v, const csVector3& up);
    00593   bool LookAtYUpX (const csVector3& v, const csVector3& up);
    00599   bool LookAtXUpZ (const csVector3& v, const csVector3& up);
    00605   bool LookAtXUpY (const csVector3& v, const csVector3& up);
    00606
    00611   friend CS_CRYSTALSPACE_EXPORT csVector3 operator/ (const csVector3& v,
    00612         const csReversibleTransform& t);
    00613
    00618   friend CS_CRYSTALSPACE_EXPORT csVector3& operator/= (csVector3& v,
    00619     const csReversibleTransform& t);
    00620
    00625   friend CS_CRYSTALSPACE_EXPORT csPlane3 operator/ (const csPlane3& p,
    00626     const csReversibleTransform& t);
    00627
    00632   friend CS_CRYSTALSPACE_EXPORT csPlane3& operator/= (csPlane3& p,
    00633     const csReversibleTransform& t);
    00634
    00639   friend CS_CRYSTALSPACE_EXPORT csSphere operator/ (const csSphere& p,
    00640     const csReversibleTransform& t);
    00641
    00646   friend CS_CRYSTALSPACE_EXPORT csBox3 operator/ (const csBox3& p,
    00647     const csReversibleTransform& t);
    00648
    00660   friend csReversibleTransform& operator*= (csReversibleTransform& t1,
    00661                                           const csReversibleTransform& t2)
    00662   {
    00663     t1.v_o2t = t2.m_t2o*t1.v_o2t;
    00664     t1.v_o2t += t2.v_o2t;
    00665     t1.m_o2t *= t2.m_o2t;
    00666     t1.m_t2o = t2.m_t2o * t1.m_t2o;
    00667     return t1;
    00668   }
    00669
    00681   friend csReversibleTransform operator* (const csReversibleTransform& t1,
    00682                                         const csReversibleTransform& t2)
    00683   {
    00684     return csReversibleTransform (t1.m_o2t*t2.m_o2t, t2.m_t2o*t1.m_t2o,
    00685                              t2.v_o2t + t2.m_t2o*t1.v_o2t);
    00686   }
    00687
    00688 #if !defined(SWIG) /* Otherwise Swig 1.3.22 thinks this is multiply declared */
    00689
    00700   friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1,
    00701                               const csReversibleTransform& t2);
    00702 #endif
    00703
    00715   friend CS_CRYSTALSPACE_EXPORT csReversibleTransform& operator/= (
    00716     csReversibleTransform& t1, const csReversibleTransform& t2);
    00717
    00729   friend CS_CRYSTALSPACE_EXPORT csReversibleTransform operator/ (
    00730     const csReversibleTransform& t1, const csReversibleTransform& t2);
    00731 };
    00732
    00739 class csOrthoTransform : public csReversibleTransform
    00740 {
    00741 public:
    00745   csOrthoTransform () : csReversibleTransform () {}
    00746
    00750   csOrthoTransform (const csMatrix3& o2t, const csVector3& pos) :
    00751     csReversibleTransform (o2t, o2t.GetTranspose (), pos) { }
    00752
    00756   csOrthoTransform (const csTransform& t) :
    00757     csReversibleTransform (t.GetO2T (), t.GetO2T ().GetTranspose (),
    00758         t.GetO2TTranslation ())
    00759   { }
    00760
    00765   virtual void SetO2T (const csMatrix3& m)
    00766   { m_o2t = m;  m_t2o = m_o2t.GetTranspose (); }
    00767
    00773   virtual void SetT2O (const csMatrix3& m)
    00774   { m_t2o = m;  m_o2t = m_t2o.GetTranspose (); }
    00775 };
    00776
    00779 #endif // __CS_TRANSFORM_H__
    00780