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 00075 csString Description() const; 00076 00080 inline void Identity () 00081 { 00082 SetO2TTranslation (csVector3 (0)); 00083 SetO2T (csMatrix3 ()); 00084 } 00085 00090 inline bool IsIdentity () const 00091 { 00092 if (ABS (v_o2t.x) >= SMALL_EPSILON) return false; 00093 if (ABS (v_o2t.y) >= SMALL_EPSILON) return false; 00094 if (ABS (v_o2t.z) >= SMALL_EPSILON) return false; 00095 if (ABS (m_o2t.m11-1) >= SMALL_EPSILON) return false; 00096 if (ABS (m_o2t.m12) >= SMALL_EPSILON) return false; 00097 if (ABS (m_o2t.m13) >= SMALL_EPSILON) return false; 00098 if (ABS (m_o2t.m21) >= SMALL_EPSILON) return false; 00099 if (ABS (m_o2t.m22-1) >= SMALL_EPSILON) return false; 00100 if (ABS (m_o2t.m23) >= SMALL_EPSILON) return false; 00101 if (ABS (m_o2t.m31) >= SMALL_EPSILON) return false; 00102 if (ABS (m_o2t.m32) >= SMALL_EPSILON) return false; 00103 if (ABS (m_o2t.m33-1) >= SMALL_EPSILON) return false; 00104 return true; 00105 } 00106 00111 inline const csMatrix3& GetO2T () const { return m_o2t; } 00112 00118 inline const csVector3& GetO2TTranslation () const { return v_o2t; } 00119 00125 inline const csVector3& GetOrigin () const { return v_o2t; } 00126 00131 virtual void SetO2T (const csMatrix3& m) { m_o2t = m; } 00132 00138 virtual void SetO2TTranslation (const csVector3& v) { v_o2t = v; } 00139 00144 inline void SetOrigin (const csVector3& v) { SetO2TTranslation (v); } 00145 00151 inline void Translate (const csVector3& v) { SetO2TTranslation (v_o2t + v); } 00152 00158 inline csVector3 Other2This (const csVector3& v) const 00159 { 00160 return m_o2t * (v - v_o2t); 00161 } 00162 00168 inline csVector3 Other2ThisRelative (const csVector3& v) const 00169 { return m_o2t * v; } 00170 00176 csPlane3 Other2This (const csPlane3& p) const; 00177 00184 csPlane3 Other2ThisRelative (const csPlane3& p) const; 00185 00193 void Other2This (const csPlane3& p, const csVector3& point, 00194 csPlane3& result) const; 00195 00199 csSphere Other2This (const csSphere& s) const; 00200 00205 friend CS_CRYSTALSPACE_EXPORT csVector3 operator* (const csVector3& v, 00206 const csTransform& t); 00207 00212 friend CS_CRYSTALSPACE_EXPORT csVector3 operator* (const csTransform& t, 00213 const csVector3& v); 00214 00219 friend CS_CRYSTALSPACE_EXPORT csVector3& operator*= (csVector3& v, 00220 const csTransform& t); 00221 00226 friend CS_CRYSTALSPACE_EXPORT csPlane3 operator* (const csPlane3& p, 00227 const csTransform& t); 00228 00233 friend CS_CRYSTALSPACE_EXPORT csPlane3 operator* (const csTransform& t, 00234 const csPlane3& p); 00235 00240 friend CS_CRYSTALSPACE_EXPORT csPlane3& operator*= (csPlane3& p, 00241 const csTransform& t); 00242 00247 friend CS_CRYSTALSPACE_EXPORT csSphere operator* (const csSphere& p, 00248 const csTransform& t); 00249 00254 friend CS_CRYSTALSPACE_EXPORT csSphere operator* (const csTransform& t, 00255 const csSphere& p); 00256 00261 friend CS_CRYSTALSPACE_EXPORT csSphere& operator*= (csSphere& p, 00262 const csTransform& t); 00263 00268 friend CS_CRYSTALSPACE_EXPORT csMatrix3 operator* (const csMatrix3& m, 00269 const csTransform& t); 00270 00275 friend CS_CRYSTALSPACE_EXPORT csMatrix3 operator* (const csTransform& t, 00276 const csMatrix3& m); 00277 00282 friend CS_CRYSTALSPACE_EXPORT csMatrix3& operator*= (csMatrix3& m, 00283 const csTransform& t); 00284 00295 friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1, 00296 const csReversibleTransform& t2); 00297 00303 static csTransform GetReflect (const csPlane3& pl); 00304 00310 csVector3 GetFront () const 00311 { 00312 return csVector3 (m_o2t.m31, m_o2t.m32, m_o2t.m33); 00313 } 00314 00320 csVector3 GetUp () const 00321 { 00322 return csVector3 (m_o2t.m21, m_o2t.m22, m_o2t.m23); 00323 } 00324 00330 csVector3 GetRight () const 00331 { 00332 return csVector3 (m_o2t.m11, m_o2t.m12, m_o2t.m13); 00333 } 00334 }; 00335 00347 class CS_CRYSTALSPACE_EXPORT csReversibleTransform : public csTransform 00348 { 00349 protected: 00351 csMatrix3 m_t2o; 00352 00356 csReversibleTransform (const csMatrix3& o2t, const csMatrix3& t2o, 00357 const csVector3& pos) : csTransform (o2t,pos), m_t2o (t2o) {} 00358 00359 public: 00363 csReversibleTransform () : csTransform (), m_t2o () {} 00364 00372 csReversibleTransform (const csMatrix3& o2t, const csVector3& pos) : 00373 csTransform (o2t,pos) { m_t2o = m_o2t.GetInverse (); } 00374 00378 csReversibleTransform (const csTransform& t) : 00379 csTransform (t) { m_t2o = m_o2t.GetInverse (); } 00380 00384 csReversibleTransform (const csReversibleTransform& t) : 00385 csTransform (t) { m_t2o = t.m_t2o; } 00386 00391 inline const csMatrix3& GetT2O () const { return m_t2o; } 00392 00397 inline csVector3 GetT2OTranslation () const { return -m_o2t*v_o2t; } 00398 00402 csReversibleTransform GetInverse () const 00403 { return csReversibleTransform (m_t2o, m_o2t, -m_o2t*v_o2t); } 00404 00409 virtual void SetO2T (const csMatrix3& m) 00410 { m_o2t = m; m_t2o = m_o2t.GetInverse (); } 00411 00417 virtual void SetT2O (const csMatrix3& m) 00418 { m_t2o = m; m_o2t = m_t2o.GetInverse (); } 00419 00425 inline csVector3 This2Other (const csVector3& v) const 00426 { return v_o2t + m_t2o * v; } 00427 00433 inline csVector3 This2OtherRelative (const csVector3& v) const 00434 { return m_t2o * v; } 00435 00442 csPlane3 This2Other (const csPlane3& p) const; 00443 00450 csPlane3 This2OtherRelative (const csPlane3& p) const; 00451 00460 void This2Other (const csPlane3& p, const csVector3& point, 00461 csPlane3& result) const; 00462 00466 csSphere This2Other (const csSphere& s) const; 00467 00473 void RotateOther (const csVector3& v, float angle); 00474 00480 void RotateThis (const csVector3& v, float angle); 00481 00489 void RotateOther (const csMatrix3& m) { SetT2O (m * m_t2o); } 00490 00498 void RotateThis (const csMatrix3& m) { SetT2O (m_t2o * m); } 00499 00508 void LookAt (const csVector3& v, const csVector3& up); 00509 00514 friend CS_CRYSTALSPACE_EXPORT csVector3 operator/ (const csVector3& v, 00515 const csReversibleTransform& t); 00516 00521 friend CS_CRYSTALSPACE_EXPORT csVector3& operator/= (csVector3& v, 00522 const csReversibleTransform& t); 00523 00528 friend CS_CRYSTALSPACE_EXPORT csPlane3 operator/ (const csPlane3& p, 00529 const csReversibleTransform& t); 00530 00535 friend CS_CRYSTALSPACE_EXPORT csPlane3& operator/= (csPlane3& p, 00536 const csReversibleTransform& t); 00537 00542 friend CS_CRYSTALSPACE_EXPORT csSphere operator/ (const csSphere& p, 00543 const csReversibleTransform& t); 00544 00556 friend csReversibleTransform& operator*= (csReversibleTransform& t1, 00557 const csReversibleTransform& t2) 00558 { 00559 t1.v_o2t = t2.m_t2o*t1.v_o2t; 00560 t1.v_o2t += t2.v_o2t; 00561 t1.m_o2t *= t2.m_o2t; 00562 t1.m_t2o = t2.m_t2o * t1.m_t2o; 00563 return t1; 00564 } 00565 00577 friend csReversibleTransform operator* (const csReversibleTransform& t1, 00578 const csReversibleTransform& t2) 00579 { 00580 return csReversibleTransform (t1.m_o2t*t2.m_o2t, t2.m_t2o*t1.m_t2o, 00581 t2.v_o2t + t2.m_t2o*t1.v_o2t); 00582 } 00583 00584 #if !defined(SWIG) /* Otherwise Swig 1.3.22 thinks this is multiply declared */ 00585 00596 friend CS_CRYSTALSPACE_EXPORT csTransform operator* (const csTransform& t1, 00597 const csReversibleTransform& t2); 00598 #endif 00599 00611 friend CS_CRYSTALSPACE_EXPORT csReversibleTransform& operator/= ( 00612 csReversibleTransform& t1, const csReversibleTransform& t2); 00613 00625 friend CS_CRYSTALSPACE_EXPORT csReversibleTransform operator/ ( 00626 const csReversibleTransform& t1, const csReversibleTransform& t2); 00627 }; 00628 00635 class csOrthoTransform : public csReversibleTransform 00636 { 00637 public: 00641 csOrthoTransform () : csReversibleTransform () {} 00642 00646 csOrthoTransform (const csMatrix3& o2t, const csVector3& pos) : 00647 csReversibleTransform (o2t, o2t.GetTranspose (), pos) { } 00648 00652 csOrthoTransform (const csTransform& t) : 00653 csReversibleTransform (t.GetO2T (), t.GetO2T ().GetTranspose (), 00654 t.GetO2TTranslation ()) 00655 { } 00656 00661 virtual void SetO2T (const csMatrix3& m) 00662 { m_o2t = m; m_t2o = m_o2t.GetTranspose (); } 00663 00669 virtual void SetT2O (const csMatrix3& m) 00670 { m_t2o = m; m_o2t = m_t2o.GetTranspose (); } 00671 }; 00672 00675 #endif // __CS_TRANSFORM_H__ 00676
Generated for Crystal Space 1.4.1 by doxygen 1.7.1