Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "cob_3d_mapping_slam/curved_polygons/polygon_merger.h"
00011
00012
00013
00014 #include <boost/polygon/polygon.hpp>
00015 #include <boost/geometry.hpp>
00016 #include <boost/geometry/geometries/geometries.hpp>
00017 #include <boost/geometry/multi/geometries/multi_polygon.hpp>
00018 #include <boost/geometry/algorithms/envelope.hpp>
00019 #include <boost/geometry/geometries/polygon.hpp>
00020 #include <boost/geometry/geometries/point_xy.hpp>
00021
00022 using namespace Slam_CurvedPolygon;
00023 using namespace boost::polygon::operators;
00024
00025 typedef boost::polygon::polygon_with_holes_data<int> BoostPolygon;
00026 typedef BoostPolygon::point_type BoostPoint;
00027 typedef std::vector<BoostPolygon> BoostPolygonSet;
00028 typedef boost::polygon::polygon_90_set_traits<BoostPolygonSet> BoostTraits;
00029
00030
00031 void getBoostPolygon2D(const PolygonData &d, BoostPolygonSet &r) {
00032 BoostPolygon poly;
00033 std::vector<BoostPoint> seg;
00034 for(size_t i=0; i+1<d.get().size(); i+=2)
00035 {
00036
00037 if(d.get()[i]==d.get()[i] && d.get()[i+1]==d.get()[i+1] && std::abs(d.get()[i])<10000 && std::abs(d.get()[i+1])<10000)
00038 seg.push_back( BoostPoint(d.get()[i]*10000,d.get()[i+1]*10000) );
00039 }
00040
00041 poly.set(seg.begin(), seg.end());
00042 r.push_back(poly);
00043 }
00044
00045 void getPolygon2D(const BoostPolygonSet &poly, PolygonData &d) {
00046 for(size_t k=0; k<poly.size(); k++) {
00047
00048 for(BoostPolygon::iterator_type it=poly[k].begin(); it!=poly[k].end(); it++) {
00049 if(it+1==poly[k].end()) break;
00050
00051
00052 d.add(it->x()/10000.f,it->y()/10000.f);
00053 }
00054 break;
00055
00056 }
00057 }
00058
00059 void Slam_CurvedPolygon::mergePolygons(const PolygonData &a, const PolygonData &b, PolygonData &out)
00060 {
00061 out.clear();
00062
00063 BoostPolygonSet s1, s2;
00064 getBoostPolygon2D(a, s1);
00065 getBoostPolygon2D(b, s2);
00066
00067 try {
00068 s1|=s2;
00069 }
00070 catch(...) {
00071 return;
00072 }
00073
00074 getPolygon2D(s1, out);
00075 }
00076
00077 float Slam_CurvedPolygon::unionPolygons(const PolygonData &a, const PolygonData &b, PolygonData &out, bool *sw)
00078 {
00079 out.clear();
00080
00081 if(a.get().size()<1||b.get().size()<1)
00082 return 0.f;
00083
00084 BoostPolygonSet s1, s2;
00085 getBoostPolygon2D(a, s1);
00086 getBoostPolygon2D(b, s2);
00087
00088 const float a1 = boost::polygon::area(s1);
00089 const float a2 = boost::polygon::area(s2);
00090
00091 if(sw)
00092 *sw = a1>a2;
00093
00094 try {
00095 s1&=s2;
00096 }
00097 catch(...) {
00098 std::cerr<<"ERRROR\n";
00099 return 0.f;
00100 }
00101
00102 if(s1.size()<1)
00103 return 0.f;
00104
00105 const float a3 = boost::polygon::area(s1);
00106 getPolygon2D(s1, out);
00107
00108
00109
00110 return a3/(std::min(a1,a2));
00111 }
00112
00113 float Slam_CurvedPolygon::area(const PolygonData &a)
00114 {
00115 if(a.get().size()<1)
00116 return 0.f;
00117
00118 BoostPolygonSet s1;
00119 getBoostPolygon2D(a, s1);
00120
00121 return boost::polygon::area(s1);
00122 }