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 00029 #include "csgeom/matrix3.h" 00030 #include "csgeom/plane3.h" 00031 #include "csgeom/sphere.h" 00032 00033 class csReversibleTransform; 00034 00041 class csTransform 00042 { 00043 protected: 00045 csMatrix3 m_o2t; 00047 csVector3 v_o2t; 00048 00049 public: 00053 csTransform () : m_o2t (), v_o2t (0, 0, 0) {} 00054 00062 csTransform (const csMatrix3& other2this, const csVector3& origin_pos) : 00063 m_o2t (other2this), v_o2t (origin_pos) {} 00064 00068 void Identity () 00069 { 00070 SetO2TTranslation (csVector3 (0)); 00071 SetO2T (csMatrix3 ()); 00072 } 00073 00078 bool IsIdentity () const 00079 { 00080 if (ABS (v_o2t.x) >= SMALL_EPSILON) return false; 00081 if (ABS (v_o2t.y) >= SMALL_EPSILON) return false; 00082 if (ABS (v_o2t.z) >= SMALL_EPSILON) return false; 00083 if (ABS (m_o2t.m11-1) >= SMALL_EPSILON) return false; 00084 if (ABS (m_o2t.m12) >= SMALL_EPSILON) return false; 00085 if (ABS (m_o2t.m13) >= SMALL_EPSILON) return false; 00086 if (ABS (m_o2t.m21) >= SMALL_EPSILON) return false; 00087 if (ABS (m_o2t.m22-1) >= SMALL_EPSILON) return false; 00088 if (ABS (m_o2t.m23) >= SMALL_EPSILON) return false; 00089 if (ABS (m_o2t.m31) >= SMALL_EPSILON) return false; 00090 if (ABS (m_o2t.m32) >= SMALL_EPSILON) return false; 00091 if (ABS (m_o2t.m33-1) >= SMALL_EPSILON) return false; 00092 return true; 00093 } 00094 00099 inline const csMatrix3& GetO2T () const { return m_o2t; } 00100 00106 inline const csVector3& GetO2TTranslation () const { return v_o2t; } 00107 00112 inline const csVector3& GetOrigin () const { return v_o2t; } 00113 00118 virtual void SetO2T (const csMatrix3& m) { m_o2t = m; } 00119 00125 virtual void SetO2TTranslation (const csVector3& v) { v_o2t = v; } 00126 00131 inline void SetOrigin (const csVector3& v) { SetO2TTranslation (v); } 00132 00138 inline void Translate (const csVector3& v) { SetO2TTranslation (v_o2t + v); } 00139 00145 inline csVector3 Other2This (const csVector3& v) const 00146 { 00147 return m_o2t * (v - v_o2t); 00148 } 00149 00155 csVector3 Other2ThisRelative (const csVector3& v) const 00156 { return m_o2t * v; } 00157 00163 csPlane3 Other2This (const csPlane3& p) const; 00164 00171 csPlane3 Other2ThisRelative (const csPlane3& p) const; 00172 00180 void Other2This (const csPlane3& p, const csVector3& point, 00181 csPlane3& result) const; 00182 00186 csSphere Other2This (const csSphere& s) const; 00187 00192 friend csVector3 operator* (const csVector3& v, const csTransform& t); 00193 00198 friend csVector3 operator* (const csTransform& t, const csVector3& v); 00199 00204 friend csVector3& operator*= (csVector3& v, const csTransform& t); 00205 00210 friend csPlane3 operator* (const csPlane3& p, const csTransform& t); 00211 00216 friend csPlane3 operator* (const csTransform& t, const csPlane3& p); 00217 00222 friend csPlane3& operator*= (csPlane3& p, const csTransform& t); 00223 00228 friend csSphere operator* (const csSphere& p, const csTransform& t); 00229 00234 friend csSphere operator* (const csTransform& t, const csSphere& p); 00235 00240 friend csSphere& operator*= (csSphere& p, const csTransform& t); 00241 00246 friend csMatrix3 operator* (const csMatrix3& m, const csTransform& t); 00247 00252 friend csMatrix3 operator* (const csTransform& t, const csMatrix3& m); 00253 00258 friend csMatrix3& operator*= (csMatrix3& m, const csTransform& t); 00259 00271 friend csTransform operator* (const csTransform& t1, 00272 const csReversibleTransform& t2); 00273 00279 static csTransform GetReflect (const csPlane3& pl); 00280 }; 00281 00289 class csReversibleTransform : public csTransform 00290 { 00291 protected: 00293 csMatrix3 m_t2o; 00294 00298 csReversibleTransform (const csMatrix3& o2t, const csMatrix3& t2o, 00299 const csVector3& pos) : csTransform (o2t,pos), m_t2o (t2o) {} 00300 00301 public: 00305 csReversibleTransform () : csTransform (), m_t2o () {} 00306 00314 csReversibleTransform (const csMatrix3& o2t, const csVector3& pos) : 00315 csTransform (o2t,pos) { m_t2o = m_o2t.GetInverse (); } 00316 00320 csReversibleTransform (const csTransform& t) : 00321 csTransform (t) { m_t2o = m_o2t.GetInverse (); } 00322 00326 csReversibleTransform (const csReversibleTransform& t) : 00327 csTransform (t) { m_t2o = t.m_t2o; } 00328 00333 inline const csMatrix3& GetT2O () const { return m_t2o; } 00334 00339 inline csVector3 GetT2OTranslation () const { return -m_o2t*v_o2t; } 00340 00344 csReversibleTransform GetInverse () const 00345 { return csReversibleTransform (m_t2o, m_o2t, -m_o2t*v_o2t); } 00346 00351 virtual void SetO2T (const csMatrix3& m) 00352 { m_o2t = m; m_t2o = m_o2t.GetInverse (); } 00353 00359 virtual void SetT2O (const csMatrix3& m) 00360 { m_t2o = m; m_o2t = m_t2o.GetInverse (); } 00361 00367 csVector3 This2Other (const csVector3& v) const 00368 { return v_o2t + m_t2o * v; } 00369 00375 inline csVector3 This2OtherRelative (const csVector3& v) const 00376 { return m_t2o * v; } 00377 00384 csPlane3 This2Other (const csPlane3& p) const; 00385 00392 csPlane3 This2OtherRelative (const csPlane3& p) const; 00393 00402 void This2Other (const csPlane3& p, const csVector3& point, 00403 csPlane3& result) const; 00404 00408 csSphere This2Other (const csSphere& s) const; 00409 00415 void RotateOther (const csVector3& v, float angle); 00416 00422 void RotateThis (const csVector3& v, float angle); 00423 00431 void RotateOther (const csMatrix3& m) { SetT2O (m * m_t2o); } 00432 00440 void RotateThis (const csMatrix3& m) { SetT2O (m_t2o * m); } 00441 00450 void LookAt (const csVector3& v, const csVector3& up); 00451 00456 friend csVector3 operator/ (const csVector3& v, 00457 const csReversibleTransform& t); 00458 00463 friend csVector3& operator/= (csVector3& v, const csReversibleTransform& t); 00464 00469 friend csPlane3 operator/ (const csPlane3& p, const csReversibleTransform& t); 00470 00475 friend csPlane3& operator/= (csPlane3& p, const csReversibleTransform& t); 00476 00481 friend csSphere operator/ (const csSphere& p, const csReversibleTransform& t); 00482 00495 friend csReversibleTransform& operator*= (csReversibleTransform& t1, 00496 const csReversibleTransform& t2) 00497 { 00498 t1.v_o2t = t2.m_t2o*t1.v_o2t; 00499 t1.v_o2t += t2.v_o2t; 00500 t1.m_o2t *= t2.m_o2t; 00501 t1.m_t2o *= t1.m_t2o; 00502 return t1; 00503 } 00504 00517 friend csReversibleTransform operator* (const csReversibleTransform& t1, 00518 const csReversibleTransform& t2) 00519 { 00520 return csReversibleTransform (t1.m_o2t*t2.m_o2t, t2.m_t2o*t1.m_t2o, 00521 t2.v_o2t + t2.m_t2o*t1.v_o2t); 00522 } 00523 00536 friend csTransform operator* (const csTransform& t1, 00537 const csReversibleTransform& t2); 00538 00551 friend csReversibleTransform& operator/= (csReversibleTransform& t1, 00552 const csReversibleTransform& t2); 00553 00566 friend csReversibleTransform operator/ (const csReversibleTransform& t1, 00567 const csReversibleTransform& t2); 00568 }; 00569 00576 class csOrthoTransform : public csReversibleTransform 00577 { 00578 public: 00582 csOrthoTransform () : csReversibleTransform () {} 00583 00587 csOrthoTransform (const csMatrix3& o2t, const csVector3& pos) : 00588 csReversibleTransform (o2t, o2t.GetTranspose (), pos) { } 00589 00593 csOrthoTransform (const csTransform& t) : 00594 csReversibleTransform (t.GetO2T (), t.GetO2T ().GetTranspose (), 00595 t.GetO2TTranslation ()) 00596 { } 00597 00602 virtual void SetO2T (const csMatrix3& m) 00603 { m_o2t = m; m_t2o = m_o2t.GetTranspose (); } 00604 00610 virtual void SetT2O (const csMatrix3& m) 00611 { m_t2o = m; m_o2t = m_t2o.GetTranspose (); } 00612 }; 00613 00616 #endif // __CS_TRANSFORM_H__ 00617
Generated for Crystal Space by doxygen 1.2.18