frames.inl

Go to the documentation of this file.
00001 /***************************************************************************
00002                         frames.inl -  description
00003                        -------------------------
00004     begin                : June 2006
00005     copyright            : (C) 2006 Erwin Aertbelien
00006     email                : firstname.lastname@mech.kuleuven.ac.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., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 namespace KDL {
00029 
00030 
00031 IMETHOD Vector::Vector(const Vector & arg)
00032 {
00033     data[0] = arg.data[0];
00034     data[1] = arg.data[1];
00035     data[2] = arg.data[2];
00036 }
00037 
00038 IMETHOD Vector::Vector(double x,double y, double z)
00039 {
00040         data[0]=x;data[1]=y;data[2]=z;
00041 }
00042 
00043 
00044 IMETHOD Vector& Vector::operator =(const Vector & arg)
00045 {
00046     data[0] = arg.data[0];
00047     data[1] = arg.data[1];
00048     data[2] = arg.data[2];
00049     return *this;
00050 }
00051 
00052 IMETHOD Vector operator +(const Vector & lhs,const Vector& rhs)
00053 {
00054     Vector tmp;
00055     tmp.data[0] = lhs.data[0]+rhs.data[0];
00056     tmp.data[1] = lhs.data[1]+rhs.data[1];
00057     tmp.data[2] = lhs.data[2]+rhs.data[2];
00058     return tmp;
00059 }
00060 
00061 IMETHOD Vector operator -(const Vector & lhs,const Vector& rhs)
00062 {
00063     Vector tmp;
00064     tmp.data[0] = lhs.data[0]-rhs.data[0];
00065     tmp.data[1] = lhs.data[1]-rhs.data[1];
00066     tmp.data[2] = lhs.data[2]-rhs.data[2];
00067     return tmp;
00068 }
00069 
00070 IMETHOD double Vector::x() const { return data[0]; }
00071 IMETHOD double Vector::y() const { return data[1]; }
00072 IMETHOD double Vector::z() const { return data[2]; }
00073 
00074 IMETHOD void Vector::x( double _x ) { data[0] = _x; }
00075 IMETHOD void Vector::y( double _y ) { data[1] = _y; }
00076 IMETHOD void Vector::z( double _z ) { data[2] = _z; }
00077 
00078 Vector operator *(const Vector& lhs,double rhs) 
00079 {
00080     Vector tmp;
00081     tmp.data[0] = lhs.data[0]*rhs;
00082     tmp.data[1] = lhs.data[1]*rhs;
00083     tmp.data[2] = lhs.data[2]*rhs;
00084     return tmp;
00085 }
00086 
00087 Vector operator *(double lhs,const Vector& rhs) 
00088 {
00089     Vector tmp;
00090     tmp.data[0] = lhs*rhs.data[0];
00091     tmp.data[1] = lhs*rhs.data[1];
00092     tmp.data[2] = lhs*rhs.data[2];
00093     return tmp;
00094 }
00095 
00096 Vector operator /(const Vector& lhs,double rhs) 
00097 {
00098     Vector tmp;
00099     tmp.data[0] = lhs.data[0]/rhs;
00100     tmp.data[1] = lhs.data[1]/rhs;
00101     tmp.data[2] = lhs.data[2]/rhs;
00102     return tmp;
00103 }
00104 
00105 Vector operator *(const Vector & lhs,const Vector& rhs)
00106 // Complexity : 6M+3A
00107 {
00108     Vector tmp;
00109     tmp.data[0] = lhs.data[1]*rhs.data[2]-lhs.data[2]*rhs.data[1];
00110     tmp.data[1] = lhs.data[2]*rhs.data[0]-lhs.data[0]*rhs.data[2];
00111     tmp.data[2] = lhs.data[0]*rhs.data[1]-lhs.data[1]*rhs.data[0];
00112     return tmp;
00113 }
00114 
00115 Vector& Vector::operator +=(const Vector & arg)
00116 // Complexity : 3A
00117 {
00118     data[0]+=arg.data[0];
00119     data[1]+=arg.data[1];
00120     data[2]+=arg.data[2];
00121     return *this;
00122 }
00123 
00124 Vector& Vector::operator -=(const Vector & arg)
00125 // Complexity : 3A
00126 {
00127     data[0]-=arg.data[0];
00128     data[1]-=arg.data[1];
00129     data[2]-=arg.data[2];
00130     return *this;
00131 }
00132 
00133 Vector Vector::Zero()
00134 {
00135     return Vector(0,0,0);
00136 }
00137 
00138 double Vector::operator()(int index) const {
00139     FRAMES_CHECKI((0<=index)&&(index<=2));
00140     return data[index];
00141 }
00142 
00143 double& Vector::operator () (int index)
00144 {
00145     FRAMES_CHECKI((0<=index)&&(index<=2));
00146     return data[index];
00147 }
00148 
00149 
00150 
00151 Wrench Frame::operator * (const Wrench& arg) const
00152 // Complexity : 24M+18A
00153 {
00154     Wrench tmp;
00155     tmp.force  = M*arg.force;
00156     tmp.torque = M*arg.torque + p*tmp.force;
00157     return tmp;
00158 }
00159 
00160 Wrench Frame::Inverse(const Wrench& arg) const
00161 {
00162     Wrench tmp;
00163     tmp.force =  M.Inverse(arg.force);
00164     tmp.torque = M.Inverse(arg.torque-p*arg.force);
00165     return tmp;
00166 }
00167 
00168 
00169 
00170 Wrench Rotation::Inverse(const Wrench& arg) const
00171 {
00172     return Wrench(Inverse(arg.force),Inverse(arg.torque));
00173 }
00174 
00175 Twist Rotation::Inverse(const Twist& arg) const
00176 {
00177     return Twist(Inverse(arg.vel),Inverse(arg.rot));
00178 }
00179 
00180 Wrench Wrench::Zero()
00181 {
00182     return Wrench(Vector::Zero(),Vector::Zero());
00183 }
00184 
00185 
00186 void Wrench::ReverseSign()
00187 {   
00188     torque.ReverseSign();
00189     force.ReverseSign();
00190 }
00191 
00192 Wrench Wrench::RefPoint(const Vector& v_base_AB) const
00193      // Changes the reference point of the Wrench.
00194      // The vector v_base_AB is expressed in the same base as the twist
00195      // The vector v_base_AB is a vector from the old point to
00196      // the new point.
00197 {
00198     return Wrench(this->force,
00199                   this->torque+this->force*v_base_AB
00200                   );
00201 }
00202 
00203 
00204 Wrench& Wrench::operator-=(const Wrench& arg)
00205 {
00206     torque-=arg.torque;
00207     force -=arg.force;
00208     return *this;
00209 }
00210 
00211 Wrench& Wrench::operator+=(const Wrench& arg)
00212 {
00213     torque+=arg.torque;
00214     force +=arg.force;
00215     return *this;
00216 }
00217 
00218 double& Wrench::operator()(int i)
00219 {
00220     // assert((0<=i)&&(i<6)); done by underlying routines
00221     if (i<3) 
00222         return force(i);
00223     else
00224         return torque(i-3);
00225 }
00226 
00227 double Wrench::operator()(int i) const
00228 {
00229     // assert((0<=i)&&(i<6)); done by underlying routines
00230     if (i<3) 
00231         return force(i);
00232     else
00233         return torque(i-3);
00234 }
00235 
00236 
00237 Wrench operator*(const Wrench& lhs,double rhs)
00238 {
00239     return Wrench(lhs.force*rhs,lhs.torque*rhs);
00240 }
00241 
00242 Wrench operator*(double lhs,const Wrench& rhs)
00243 {
00244     return Wrench(lhs*rhs.force,lhs*rhs.torque);
00245 }
00246 
00247 Wrench operator/(const Wrench& lhs,double rhs)
00248 {
00249     return Wrench(lhs.force/rhs,lhs.torque/rhs);
00250 }
00251 
00252 // addition of Wrench's
00253 Wrench operator+(const Wrench& lhs,const Wrench& rhs)
00254 {
00255     return Wrench(lhs.force+rhs.force,lhs.torque+rhs.torque);
00256 }
00257 
00258 Wrench operator-(const Wrench& lhs,const Wrench& rhs)
00259 {
00260     return Wrench(lhs.force-rhs.force,lhs.torque-rhs.torque);
00261 }
00262 
00263 // unary -
00264 Wrench operator-(const Wrench& arg) 
00265 {
00266     return Wrench(-arg.force,-arg.torque);
00267 }
00268 
00269 Twist Frame::operator * (const Twist& arg) const
00270 // Complexity : 24M+18A
00271 {
00272     Twist tmp;
00273     tmp.rot = M*arg.rot;
00274     tmp.vel = M*arg.vel+p*tmp.rot;
00275     return tmp;
00276 }
00277 Twist Frame::Inverse(const Twist& arg) const
00278 {
00279     Twist tmp;
00280     tmp.rot =  M.Inverse(arg.rot);
00281     tmp.vel = M.Inverse(arg.vel-p*arg.rot);
00282     return tmp;
00283 }
00284 
00285 Twist Twist::Zero()
00286 {
00287     return Twist(Vector::Zero(),Vector::Zero());
00288 }
00289 
00290 
00291 void Twist::ReverseSign()
00292 {   
00293     vel.ReverseSign();
00294     rot.ReverseSign();
00295 }
00296 
00297 Twist Twist::RefPoint(const Vector& v_base_AB) const
00298      // Changes the reference point of the twist.
00299      // The vector v_base_AB is expressed in the same base as the twist
00300      // The vector v_base_AB is a vector from the old point to
00301      // the new point.
00302      // Complexity : 6M+6A
00303 {
00304     return Twist(this->vel+this->rot*v_base_AB,this->rot);
00305 }
00306 
00307 Twist& Twist::operator-=(const Twist& arg)
00308 {
00309     vel-=arg.vel;
00310     rot -=arg.rot;
00311     return *this;
00312 }
00313 
00314 Twist& Twist::operator+=(const Twist& arg)
00315 {
00316     vel+=arg.vel;
00317     rot +=arg.rot;
00318     return *this;
00319 }
00320 
00321 double& Twist::operator()(int i)
00322 {
00323     // assert((0<=i)&&(i<6)); done by underlying routines
00324     if (i<3) 
00325         return vel(i);
00326     else
00327         return rot(i-3);
00328 }
00329 
00330 double Twist::operator()(int i) const
00331 {
00332     // assert((0<=i)&&(i<6)); done by underlying routines
00333     if (i<3) 
00334         return vel(i);
00335     else
00336         return rot(i-3);
00337 }
00338 
00339 
00340 Twist operator*(const Twist& lhs,double rhs)
00341 {
00342     return Twist(lhs.vel*rhs,lhs.rot*rhs);
00343 }
00344 
00345 Twist operator*(double lhs,const Twist& rhs)
00346 {
00347     return Twist(lhs*rhs.vel,lhs*rhs.rot);
00348 }
00349 
00350 Twist operator/(const Twist& lhs,double rhs)
00351 {
00352     return Twist(lhs.vel/rhs,lhs.rot/rhs);
00353 }
00354 
00355 // addition of Twist's
00356 Twist operator+(const Twist& lhs,const Twist& rhs)
00357 {
00358     return Twist(lhs.vel+rhs.vel,lhs.rot+rhs.rot);
00359 }
00360 
00361 Twist operator-(const Twist& lhs,const Twist& rhs)
00362 {
00363     return Twist(lhs.vel-rhs.vel,lhs.rot-rhs.rot);
00364 }
00365 
00366 // unary -
00367 Twist operator-(const Twist& arg) 
00368 {
00369     return Twist(-arg.vel,-arg.rot);
00370 }
00371 
00372 
00373 //Spatial products for twists
00374 Twist operator*(const Twist& lhs,const Twist& rhs)
00375 {
00376     return Twist(lhs.rot*rhs.vel+lhs.vel*rhs.rot,lhs.rot*rhs.rot);
00377 }
00378 Wrench operator*(const Twist& lhs,const Wrench& rhs)
00379 {
00380     return Wrench(lhs.rot*rhs.force,lhs.rot*rhs.torque+lhs.vel*rhs.force);
00381 }
00382 
00383 Frame::Frame(const Rotation & R)
00384 {
00385     M=R;
00386     p=Vector::Zero();
00387 }
00388 
00389 Frame::Frame(const Vector & V)
00390 {
00391     M = Rotation::Identity();
00392     p = V;
00393 }
00394 
00395 Frame::Frame(const Rotation & R, const Vector & V)
00396 {
00397     M = R;
00398     p = V;
00399 }
00400 
00401  Frame operator *(const Frame& lhs,const Frame& rhs)
00402 // Complexity : 36M+36A
00403 {
00404     return Frame(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p);
00405 }
00406 
00407 Vector Frame::operator *(const Vector & arg) const
00408 {
00409     return M*arg+p;
00410 }
00411 
00412 Vector Frame::Inverse(const Vector& arg) const
00413 {
00414     return M.Inverse(arg-p);
00415 }
00416 
00417 Frame Frame::Inverse() const
00418 {
00419     return Frame(M.Inverse(),-M.Inverse(p));
00420 }
00421 
00422 
00423 Frame& Frame::operator =(const Frame & arg)
00424 { 
00425     M = arg.M;
00426     p = arg.p;
00427     return *this;
00428 }
00429 
00430 Frame::Frame(const Frame & arg) :
00431     p(arg.p),M(arg.M)
00432 {}
00433 
00434 
00435 void Vector::ReverseSign()
00436 {
00437     data[0] = -data[0];
00438     data[1] = -data[1];
00439     data[2] = -data[2];
00440 }
00441 
00442 
00443 
00444 Vector operator-(const Vector & arg)
00445 {
00446     Vector tmp;
00447     tmp.data[0]=-arg.data[0];
00448     tmp.data[1]=-arg.data[1];
00449     tmp.data[2]=-arg.data[2];
00450     return tmp;
00451 }
00452 
00453 void Vector::Set2DXY(const Vector2& v)
00454 // a 3D vector where the 2D vector v is put in the XY plane
00455 {
00456     data[0]=v(0);
00457     data[1]=v(1);
00458     data[2]=0;
00459 
00460 }
00461 void Vector::Set2DYZ(const Vector2& v)
00462 // a 3D vector where the 2D vector v is put in the YZ plane
00463 {
00464     data[1]=v(0);
00465     data[2]=v(1);
00466     data[0]=0;
00467 
00468 }
00469 
00470 void Vector::Set2DZX(const Vector2& v)
00471 // a 3D vector where the 2D vector v is put in the ZX plane
00472 {
00473     data[2]=v(0);
00474     data[0]=v(1);
00475     data[1]=0;
00476 
00477 }
00478 
00479 
00480 
00481 
00482 
00483 double& Rotation::operator()(int i,int j) {
00484     FRAMES_CHECKI((0<=i)&&(i<=2)&&(0<=j)&&(j<=2));
00485     return data[i*3+j];
00486 }
00487 
00488 double Rotation::operator()(int i,int j) const {
00489     FRAMES_CHECKI((0<=i)&&(i<=2)&&(0<=j)&&(j<=2));
00490     return data[i*3+j];
00491 }
00492 
00493 Rotation::Rotation( double Xx,double Yx,double Zx,
00494                             double Xy,double Yy,double Zy,
00495                             double Xz,double Yz,double Zz)
00496 {
00497     data[0] = Xx;data[1]=Yx;data[2]=Zx;
00498     data[3] = Xy;data[4]=Yy;data[5]=Zy;
00499     data[6] = Xz;data[7]=Yz;data[8]=Zz;
00500 }
00501 
00502 
00503 Rotation::Rotation(const Vector& x,const Vector& y,const Vector& z) 
00504 {
00505     data[0] = x.data[0];data[3] = x.data[1];data[6] = x.data[2];
00506     data[1] = y.data[0];data[4] = y.data[1];data[7] = y.data[2];
00507     data[2] = z.data[0];data[5] = z.data[1];data[8] = z.data[2];
00508 }
00509 
00510 Rotation& Rotation::operator=(const Rotation& arg) {
00511     int count=9;
00512     while (count--) data[count] = arg.data[count];
00513     return *this;
00514 }
00515 
00516 Vector Rotation::operator*(const Vector& v) const {
00517 // Complexity : 9M+6A
00518     return Vector(
00519      data[0]*v.data[0] + data[1]*v.data[1] + data[2]*v.data[2],
00520      data[3]*v.data[0] + data[4]*v.data[1] + data[5]*v.data[2],
00521      data[6]*v.data[0] + data[7]*v.data[1] + data[8]*v.data[2] 
00522     );
00523 }
00524 
00525 Twist Rotation::operator * (const Twist& arg) const
00526      // Transformation of the base to which the twist is expressed.
00527      // look at Frame*Twist for a transformation that also transforms
00528      // the velocity reference point.
00529      // Complexity : 18M+12A
00530 {
00531     return Twist((*this)*arg.vel,(*this)*arg.rot);
00532 }
00533 
00534 Wrench Rotation::operator * (const Wrench& arg) const
00535      // Transformation of the base to which the wrench is expressed.
00536      // look at Frame*Twist for a transformation that also transforms
00537      // the force reference point.
00538 {
00539     return Wrench((*this)*arg.force,(*this)*arg.torque);
00540 }
00541 
00542 Rotation Rotation::Identity() {
00543         return Rotation(1,0,0,0,1,0,0,0,1);
00544 }
00545 // *this = *this * ROT(X,angle)
00546 void Rotation::DoRotX(double angle)
00547 {
00548     double cs = cos(angle);
00549     double sn = sin(angle);
00550     double x1,x2,x3;
00551     x1  = cs* (*this)(0,1) + sn* (*this)(0,2);
00552     x2  = cs* (*this)(1,1) + sn* (*this)(1,2);
00553     x3  = cs* (*this)(2,1) + sn* (*this)(2,2);
00554     (*this)(0,2) = -sn* (*this)(0,1) + cs* (*this)(0,2);
00555     (*this)(1,2) = -sn* (*this)(1,1) + cs* (*this)(1,2);
00556     (*this)(2,2) = -sn* (*this)(2,1) + cs* (*this)(2,2);
00557     (*this)(0,1) = x1;
00558     (*this)(1,1) = x2;
00559     (*this)(2,1) = x3;
00560 }
00561 
00562 void Rotation::DoRotY(double angle)
00563 {
00564     double cs = cos(angle);
00565     double sn = sin(angle);
00566     double x1,x2,x3;
00567     x1  = cs* (*this)(0,0) - sn* (*this)(0,2);
00568     x2  = cs* (*this)(1,0) - sn* (*this)(1,2);
00569     x3  = cs* (*this)(2,0) - sn* (*this)(2,2);
00570     (*this)(0,2) = sn* (*this)(0,0) + cs* (*this)(0,2);
00571     (*this)(1,2) = sn* (*this)(1,0) + cs* (*this)(1,2);
00572     (*this)(2,2) = sn* (*this)(2,0) + cs* (*this)(2,2);
00573     (*this)(0,0) = x1;
00574     (*this)(1,0) = x2;
00575     (*this)(2,0) = x3;
00576 }
00577 
00578 void Rotation::DoRotZ(double angle)
00579 {
00580     double cs = cos(angle);
00581     double sn = sin(angle);
00582     double x1,x2,x3;
00583     x1  = cs* (*this)(0,0) + sn* (*this)(0,1);
00584     x2  = cs* (*this)(1,0) + sn* (*this)(1,1);
00585     x3  = cs* (*this)(2,0) + sn* (*this)(2,1);
00586     (*this)(0,1) = -sn* (*this)(0,0) + cs* (*this)(0,1);
00587     (*this)(1,1) = -sn* (*this)(1,0) + cs* (*this)(1,1);
00588     (*this)(2,1) = -sn* (*this)(2,0) + cs* (*this)(2,1);
00589     (*this)(0,0) = x1;
00590     (*this)(1,0) = x2;
00591     (*this)(2,0) = x3;
00592 }
00593 
00594 
00595 Rotation Rotation::RotX(double angle) {
00596     double cs=cos(angle);
00597     double sn=sin(angle);
00598     return Rotation(1,0,0,0,cs,-sn,0,sn,cs);
00599 }
00600 Rotation Rotation::RotY(double angle) {
00601     double cs=cos(angle);
00602     double sn=sin(angle);
00603     return Rotation(cs,0,sn,0,1,0,-sn,0,cs);
00604 }
00605 Rotation Rotation::RotZ(double angle) {
00606     double cs=cos(angle);
00607     double sn=sin(angle);
00608     return Rotation(cs,-sn,0,sn,cs,0,0,0,1);
00609 }
00610 
00611 
00612 
00613 
00614 void Frame::Integrate(const Twist& t_this,double samplefrequency)
00615 {
00616     double n = t_this.rot.Norm()/samplefrequency;
00617     if (n<epsilon) {
00618         p += M*(t_this.vel/samplefrequency);
00619     } else {
00620         (*this) = (*this) * 
00621             Frame ( Rotation::Rot( t_this.rot, n ),
00622                     t_this.vel/samplefrequency
00623                 );
00624     }
00625 }
00626 
00627 Rotation Rotation::Inverse() const
00628 {
00629     Rotation tmp(*this);
00630     tmp.SetInverse();
00631     return tmp;
00632 }
00633 
00634 Vector Rotation::Inverse(const Vector& v) const {
00635     return Vector(
00636      data[0]*v.data[0] + data[3]*v.data[1] + data[6]*v.data[2],
00637      data[1]*v.data[0] + data[4]*v.data[1] + data[7]*v.data[2],
00638      data[2]*v.data[0] + data[5]*v.data[1] + data[8]*v.data[2] 
00639     );
00640 }
00641 
00642 
00643 void Rotation::SetInverse()
00644 {
00645     double tmp;
00646     tmp = data[1];data[1]=data[3];data[3]=tmp;
00647     tmp = data[2];data[2]=data[6];data[6]=tmp;
00648     tmp = data[5];data[5]=data[7];data[7]=tmp;
00649 }
00650 
00651 
00652 
00653 
00654 
00655 
00656 
00657 double Frame::operator()(int i,int j) {
00658     FRAMES_CHECKI((0<=i)&&(i<=3)&&(0<=j)&&(j<=3));
00659     if (i==3) {
00660         if (j==3)
00661             return 1.0;
00662         else
00663             return 0.0;
00664     } else {
00665         if (j==3) 
00666             return p(i);
00667         else
00668             return M(i,j);
00669 
00670     }
00671 }
00672 
00673 double Frame::operator()(int i,int j) const {
00674     FRAMES_CHECKI((0<=i)&&(i<=3)&&(0<=j)&&(j<=3));
00675         if (i==3) {
00676         if (j==3)
00677             return 1;
00678         else
00679             return 0;
00680     } else {
00681         if (j==3) 
00682             return p(i);
00683         else
00684             return M(i,j);
00685 
00686     }
00687 }
00688 
00689 
00690 Frame Frame::Identity() {
00691     return Frame(Rotation::Identity(),Vector::Zero());
00692 }
00693 
00694 
00695 
00696 
00697 void Vector::Set2DPlane(const Frame& F_someframe_XY,const Vector2& v_XY)
00698 // a 3D vector where the 2D vector v is put in the XY plane of the frame
00699 // F_someframe_XY.
00700 {
00701 Vector tmp_XY;
00702 tmp_XY.Set2DXY(v_XY);
00703 tmp_XY = F_someframe_XY*(tmp_XY);
00704 }
00705 
00706 
00707 
00708 
00709 
00710 
00711 
00712 
00713 
00714 //============ 2 dimensional version of the frames objects =============
00715 IMETHOD Vector2::Vector2(const Vector2 & arg)
00716 {
00717     data[0] = arg.data[0];
00718     data[1] = arg.data[1];
00719 }
00720 
00721 IMETHOD Vector2::Vector2(double x,double y)
00722 {
00723         data[0]=x;data[1]=y;
00724 }
00725 
00726 
00727 IMETHOD Vector2& Vector2::operator =(const Vector2 & arg)
00728 {
00729     data[0] = arg.data[0];
00730     data[1] = arg.data[1];
00731     return *this;
00732 }
00733 
00734 
00735 IMETHOD Vector2 operator +(const Vector2 & lhs,const Vector2& rhs)
00736 {
00737     return Vector2(lhs.data[0]+rhs.data[0],lhs.data[1]+rhs.data[1]);
00738 }
00739 
00740 IMETHOD Vector2 operator -(const Vector2 & lhs,const Vector2& rhs)
00741 {
00742     return Vector2(lhs.data[0]-rhs.data[0],lhs.data[1]-rhs.data[1]);
00743 }
00744 
00745 IMETHOD Vector2 operator *(const Vector2& lhs,double rhs) 
00746 {
00747     return Vector2(lhs.data[0]*rhs,lhs.data[1]*rhs);
00748 }
00749 
00750 IMETHOD Vector2 operator *(double lhs,const Vector2& rhs) 
00751 {
00752     return Vector2(lhs*rhs.data[0],lhs*rhs.data[1]);
00753 }
00754 
00755 IMETHOD Vector2 operator /(const Vector2& lhs,double rhs) 
00756 {
00757     return Vector2(lhs.data[0]/rhs,lhs.data[1]/rhs);
00758 }
00759 
00760 IMETHOD Vector2& Vector2::operator +=(const Vector2 & arg)
00761 {
00762     data[0]+=arg.data[0];
00763     data[1]+=arg.data[1];
00764     return *this;
00765 }
00766 
00767 IMETHOD Vector2& Vector2::operator -=(const Vector2 & arg)
00768 {
00769     data[0]-=arg.data[0];
00770     data[1]-=arg.data[1];
00771     return *this;
00772 }
00773 
00774 IMETHOD Vector2 Vector2::Zero() {
00775     return Vector2(0,0);
00776 }
00777 
00778 IMETHOD double Vector2::operator()(int index) const {
00779     FRAMES_CHECKI((0<=index)&&(index<=1));
00780     return data[index];
00781 }
00782 
00783 IMETHOD double& Vector2::operator () (int index)
00784 {
00785     FRAMES_CHECKI((0<=index)&&(index<=1));
00786     return data[index];
00787 }
00788 
00789 IMETHOD double Vector2::x() const { return data[0]; }
00790 IMETHOD double Vector2::y() const { return data[1]; }
00791 
00792 IMETHOD void Vector2::x( double _x ) { data[0] = _x; }
00793 IMETHOD void Vector2::y( double _y ) { data[1] = _y; }
00794 
00795 
00796 IMETHOD void Vector2::ReverseSign()
00797 {
00798     data[0] = -data[0];
00799     data[1] = -data[1];
00800 }
00801 
00802 
00803 IMETHOD Vector2 operator-(const Vector2 & arg)
00804 {
00805     return Vector2(-arg.data[0],-arg.data[1]);
00806 }
00807 
00808 
00809 IMETHOD void Vector2::Set3DXY(const Vector& v)
00810 // projects v in its XY plane, and sets *this to these values
00811 {
00812     data[0]=v(0);
00813     data[1]=v(1);
00814 }
00815 IMETHOD void Vector2::Set3DYZ(const Vector& v)
00816 // projects v in its XY plane, and sets *this to these values
00817 {
00818     data[0]=v(1);
00819     data[1]=v(2);
00820 }
00821 IMETHOD void Vector2::Set3DZX(const Vector& v)
00822 // projects v in its XY plane, and and sets *this to these values
00823 {
00824     data[0]=v(2);
00825     data[1]=v(0);
00826 }
00827 
00828 IMETHOD void Vector2::Set3DPlane(const Frame& F_someframe_XY,const Vector& v_someframe) 
00829 // projects v in the XY plane of F_someframe_XY, and sets *this to these values
00830 // expressed wrt someframe.
00831 {
00832     Vector tmp = F_someframe_XY.Inverse(v_someframe);
00833     data[0]=tmp(0);
00834     data[1]=tmp(1);
00835 }
00836 
00837 
00838 
00839 IMETHOD Rotation2& Rotation2::operator=(const Rotation2& arg) {
00840     c=arg.c;s=arg.s;
00841     return *this;
00842 }
00843 
00844 IMETHOD Vector2 Rotation2::operator*(const Vector2& v) const {
00845     return Vector2(v.data[0]*c-v.data[1]*s,v.data[0]*s+v.data[1]*c);
00846 }
00847 
00848 IMETHOD double Rotation2::operator()(int i,int j) const {
00849     FRAMES_CHECKI((0<=i)&&(i<=1)&&(0<=j)&&(j<=1));
00850     if (i==j) return c;
00851     if (i==0) 
00852         return s;
00853     else
00854         return -s;
00855 }
00856 
00857 
00858 IMETHOD Rotation2 operator *(const Rotation2& lhs,const Rotation2& rhs) {
00859     return Rotation2(lhs.c*rhs.c-lhs.s*rhs.s,lhs.s*rhs.c+lhs.c*rhs.s);
00860 }
00861 
00862 IMETHOD void Rotation2::SetInverse() {
00863     s=-s;
00864 }
00865 
00866 IMETHOD Rotation2 Rotation2::Inverse() const {
00867     return Rotation2(c,-s);
00868 }
00869 
00870 IMETHOD Vector2 Rotation2::Inverse(const Vector2& v) const {
00871     return Vector2(v.data[0]*c+v.data[1]*s,-v.data[0]*s+v.data[1]*c);
00872 }
00873 
00874 IMETHOD Rotation2 Rotation2::Identity() {
00875     return Rotation2(1,0);
00876 }
00877      
00878 IMETHOD void Rotation2::SetIdentity()
00879 {
00880         c = 1;
00881         s = 0;
00882 }
00883 
00884 IMETHOD void Rotation2::SetRot(double angle) {
00885     c=cos(angle);s=sin(angle);
00886 }
00887 
00888 IMETHOD Rotation2 Rotation2::Rot(double angle) {
00889     return Rotation2(cos(angle),sin(angle));
00890 }
00891 
00892 IMETHOD double Rotation2::GetRot() const {
00893     return atan2(s,c);
00894 }
00895 
00896 
00897 IMETHOD Frame2::Frame2() {
00898 }
00899 
00900 IMETHOD Frame2::Frame2(const Rotation2 & R)
00901 {
00902     M=R;
00903     p=Vector2::Zero();
00904 }
00905 
00906 IMETHOD Frame2::Frame2(const Vector2 & V)
00907 {
00908     M = Rotation2::Identity();
00909     p = V;
00910 }
00911 
00912 IMETHOD Frame2::Frame2(const Rotation2 & R, const Vector2 & V)
00913 {
00914     M = R;
00915     p = V;
00916 }
00917 
00918 IMETHOD Frame2 operator *(const Frame2& lhs,const Frame2& rhs)
00919 {
00920     return Frame2(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p);
00921 }
00922 
00923 IMETHOD Vector2 Frame2::operator *(const Vector2 & arg)
00924 {
00925     return M*arg+p;
00926 }
00927 
00928 IMETHOD Vector2 Frame2::Inverse(const Vector2& arg) const
00929 {
00930     return M.Inverse(arg-p);
00931 }
00932 
00933 IMETHOD void Frame2::SetIdentity()
00934 {
00935         M.SetIdentity();
00936         p = Vector2::Zero();
00937 }
00938 
00939 IMETHOD void Frame2::SetInverse()
00940 {
00941     M.SetInverse();
00942     p = M*p;
00943     p.ReverseSign();
00944 }
00945 
00946 
00947 IMETHOD Frame2 Frame2::Inverse() const
00948 {
00949     Frame2 tmp(*this);
00950     tmp.SetInverse();
00951     return tmp;
00952 }
00953 
00954 IMETHOD Frame2& Frame2::operator =(const Frame2 & arg)
00955 { 
00956     M = arg.M;
00957     p = arg.p;
00958     return *this;
00959 }
00960 
00961 IMETHOD Frame2::Frame2(const Frame2 & arg) :
00962     p(arg.p), M(arg.M)
00963 {}
00964 
00965 
00966 IMETHOD double Frame2::operator()(int i,int j) {
00967     FRAMES_CHECKI((0<=i)&&(i<=2)&&(0<=j)&&(j<=2));
00968     if (i==2) {
00969         if (j==2)
00970             return 1;
00971         else
00972             return 0;
00973     } else {
00974         if (j==2) 
00975             return p(i);
00976         else
00977             return M(i,j);
00978 
00979     }
00980 }
00981 
00982 IMETHOD double Frame2::operator()(int i,int j) const {
00983     FRAMES_CHECKI((0<=i)&&(i<=2)&&(0<=j)&&(j<=2));
00984     if (i==2) {
00985         if (j==2)
00986             return 1;
00987         else
00988             return 0;
00989     } else {
00990         if (j==2) 
00991             return p(i);
00992         else
00993             return M(i,j);
00994 
00995     }
00996 }
00997 
00998 // Scalar products.
00999 
01000 IMETHOD double dot(const Vector& lhs,const Vector& rhs) {
01001     return rhs(0)*lhs(0)+rhs(1)*lhs(1)+rhs(2)*lhs(2);
01002 }
01003 
01004 IMETHOD double dot(const Twist& lhs,const Wrench& rhs) {
01005     return dot(lhs.vel,rhs.force)+dot(lhs.rot,rhs.torque);
01006 }
01007 
01008 IMETHOD double dot(const Wrench& rhs,const Twist& lhs) {
01009     return dot(lhs.vel,rhs.force)+dot(lhs.rot,rhs.torque);
01010 }
01011 
01012 
01013 
01014 
01015 
01016 // Equality operators
01017 
01018 
01019 
01020 IMETHOD bool Equal(const Vector& a,const Vector& b,double eps) {
01021         return (Equal(a.data[0],b.data[0],eps)&&
01022                 Equal(a.data[1],b.data[1],eps)&&
01023                 Equal(a.data[2],b.data[2],eps)   );
01024      }
01025      
01026 
01027 IMETHOD bool Equal(const Frame& a,const Frame& b,double eps) {
01028         return (Equal(a.p,b.p,eps)&&
01029                 Equal(a.M,b.M,eps)   );
01030 }
01031 
01032 IMETHOD bool Equal(const Wrench& a,const Wrench& b,double eps) {
01033         return (Equal(a.force,b.force,eps)&&
01034                 Equal(a.torque,b.torque,eps)  );
01035 }
01036 
01037 IMETHOD bool Equal(const Twist& a,const Twist& b,double eps) {
01038         return (Equal(a.rot,b.rot,eps)&&
01039                 Equal(a.vel,b.vel,eps)  );
01040 }
01041 
01042 IMETHOD bool Equal(const Vector2& a,const Vector2& b,double eps) {
01043         return (Equal(a.data[0],b.data[0],eps)&&
01044                 Equal(a.data[1],b.data[1],eps)   );
01045      }
01046      
01047 IMETHOD bool Equal(const Rotation2& a,const Rotation2& b,double eps) {
01048     return ( Equal(a.c,b.c,eps) && Equal(a.s,b.s,eps) );
01049 }
01050 
01051 IMETHOD bool Equal(const Frame2& a,const Frame2& b,double eps) {
01052         return (Equal(a.p,b.p,eps)&&
01053                 Equal(a.M,b.M,eps)   );
01054 }
01055 
01056 IMETHOD void SetToZero(Vector& v) {
01057     v=Vector::Zero();
01058 }
01059 IMETHOD void SetToZero(Twist& v) {
01060     SetToZero(v.rot);
01061     SetToZero(v.vel);
01062 }
01063 IMETHOD void SetToZero(Wrench& v) {
01064     SetToZero(v.force);
01065     SetToZero(v.torque);
01066 }
01067 
01068 IMETHOD void SetToZero(Vector2& v) {
01069     v = Vector2::Zero();
01070 }
01071 
01072 
01074 // The following defines the operations
01075 //   diff
01076 //   addDelta
01077 //   random
01078 //   posrandom
01079 // on all the types defined in this library.
01080 // (mostly for uniform integration, differentiation and testing).
01081 // Defined as functions because double is not a class and a method
01082 // would brake uniformity when defined for a double.
01084 
01085 
01086 
01087 
01088 
01089 
01095 IMETHOD Rotation Rot(const Vector& axis_a_b) {
01096     // The formula is 
01097     // V.(V.tr) + st*[V x] + ct*(I-V.(V.tr))
01098     // can be found by multiplying it with an arbitrary vector p
01099     // and noting that this vector is rotated.
01100         Vector rotvec = axis_a_b;
01101         double angle = rotvec.Normalize(1E-10);
01102     double ct = ::cos(angle);
01103     double st = ::sin(angle);
01104     double vt = 1-ct;
01105     return Rotation(
01106         ct            +  vt*rotvec(0)*rotvec(0), 
01107         -rotvec(2)*st +  vt*rotvec(0)*rotvec(1), 
01108         rotvec(1)*st  +  vt*rotvec(0)*rotvec(2),
01109         rotvec(2)*st  +  vt*rotvec(1)*rotvec(0),
01110         ct            +  vt*rotvec(1)*rotvec(1),
01111         -rotvec(0)*st +  vt*rotvec(1)*rotvec(2),
01112         -rotvec(1)*st +  vt*rotvec(2)*rotvec(0),
01113         rotvec(0)*st  +  vt*rotvec(2)*rotvec(1),
01114         ct            +  vt*rotvec(2)*rotvec(2)
01115         );
01116     }
01117 
01118 IMETHOD Vector diff(const Vector& a,const Vector& b,double dt) {
01119         return (b-a)/dt;
01120 }
01121 
01154 IMETHOD Vector diff(const Rotation& R_a_b1,const Rotation& R_a_b2,double dt) {
01155         Rotation R_b1_b2(R_a_b1.Inverse()*R_a_b2);
01156         return R_a_b1 * R_b1_b2.GetRot() / dt;
01157 }
01158 IMETHOD Twist diff(const Frame& F_a_b1,const Frame& F_a_b2,double dt) {
01159         return Twist(
01160                         diff(F_a_b1.p,F_a_b2.p,dt),
01161                         diff(F_a_b1.M,F_a_b2.M,dt)
01162                         );
01163 }
01164 IMETHOD Twist diff(const Twist& a,const Twist& b,double dt) {
01165         return Twist(diff(a.vel,b.vel,dt),diff(a.rot,b.rot,dt));
01166 }
01167 
01168 IMETHOD Wrench diff(const Wrench& a,const Wrench& b,double dt) {
01169         return Wrench(
01170                         diff(a.force,b.force,dt),
01171                         diff(a.torque,b.torque,dt)
01172                         );
01173 }
01174 
01175 
01176 IMETHOD Vector addDelta(const Vector& a,const Vector&da,double dt) {
01177         return a+da*dt;
01178 }
01179 
01180 IMETHOD Rotation addDelta(const Rotation& a,const Vector&da,double dt) {
01181         return a*Rot(a.Inverse(da)*dt);
01182 }
01183 IMETHOD Frame addDelta(const Frame& a,const Twist& da,double dt) {
01184         return Frame(
01185                         addDelta(a.M,da.rot,dt),
01186                         addDelta(a.p,da.vel,dt)
01187                    );
01188 }
01189 IMETHOD Twist addDelta(const Twist& a,const Twist&da,double dt) {
01190         return Twist(addDelta(a.vel,da.vel,dt),addDelta(a.rot,da.rot,dt));
01191 }
01192 IMETHOD Wrench addDelta(const Wrench& a,const Wrench&da,double dt) {
01193         return Wrench(addDelta(a.force,da.force,dt),addDelta(a.torque,da.torque,dt));
01194 }
01195 
01196 
01197 /* Commented code section
01198  * \brief addDelta operator for displacement rotational velocity.
01199  *
01200  * The Vector arguments here represent a displacement rotational velocity.  i.e. a rotation
01201  * around a fixed axis for a certain angle.  For this representation you cannot use diff() but
01202  * have to use diff_displ().
01203  *
01204  * \param a  : displacement rotational velocity
01205  * \param da : rotational velocity 
01206  * \return   displacement rotational velocity
01207  *
01208  * \warning do not confuse displacement rotational velocities and velocities
01209  * \warning do not confuse displacement twist and twist.
01210  *
01211 IMETHOD Vector addDelta_displ(const Vector& a,const Vector&da,double dt) {
01212     return getRot(addDelta(Rot(a),da,dt)); 
01213 }*/
01214 
01215 /* Commented code section
01216  * \brief addDelta operator for displacement twist.
01217  *
01218  * The Vector arguments here represent a displacement rotational velocity.  i.e. a rotation
01219  * around a fixed axis for a certain angle.  For this representation you cannot use diff() but
01220  * have to use diff_displ().
01221  *
01222  * \param a  : displacement twist 
01223  * \param da : twist 
01224  * \return   displacement twist 
01225  *
01226  * \warning do not confuse displacement rotational velocities and velocities
01227  * \warning do not confuse displacement twist and twist.
01228  *
01229 IMETHOD Twist addDelta_displ(const Twist& a,const Twist&da,double dt) {
01230     return Twist(addDelta(a.vel,da.vel,dt),addDelta_displ(a.rot,da.rot,dt)); 
01231 }*/
01232 
01233 
01234 IMETHOD void random(Vector& a) {
01235         random(a[0]);
01236         random(a[1]);
01237         random(a[2]);
01238 }
01239 IMETHOD void random(Twist& a) {
01240         random(a.rot);
01241         random(a.vel);
01242 }
01243 IMETHOD void random(Wrench& a) {
01244         random(a.torque);
01245         random(a.force);
01246 }
01247 
01248 IMETHOD void random(Rotation& R) {
01249         double alfa;
01250         double beta;
01251         double gamma;
01252         random(alfa);
01253         random(beta);
01254         random(gamma);
01255         R = Rotation::EulerZYX(alfa,beta,gamma);
01256 }
01257 
01258 IMETHOD void random(Frame& F) {
01259         random(F.M);
01260         random(F.p);
01261 }
01262 
01263 IMETHOD void posrandom(Vector& a) {
01264         posrandom(a[0]);
01265         posrandom(a[1]);
01266         posrandom(a[2]);
01267 }
01268 IMETHOD void posrandom(Twist& a) {
01269         posrandom(a.rot);
01270         posrandom(a.vel);
01271 }
01272 IMETHOD void posrandom(Wrench& a) {
01273         posrandom(a.torque);
01274         posrandom(a.force);
01275 }
01276 
01277 IMETHOD void posrandom(Rotation& R) {
01278         double alfa;
01279         double beta;
01280         double gamma;
01281         posrandom(alfa);
01282         posrandom(beta);
01283         posrandom(gamma);
01284         R = Rotation::EulerZYX(alfa,beta,gamma);
01285 }
01286 
01287 IMETHOD void posrandom(Frame& F) {
01288         random(F.M);
01289         random(F.p);
01290 }
01291 
01292 
01293 
01294 
01295 IMETHOD bool operator==(const Frame& a,const Frame& b ) {
01296 #ifdef KDL_USE_EQUAL
01297     return Equal(a,b);
01298 #else
01299         return (a.p == b.p &&
01300                 a.M == b.M );
01301 #endif
01302 }
01303 
01304 IMETHOD bool operator!=(const Frame& a,const Frame& b) {
01305     return !operator==(a,b);
01306 }
01307 
01308 IMETHOD bool operator==(const Vector& a,const Vector& b) {
01309 #ifdef KDL_USE_EQUAL
01310     return Equal(a,b);
01311 #else
01312         return (a.data[0]==b.data[0]&&
01313                 a.data[1]==b.data[1]&&
01314                 a.data[2]==b.data[2] );
01315 #endif
01316      }
01317 
01318 IMETHOD bool operator!=(const Vector& a,const Vector& b) {
01319     return !operator==(a,b);
01320 }
01321 
01322 IMETHOD bool operator==(const Twist& a,const Twist& b) {
01323 #ifdef KDL_USE_EQUAL
01324     return Equal(a,b);
01325 #else
01326         return (a.rot==b.rot &&
01327                 a.vel==b.vel  );
01328 #endif
01329 }
01330 
01331 IMETHOD bool operator!=(const Twist& a,const Twist& b) {
01332     return !operator==(a,b);
01333 }
01334 
01335 IMETHOD bool operator==(const Wrench& a,const Wrench& b ) {
01336 #ifdef KDL_USE_EQUAL
01337     return Equal(a,b);
01338 #else
01339     return (a.force==b.force &&
01340             a.torque==b.torque );
01341 #endif
01342 }
01343 
01344 IMETHOD bool operator!=(const Wrench& a,const Wrench& b) {
01345     return !operator==(a,b);
01346 }
01347 IMETHOD bool operator!=(const Rotation& a,const Rotation& b) {
01348     return !operator==(a,b);
01349 }
01350 
01351 IMETHOD bool operator==(const Vector2& a,const Vector2& b) {
01352 #ifdef KDL_USE_EQUAL
01353     return Equal(a,b);
01354 #else
01355         return (a.data[0]==b.data[0]&&
01356                 a.data[1]==b.data[1] );
01357 #endif
01358      }
01359 
01360 IMETHOD bool operator!=(const Vector2& a,const Vector2& b) {
01361     return !operator==(a,b);
01362 }
01363 
01364 } // namespace KDL
01365 
01366 

Generated on Wed Nov 23 19:00:14 2011 for FreeCAD by  doxygen 1.6.1