Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_MATH_COORDSYS_H
00002 #define OPENTISSUE_CORE_MATH_MATH_COORDSYS_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_vector3.h>
00013 #include <OpenTissue/core/math/math_matrix3x3.h>
00014 #include <OpenTissue/core/math/math_quaternion.h>
00015 #include <OpenTissue/core/math/math_value_traits.h>
00016
00017 #include <iosfwd>
00018
00019 namespace OpenTissue
00020 {
00021
00022 namespace math
00023 {
00024
00033 template<
00034 typename value_type_
00035
00036 >
00037 class CoordSys
00038 {
00039 protected:
00040
00041 typedef typename OpenTissue::math::ValueTraits<value_type_> value_traits_ ;
00042
00043 public:
00044
00045 typedef value_traits_ value_traits;
00046 typedef value_type_ value_type;
00047
00048 typedef Vector3<value_type> vector3_type;
00049 typedef Quaternion<value_type> quaternion_type;
00050 typedef Matrix3x3<value_type> matrix3x3_type;
00051 typedef Quaternion<value_type> rotation_type;
00052
00053 protected:
00054
00055 vector3_type m_T;
00056 quaternion_type m_Q;
00057
00058 public:
00059
00060 vector3_type & T() { return m_T; }
00061 vector3_type const & T() const { return m_T; }
00062 quaternion_type & Q() { return m_Q; }
00063 quaternion_type const & Q() const { return m_Q; }
00064
00065 public:
00066
00067 CoordSys()
00068 : m_T(value_traits::zero(),value_traits::zero(),value_traits::zero())
00069 , m_Q(value_traits::one(),value_traits::zero(),value_traits::zero(),value_traits::zero())
00070 {}
00071
00072 explicit CoordSys(vector3_type const & T_val, quaternion_type const & Q_val)
00073 {
00074 m_T = T_val;
00075 m_Q = unit(Q_val);
00076 }
00077
00078 explicit CoordSys(vector3_type const & T_val, matrix3x3_type const & R_val)
00079 {
00080 m_T = T_val;
00081 m_Q = R_val;
00082 }
00083
00084 CoordSys & operator=(CoordSys const & C)
00085 {
00086 m_T = C.m_T;
00087 m_Q = C.m_Q;
00088 return *this;
00089 }
00090
00091 public:
00092
00093
00094 bool operator==(CoordSys const & C) const { return m_T == C.m_T && m_Q==C.m_Q; }
00095
00096 public:
00097
00098 void identity()
00099 {
00100 m_T.clear();
00101 m_Q.identity();
00102 }
00103
00117 void xform_point(vector3_type & p) const { p = m_Q.rotate(p) + m_T; }
00118
00124 void xform_vector(vector3_type & v) const { v = m_Q.rotate(v); }
00125
00131 void xform_matrix(matrix3x3_type & O) const
00132 {
00133 O = matrix3x3_type(m_Q) * O;
00134 }
00135
00149 CoordSys operator*(CoordSys const & X1) const
00150 {
00151 return CoordSys( m_Q.rotate(X1.T()) + m_T , unit( prod( m_Q , X1.Q()) ) );
00152 }
00153
00154 public:
00155
00156 bool is_equal(CoordSys const & C, value_type const & accuracy) const
00157 {
00158 return m_T.is_equal(C.m_T,accuracy) && m_Q.is_equal(C.m_Q,accuracy);
00159 }
00160
00161 };
00162
00163
00164
00191 template<typename T>
00192 inline CoordSys<T> prod(CoordSys<T> const & L, CoordSys<T> const & R)
00193 {
00194 return CoordSys<T>( L.Q().rotate( R.T() ) + L.T() , unit( prod( L.Q() , R.Q()) ) );
00195 }
00196
00209 template<typename T>
00210 inline CoordSys<T> inverse(CoordSys<T> const & X)
00211 {
00212
00213
00214
00215
00216
00217
00218
00219 return CoordSys<T>( conj(X.Q()).rotate(-X.T()) , conj(X.Q()) );
00220 }
00221
00241 template<typename T>
00242 inline CoordSys<T> model_update(CoordSys<T> const & A, CoordSys<T> const & B)
00243 {
00244 return model_update(A.T(),A.Q(),B.T(),B.Q());
00245 }
00246
00268 template<typename T>
00269 inline CoordSys<T> model_update(Vector3<T> const & TA, Quaternion<T> const & QA, Vector3<T> const & TB, Quaternion<T> const & QB)
00270 {
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 Quaternion<T> q;
00285 if(QA==QB)
00286 {
00287 q.identity();
00288 }
00289 else
00290 {
00291 q = unit( prod( conj(QB), QA) );
00292 }
00293 return CoordSys<T>( conj(QB).rotate(TA - TB), q);
00294 }
00295
00296 template<typename T>
00297 inline std::ostream & operator<< (std::ostream & o, CoordSys<T> const & C)
00298 {
00299 o << "[" << C.T() << "," << C.Q() << "]";
00300 return o;
00301 }
00302
00303 template<typename T>
00304 inline std::istream & operator>>(std::istream & i, CoordSys<T> & C)
00305 {
00306 char dummy;
00307 i >> dummy;
00308 i >> C.T();
00309 i >> dummy;
00310 i >> C.Q();
00311 i >> dummy;
00312 return i;
00313 }
00314
00315 }
00316
00317 }
00318
00319
00320 #endif