framevel.inl 12 KB

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