Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

PlannerObstaclesShapeConversions.cc

Go to the documentation of this file.
00001 #include "PlannerObstacles.h"
00002 
00003 /* Splitting the DualCoding stuff into a separate file allows us to
00004  * avoid compilation dependancy on the DualCoding ecosystem when
00005  * we just want to test PlannerObstacle math in external tools */
00006 
00007 #include "DualCoding/ShapeBlob.h"
00008 #include "DualCoding/ShapeCylinder.h"
00009 #include "DualCoding/ShapeEllipse.h"
00010 #include "DualCoding/ShapeLine.h"
00011 #include "DualCoding/ShapePoint.h"
00012 #include "DualCoding/ShapePolygon.h"
00013 #include "DualCoding/ShapeSphere.h"
00014 #include "DualCoding/ShapeBrick.h"
00015 
00016 using namespace DualCoding;
00017 
00018 template <>
00019 void PlannerObstacle2D::convertShapeToPlannerObstacle
00020 (const DualCoding::ShapeRoot &shape, float inflation, std::vector<PlannerObstacle2D*> &obstacles) {
00021   switch ( shape->getType() ) {
00022       
00023     case pointDataType: {
00024       const Shape<PointData> &p = ShapeRootTypeConst(shape,PointData);
00025       float pointWidth = 1;  // default point width 1 mm
00026       CircularObstacle *circ =
00027       new CircularObstacle(p->getCentroid().coordX(),
00028                            p->getCentroid().coordY(),
00029                            pointWidth+inflation);
00030       std::ostringstream nameStream;
00031       nameStream << p->getName() << "-" << p->getId();
00032       circ->name = nameStream.str();
00033       circ->shapeId = p->getId();
00034       obstacles.push_back(circ);
00035     } break;
00036       
00037     case aprilTagDataType:
00038       // Can't handle AprilTags correctly until we have a way to find their 3D orientation
00039       break;
00040       
00041     case lineDataType: {
00042       const Shape<LineData> &line = ShapeRootTypeConst(shape,LineData);
00043       const fmat::SubVector<2, const fmat::fmatReal> p1(line->end1Pt().coords);
00044       const fmat::SubVector<2, const fmat::fmatReal> p2(line->end2Pt().coords);
00045       const fmat::Column<2> axis = p2 - p1;   
00046       float const lineWidth = 1; // default line width 1 mm
00047       RectangularObstacle *rect =
00048       new RectangularObstacle((p1 + p2)/2,
00049                               fmat::pack(axis.norm()/2+inflation, lineWidth+inflation),
00050                               line->getOrientation());
00051       std::ostringstream nameStream;
00052       nameStream << line->getName() << "-" << line->getId();
00053       rect->name = nameStream.str();
00054       rect->shapeId = line->getId();
00055       obstacles.push_back(rect);
00056     } break;
00057       
00058     case polygonDataType: {
00059       const Shape<PolygonData>& poly = ShapeRootTypeConst(shape,PolygonData);
00060       std::vector<LineData> lines =  poly->getEdges();
00061       for (unsigned int i = 0; i < lines.size(); i++) {
00062         const fmat::SubVector<2, const fmat::fmatReal> p1(lines[i].end1Pt().coords);
00063         const fmat::SubVector<2, const fmat::fmatReal> p2(lines[i].end2Pt().coords);
00064         const fmat::Column<2> axis = p2 - p1;
00065   float const lineWidth = 1; // default line width 1 mm
00066         
00067         RectangularObstacle *rect =
00068     new RectangularObstacle((p1 + p2)/2, fmat::pack(axis.norm()/2+inflation, lineWidth+inflation),
00069           lines[i].getOrientation());
00070         std::ostringstream nameStream;
00071         nameStream << poly->getName() << "-" << poly->getId() << ":" << i;
00072         rect->name = nameStream.str();
00073   rect->shapeId = poly->getId();
00074         obstacles.push_back(rect);
00075       }
00076       break;
00077     }
00078       
00079     case ellipseDataType: {
00080       const Shape<EllipseData> &e = ShapeRootTypeConst(shape,EllipseData);
00081       if (e->getSemimajor() == e->getSemiminor()) {
00082         CircularObstacle* circle = 
00083         new CircularObstacle(e->getCentroid().coordX(),
00084                              e->getCentroid().coordY(),
00085                              e->getSemimajor()+inflation);
00086         std::ostringstream nameStream;
00087         nameStream << e->getName() << "-" << e->getId();
00088         circle->name = nameStream.str();
00089         obstacles.push_back(circle);
00090       }
00091       else {
00092         EllipticalObstacle* ellipse =
00093         new EllipticalObstacle(fmat::Column<2>(e->getCentroid().coords),
00094                                e->getSemimajor()+inflation,
00095                                e->getSemiminor()+inflation,
00096                                e->getOrientation());
00097         std::ostringstream nameStream;
00098         nameStream << e->getName() << "-" << e->getId();
00099         ellipse->name = nameStream.str();
00100   ellipse->shapeId = e->getId();
00101         obstacles.push_back(ellipse);
00102       }
00103     } break;
00104       
00105     case sphereDataType: {
00106       const Shape<SphereData> &sph = ShapeRootTypeConst(shape,SphereData);
00107       CircularObstacle *circ =
00108       new CircularObstacle(sph->getCentroid().coordX(),
00109                            sph->getCentroid().coordY(),
00110                            sph->getRadius()+inflation);
00111       std::ostringstream nameStream;
00112       nameStream << sph->getName() << "-" << sph->getId();
00113       circ->name = nameStream.str();
00114       circ->shapeId = sph->getId();
00115       obstacles.push_back(circ);
00116     } break;
00117       
00118     case cylinderDataType: {
00119       const Shape<CylinderData>& c = ShapeRootTypeConst(shape,CylinderData);
00120       CircularObstacle *circ =
00121       new CircularObstacle(c->getCentroid().coordX(), c->getCentroid().coordY(), c->getRadius()+inflation);
00122       std::ostringstream nameStream;
00123       nameStream << c->getName() << "-" << c->getId();
00124       circ->name = nameStream.str();
00125       circ->shapeId = c->getId();
00126       obstacles.push_back(circ);
00127     } break;
00128       
00129     case blobDataType: {
00130       const Shape<BlobData> &b = ShapeRootTypeConst(shape,BlobData);
00131       RectangularObstacle *rect =
00132       new RectangularObstacle(fmat::Column<2>(b->getCentroid().coords), b->getBoundingBox().getDimensions()+inflation, 0);
00133       std::ostringstream nameStream;
00134       nameStream << b->getName() << "-" << b->getId();
00135       rect->name = nameStream.str();
00136       rect->shapeId = b->getId();
00137       obstacles.push_back(rect);
00138     } break;
00139       
00140     default:
00141       std::cout << std::endl << std::endl 
00142       <<"Planner error: conversion of '" << shape->getName() << "' " 
00143       << " to PlannerObstacle2D not yet implemented." << std::endl << std::endl;
00144   }
00145 }
00146 
00147 template <>
00148 void PlannerObstacle3D::convertShapeToPlannerObstacle
00149 (const DualCoding::ShapeRoot &shape, float inflation, std::vector<PlannerObstacle3D*> &obstacles) {
00150   switch ( shape->getType() ) {
00151       
00152     case pointDataType: {
00153       const Shape<PointData> &p = ShapeRootTypeConst(shape,PointData);
00154       float pointWidth = 1;  // default point width 1 mm
00155       SphericalObstacle *sphere =
00156       new SphericalObstacle(p->getCentroid().coordX(),
00157                             p->getCentroid().coordY(),
00158                             p->getCentroid().coordZ(),
00159                             pointWidth+inflation);
00160       std::ostringstream nameStream;
00161       nameStream << p->getName() << "-" << p->getId();
00162       sphere->name = nameStream.str();
00163       sphere->shapeId = p->getId();
00164       obstacles.push_back(sphere);
00165     } break;
00166       
00167     case sphereDataType: {
00168       const Shape<SphereData> &sph = ShapeRootTypeConst(shape,SphereData);
00169       SphericalObstacle *so =
00170       new SphericalObstacle(sph->getCentroid().coordX(),
00171                             sph->getCentroid().coordY(),
00172                             sph->getCentroid().coordZ(),
00173                             sph->getRadius()+inflation);
00174       std::ostringstream nameStream;
00175       nameStream << sph->getName() << "-" << sph->getId();
00176       so->name = nameStream.str();
00177       so->shapeId = sph->getId();
00178       obstacles.push_back(so);
00179     } break;
00180       
00181     case cylinderDataType: {
00182       const Shape<CylinderData>& c = ShapeRootTypeConst(shape,CylinderData);
00183       CylindricalObstacle *cyl =
00184       new CylindricalObstacle(fmat::pack(c->getCentroid().coordX(),
00185                                          c->getCentroid().coordY(),
00186                                          c->getCentroid().coordZ()),
00187                               fmat::Matrix<3,3>::identity(),
00188                               c->getRadius(),
00189                               c->getHeight()/2);
00190       std::ostringstream nameStream;
00191       nameStream << c->getName() << "-" << c->getId();
00192       cyl->name = nameStream.str();
00193       cyl->shapeId = c->getId();
00194       obstacles.push_back(cyl);
00195     } break;
00196       
00197     case brickDataType: {
00198       const Shape<BrickData> &br = ShapeRootTypeConst(shape,BrickData);
00199       // get centroid
00200       const fmat::Column<3> centroid = br->getCentroid().coords;
00201       // determine extents
00202       fmat::Column<3> extents = br->getTFR().coords - centroid;
00203       // create orientation matrix from normalized axes
00204       fmat::Matrix<3,3> o;
00205       o.column(0) = (br->getTFR() - br->getTFL()).coords; o.column(0) /= o.column(0).norm();
00206       o.column(1) = (br->getTFR() - br->getTBR()).coords; o.column(1) /= o.column(1).norm();
00207       o.column(2) = (br->getTFR() - br->getGFR()).coords; o.column(2) /= o.column(2).norm();
00208       BoxObstacle *box = new BoxObstacle(centroid, o.transpose() * extents, o);
00209       std::ostringstream nameStream;
00210       nameStream << br->getName() << "-" << br->getId();
00211       box->name = nameStream.str();
00212       box->shapeId = br->getId();
00213       obstacles.push_back(box);
00214     } break;
00215       
00216     default:
00217       std::cout << std::endl << std::endl 
00218       <<"Planner error: conversion of '" << shape->getName() << "' " 
00219       << " to PlannerObstacle3D not yet implemented." << std::endl << std::endl;
00220   }
00221 }

Tekkotsu v5.1CVS
Generated Fri Mar 16 05:26:47 2012 by Doxygen 1.6.3