Blender V2.61 - r43446
|
00001 /*************************************************************************** 00002 frames.hpp `- description 00003 ------------------------- 00004 begin : June 2006 00005 copyright : (C) 2006 Erwin Aertbelien 00006 email : firstname.lastname@mech.kuleuven.be 00007 00008 History (only major changes)( AUTHOR-Description ) : 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU Lesser General Public * 00013 * License as published by the Free Software Foundation; either * 00014 * version 2.1 of the License, or (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00019 * Lesser General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Lesser General Public * 00022 * License along with this library; if not, write to the Free Software * 00023 * Foundation, Inc., 51 Franklin Street, * 00024 * Fifth Floor, Boston, MA 02110-1301, USA. * 00025 * * 00026 ***************************************************************************/ 00027 00116 #ifndef KDL_FRAMES_H 00117 #define KDL_FRAMES_H 00118 00119 00120 #include "utilities/kdl-config.h" 00121 #include "utilities/utility.h" 00122 00124 00125 namespace KDL { 00126 00127 00128 00129 class Vector; 00130 class Rotation; 00131 class Frame; 00132 class Wrench; 00133 class Twist; 00134 class Vector2; 00135 class Rotation2; 00136 class Frame2; 00137 00138 00139 00143 class Vector 00144 { 00145 public: 00146 double data[3]; 00148 inline Vector() {data[0]=data[1]=data[2] = 0.0;} 00149 00151 inline Vector(double x,double y, double z); 00152 00154 inline Vector(double* xyz); 00155 00157 inline Vector(float* xyz); 00158 00160 inline Vector(const Vector& arg); 00161 00163 inline void GetValue(double* xyz) const; 00164 00166 inline Vector& operator = ( const Vector& arg); 00167 00169 inline double operator()(int index) const; 00170 00172 inline double& operator() (int index); 00173 00175 double operator[] ( int index ) const 00176 { 00177 return this->operator() ( index ); 00178 } 00179 00181 double& operator[] ( int index ) 00182 { 00183 return this->operator() ( index ); 00184 } 00185 00186 inline double x() const; 00187 inline double y() const; 00188 inline double z() const; 00189 inline void x(double); 00190 inline void y(double); 00191 inline void z(double); 00192 00194 inline void ReverseSign(); 00195 00196 00198 inline Vector& operator-=(const Vector& arg); 00199 00200 00202 inline Vector& operator +=(const Vector& arg); 00203 00205 inline friend Vector operator*(const Vector& lhs,double rhs); 00207 inline friend Vector operator*(double lhs,const Vector& rhs); 00209 00210 inline friend Vector operator/(const Vector& lhs,double rhs); 00211 inline friend Vector operator+(const Vector& lhs,const Vector& rhs); 00212 inline friend Vector operator-(const Vector& lhs,const Vector& rhs); 00213 inline friend Vector operator*(const Vector& lhs,const Vector& rhs); 00214 inline friend Vector operator-(const Vector& arg); 00215 inline friend double dot(const Vector& lhs,const Vector& rhs); 00216 00219 inline friend void SetToZero(Vector& v); 00220 00222 inline static Vector Zero(); 00223 00229 double Normalize(double eps=epsilon); 00230 00232 double Norm() const; 00233 00234 00235 00237 inline void Set2DXY(const Vector2& v); 00239 inline void Set2DYZ(const Vector2& v); 00241 inline void Set2DZX(const Vector2& v); 00243 inline void Set2DPlane(const Frame& F_someframe_XY,const Vector2& v_XY); 00244 00245 00248 inline friend bool Equal(const Vector& a,const Vector& b,double eps=epsilon); 00249 00251 inline friend Vector Normalize(const Vector& a, double eps=epsilon); 00252 00254 inline friend bool operator==(const Vector& a,const Vector& b); 00256 inline friend bool operator!=(const Vector& a,const Vector& b); 00257 00258 friend class Rotation; 00259 friend class Frame; 00260 }; 00261 00262 00296 class Rotation 00297 { 00298 public: 00299 double data[9]; 00300 00301 inline Rotation() { 00302 *this = Identity(); 00303 } 00304 inline Rotation(double Xx,double Yx,double Zx, 00305 double Xy,double Yy,double Zy, 00306 double Xz,double Yz,double Zz); 00307 inline Rotation(const Vector& x,const Vector& y,const Vector& z); 00308 // default copy constructor is sufficient 00309 00310 inline void setValue(float* oglmat); 00311 inline void getValue(float* oglmat) const; 00312 00313 inline Rotation& operator=(const Rotation& arg); 00314 00317 inline Vector operator*(const Vector& v) const; 00318 00320 inline double& operator()(int i,int j); 00321 00323 inline double operator() (int i,int j) const; 00324 00325 friend Rotation operator *(const Rotation& lhs,const Rotation& rhs); 00326 00328 inline void SetInverse(); 00329 00331 inline Rotation Inverse() const; 00332 00334 inline Vector Inverse(const Vector& v) const; 00335 00337 inline Wrench Inverse(const Wrench& arg) const; 00338 00340 inline Twist Inverse(const Twist& arg) const; 00341 00343 inline static Rotation Identity(); 00344 00345 00346 // = Rotations 00348 inline static Rotation RotX(double angle); 00350 inline static Rotation RotY(double angle); 00352 inline static Rotation RotZ(double angle); 00355 inline void DoRotX(double angle); 00358 inline void DoRotY(double angle); 00361 inline void DoRotZ(double angle); 00362 00366 // @see Rot2 if you want to handle this error in another way. 00367 static Rotation Rot(const Vector& rotaxis,double angle); 00368 00370 static Rotation Rot2(const Vector& rotvec,double angle); 00371 00372 // make sure the matrix is a pure rotation (no scaling) 00373 void Ortho(); 00374 00377 Vector GetRot() const; 00378 00381 Vector2 GetXZRot() const; 00382 00391 double GetRotAngle(Vector& axis,double eps=epsilon) const; 00392 00393 00398 static Rotation EulerZYZ(double Alfa,double Beta,double Gamma); 00399 00409 void GetEulerZYZ(double& alfa,double& beta,double& gamma) const; 00410 00411 00415 static Rotation RPY(double roll,double pitch,double yaw); 00416 00424 void GetRPY(double& roll,double& pitch,double& yaw) const; 00425 00426 00433 inline static Rotation EulerZYX(double Alfa,double Beta,double Gamma) { 00434 return RPY(Gamma,Beta,Alfa); 00435 } 00436 00448 inline void GetEulerZYX(double& Alfa,double& Beta,double& Gamma) const { 00449 GetRPY(Gamma,Beta,Alfa); 00450 } 00451 00456 inline Twist operator * (const Twist& arg) const; 00457 00462 inline Wrench operator * (const Wrench& arg) const; 00463 00465 inline Vector UnitX() const { 00466 return Vector(data[0],data[3],data[6]); 00467 } 00468 00470 inline void UnitX(const Vector& X) { 00471 data[0] = X(0); 00472 data[3] = X(1); 00473 data[6] = X(2); 00474 } 00475 00477 inline Vector UnitY() const { 00478 return Vector(data[1],data[4],data[7]); 00479 } 00480 00482 inline void UnitY(const Vector& X) { 00483 data[1] = X(0); 00484 data[4] = X(1); 00485 data[7] = X(2); 00486 } 00487 00489 inline Vector UnitZ() const { 00490 return Vector(data[2],data[5],data[8]); 00491 } 00492 00494 inline void UnitZ(const Vector& X) { 00495 data[2] = X(0); 00496 data[5] = X(1); 00497 data[8] = X(2); 00498 } 00499 00502 friend bool Equal(const Rotation& a,const Rotation& b,double eps=epsilon); 00503 00505 friend bool operator==(const Rotation& a,const Rotation& b); 00507 friend bool operator!=(const Rotation& a,const Rotation& b); 00508 00509 friend class Frame; 00510 }; 00511 bool operator==(const Rotation& a,const Rotation& b); 00512 00513 00514 00524 class Frame { 00525 public: 00526 Vector p; 00527 Rotation M; 00528 00529 public: 00530 00531 inline Frame(const Rotation& R,const Vector& V); 00532 00534 explicit inline Frame(const Vector& V); 00536 explicit inline Frame(const Rotation& R); 00537 00538 inline void setValue(float* oglmat); 00539 inline void getValue(float* oglmat) const; 00540 00541 inline Frame() {} 00543 inline Frame(const Frame& arg); 00544 00546 //\TODO should be formulated as a constructor 00547 void Make4x4(double* d); 00548 00551 inline double operator()(int i,int j); 00552 00555 inline double operator() (int i,int j) const; 00556 00557 // = Inverse 00559 inline Frame Inverse() const; 00560 00562 inline Vector Inverse(const Vector& arg) const; 00563 00565 inline Wrench Inverse(const Wrench& arg) const; 00566 00568 inline Twist Inverse(const Twist& arg) const; 00569 00571 inline Frame& operator = (const Frame& arg); 00572 00575 inline Vector operator * (const Vector& arg) const; 00576 00583 inline Wrench operator * (const Wrench& arg) const; 00584 00591 inline Twist operator * (const Twist& arg) const; 00592 00594 inline friend Frame operator *(const Frame& lhs,const Frame& rhs); 00595 00597 inline static Frame Identity(); 00598 00602 inline void Integrate(const Twist& t_this,double frequency); 00603 00604 /* 00605 // DH_Craig1989 : constructs a transformationmatrix 00606 // T_link(i-1)_link(i) with the Denavit-Hartenberg convention as 00607 // described in the Craigs book: Craig, J. J.,Introduction to 00608 // Robotics: Mechanics and Control, Addison-Wesley, 00609 // isbn:0-201-10326-5, 1986. 00610 // 00611 // Note that the frame is a redundant way to express the information 00612 // in the DH-convention. 00613 // \verbatim 00614 // Parameters in full : a(i-1),alpha(i-1),d(i),theta(i) 00615 // 00616 // axis i-1 is connected by link i-1 to axis i numbering axis 1 00617 // to axis n link 0 (immobile base) to link n 00618 // 00619 // link length a(i-1) length of the mutual perpendicular line 00620 // (normal) between the 2 axes. This normal runs from (i-1) to 00621 // (i) axis. 00622 // 00623 // link twist alpha(i-1): construct plane perpendicular to the 00624 // normal project axis(i-1) and axis(i) into plane angle from 00625 // (i-1) to (i) measured in the direction of the normal 00626 // 00627 // link offset d(i) signed distance between normal (i-1) to (i) 00628 // and normal (i) to (i+1) along axis i joint angle theta(i) 00629 // signed angle between normal (i-1) to (i) and normal (i) to 00630 // (i+1) along axis i 00631 // 00632 // First and last joints : a(0)= a(n) = 0 00633 // alpha(0) = alpha(n) = 0 00634 // 00635 // PRISMATIC : theta(1) = 0 d(1) arbitrarily 00636 // 00637 // REVOLUTE : theta(1) arbitrarily d(1) = 0 00638 // 00639 // Not unique : if intersecting joint axis 2 choices for normal 00640 // Frame assignment of the DH convention : Z(i-1) follows axis 00641 // (i-1) X(i-1) is the normal between axis(i-1) and axis(i) 00642 // Y(i-1) follows out of Z(i-1) and X(i-1) 00643 // 00644 // a(i-1) = distance from Z(i-1) to Z(i) along X(i-1) 00645 // alpha(i-1) = angle between Z(i-1) to Z(i) along X(i-1) 00646 // d(i) = distance from X(i-1) to X(i) along Z(i) 00647 // theta(i) = angle between X(i-1) to X(i) along X(i) 00648 // \endverbatim 00649 */ 00650 static Frame DH_Craig1989(double a,double alpha,double d,double theta); 00651 00652 // DH : constructs a transformationmatrix T_link(i-1)_link(i) with 00653 // the Denavit-Hartenberg convention as described in the original 00654 // publictation: Denavit, J. and Hartenberg, R. S., A kinematic 00655 // notation for lower-pair mechanisms based on matrices, ASME 00656 // Journal of Applied Mechanics, 23:215-221, 1955. 00657 00658 static Frame DH(double a,double alpha,double d,double theta); 00659 00660 00663 inline friend bool Equal(const Frame& a,const Frame& b,double eps=epsilon); 00664 00666 inline friend bool operator==(const Frame& a,const Frame& b); 00668 inline friend bool operator!=(const Frame& a,const Frame& b); 00669 }; 00670 00677 class Twist { 00678 public: 00679 Vector vel; 00680 Vector rot; 00681 public: 00682 00684 Twist():vel(),rot() {}; 00685 00686 Twist(const Vector& _vel,const Vector& _rot):vel(_vel),rot(_rot) {}; 00687 00688 inline Twist& operator-=(const Twist& arg); 00689 inline Twist& operator+=(const Twist& arg); 00691 inline double& operator()(int i); 00692 00695 inline double operator()(int i) const; 00696 00697 double operator[] ( int index ) const 00698 { 00699 return this->operator() ( index ); 00700 } 00701 00702 double& operator[] ( int index ) 00703 { 00704 return this->operator() ( index ); 00705 } 00706 00707 inline friend Twist operator*(const Twist& lhs,double rhs); 00708 inline friend Twist operator*(double lhs,const Twist& rhs); 00709 inline friend Twist operator/(const Twist& lhs,double rhs); 00710 inline friend Twist operator+(const Twist& lhs,const Twist& rhs); 00711 inline friend Twist operator-(const Twist& lhs,const Twist& rhs); 00712 inline friend Twist operator-(const Twist& arg); 00713 inline friend double dot(const Twist& lhs,const Wrench& rhs); 00714 inline friend double dot(const Wrench& rhs,const Twist& lhs); 00715 inline friend void SetToZero(Twist& v); 00716 00717 00719 static inline Twist Zero(); 00720 00722 inline void ReverseSign(); 00723 00730 inline Twist RefPoint(const Vector& v_base_AB) const; 00731 00732 00735 inline friend bool Equal(const Twist& a,const Twist& b,double eps=epsilon); 00736 00738 inline friend bool operator==(const Twist& a,const Twist& b); 00740 inline friend bool operator!=(const Twist& a,const Twist& b); 00741 00742 // = Friends 00743 friend class Rotation; 00744 friend class Frame; 00745 00746 }; 00747 00755 /* 00756 class AccelerationTwist { 00757 public: 00758 Vector trans; //!< The translational acceleration of that point 00759 Vector rot; //!< The rotational acceleration of that point. 00760 public: 00761 00763 AccelerationTwist():trans(),rot() {}; 00764 00765 AccelerationTwist(const Vector& _trans,const Vector& _rot):trans(_trans),rot(_rot) {}; 00766 00767 inline AccelerationTwist& operator-=(const AccelerationTwist& arg); 00768 inline AccelerationTwist& operator+=(const AccelerationTwist& arg); 00770 inline double& operator()(int i); 00771 00774 inline double operator()(int i) const; 00775 00776 double operator[] ( int index ) const 00777 { 00778 return this->operator() ( index ); 00779 } 00780 00781 double& operator[] ( int index ) 00782 { 00783 return this->operator() ( index ); 00784 } 00785 00786 inline friend AccelerationTwist operator*(const AccelerationTwist& lhs,double rhs); 00787 inline friend AccelerationTwist operator*(double lhs,const AccelerationTwist& rhs); 00788 inline friend AccelerationTwist operator/(const AccelerationTwist& lhs,double rhs); 00789 inline friend AccelerationTwist operator+(const AccelerationTwist& lhs,const AccelerationTwist& rhs); 00790 inline friend AccelerationTwist operator-(const AccelerationTwist& lhs,const AccelerationTwist& rhs); 00791 inline friend AccelerationTwist operator-(const AccelerationTwist& arg); 00792 //inline friend double dot(const AccelerationTwist& lhs,const Wrench& rhs); 00793 //inline friend double dot(const Wrench& rhs,const AccelerationTwist& lhs); 00794 inline friend void SetToZero(AccelerationTwist& v); 00795 00796 00798 static inline AccelerationTwist Zero(); 00799 00801 inline void ReverseSign(); 00802 00809 inline AccelerationTwist RefPoint(const Vector& v_base_AB) const; 00810 00811 00814 inline friend bool Equal(const AccelerationTwist& a,const AccelerationTwist& b,double eps=epsilon); 00815 00817 inline friend bool operator==(const AccelerationTwist& a,const AccelerationTwist& b); 00819 inline friend bool operator!=(const AccelerationTwist& a,const AccelerationTwist& b); 00820 00821 // = Friends 00822 friend class Rotation; 00823 friend class Frame; 00824 00825 }; 00826 */ 00832 class Wrench 00833 { 00834 public: 00835 Vector force; 00836 Vector torque; 00837 public: 00838 00840 Wrench():force(),torque() {}; 00841 Wrench(const Vector& _force,const Vector& _torque):force(_force),torque(_torque) {}; 00842 00843 // = Operators 00844 inline Wrench& operator-=(const Wrench& arg); 00845 inline Wrench& operator+=(const Wrench& arg); 00846 00848 inline double& operator()(int i); 00849 00852 inline double operator()(int i) const; 00853 00854 double operator[] ( int index ) const 00855 { 00856 return this->operator() ( index ); 00857 } 00858 00859 double& operator[] ( int index ) 00860 { 00861 return this->operator() ( index ); 00862 } 00863 00865 inline friend Wrench operator*(const Wrench& lhs,double rhs); 00867 inline friend Wrench operator*(double lhs,const Wrench& rhs); 00869 inline friend Wrench operator/(const Wrench& lhs,double rhs); 00870 00871 inline friend Wrench operator+(const Wrench& lhs,const Wrench& rhs); 00872 inline friend Wrench operator-(const Wrench& lhs,const Wrench& rhs); 00873 00875 inline friend Wrench operator-(const Wrench& arg); 00876 00879 inline friend void SetToZero(Wrench& v); 00880 00882 static inline Wrench Zero(); 00883 00885 inline void ReverseSign(); 00886 00893 inline Wrench RefPoint(const Vector& v_base_AB) const; 00894 00895 00898 inline friend bool Equal(const Wrench& a,const Wrench& b,double eps=epsilon); 00899 00901 inline friend bool operator==(const Wrench& a,const Wrench& b); 00903 inline friend bool operator!=(const Wrench& a,const Wrench& b); 00904 00905 friend class Rotation; 00906 friend class Frame; 00907 00908 00909 }; 00910 00911 00913 class Vector2 00914 { 00915 double data[2]; 00916 public: 00918 Vector2() {data[0]=data[1] = 0.0;} 00919 inline Vector2(double x,double y); 00920 inline Vector2(const Vector2& arg); 00921 inline Vector2(double* xyz); 00922 inline Vector2(float* xyz); 00923 00924 inline Vector2& operator = ( const Vector2& arg); 00925 00927 inline double operator()(int index) const; 00928 00930 inline double& operator() (int index); 00931 00933 inline void GetValue(double* xy) const; 00934 00935 inline void ReverseSign(); 00936 inline Vector2& operator-=(const Vector2& arg); 00937 inline Vector2& operator +=(const Vector2& arg); 00938 00939 00940 inline friend Vector2 operator*(const Vector2& lhs,double rhs); 00941 inline friend Vector2 operator*(double lhs,const Vector2& rhs); 00942 inline friend Vector2 operator/(const Vector2& lhs,double rhs); 00943 inline friend Vector2 operator+(const Vector2& lhs,const Vector2& rhs); 00944 inline friend Vector2 operator-(const Vector2& lhs,const Vector2& rhs); 00945 inline friend Vector2 operator*(const Vector2& lhs,const Vector2& rhs); 00946 inline friend Vector2 operator-(const Vector2& arg); 00947 inline friend void SetToZero(Vector2& v); 00948 00950 inline static Vector2 Zero(); 00951 00957 double Normalize(double eps=epsilon); 00958 00960 inline double Norm() const; 00961 00963 inline void Set3DXY(const Vector& v); 00964 00966 inline void Set3DYZ(const Vector& v); 00967 00969 inline void Set3DZX(const Vector& v); 00970 00974 inline void Set3DPlane(const Frame& F_someframe_XY,const Vector& v_someframe); 00975 00976 00979 inline friend bool Equal(const Vector2& a,const Vector2& b,double eps=epsilon); 00980 00981 friend class Rotation2; 00982 }; 00983 00984 00987 class Rotation2 00988 { 00989 double s,c; 00992 public: 00994 Rotation2() {c=1.0;s=0.0;} 00995 00996 explicit Rotation2(double angle_rad):s(sin(angle_rad)),c(cos(angle_rad)) {} 00997 00998 Rotation2(double ca,double sa):s(sa),c(ca){} 00999 01000 inline Rotation2& operator=(const Rotation2& arg); 01001 inline Vector2 operator*(const Vector2& v) const; 01003 inline double operator() (int i,int j) const; 01004 01005 inline friend Rotation2 operator *(const Rotation2& lhs,const Rotation2& rhs); 01006 01007 inline void SetInverse(); 01008 inline Rotation2 Inverse() const; 01009 inline Vector2 Inverse(const Vector2& v) const; 01010 01011 inline void SetIdentity(); 01012 inline static Rotation2 Identity(); 01013 01014 01016 inline void SetRot(double angle); 01017 01019 inline static Rotation2 Rot(double angle); 01020 01022 inline double GetRot() const; 01023 01026 inline friend bool Equal(const Rotation2& a,const Rotation2& b,double eps=epsilon); 01027 }; 01028 01031 class Frame2 01032 { 01033 public: 01034 Vector2 p; 01035 Rotation2 M; 01036 01037 public: 01038 01039 inline Frame2(const Rotation2& R,const Vector2& V); 01040 explicit inline Frame2(const Vector2& V); 01041 explicit inline Frame2(const Rotation2& R); 01042 inline Frame2(void); 01043 inline Frame2(const Frame2& arg); 01044 inline void Make4x4(double* d); 01045 01048 inline double operator()(int i,int j); 01049 01052 inline double operator() (int i,int j) const; 01053 01054 inline void SetInverse(); 01055 inline Frame2 Inverse() const; 01056 inline Vector2 Inverse(const Vector2& arg) const; 01057 inline Frame2& operator = (const Frame2& arg); 01058 inline Vector2 operator * (const Vector2& arg); 01059 inline friend Frame2 operator *(const Frame2& lhs,const Frame2& rhs); 01060 inline void SetIdentity(); 01061 inline void Integrate(const Twist& t_this,double frequency); 01062 inline static Frame2 Identity() { 01063 Frame2 tmp; 01064 tmp.SetIdentity(); 01065 return tmp; 01066 } 01067 inline friend bool Equal(const Frame2& a,const Frame2& b,double eps=epsilon); 01068 }; 01069 01070 IMETHOD Vector diff(const Vector& a,const Vector& b,double dt=1); 01071 IMETHOD Vector diff(const Rotation& R_a_b1,const Rotation& R_a_b2,double dt=1); 01072 IMETHOD Twist diff(const Frame& F_a_b1,const Frame& F_a_b2,double dt=1); 01073 IMETHOD Twist diff(const Twist& a,const Twist& b,double dt=1); 01074 IMETHOD Wrench diff(const Wrench& W_a_p1,const Wrench& W_a_p2,double dt=1); 01075 IMETHOD Vector addDelta(const Vector& a,const Vector&da,double dt=1); 01076 IMETHOD Rotation addDelta(const Rotation& a,const Vector&da,double dt=1); 01077 IMETHOD Frame addDelta(const Frame& a,const Twist& da,double dt=1); 01078 IMETHOD Twist addDelta(const Twist& a,const Twist&da,double dt=1); 01079 IMETHOD Wrench addDelta(const Wrench& a,const Wrench&da,double dt=1); 01080 #ifdef KDL_INLINE 01081 // #include "vector.inl" 01082 // #include "wrench.inl" 01083 //#include "rotation.inl" 01084 //#include "frame.inl" 01085 //#include "twist.inl" 01086 //#include "vector2.inl" 01087 //#include "rotation2.inl" 01088 //#include "frame2.inl" 01089 #include "frames.inl" 01090 #endif 01091 01092 01093 01094 } 01095 01096 01097 #endif