00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00038 #include <math.h>
00039
00040 #include "grasp_coordinates.h"
00041 #include <iostream>
00042
00043 #define cout std::cout
00044 #define endl std::endl
00045
00046
00050 coordinates::coordinates(double a, double b, double c) : vec3(a,b,c)
00051 {
00052 }
00053
00057 coordinates::coordinates(coordinates *c) : vec3(c->x(), c->y(), c->z())
00058 {
00059 }
00060
00064 coordinates::coordinates(vec3 v) : vec3(v.x(), v.y(), v.z())
00065 {
00066 }
00067
00071 coordinates::coordinates() : vec3(0.,0.,0.)
00072 {
00073 }
00074
00078 coordinates::coord_system_type
00079 coordinates::get_coord_system_type()
00080 {
00081 return coord_type;
00082 }
00083
00084
00088 void
00089 coordinates::set_coord_system_type(coord_system_type ct)
00090 {
00091 coord_type = ct;
00092 }
00093
00097 cartesian_coordinates
00098 coordinates::get_pos_cartesian() const
00099 {
00100 return cartesian_coordinates(x(), y(), z());
00101 }
00102
00106 cylindrical_coordinates
00107 coordinates::get_pos_cylindrical() const
00108 {
00109 return cylindrical_coordinates(x(), y(), z());
00110 }
00111
00115 spherical_coordinates
00116 coordinates::get_pos_spherical() const
00117 {
00118 return spherical_coordinates(x(), y(), z());
00119 }
00120
00124 cartesian_coordinates
00125 coordinates::get_vec_cartesian(coordinates from) const
00126 {
00127 cout << "PL_OUT: get_vec_whatever of base class coordinates should not be called; makes no sense" << from << endl;
00128 return cartesian_coordinates(x(), y(), z());
00129 }
00130
00134 cylindrical_coordinates
00135 coordinates::get_vec_cylindrical(coordinates from) const
00136 {
00137 cout << "PL_OUT: get_vec_whatever of base class coordinates should not be called; makes no sense" << from << endl;
00138 return cylindrical_coordinates(x(), y(), z());
00139 }
00140
00144 spherical_coordinates
00145 coordinates::get_vec_spherical(coordinates from) const
00146 {
00147 cout << "PL_OUT: get_vec_whatever of base class coordinates should not be called; makes no sense" << from <<endl;
00148 return spherical_coordinates(x(), y(), z());
00149 }
00150
00155 bool
00156 coordinates::operator==(coordinates c)
00157 {
00158 if (c.x() == x() &&
00159 c.y() == y() &&
00160 c.z() == z())
00161 return true;
00162 return false;
00163 }
00164
00168 double
00169 coordinates::distanceTo(coordinates to) const
00170 {
00171 cout << "PL_OUT: Distance in coordinates base class makes no sense: NOT IMPLEMENTED. Returning 0.0" << to <<endl;
00172 return 0.0;
00173 }
00174
00175
00176
00177
00178
00182 cartesian_coordinates::cartesian_coordinates(double x, double y, double z) : coordinates(x,y,z)
00183 {
00184 set_coord_system_type(cartesian);
00185 }
00186
00190 cartesian_coordinates::cartesian_coordinates(cartesian_coordinates *cc) : coordinates(cc->x(),cc->y(),cc->z())
00191 {
00192 set_coord_system_type(cartesian);
00193 }
00194
00198 cartesian_coordinates::cartesian_coordinates(const cartesian_coordinates& cc) : coordinates(cc.x(),cc.y(),cc.z())
00199 {
00200 set_coord_system_type(cartesian);
00201 }
00202
00206 cartesian_coordinates::cartesian_coordinates(vec3 v) : coordinates(v)
00207 {
00208 set_coord_system_type(cartesian);
00209 }
00210
00214 cartesian_coordinates::cartesian_coordinates() : coordinates()
00215 {
00216 set_coord_system_type(cartesian);
00217 }
00218
00222 cartesian_coordinates
00223 cartesian_coordinates::get_pos_cartesian() const
00224 {
00225 return cartesian_coordinates(*this);
00226 }
00227
00232 cylindrical_coordinates
00233 cartesian_coordinates::get_pos_cylindrical() const
00234 {
00235 double R,phi;
00236 R = sqrt( x() * x() + y() * y() );
00237 phi = atan2( y(), x() );
00238
00239 return cylindrical_coordinates(R ,phi ,z() );
00240 }
00241
00246 spherical_coordinates
00247 cartesian_coordinates::get_pos_spherical() const
00248 {
00249 double r, teta, phi;
00250 r = sqrt( x() * x() + y() * y() + z() * z() );
00251 teta = atan2(sqrt(x() * x() + y() * y()), z() );
00252 phi = atan2(y(), x());
00253 return spherical_coordinates(r, teta, phi);
00254 }
00255
00259 cartesian_coordinates
00260 cartesian_coordinates::get_vec_cartesian(cartesian_coordinates from) const
00261 {
00262 from.x() = 0;
00263 return cartesian_coordinates(*this);
00264 }
00265
00269 cylindrical_coordinates
00270 cartesian_coordinates::get_vec_cylindrical(cartesian_coordinates from) const
00271 {
00272 double Ar,Aphi;
00273 double phi = from.get_pos_cylindrical().y();
00274 Ar = x() * cos(phi) + y() * sin(phi);
00275 Aphi = (-1) * x() * sin(phi) + y() * cos(phi);
00276
00277 return cylindrical_coordinates(Ar ,Aphi ,z() );
00278 }
00279
00283 spherical_coordinates
00284 cartesian_coordinates::get_vec_spherical(cartesian_coordinates from) const
00285 {
00286 double Ar, Ateta, Aphi;
00287 double teta = from.get_pos_spherical().y();
00288 double phi = from.get_pos_spherical().z();
00289 Ar = x() * sin(teta) * cos(phi)
00290 + y() * sin(teta) * sin(phi)
00291 + z() * cos(teta);
00292 Ateta = x() * cos(teta) * cos(phi)
00293 + y() * cos(teta) * sin(phi)
00294 - z() * sin(teta);
00295 Aphi = - x() * sin(phi)
00296 + y() * cos(phi);
00297 return spherical_coordinates(Ar, Ateta, Aphi);
00298 }
00299
00303 inline cartesian_coordinates
00304 cartesian_coordinates::operator+(cartesian_coordinates in)
00305 {
00306 return cartesian_coordinates(x()+in[0], y()+in[1], z()+in[2]);
00307 }
00308
00312 inline cartesian_coordinates
00313 cartesian_coordinates::operator-(cartesian_coordinates in)
00314 {
00315 return cartesian_coordinates(x()-in[0], y()-in[1], z()-in[2]);
00316 }
00317
00322 double
00323 cartesian_coordinates::distanceTo(coordinates to) const
00324 {
00325
00326 return ((*this - to).len() / (len() > to.len() ? len():to.len())) / 2.0;
00327 }
00328
00329
00330
00331
00332
00336 cylindrical_coordinates::cylindrical_coordinates(double R, double phi, double z) : coordinates(R,phi,z)
00337 {
00338 set_coord_system_type(cylindrical);
00339 }
00340
00344 cylindrical_coordinates::cylindrical_coordinates(cylindrical_coordinates *cc) : coordinates(cc->x(),cc->y(),cc->z())
00345 {
00346 set_coord_system_type(cylindrical);
00347 }
00348
00352 cylindrical_coordinates::cylindrical_coordinates(const cylindrical_coordinates& cc) : coordinates(cc.x(),cc.y(),cc.z())
00353 {
00354 set_coord_system_type(cylindrical);
00355 }
00356
00360 cylindrical_coordinates::cylindrical_coordinates(vec3 v) : coordinates(v)
00361 {
00362 set_coord_system_type(cylindrical);
00363 }
00364
00368 cylindrical_coordinates::cylindrical_coordinates() : coordinates()
00369 {
00370 set_coord_system_type(cylindrical);
00371 }
00372
00377 cartesian_coordinates
00378 cylindrical_coordinates::get_pos_cartesian() const
00379 {
00380 double cx,cy;
00381 cx = x() * cos(y());
00382 cy = x() * sin(y());
00383
00384 return cartesian_coordinates(cx, cy, z());
00385 }
00386
00387
00391 cylindrical_coordinates
00392 cylindrical_coordinates::get_pos_cylindrical() const
00393 {
00394 return cylindrical_coordinates(*this);
00395 }
00396
00401 spherical_coordinates
00402 cylindrical_coordinates::get_pos_spherical() const
00403 {
00404 double r, teta;
00405 r = sqrt( x() * x() + z() * z() );
00406 teta = atan2( x(), z());
00407 return spherical_coordinates(r, teta, y());
00408 }
00409
00413 cartesian_coordinates
00414 cylindrical_coordinates::get_vec_cartesian(cylindrical_coordinates from) const
00415 {
00416 double Ax, Ay;
00417 double phi = from.y();
00418 Ax = x() * cos(phi) - y() * sin(phi);
00419 Ay = x() * sin(phi) + y() * cos(phi);
00420
00421 return cartesian_coordinates(Ax, Ay, z());
00422 }
00423
00427 cylindrical_coordinates
00428 cylindrical_coordinates::get_vec_cylindrical(cylindrical_coordinates from) const
00429 {
00430 from.x() = 0;
00431 return cylindrical_coordinates(*this);
00432 }
00433
00437 spherical_coordinates
00438 cylindrical_coordinates::get_vec_spherical(cylindrical_coordinates from) const
00439 {
00440 double Ar, Ateta;
00441 double teta = from.get_pos_spherical().y();
00442 Ar = x() * sin(teta) + z() * cos(teta);
00443 Ateta = x() * cos(teta) - z() * sin(teta);
00444
00445 return spherical_coordinates(Ar, Ateta, y());
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00467 double
00468 cylindrical_coordinates::distanceTo(cylindrical_coordinates to) const
00469 {
00470
00471 return ((this->get_pos_cartesian() - to.get_pos_cartesian()).len() /
00472 (get_pos_cartesian().len() > to.get_pos_cartesian().len() ?
00473 get_pos_cartesian().len() : to.get_pos_cartesian().len())) /
00474 2.0;
00475 }
00476
00477
00478
00479
00480
00484 spherical_coordinates::spherical_coordinates(double r, double teta, double phi) : coordinates(r,teta,phi)
00485 {
00486 set_coord_system_type(spherical);
00487 }
00488
00492 spherical_coordinates::spherical_coordinates(spherical_coordinates *cc) : coordinates(cc->x(),cc->y(),cc->z())
00493 {
00494 set_coord_system_type(spherical);
00495 }
00496
00500 spherical_coordinates::spherical_coordinates(const spherical_coordinates& cc) : coordinates(cc.x(),cc.y(),cc.z())
00501 {
00502 set_coord_system_type(spherical);
00503 }
00504
00508 spherical_coordinates::spherical_coordinates(vec3 v) : coordinates(v)
00509 {
00510 set_coord_system_type(spherical);
00511 }
00512
00516 spherical_coordinates::spherical_coordinates() : coordinates()
00517 {
00518 set_coord_system_type(spherical);
00519 }
00520
00524 cartesian_coordinates
00525 spherical_coordinates::get_pos_cartesian() const
00526 {
00527 double cx,cy,cz;
00528 cx = x() * sin(y()) * cos(z());
00529 cy = x() * sin(y()) * sin(z());
00530 cz = x() * cos(y());
00531 return cartesian_coordinates(cx, cy, cz);
00532 }
00533
00537 cylindrical_coordinates
00538 spherical_coordinates::get_pos_cylindrical() const
00539 {
00540 double R,cz;
00541 R = x() * sin(y());
00542 cz = x() * cos(y());
00543
00544 return cylindrical_coordinates(R, z(), cz );
00545 }
00546
00550 spherical_coordinates
00551 spherical_coordinates::get_pos_spherical() const
00552 {
00553 return spherical_coordinates(*this);
00554 }
00555
00559 cartesian_coordinates
00560 spherical_coordinates::get_vec_cartesian(spherical_coordinates from) const
00561 {
00562 double Ax, Ay, Az;
00563 double teta = from.y();
00564 double phi = from.z();
00565 Ax = x() * sin(teta) * cos(phi)
00566 + y() * cos(teta) * cos(phi)
00567 - z() * sin(phi);
00568 Ay = x() * sin(teta) * sin(phi)
00569 + y() * cos(teta) * sin(phi)
00570 + z() * cos(phi);
00571 Az = x() * cos(teta)
00572 - y() * sin(teta);
00573 return cartesian_coordinates(Ax, Ay, Az);
00574 }
00575
00579 cylindrical_coordinates
00580 spherical_coordinates::get_vec_cylindrical(spherical_coordinates from) const
00581 {
00582 double Ar, Az;
00583 double teta = from.y();
00584 Ar = x() * sin(teta)
00585 + y() * cos(teta);
00586
00587 Az = x() * cos(teta)
00588 - y() * sin(teta);
00589 return cylindrical_coordinates(Ar, z(), Az);
00590 }
00591
00595 spherical_coordinates
00596 spherical_coordinates::get_vec_spherical(spherical_coordinates from) const
00597 {
00598 from.x() = 0;
00599 return spherical_coordinates(*this);
00600 }
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615 double
00616 spherical_coordinates::distanceTo(spherical_coordinates to) const
00617 {
00618
00619 return ((this->get_pos_cartesian() - to.get_pos_cartesian()).len() /
00620 (get_pos_cartesian().len() > to.get_pos_cartesian().len() ?
00621 get_pos_cartesian().len() : to.get_pos_cartesian().len())) /
00622 2.0;
00623 }
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642