Open
Graph Drawing
Framework

 v.2010.10
 

NearestRectangleFinder.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 2027 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2010-09-01 11:55:17 +0200 (Wed, 01 Sep 2010) $ 
00007  ***************************************************************/
00008  
00053 #ifdef _MSC_VER
00054 #pragma once
00055 #endif
00056 
00057 
00058 #ifndef OGDF_NEAREST_RECTANGLE_FINDER_H
00059 #define OGDF_NEAREST_RECTANGLE_FINDER_H
00060 
00061 
00062 #include <ogdf/basic/Array.h>
00063 #include <ogdf/basic/geometry.h>
00064 
00065 
00066 namespace ogdf {
00067 
00068 
00069 //---------------------------------------------------------
00070 // NearestRectangleFinder
00071 // finds in a given set of rectangles for each point in a given
00072 // set of points the nearest rectangle
00073 //---------------------------------------------------------
00074 class OGDF_EXPORT NearestRectangleFinder
00075 {
00076 public:
00077     struct RectRegion;
00078     struct PairRectDist;
00079     struct PairCoordId;
00080 
00081     NearestRectangleFinder(double mad = 20, double td = 5) {
00082         m_maxAllowedDistance = mad;
00083         m_toleranceDistance = td;
00084     }
00085 
00086     // the maximal allowed distance between a rectangle and a point
00087     // rectangles with a greater distance are not considered
00088     void maxAllowedDistance(double mad) { m_maxAllowedDistance = mad; }
00089     double maxAllowedDistance() const { return m_maxAllowedDistance; }
00090 
00091     // the tolerance in which rectangles are considered to be ambigous, i.e.
00092     // if the rectangle with the minimum distance to point p has distance mindist
00093     // and there is another rectangle with distance dist such that
00094     // dist <= minDist + toleranceDistance, we say that the closest rectangle is not unique.
00095     void toleranceDistance(double td) { m_toleranceDistance = td; }
00096     double toleranceDistance() const { return m_toleranceDistance; }
00097 
00098 
00099     // finds the nearest rectangles for a given set of points
00100     // The nearest rectangles are passed in a list. If the list is empty, there
00101     // is no rectangle within the ,aximal allowed distance. If the list contains
00102     // more than one element, the nearest rectangle is not unique for the
00103     // given tolerance.
00104     void find(
00105         const Array<RectRegion> &region, // given rectangles
00106         const Array<DPoint> &point,      // given points
00107         Array<List<PairRectDist> > &nearest); // nearest rectangles
00108 
00109     // trivial implementation of find(). Can be used in order to check
00110     // correctness. Computes only rectangle with minimum distance without
00111     // considering maxAllowedDistance and toleranceDistance.
00112     void findSimple(
00113         const Array<RectRegion> &region,
00114         const Array<DPoint> &point,
00115         Array<List<PairRectDist> > &nearest);
00116 
00117 private:
00118     class CoordComparer;
00119     class YCoordComparer;
00120 
00121     double m_maxAllowedDistance;
00122     double m_toleranceDistance;
00123 };
00124 
00125 
00126 //---------------------------------------------------------
00127 // RectRegion
00128 // represents a rectangle given by center point, width and height
00129 //---------------------------------------------------------
00130 struct NearestRectangleFinder::RectRegion
00131 {
00132     friend ostream &operator<<(ostream &os, const RectRegion &rect) {
00133         os << "(" << rect.m_x << "," << rect.m_y << ":" <<
00134             rect.m_width << "," << rect.m_height << ")";
00135         return os;
00136     }
00137 
00138     double m_x, m_y, m_width, m_height;
00139 };
00140 
00141 
00142 //---------------------------------------------------------
00143 // PairRectDist
00144 // represents a rectangle (given by its index) and a
00145 // distance value
00146 //---------------------------------------------------------
00147 struct OGDF_EXPORT NearestRectangleFinder::PairRectDist
00148 {
00149     PairRectDist() { }
00150 
00151     PairRectDist(int index, double distance) {
00152         m_index = index;
00153         m_distance = distance;
00154     }
00155 
00156     friend ostream &operator<<(ostream &os, const PairRectDist &p) {
00157         os << "(" << p.m_index << "," << p.m_distance << ")";
00158         return os;
00159     }
00160 
00161     int m_index;
00162     double m_distance;
00163 };
00164 
00165 
00166 
00167 } // end namespace ogdf
00168 
00169 
00170 #endif