00001
00002
00003 #ifndef JETMOMENTTOOLS_JETVORONOIDIAGRAMHELPERS_H
00004 #define JETMOMENTTOOLS_JETVORONOIDIAGRAMHELPERS_H
00005
00011
00012
00013 #include "AsgTools/AsgTool.h"
00014
00015
00016 #include <string>
00017 #include <vector>
00018
00019
00020
00021 #include <boost/polygon/voronoi.hpp>
00022
00023
00024 typedef boost::polygon::voronoi_diagram<double> VoronoiBoost;
00025 typedef boost::polygon::voronoi_diagram<double>::vertex_type VoronoiVtxBoost;
00026 typedef boost::polygon::voronoi_diagram<double>::cell_type VoronoiCellBoost;
00027 typedef boost::polygon::voronoi_diagram<double>::edge_type VoronoiEdgeBoost;
00028
00029
00030 #include <boost/geometry.hpp>
00031 #include <boost/geometry/geometries/point_xy.hpp>
00032 #include <boost/geometry/geometries/polygon.hpp>
00033
00034
00035 typedef boost::geometry::model::d2::point_xy<double> VoronoiPointBoost;
00036 typedef boost::geometry::model::polygon<VoronoiPointBoost> VoronoiPolygonBoost;
00037
00038 namespace JetVoronoiDiagramHelpers {
00041 typedef double coord;
00042
00043
00046 struct Point{
00047 Point(coord the_x=0, coord the_y=0): x(the_x), y(the_y){};
00048 Point(const Point &a): x(a.x), y(a.y) {};
00049
00050 coord x;
00051 coord y;
00052 };
00053
00054
00055 Point operator*(double a, const Point &b);
00056 Point operator*(const Point &b,double a);
00057 double operator*(const Point &a, const Point &b);
00058
00059
00060 Point operator+(const Point &a, const Point &b);
00061 Point operator+(double a, const Point &b);
00062 Point operator+(const Point &b, double a);
00063
00064
00065 Point operator-(const Point &a, const Point &b);
00066 Point operator-(const Point &b);
00067
00068
00069 bool operator== (const Point &a, const Point &b);
00070 bool operator!= (const Point &a, const Point &b);
00071
00072
00073 Point Center(const Point &a, const Point &b) ;
00074 Point Norm(const Point &a);
00075
00076
00079 struct Segment{
00080 Segment(coord x1, coord y1, coord x2, coord y2) : p0(x1, y1), p1(x2, y2) {}
00081 Segment(Point a, Point b) : p0(a), p1(b) {}
00082
00083 Point p0;
00084 Point p1;
00085 };
00086
00087
00090 struct Polygon : public std::vector<Point> {
00091 void Add(coord x, coord y){
00092 push_back(Point(x,y));
00093 }
00094 void FillVoroPolygon(VoronoiPolygonBoost & out) const {
00095 if (empty()) return;
00096
00097 for ( const Point p : *this ) out.outer().push_back(VoronoiPointBoost(p.x,p.y));
00098
00099 if ( front() != back() ) out.outer().push_back(VoronoiPointBoost(front().x,front().y));
00100
00101 boost::geometry::correct(out);
00102 }
00103 };
00104
00105
00108 typedef std::vector<Polygon> PolygonList;
00109
00110
00113 struct SegmentList : public std::vector<Segment> {
00114 void Add(coord x1, coord y1, coord x2, coord y2) {
00115 push_back(Segment(x1,y1,x2,y2));
00116 }
00117 };
00118
00119
00122 struct Diagram : public asg::AsgTool {
00123 Diagram ( const std::string & name);
00124
00125 StatusCode initialize();
00126 float getCellArea( const coord x, const coord y) const;
00127 size_t findPointIndex(const Point &a) const;
00128 void clearDiagram();
00129 StatusCode createVoronoiDiagram();
00130 Point interpolateInfinityVertex(const int i_a, const int i_b);
00131 bool checkSameNumber(double in, double out, const std::string & description);
00132 StatusCode checkStatus(const VoronoiBoost &vd, size_t n_cells_processed);
00133 double intersectionAndArea(JetVoronoiDiagramHelpers::Polygon const & geo1, JetVoronoiDiagramHelpers::Polygon const & geo2, JetVoronoiDiagramHelpers::Polygon &out);
00134
00135
00136 double m_x_min;
00137 double m_x_max;
00138 double m_y_min;
00139 double m_y_max;
00140 double m_scaleIntFloat;
00141 Polygon m_borders;
00142 Polygon m_voro_vtx;
00143 std::vector<double> m_area_cells;
00144 double m_area_borders;
00145
00146 size_t m_N_points;
00147 };
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 namespace boost {
00161 namespace polygon {
00162
00163 template <>
00164 struct geometry_concept<JetVoronoiDiagramHelpers::Point> {
00165 typedef point_concept type;
00166 };
00167 template <>
00168 struct point_traits<JetVoronoiDiagramHelpers::Point> {
00169 typedef JetVoronoiDiagramHelpers::coord coordinate_type;
00170 static inline coordinate_type get(
00171 const JetVoronoiDiagramHelpers::Point& point, orientation_2d orient) {
00172 return (orient == HORIZONTAL) ? point.x : point.y;
00173 }
00174 };
00175 template <>
00176 struct point_mutable_traits<JetVoronoiDiagramHelpers::Point> {
00177 typedef JetVoronoiDiagramHelpers::coord coordinate_type;
00178 static inline void set(JetVoronoiDiagramHelpers::Point& point, orientation_2d orient, int value) {
00179 if(orient == HORIZONTAL)
00180 point.x = value;
00181 else
00182 point.y = value;
00183 }
00184 static inline JetVoronoiDiagramHelpers::Point construct(int x_value, int y_value) {
00185 JetVoronoiDiagramHelpers::Point retval;
00186 retval.x = x_value;
00187 retval.y = y_value;
00188 return retval;
00189 }
00190 };
00191
00192
00193 template <>
00194 struct geometry_concept<JetVoronoiDiagramHelpers::Segment> {
00195 typedef segment_concept type;
00196 };
00197 template <>
00198 struct segment_traits<JetVoronoiDiagramHelpers::Segment> {
00199 typedef JetVoronoiDiagramHelpers::coord coordinate_type;
00200 typedef JetVoronoiDiagramHelpers::Point point_type;
00201 static inline point_type get(const JetVoronoiDiagramHelpers::Segment& segment, direction_1d dir) {
00202 return dir.to_int() ? segment.p1 : segment.p0;
00203 }
00204 };
00205 }
00206 }
00207
00208 #endif
00209