Open
Graph Drawing
Framework

 v.2012.05
 

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