Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

UniformGrid.h
Go to the documentation of this file.
1 
36 #pragma once
37 
38 #include<ogdf/basic/geometry.h>
39 #include<ogdf/basic/SList.h>
40 #include<ogdf/basic/Array2D.h>
43 
44 namespace ogdf {
45 namespace davidson_harel {
46 
47 class UniformGrid {
48 
49 public:
50  // This constructor takes a GraphAttributes and computes a grid for the given layout.
51  explicit UniformGrid(const GraphAttributes &);
52 
53  // This constructor gets the current layout, the node that may be
54  // moved and its new position and computes the data for the
55  // modified layout.
56  UniformGrid(const GraphAttributes &,const node, const DPoint&);
57 
58  // Takes a UniformGrid and produces a new grid for the updated layout
59  UniformGrid(const UniformGrid &, const node, const DPoint&);
60 
61  int numberOfCrossings() const {return m_crossNum;}
62 
63  bool newGridNecessary(const node v, const DPoint& p) {
64  bool resize = false;
66  computeGridGeometry(v,p,ir);
67  double l = max(ir.width(),ir.height());
68  l /= m_edgeMultiplier*(m_graph).numberOfEdges();
69  if(l <= m_CellSize/2.0 || l >= m_CellSize*2.0) resize = true;
70  return resize;
71  }
72 
73 private:
74  void ModifiedBresenham(const IPoint &, const IPoint &, SList<IPoint> &) const;
75 
76  // This takes two DPoints with and computes a list of points
77  // that are the lower left corners of the cells that may possibly contain points
78  // of the straight line segment connecting the two points
79  void DoubleModifiedBresenham(const DPoint &, const DPoint &, SList<IPoint> &) const;
80 
81  // this function computes the grid coordinate of a point that depends on the
82  // coordiantes of the point, the lower left corner of the bounding rectangle
83  // and the size of a cell
84  IPoint computeGridPoint(const DPoint &dp) const {
85  double x = floor(dp.m_x / m_CellSize);
86  OGDF_ASSERT(isInt(x));
87  double y = floor(dp.m_y / m_CellSize);
88  OGDF_ASSERT(isInt(y));
89  return IPoint(int(x),int(y));
90  }
91 
92  // computes for a grid point the corresponding DPoint
93  DPoint computeRealPoint(const IPoint &ip) const {
94  DPoint p;
95  p.m_x = ip.m_x*m_CellSize;
96  p.m_y = ip.m_y*m_CellSize;
97  return p;
98  }
99 
100  // checks if a double number is an integer
101  bool isInt(double d) const {
102  if(d - floor(d) > 0) return false;
103  if(d < numeric_limits<int>::min() || d > numeric_limits<int>::max())
104  return false;
105  return true;
106  }
107 
108  // computes the crossings of the given edges for the given layout
109  // with the node moved to the position given as argument
110  void computeCrossings(const List<edge>&, const node, const DPoint&);
111 
112  // computes the geometry of the grid if the node is moved
113  // to the position given by the point
114  void computeGridGeometry(const node, const DPoint&, DIntersectableRect&) const;
115 
116  // Checks if two edges cross inside the given cell.
117  // The node and the point are the moved node and its/new position
118  bool crossingTest(
119  const edge,
120  const edge,
121  const node,
122  const DPoint&,
123  const IPoint&);
124 
125 #ifdef OGDF_DEBUG
126  void markCells(SList<IPoint> &, Array2D<bool> &) const;
127 
128  template<typename TPoint, typename TNum>
129  bool crossesCell(TPoint &A, TPoint &B,
130  TNum xlow, TNum xhigh, TNum ylow, TNum yhigh,
131  const IPoint &CellAdr) const
132  {
133  bool crosses;
134  if (A.m_x == B.m_x) { // line segment is vertical
135  crosses = A.m_x >= xlow
136  && A.m_x < xhigh
137  && intervalIntersect(A.m_y, B.m_y, ylow, yhigh);
138  } else { // line segment not vertical
139  if (A.m_x > B.m_x) {
140  swap(A, B);
141  }
142  double m = (B.m_y - A.m_y) / (B.m_x - A.m_x);
143  double c = A.m_y - A.m_x * m;
144  double y1 = m * xlow + c;
145  double y2 = m * xhigh + c;
146  crosses = intervalIntersect(A.m_x, B.m_x, xlow, xhigh)
147  && intervalIntersect(min(A.m_y, B.m_y),
148  max(A.m_y, B.m_y),
149  ylow, yhigh)
150  && intervalIntersect(y1, y2, ylow, yhigh);
151  }
152  return crosses;
153  }
154 
155  bool crossesCell(IPoint, IPoint, const IPoint &) const;
156  bool crossesCell(DPoint, DPoint, const IPoint &) const;
157 
158  void checkBresenham(DPoint, DPoint) const;
159  void checkBresenham(IPoint, IPoint) const;
160  bool intervalIntersect(double,double,double,double) const;
161  friend ostream& operator<<(ostream &,const UniformGrid&);
162  int m_crossingTests;
163  int m_maxEdgesPerCell;
164  double m_time;
165 #endif
166  const GraphAttributes &m_layout; // the layout
167  const Graph &m_graph;
168  HashArray2D<int,int,List<edge> > m_grid; // stores for each grid cell
169  // the Array of edges that cross that cell
170  EdgeArray<List<edge> > m_crossings; // stores for each edge the edges
171  //its crossings in the current layout
172  EdgeArray<List<IPoint> > m_cells; //Contains for each edge the list of cells it crosses
173  double m_CellSize; // Sidelength of one cell
174  const static double m_epsilon; // tolerance fo double computation
175  const static double m_edgeMultiplier; // this controls the gridsize
176  int m_crossNum; // number of crossings
177 
178  UniformGrid& operator=(const UniformGrid& ug);
179 };
180 
181 }}
Declaration of singly linked lists and iterators.
Declaration of class HashArray2D.
bool crossingTest(const edge, const edge, const node, const DPoint &, const IPoint &)
NUMBER m_y
The y-coordinate.
Definition: geometry.h:78
Declaration of class GraphAttributes which extends a Graph by additional attributes.
double height() const
Returns the height of the rectangle.
Definition: geometry.h:573
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
double width() const
Returns the width of the rectangle.
Definition: geometry.h:568
void ModifiedBresenham(const IPoint &, const IPoint &, SList< IPoint > &) const
void computeCrossings(const List< edge > &, const node, const DPoint &)
NUMBER m_x
The x-coordinate.
Definition: geometry.h:77
Rectangles with real coordinates.
Definition: geometry.h:657
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
void DoubleModifiedBresenham(const DPoint &, const DPoint &, SList< IPoint > &) const
Indexed 2-dimensional arrays using hashing for element access.
Definition: HashArray2D.h:65
Real points.
Definition: geometry.h:219
Singly linked lists (maintaining the length of the list).
Definition: SList.h:777
ostream & operator<<(ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:764
Integer points.
Definition: geometry.h:156
UniformGrid(const GraphAttributes &)
static const double m_epsilon
Definition: UniformGrid.h:174
Stores additional attributes of a graph (like layout information).
const GraphAttributes & m_layout
Definition: UniformGrid.h:166
EdgeArray< List< edge > > m_crossings
Definition: UniformGrid.h:170
EdgeArray< List< IPoint > > m_cells
Definition: UniformGrid.h:172
bool newGridNecessary(const node v, const DPoint &p)
Definition: UniformGrid.h:63
Class for the representation of edges.
Definition: Graph_d.h:291
IPoint computeGridPoint(const DPoint &dp) const
Definition: UniformGrid.h:84
Class for the representation of nodes.
Definition: Graph_d.h:167
static const double m_edgeMultiplier
Definition: UniformGrid.h:175
UniformGrid & operator=(const UniformGrid &ug)
HashArray2D< int, int, List< edge > > m_grid
Definition: UniformGrid.h:168
DPoint computeRealPoint(const IPoint &ip) const
Definition: UniformGrid.h:93
void computeGridGeometry(const node, const DPoint &, DIntersectableRect &) const
bool isInt(double d) const
Definition: UniformGrid.h:101
The namespace for all OGDF objects.
Declaration of classes DPoint, DPolyline, DLine, DRect, DIntersectableRect, DScaler.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
Declaration and implementation of class Array2D which implements dynamic two dimensional arrays...