Open
Graph Drawing
Framework

 v.2012.05
 

UniformGrid.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  
00047 #ifdef _MSC_VER
00048 #pragma once
00049 #endif
00050 
00051 #ifndef OGDF_UNIFORMGRID_H
00052 #define OGDF_UNIFORMGRID_H
00053 
00054 #include<ogdf/basic/geometry.h>
00055 #include<ogdf/basic/SList.h>
00056 #include<ogdf/basic/Array2D.h>
00057 #include<ogdf/basic/GraphAttributes.h>
00058 #include<ogdf/basic/HashArray2D.h>
00059 #include<ogdf/internal/energybased/IntersectionRectangle.h>
00060 #include<limits.h>
00061 
00062 namespace ogdf {
00063 
00064 
00065     class UniformGrid{
00066     public:
00067         UniformGrid(const GraphAttributes &);
00068         //This constructor takes an GraphAttributes and computes a grid for the given
00069         //layout.
00070         UniformGrid(const GraphAttributes &,const node, const DPoint&);
00071         //This constructor gets the current layout, the node that may be
00072         //moved and its new position and computes the data for the
00073         //modified layout.
00074         UniformGrid(const UniformGrid &, const node, const DPoint&);
00075         //Takes a UniformGrid and produces a new grid for the updated layout
00076         int numberOfCrossings() const {return m_crossNum;}
00077         bool newGridNecessary(const node v, const DPoint& p) {
00078             bool resize = false;
00079             IntersectionRectangle ir;
00080             computeGridGeometry(v,p,ir);
00081             double l = max(ir.width(),ir.height());
00082             l/=m_edgeMultiplier*(m_graph).numberOfEdges();
00083             if(l <= m_CellSize/2.0 || l >= m_CellSize*2.0) resize = true;
00084             return resize;
00085         }
00086             
00087     private:
00088         void ModifiedBresenham(const IPoint &, const IPoint &, SList<IPoint> &) const;
00089         //This takes two DPoints with and computes a list of points
00090         //that are the lower left corners of the cells that may possibly contain points
00091         //of the straight line segment connecting the two points
00092         void DoubleModifiedBresenham(const DPoint &, const DPoint &, SList<IPoint> &) const;
00093         //this function computes the grid coordinate of a point that depends on the
00094         //coordiantes of the point, the lower left corner of the bounding rectangle
00095         //and the size of a cell
00096         IPoint computeGridPoint(const DPoint &dp) const {
00097             double x = floor(dp.m_x/m_CellSize);
00098             OGDF_ASSERT(isInt(x));
00099             double y = floor(dp.m_y/m_CellSize);
00100             OGDF_ASSERT(isInt(y));
00101             return IPoint(int(x),int(y));
00102         }
00103         //computes for a grid point the corresponding DPoint
00104         DPoint computeRealPoint(const IPoint &ip) const {
00105             DPoint p;
00106             p.m_x = ip.m_x*m_CellSize;
00107             p.m_y = ip.m_y*m_CellSize;
00108             return p;
00109         }
00110         //checks if a double number is an integer
00111         bool isInt(double d) const {
00112             if(d - floor(d) > 0) return false;
00113             if(d < INT_MIN || d > INT_MAX) return false;
00114             return true;
00115         }
00116         //computes the crossings of the given edges for the given layout
00117         //with the node moved to the position given as argument
00118         void computeCrossings(const List<edge>&, const node, const DPoint&);
00119         //computes the geometry of the grid if the node is moved
00120         //to the position given by the point
00121         void computeGridGeometry(const node, const DPoint&, IntersectionRectangle&) const;
00122         //Checks if two edges cross inside the given cell.
00123         //The node and the point are the moved node and its
00124         //new position
00125         bool crossingTest(const edge, const edge, const node,
00126                                const DPoint&, const IPoint&);
00127 #ifdef OGDF_DEBUG
00128         void markCells(SList<IPoint> &, Array2D<bool> &) const;
00129         bool crossesCell(IPoint, IPoint, const IPoint &) const;
00130         bool crossesCell(DPoint, DPoint, const IPoint &) const;
00131         void checkBresenham(DPoint, DPoint) const;
00132         void checkBresenham(IPoint, IPoint) const;
00133         bool intervalIntersect(double,double,double,double) const;
00134         friend ostream& operator<<(ostream &,const UniformGrid&);
00135         int m_crossingTests;
00136         int m_maxEdgesPerCell;
00137         double m_time;
00138 #endif
00139         const GraphAttributes &m_layout; //the layout
00140         const Graph &m_graph;
00141         HashArray2D<int,int,List<edge> > m_grid; //stores for each grid cell
00142         //the Array of edges that cross that cell
00143         EdgeArray<List<edge> > m_crossings; //stores for each edge the edges
00144         //its crossings in the current layout
00145         EdgeArray<List<IPoint> > m_cells; //Contains for each edge the
00146         //list of cells it crosses
00147         double m_CellSize; //Sidelength of one cell
00148         const static double m_epsilon; //tolerance fo double computation
00149         const static double m_edgeMultiplier; //this controls the gridsize
00150         int m_crossNum; //number of crossings
00151 
00152         UniformGrid& operator=(const UniformGrid& ug);
00153     };
00154 #ifdef OGDF_DEBUG
00155 ostream &operator<<(ostream &, const IPoint &);
00156 #endif
00157 } //namespace
00158 #endif