00001
00002
00003 #include <iostream>
00004 #include <vector>
00005 #include <fstream>
00006
00007 #include "DualCoding/ShapeRoot.h"
00008 #include "DualCoding/ShapeEllipse.h"
00009 #include "DualCoding/ShapeBlob.h"
00010 #include "DualCoding/ShapeLine.h"
00011 #include "DualCoding/ShapePoint.h"
00012 #include "DualCoding/ShapePolygon.h"
00013
00014 #include "ShapeLandmarks.h"
00015
00016 using namespace std;
00017
00018 void PfRoot::loadLms(const std::vector<ShapeRoot> &lms, bool isWorld, std::vector<PfRoot*>& landmarks){
00019 int id;
00020 int type;
00021 rgb color;
00022 bool mobile;
00023 deleteLms(landmarks);
00024 landmarks.reserve(lms.size());
00025 for (unsigned int i = 0; i<lms.size(); i++){
00026 id = lms[i]->getId();
00027 type = lms[i]->getType();
00028 color = lms[i]->getColor();
00029 mobile = lms[i]->getMobile();
00030 switch ( type ) {
00031
00032 case lineDataType: {
00033 const Shape<LineData> &line = ShapeRootTypeConst(lms[i],LineData);
00034 const EndPoint &pt1 = line->end1Pt();
00035 const EndPoint &pt2 = line->end2Pt();
00036 PfLine *land = new PfLine(id, color, mobile, pt1.coordX(), pt1.coordY(),
00037 pt2.coordX(), pt2.coordY(), pt1.isValid(), pt2.isValid());
00038 land->link = &lms[i];
00039 land->orientation = atan2(pt2.coordY()-pt1.coordY(), pt2.coordX()-pt1.coordX());
00040 land->length = sqrt((pt2.coordX()-pt1.coordX())*(pt2.coordX()-pt1.coordX()) +
00041 (pt2.coordY()-pt1.coordY())*(pt2.coordY()-pt1.coordY()));
00042 landmarks.push_back(land);
00043 if ( isWorld ) {
00044 PfLine *land2 = new PfLine(id, color, mobile, pt2.coordX(), pt2.coordY(),
00045 pt1.coordX(), pt1.coordY(), pt2.isValid(), pt1.isValid());
00046 land2->link = &lms[i];
00047 land2->orientation = land->orientation;
00048 land2->length = land->length;
00049 landmarks.push_back(land2);
00050 }
00051 }
00052 break;
00053
00054 case polygonDataType: {
00055 const Shape<PolygonData> &poly = ShapeRootTypeConst(lms[i],PolygonData);
00056 const std::vector<LineData> &edges = poly->getEdges();
00057 for (unsigned int j = 0; j < edges.size(); j++) {
00058 const LineData &line = edges[j];
00059 const EndPoint &pt1 = line.end1Pt();
00060 const EndPoint &pt2 = line.end2Pt();
00061 PfLine *land = new PfLine(id, color, mobile, pt1.coordX(), pt1.coordY(),
00062 pt2.coordX(), pt2.coordY(), pt1.isValid(), pt2.isValid());
00063 land->link = &lms[i];
00064 land->orientation = atan2(pt2.coordY()-pt1.coordY(), pt2.coordX()-pt1.coordX());
00065 land->length = sqrt((pt2.coordX()-pt1.coordX())*(pt2.coordX()-pt1.coordX()) +
00066 (pt2.coordY()-pt1.coordY())*(pt2.coordY()-pt1.coordY()));
00067 landmarks.push_back(land);
00068 if ( isWorld ) {
00069 PfLine *land2 = new PfLine(id, color, mobile, pt2.coordX(), pt2.coordY(),
00070 pt1.coordX(), pt1.coordY(), pt2.isValid(), pt1.isValid());
00071 land2->link = &lms[i];
00072 land2->orientation = land->orientation;
00073 land2->length = land->length;
00074 landmarks.push_back(land2);
00075 }
00076 }
00077 }
00078 break;
00079
00080 case ellipseDataType: {
00081 const Shape<EllipseData> &ellipse = ShapeRootTypeConst(lms[i],EllipseData);
00082 const Point &pt = ellipse->getCentroid();
00083 PfEllipse* land = new PfEllipse(id, color, mobile, pt.coordX(), pt.coordY());
00084 land->link = &lms[i];
00085 landmarks.push_back(land);
00086 }
00087 break;
00088
00089 case pointDataType: {
00090 const Shape<PointData> &point = ShapeRootTypeConst(lms[i],PointData);
00091 const Point &pt = point->getCentroid();
00092 PfPoint* land = new PfPoint(id, color, mobile, pt.coordX(), pt.coordY());
00093 land->link = &lms[i];
00094 landmarks.push_back(land);
00095 }
00096 break;
00097
00098 case blobDataType: {
00099 const Shape<BlobData> &blob = ShapeRootTypeConst(lms[i],BlobData);
00100 Point pt = blob->getCentroid();
00101 PfBlob* land = new PfBlob(id, color, mobile, pt.coordX(), pt.coordY());
00102 land->link = &lms[i];
00103 landmarks.push_back(land);
00104 }
00105 break;
00106
00107 case markerDataType: {
00108 const Shape<MarkerData> &marker = ShapeRootTypeConst(lms[i],MarkerData);
00109 Point pt = marker->getCentroid();
00110 PfMarker* land = new PfMarker(id, color, mobile, pt.coordX(), pt.coordY(), marker);
00111 land->link = &lms[i];
00112 landmarks.push_back(land);
00113 }
00114 break;
00115
00116 case aprilTagDataType: {
00117 const Shape<AprilTagData> &tag = ShapeRootTypeConst(lms[i],AprilTagData);
00118 Point pt = tag->getCentroid();
00119 PfAprilTag* land = new PfAprilTag(id, color, mobile, pt.coordX(), pt.coordY(), tag);
00120 land->link = &lms[i];
00121 landmarks.push_back(land);
00122 }
00123 break;
00124
00125 case cylinderDataType: {
00126 const Shape<CylinderData> &cyl = ShapeRootTypeConst(lms[i],CylinderData);
00127 Point pt = cyl->getCentroid();
00128 PfCylinder* land = new PfCylinder(id, color, mobile, pt.coordX(), pt.coordY(), cyl);
00129 land->link = &lms[i];
00130 landmarks.push_back(land);
00131 }
00132 break;
00133
00134 default:
00135 break;
00136 }
00137 }
00138 }
00139
00140 void PfRoot::deleteLms(std::vector<PfRoot*>& vec) {
00141 for ( unsigned int i = 0; i < vec.size(); i++ )
00142 delete vec[i];
00143 vec.clear();
00144 }
00145
00146 void PfRoot::findBounds(const std::vector<PfRoot*> &landmarks,
00147 coordinate_t &xmin, coordinate_t &ymin,
00148 coordinate_t &xmax, coordinate_t &ymax) {
00149 if ( landmarks.size() == 0 ) {
00150 cout << "Error in PFRoot::findBounds -- empty landmark vector" << endl;
00151 return;
00152 }
00153 xmin = xmax = landmarks[0]->x;
00154 ymin = ymax = landmarks[0]->y;
00155 for ( size_t i = 1; i<landmarks.size(); i++ ) {
00156 if ( (*landmarks[i]).x < xmin )
00157 xmin = landmarks[i]->x;
00158 else if ( landmarks[i]->x > xmax )
00159 xmax = landmarks[i]->x;
00160 if ( landmarks[i]->y < ymin )
00161 ymin = landmarks[i]->y;
00162 else if ( landmarks[i]->y > ymax )
00163 ymax = landmarks[i]->y;
00164 }
00165 }
00166
00167 void PfRoot::printLms(const std::vector<PfRoot*> &landmarks) {
00168 for (unsigned int i = 0; i<landmarks.size(); i++)
00169 cout << *landmarks[i] << endl;
00170 }
00171
00172 void PfRoot::printRootInfo(std::ostream &os) const {
00173 os << "id=" << id
00174 << ", x=" << x
00175 << ", y=" << y
00176 << ", mobile=" << mobile;
00177 }
00178
00179 ostream& operator<<(std::ostream &os, const PfRoot &obj) {
00180 obj.print(os);
00181 return os;
00182 }
00183
00184 void PfLine::print(std::ostream &os) const {
00185 os << "PfLine(";
00186 printRootInfo(os);
00187 os << ", x2=" << x2
00188 << ", y2=" << y2
00189 << ", length=" << length
00190 << ")";
00191 }
00192
00193 void PfEllipse::print(std::ostream &os) const {
00194 os << "PfEllipse(";
00195 printRootInfo(os);
00196 os << ")";
00197 }
00198
00199 void PfPoint::print(std::ostream &os) const {
00200 os << "PfPoint(";
00201 printRootInfo(os);
00202 os << ")";
00203 }
00204
00205 void PfBlob::print(std::ostream &os) const {
00206 os << "PfBlob(";
00207 printRootInfo(os);
00208 os << ")";
00209 }
00210
00211 void PfMarker::print(std::ostream &os) const {
00212 os << "PfMarker(";
00213 printRootInfo(os);
00214 os << ")";
00215 }
00216
00217 void PfAprilTag::print(std::ostream &os) const {
00218 os << "PfAprilTag(";
00219 printRootInfo(os);
00220 os << ")";
00221 }
00222
00223 void PfCylinder::print(std::ostream &os) const {
00224 os << "PfCylinder(";
00225 printRootInfo(os);
00226 os << ")";
00227 }