33 #ifdef OGDF_INCLUDE_CGAL
61 template<
typename Kernel_,
typename Graph_>
62 class GeometricDrawing :
public Drawing<Kernel_, Graph_> {
64 using Kernel = Kernel_;
66 using Node =
typename Graph::Node;
67 using Edge =
typename Graph::Edge;
69 using Point = geometry::Point_t<Kernel>;
70 using LineSegment = geometry::LineSegment_t<Kernel>;
75 using PointToNodeMap = std::map<Point, Node>;
76 using PointToNodeMapIterator =
typename PointToNodeMap::iterator;
78 std::vector<Edge> visited_edges;
80 PointToNodeMap point_to_node;
82 datastructure::NodeVector<Point, Graph> node_to_point;
84 OGDFFaceWrapper m_outer_face;
85 bool outer_face_initialized {
false};
88 explicit GeometricDrawing(Graph& _g) : Drawing<Kernel_,
Graph>(_g), g(_g), node_to_point(g) {
92 inline void set_outer_face(OGDFFaceWrapper ef) {
94 outer_face_initialized =
true;
97 inline OGDFFaceWrapper& outer_face() {
102 inline const OGDFFaceWrapper& outer_face()
const {
107 inline void clear() {
108 point_to_node.clear();
109 node_to_point.clear();
110 outer_face_initialized =
false;
113 size_t number_of_points()
const {
return node_to_point.size(); }
115 const std::vector<Point>& points()
const {
return node_to_point; }
118 inline void set_point(
const Node& v,
const Point& p) {
119 OGDF_ASSERT((
size_t)v->index() <= g.max_node_index());
120 if (g.max_node_index() >= node_to_point.size()) {
121 node_to_point.resize(g.max_node_index() + 1);
123 node_to_point[v] = p;
126 inline Point get_point(
const Node& v)
const {
127 OGDF_ASSERT((
size_t)v->index() < node_to_point.size());
128 return node_to_point[v];
131 inline Point& operator[](
const Node& v) {
132 OGDF_ASSERT((
size_t)v->index() < node_to_point.size());
133 return node_to_point[v];
136 inline Point operator[](
const Node& v)
const {
return get_point(v); }
138 inline LineSegment get_segment(
const Edge& e)
const {
139 return {get_point(e->source()), get_point(e->target())};
142 inline geometry::Bbox bbox()
const {
148 geometry::Bbox bb(xmin, ymin, xmax, ymax);
149 for (Node v : g.nodes()) {
150 bb += get_point(v).bbox();
156 template<
typename Kernel,
typename Graph,
typename EdgeList>
157 void generate_graph_from_list(
const std::vector<geometry::Point_t<Kernel>>& nodes,
158 const EdgeList& edge_list, std::vector<typename Graph::Edge>& map_edge,
159 GeometricDrawing<Kernel, Graph>& d) {
160 using Node =
typename Graph::Node;
161 using Edge =
typename Graph::Edge;
162 using Point =
typename geometry::Point_t<Kernel>;
164 std::vector<Node> node_map;
165 std::vector<Point> map;
166 Graph& g = d.get_graph();
167 for (
const Point& p : nodes) {
168 node_map.push_back(g.add_node());
169 d.set_point(node_map.back(), p);
171 for (
const std::tuple<unsigned int, unsigned int, unsigned int>&
edge : edge_list) {
172 if (node_map[std::get<0>(
edge)] != node_map[std::get<1>(
edge)]) {
173 const Edge e = g.add_edge(node_map[std::get<0>(
edge)], node_map[std::get<1>(
edge)]);
174 map_edge.push_back(e);
176 map_edge.push_back(
nullptr);
180 for (
const auto e : map_edge) {
181 OGDF_ASSERT(!e || e->graphOf() == &g.get_ogdf_graph());
186 template<
typename Kernel,
typename Graph,
typename EdgeList>
187 void generate_graph_from_list(std::map<
unsigned int, geometry::Point_t<Kernel>>& nodes,
188 const EdgeList& edge_list, std::vector<typename Graph::Edge>& map_edge,
189 std::map<unsigned int, typename Graph::Node>& node_map, GeometricDrawing<Kernel, Graph>& d) {
190 using Edge =
typename Graph::Edge;
192 Graph& g = d.get_graph();
193 for (
const std::tuple<unsigned int, unsigned int, unsigned int>&
edge : edge_list) {
194 unsigned int source = std::get<0>(
edge);
195 unsigned int target = std::get<1>(
edge);
198 if (node_map.find(source) == node_map.end()) {
199 auto u = g.add_node();
200 d.set_point(u, nodes[source]);
201 node_map.insert(std::make_pair(source, u));
203 if (node_map.find(target) == node_map.end()) {
204 auto u = g.add_node();
205 d.set_point(u, nodes[target]);
206 node_map.insert(std::make_pair(target, u));
210 for (
const std::tuple<unsigned int, unsigned int, unsigned int>&
edge : edge_list) {
211 if (node_map[std::get<0>(
edge)] != node_map[std::get<1>(
edge)]) {
212 const Edge e = g.add_edge(node_map[std::get<0>(
edge)], node_map[std::get<1>(
edge)]);
214 map_edge.push_back(e);
216 map_edge.push_back(
nullptr);
221 for (
const auto e : map_edge) {
222 OGDF_ASSERT(!e || e->graphOf() == &g.get_ogdf_graph());
227 template<
typename Kernel,
typename Graph,
typename EdgeList>
228 void generate_graph_from_list(std::map<
unsigned int, geometry::Point_t<Kernel>>& nodes,
229 const EdgeList& edge_list, std::vector<typename Graph::Edge>& map_edge,
230 GeometricDrawing<Kernel, Graph>& g) {
231 std::map<unsigned int, typename Graph::Node> node_map;
232 generate_graph_from_list(nodes, edge_list, map_edge, node_map, g);
235 template<
typename Kernel,
typename Graph>
236 std::tuple<datastructure::NodeVector<typename Graph::Node, Graph>,
237 datastructure::EdgeVector<typename Graph::Edge, Graph>>
238 copy(GeometricDrawing<Kernel, Graph>& original, GeometricDrawing<Kernel, Graph>&
copy) {
239 using Node =
typename Graph::Node;
240 using Edge =
typename Graph::Edge;
242 datastructure::NodeVector<Node, Graph> node_map(original);
243 datastructure::EdgeVector<Edge, Graph> edge_map(original);
245 for (
auto v : original.nodes()) {
246 node_map[v] =
copy.add_node(original.get_point(v));
249 for (
auto e : original.edges()) {
250 edge_map[e] =
copy.add_edge(node_map[e->source()], node_map[e->target()]);
253 return std::make_tuple(node_map, edge_map);
256 template<
typename Kernel,
typename Graph>
257 void ogdf_attributes_to_geometric_drawing(
const GraphAttributes& ga,
258 GeometricDrawing<Kernel, Graph>& d) {
259 OGDF_ASSERT(&d.get_graph().get_ogdf_graph() == &ga.constGraph());
260 for (
auto v : d.get_graph().nodes()) {
261 geometry::Point_t<Kernel> p(ga.x(v), ga.y(v));
266 template<
typename Kernel,
typename Graph>
267 void geometric_drawing_to_ogdf_attributes(
const GeometricDrawing<Kernel, Graph>& d,
268 GraphAttributes& ga) {
269 OGDF_ASSERT(&d.get_graph().get_ogdf_graph() == &ga.constGraph());
270 for (
auto v : d.get_graph().nodes()) {
271 auto p = d.get_point(v);
272 ga.x(v) = CGAL::to_double(p.x());
273 ga.y(v) = CGAL::to_double(p.y());