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