Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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