frameacc.inl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*****************************************************************************
  2. * \file
  3. * provides inline functions of rrframes.h
  4. *
  5. * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven
  6. *
  7. * \version
  8. * ORO_Geometry V0.2
  9. *
  10. * \par History
  11. * - $log$
  12. *
  13. * \par Release
  14. * $Name: $
  15. ****************************************************************************/
  16. /////////////////// VectorAcc /////////////////////////////////////
  17. VectorAcc operator + (const VectorAcc& r1,const VectorAcc& r2) {
  18. return VectorAcc(r1.p+r2.p,r1.v+r2.v,r1.dv+r2.dv);
  19. }
  20. VectorAcc operator - (const VectorAcc& r1,const VectorAcc& r2) {
  21. return VectorAcc(r1.p-r2.p, r1.v-r2.v, r1.dv-r2.dv);
  22. }
  23. VectorAcc operator + (const Vector& r1,const VectorAcc& r2) {
  24. return VectorAcc(r1+r2.p,r2.v,r2.dv);
  25. }
  26. VectorAcc operator - (const Vector& r1,const VectorAcc& r2) {
  27. return VectorAcc(r1-r2.p, -r2.v, -r2.dv);
  28. }
  29. VectorAcc operator + (const VectorAcc& r1,const Vector& r2) {
  30. return VectorAcc(r1.p+r2,r1.v,r1.dv);
  31. }
  32. VectorAcc operator - (const VectorAcc& r1,const Vector& r2) {
  33. return VectorAcc(r1.p-r2, r1.v, r1.dv);
  34. }
  35. // unary -
  36. VectorAcc operator - (const VectorAcc& r) {
  37. return VectorAcc(-r.p,-r.v,-r.dv);
  38. }
  39. // cross prod.
  40. VectorAcc operator * (const VectorAcc& r1,const VectorAcc& r2) {
  41. return VectorAcc(r1.p*r2.p,
  42. r1.p*r2.v+r1.v*r2.p,
  43. r1.dv*r2.p+2*r1.v*r2.v+r1.p*r2.dv
  44. );
  45. }
  46. VectorAcc operator * (const VectorAcc& r1,const Vector& r2) {
  47. return VectorAcc(r1.p*r2, r1.v*r2, r1.dv*r2 );
  48. }
  49. VectorAcc operator * (const Vector& r1,const VectorAcc& r2) {
  50. return VectorAcc(r1*r2.p, r1*r2.v, r1*r2.dv );
  51. }
  52. // scalar mult.
  53. VectorAcc operator * (double r1,const VectorAcc& r2) {
  54. return VectorAcc(r1*r2.p, r1*r2.v, r1*r2.dv );
  55. }
  56. VectorAcc operator * (const VectorAcc& r1,double r2) {
  57. return VectorAcc(r1.p*r2, r1.v*r2, r1.dv*r2 );
  58. }
  59. VectorAcc operator * (const doubleAcc& r1,const VectorAcc& r2) {
  60. return VectorAcc(r1.t*r2.p,
  61. r1.t*r2.v + r1.d*r2.p,
  62. r1.t*r2.dv + 2*r1.d*r2.v + r1.dd*r2.p
  63. );
  64. }
  65. VectorAcc operator * (const VectorAcc& r2,const doubleAcc& r1) {
  66. return VectorAcc(r1.t*r2.p,
  67. r1.t*r2.v + r1.d*r2.p,
  68. r1.t*r2.dv + 2*r1.d*r2.v + r1.dd*r2.p
  69. );
  70. }
  71. VectorAcc& VectorAcc::operator = (const VectorAcc& arg) {
  72. p=arg.p;
  73. v=arg.v;
  74. dv=arg.dv;
  75. return *this;
  76. }
  77. VectorAcc& VectorAcc::operator = (const Vector& arg) {
  78. p=arg;
  79. v=Vector::Zero();
  80. dv=Vector::Zero();
  81. return *this;
  82. }
  83. VectorAcc& VectorAcc::operator += (const VectorAcc& arg) {
  84. p+=arg.p;
  85. v+=arg.v;
  86. dv+= arg.dv;
  87. return *this;
  88. }
  89. VectorAcc& VectorAcc::operator -= (const VectorAcc& arg) {
  90. p-=arg.p;
  91. v-=arg.v;
  92. dv-=arg.dv;
  93. return *this;
  94. }
  95. VectorAcc VectorAcc::Zero() {
  96. return VectorAcc(Vector::Zero(),Vector::Zero(),Vector::Zero());
  97. }
  98. void VectorAcc::ReverseSign() {
  99. p.ReverseSign();
  100. v.ReverseSign();
  101. dv.ReverseSign();
  102. }
  103. doubleAcc VectorAcc::Norm() {
  104. doubleAcc res;
  105. res.t = p.Norm();
  106. res.d = dot(p,v)/res.t;
  107. res.dd = (dot(p,dv)+dot(v,v)-res.d*res.d)/res.t;
  108. return res;
  109. }
  110. doubleAcc dot(const VectorAcc& lhs,const VectorAcc& rhs) {
  111. return doubleAcc( dot(lhs.p,rhs.p),
  112. dot(lhs.p,rhs.v)+dot(lhs.v,rhs.p),
  113. dot(lhs.p,rhs.dv)+2*dot(lhs.v,rhs.v)+dot(lhs.dv,rhs.p)
  114. );
  115. }
  116. doubleAcc dot(const VectorAcc& lhs,const Vector& rhs) {
  117. return doubleAcc( dot(lhs.p,rhs),
  118. dot(lhs.v,rhs),
  119. dot(lhs.dv,rhs)
  120. );
  121. }
  122. doubleAcc dot(const Vector& lhs,const VectorAcc& rhs) {
  123. return doubleAcc( dot(lhs,rhs.p),
  124. dot(lhs,rhs.v),
  125. dot(lhs,rhs.dv)
  126. );
  127. }
  128. bool Equal(const VectorAcc& r1,const VectorAcc& r2,double eps) {
  129. return (Equal(r1.p,r2.p,eps)
  130. && Equal(r1.v,r2.v,eps)
  131. && Equal(r1.dv,r2.dv,eps)
  132. );
  133. }
  134. bool Equal(const Vector& r1,const VectorAcc& r2,double eps) {
  135. return (Equal(r1,r2.p,eps)
  136. && Equal(Vector::Zero(),r2.v,eps)
  137. && Equal(Vector::Zero(),r2.dv,eps)
  138. );
  139. }
  140. bool Equal(const VectorAcc& r1,const Vector& r2,double eps) {
  141. return (Equal(r1.p,r2,eps)
  142. && Equal(r1.v,Vector::Zero(),eps)
  143. && Equal(r1.dv,Vector::Zero(),eps)
  144. );
  145. }
  146. VectorAcc operator / (const VectorAcc& r1,double r2) {
  147. return r1*(1.0/r2);
  148. }
  149. VectorAcc operator / (const VectorAcc& r2,const doubleAcc& r1) {
  150. return r2*(1.0/r1);
  151. }
  152. /////////////////// RotationAcc /////////////////////////////////////
  153. RotationAcc operator* (const RotationAcc& r1,const RotationAcc& r2) {
  154. return RotationAcc( r1.R * r2.R,
  155. r1.w + r1.R*r2.w,
  156. r1.dw + r1.w*(r1.R*r2.w) + r1.R*r2.dw
  157. );
  158. }
  159. RotationAcc operator* (const Rotation& r1,const RotationAcc& r2) {
  160. return RotationAcc( r1*r2.R, r1*r2.w, r1*r2.dw);
  161. }
  162. RotationAcc operator* (const RotationAcc& r1,const Rotation& r2) {
  163. return RotationAcc( r1.R*r2, r1.w, r1.dw );
  164. }
  165. RotationAcc& RotationAcc::operator = (const RotationAcc& arg) {
  166. R=arg.R;
  167. w=arg.w;
  168. dw=arg.dw;
  169. return *this;
  170. }
  171. RotationAcc& RotationAcc::operator = (const Rotation& arg) {
  172. R = arg;
  173. w = Vector::Zero();
  174. dw = Vector::Zero();
  175. return *this;
  176. }
  177. RotationAcc RotationAcc::Identity() {
  178. return RotationAcc(Rotation::Identity(),Vector::Zero(),Vector::Zero());
  179. }
  180. RotationAcc RotationAcc::Inverse() const {
  181. return RotationAcc(R.Inverse(),-R.Inverse(w),-R.Inverse(dw));
  182. }
  183. VectorAcc RotationAcc::Inverse(const VectorAcc& arg) const {
  184. VectorAcc tmp;
  185. tmp.p = R.Inverse(arg.p);
  186. tmp.v = R.Inverse(arg.v - w * arg.p);
  187. tmp.dv = R.Inverse(arg.dv - dw*arg.p - w*(arg.v+R*tmp.v));
  188. return tmp;
  189. }
  190. VectorAcc RotationAcc::Inverse(const Vector& arg) const {
  191. VectorAcc tmp;
  192. tmp.p = R.Inverse(arg);
  193. tmp.v = R.Inverse(-w*arg);
  194. tmp.dv = R.Inverse(-dw*arg - w*(R*tmp.v));
  195. return tmp;
  196. }
  197. VectorAcc RotationAcc::operator*(const VectorAcc& arg) const {
  198. VectorAcc tmp;
  199. tmp.p = R*arg.p;
  200. tmp.dv = R*arg.v;
  201. tmp.v = w*tmp.p + tmp.dv;
  202. tmp.dv = dw*tmp.p + w*(tmp.v + tmp.dv) + R*arg.dv;
  203. return tmp;
  204. }
  205. VectorAcc operator*(const Rotation& R,const VectorAcc& x) {
  206. return VectorAcc(R*x.p,R*x.v,R*x.dv);
  207. }
  208. VectorAcc RotationAcc::operator*(const Vector& arg) const {
  209. VectorAcc tmp;
  210. tmp.p = R*arg;
  211. tmp.v = w*tmp.p;
  212. tmp.dv = dw*tmp.p + w*tmp.v;
  213. return tmp;
  214. }
  215. /*
  216. // = Rotations
  217. // The Rot... static functions give the value of the appropriate rotation matrix back.
  218. // The DoRot... functions apply a rotation R to *this,such that *this = *this * R.
  219. void RRotation::DoRotX(const RDouble& angle) {
  220. w+=R*Vector(angle.grad,0,0);
  221. R.DoRotX(angle.t);
  222. }
  223. RotationAcc RotationAcc::RotX(const doubleAcc& angle) {
  224. return RotationAcc(Rotation::RotX(angle.t),
  225. Vector(angle.d,0,0),
  226. Vector(angle.dd,0,0)
  227. );
  228. }
  229. void RRotation::DoRotY(const RDouble& angle) {
  230. w+=R*Vector(0,angle.grad,0);
  231. R.DoRotY(angle.t);
  232. }
  233. RotationAcc RotationAcc::RotY(const doubleAcc& angle) {
  234. return RotationAcc(
  235. Rotation::RotX(angle.t),
  236. Vector(0,angle.d,0),
  237. Vector(0,angle.dd,0)
  238. );
  239. }
  240. void RRotation::DoRotZ(const RDouble& angle) {
  241. w+=R*Vector(0,0,angle.grad);
  242. R.DoRotZ(angle.t);
  243. }
  244. RotationAcc RotationAcc::RotZ(const doubleAcc& angle) {
  245. return RotationAcc(
  246. Rotation::RotZ(angle.t),
  247. Vector(0,0,angle.d),
  248. Vector(0,0,angle.dd)
  249. );
  250. }
  251. RRotation RRotation::Rot(const Vector& rotvec,const RDouble& angle)
  252. // rotvec has arbitrary norm
  253. // rotation around a constant vector !
  254. {
  255. Vector v = rotvec.Normalize();
  256. return RRotation(Rotation::Rot2(v,angle.t),v*angle.grad);
  257. }
  258. RRotation RRotation::Rot2(const Vector& rotvec,const RDouble& angle)
  259. // rotvec is normalized.
  260. {
  261. return RRotation(Rotation::Rot2(rotvec,angle.t),rotvec*angle.grad);
  262. }
  263. */
  264. bool Equal(const RotationAcc& r1,const RotationAcc& r2,double eps) {
  265. return (Equal(r1.w,r2.w,eps) && Equal(r1.R,r2.R,eps) && Equal(r1.dw,r2.dw,eps) );
  266. }
  267. bool Equal(const Rotation& r1,const RotationAcc& r2,double eps) {
  268. return (Equal(Vector::Zero(),r2.w,eps) && Equal(r1,r2.R,eps) &&
  269. Equal(Vector::Zero(),r2.dw,eps) );
  270. }
  271. bool Equal(const RotationAcc& r1,const Rotation& r2,double eps) {
  272. return (Equal(r1.w,Vector::Zero(),eps) && Equal(r1.R,r2,eps) &&
  273. Equal(r1.dw,Vector::Zero(),eps) );
  274. }
  275. // Methods and operators related to FrameAcc
  276. // They all delegate most of the work to RotationAcc and VectorAcc
  277. FrameAcc& FrameAcc::operator = (const FrameAcc& arg) {
  278. M=arg.M;
  279. p=arg.p;
  280. return *this;
  281. }
  282. FrameAcc FrameAcc::Identity() {
  283. return FrameAcc(RotationAcc::Identity(),VectorAcc::Zero());
  284. }
  285. FrameAcc operator *(const FrameAcc& lhs,const FrameAcc& rhs)
  286. {
  287. return FrameAcc(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p);
  288. }
  289. FrameAcc operator *(const FrameAcc& lhs,const Frame& rhs)
  290. {
  291. return FrameAcc(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p);
  292. }
  293. FrameAcc operator *(const Frame& lhs,const FrameAcc& rhs)
  294. {
  295. return FrameAcc(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p);
  296. }
  297. VectorAcc FrameAcc::operator *(const VectorAcc & arg) const
  298. {
  299. return M*arg+p;
  300. }
  301. VectorAcc FrameAcc::operator *(const Vector & arg) const
  302. {
  303. return M*arg+p;
  304. }
  305. VectorAcc FrameAcc::Inverse(const VectorAcc& arg) const
  306. {
  307. return M.Inverse(arg-p);
  308. }
  309. VectorAcc FrameAcc::Inverse(const Vector& arg) const
  310. {
  311. return M.Inverse(arg-p);
  312. }
  313. FrameAcc FrameAcc::Inverse() const
  314. {
  315. return FrameAcc(M.Inverse(),-M.Inverse(p));
  316. }
  317. FrameAcc& FrameAcc::operator =(const Frame & arg)
  318. {
  319. M = arg.M;
  320. p = arg.p;
  321. return *this;
  322. }
  323. bool Equal(const FrameAcc& r1,const FrameAcc& r2,double eps) {
  324. return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps));
  325. }
  326. bool Equal(const Frame& r1,const FrameAcc& r2,double eps) {
  327. return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps));
  328. }
  329. bool Equal(const FrameAcc& r1,const Frame& r2,double eps) {
  330. return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps));
  331. }
  332. Frame FrameAcc::GetFrame() const {
  333. return Frame(M.R,p.p);
  334. }
  335. Twist FrameAcc::GetTwist() const {
  336. return Twist(p.v,M.w);
  337. }
  338. Twist FrameAcc::GetAccTwist() const {
  339. return Twist(p.dv,M.dw);
  340. }
  341. TwistAcc TwistAcc::Zero()
  342. {
  343. return TwistAcc(VectorAcc::Zero(),VectorAcc::Zero());
  344. }
  345. void TwistAcc::ReverseSign()
  346. {
  347. vel.ReverseSign();
  348. rot.ReverseSign();
  349. }
  350. TwistAcc TwistAcc::RefPoint(const VectorAcc& v_base_AB)
  351. // Changes the reference point of the TwistAcc.
  352. // The RVector v_base_AB is expressed in the same base as the TwistAcc
  353. // The RVector v_base_AB is a RVector from the old point to
  354. // the new point.
  355. // Complexity : 6M+6A
  356. {
  357. return TwistAcc(this->vel+this->rot*v_base_AB,this->rot);
  358. }
  359. TwistAcc& TwistAcc::operator-=(const TwistAcc& arg)
  360. {
  361. vel-=arg.vel;
  362. rot -=arg.rot;
  363. return *this;
  364. }
  365. TwistAcc& TwistAcc::operator+=(const TwistAcc& arg)
  366. {
  367. vel+=arg.vel;
  368. rot +=arg.rot;
  369. return *this;
  370. }
  371. TwistAcc operator*(const TwistAcc& lhs,double rhs)
  372. {
  373. return TwistAcc(lhs.vel*rhs,lhs.rot*rhs);
  374. }
  375. TwistAcc operator*(double lhs,const TwistAcc& rhs)
  376. {
  377. return TwistAcc(lhs*rhs.vel,lhs*rhs.rot);
  378. }
  379. TwistAcc operator/(const TwistAcc& lhs,double rhs)
  380. {
  381. return TwistAcc(lhs.vel/rhs,lhs.rot/rhs);
  382. }
  383. TwistAcc operator*(const TwistAcc& lhs,const doubleAcc& rhs)
  384. {
  385. return TwistAcc(lhs.vel*rhs,lhs.rot*rhs);
  386. }
  387. TwistAcc operator*(const doubleAcc& lhs,const TwistAcc& rhs)
  388. {
  389. return TwistAcc(lhs*rhs.vel,lhs*rhs.rot);
  390. }
  391. TwistAcc operator/(const TwistAcc& lhs,const doubleAcc& rhs)
  392. {
  393. return TwistAcc(lhs.vel/rhs,lhs.rot/rhs);
  394. }
  395. // addition of TwistAcc's
  396. TwistAcc operator+(const TwistAcc& lhs,const TwistAcc& rhs)
  397. {
  398. return TwistAcc(lhs.vel+rhs.vel,lhs.rot+rhs.rot);
  399. }
  400. TwistAcc operator-(const TwistAcc& lhs,const TwistAcc& rhs)
  401. {
  402. return TwistAcc(lhs.vel-rhs.vel,lhs.rot-rhs.rot);
  403. }
  404. // unary -
  405. TwistAcc operator-(const TwistAcc& arg)
  406. {
  407. return TwistAcc(-arg.vel,-arg.rot);
  408. }
  409. TwistAcc RotationAcc::Inverse(const TwistAcc& arg) const
  410. {
  411. return TwistAcc(Inverse(arg.vel),Inverse(arg.rot));
  412. }
  413. TwistAcc RotationAcc::operator * (const TwistAcc& arg) const
  414. {
  415. return TwistAcc((*this)*arg.vel,(*this)*arg.rot);
  416. }
  417. TwistAcc RotationAcc::Inverse(const Twist& arg) const
  418. {
  419. return TwistAcc(Inverse(arg.vel),Inverse(arg.rot));
  420. }
  421. TwistAcc RotationAcc::operator * (const Twist& arg) const
  422. {
  423. return TwistAcc((*this)*arg.vel,(*this)*arg.rot);
  424. }
  425. TwistAcc FrameAcc::operator * (const TwistAcc& arg) const
  426. {
  427. TwistAcc tmp;
  428. tmp.rot = M*arg.rot;
  429. tmp.vel = M*arg.vel+p*tmp.rot;
  430. return tmp;
  431. }
  432. TwistAcc FrameAcc::operator * (const Twist& arg) const
  433. {
  434. TwistAcc tmp;
  435. tmp.rot = M*arg.rot;
  436. tmp.vel = M*arg.vel+p*tmp.rot;
  437. return tmp;
  438. }
  439. TwistAcc FrameAcc::Inverse(const TwistAcc& arg) const
  440. {
  441. TwistAcc tmp;
  442. tmp.rot = M.Inverse(arg.rot);
  443. tmp.vel = M.Inverse(arg.vel-p*arg.rot);
  444. return tmp;
  445. }
  446. TwistAcc FrameAcc::Inverse(const Twist& arg) const
  447. {
  448. TwistAcc tmp;
  449. tmp.rot = M.Inverse(arg.rot);
  450. tmp.vel = M.Inverse(arg.vel-p*arg.rot);
  451. return tmp;
  452. }
  453. Twist TwistAcc::GetTwist() const {
  454. return Twist(vel.p,rot.p);
  455. }
  456. Twist TwistAcc::GetTwistDot() const {
  457. return Twist(vel.v,rot.v);
  458. }
  459. bool Equal(const TwistAcc& a,const TwistAcc& b,double eps) {
  460. return (Equal(a.rot,b.rot,eps)&&
  461. Equal(a.vel,b.vel,eps) );
  462. }
  463. bool Equal(const Twist& a,const TwistAcc& b,double eps) {
  464. return (Equal(a.rot,b.rot,eps)&&
  465. Equal(a.vel,b.vel,eps) );
  466. }
  467. bool Equal(const TwistAcc& a,const Twist& b,double eps) {
  468. return (Equal(a.rot,b.rot,eps)&&
  469. Equal(a.vel,b.vel,eps) );
  470. }