Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_euclidian_graph.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
78#ifndef TPL_EUCLIDIAN_GRAPH_H
79#define TPL_EUCLIDIAN_GRAPH_H
80
81#include <tpl_graph.H>
82#include <point.H>
83#include <ah-errors.H>
84
85namespace Aleph {
90template <typename Node_Info>
91class Euclidian_Node : public Graph_Node<Node_Info>
92{
93public:
95
97
99
100private:
102
103public:
105 {
106 /* Empty */
107 }
108
110 {
111 /* Empty */
112 }
113
114 Euclidian_Node(const Point &_position) : Graph_Node<Node_Info>(), position(_position)
115 {
116 /* Empty */
117 }
118
119 Euclidian_Node(const Node_Info &info, const Point &_position)
120 : Graph_Node<Node_Info>(info), position(_position)
121 {
122 /* Empty */
123 }
124
126 {
127 /* Empty */
128 }
129
131 {
132 return position;
133 }
134
135 const Point &get_position() const
136 {
137 return position;
138 }
139}; // End class Euclidian_Node
140
141template <typename Arc_Info>
142class Euclidian_Arc : public Graph_Arc<Arc_Info>
143{
144public:
146
148
150
152 {
153 /* Empty */
154 }
155
157 {
158 /* Empty */
159 }
160
161 Euclidian_Arc(void *src, void *tgt, const Arc_Info &info) : Graph_Arc<Arc_Info>(src, tgt, info)
162 {
163 /* Empty */
164 }
165
166 Euclidian_Arc(void *src, void *tgt) : Graph_Arc<Arc_Info>(src, tgt)
167 {
168 /* Empty */
169 }
170}; // End class Euclidian_Arc
171
172template <class __Euclidian_Node, class __Euclidian_Arc>
173class Euclidian_Graph : public List_Graph<__Euclidian_Node, __Euclidian_Arc>
174{
175public:
176 typedef __Euclidian_Node Node;
177
178 typedef __Euclidian_Arc Arc;
179
181
182 typedef typename Node::Node_Type Node_Type;
183
184 typedef typename Arc::Arc_Type Arc_Type;
185
187 {
188 /* Empty */
189 }
190
192 {
193 copy_from(euclidian_graph);
194 }
195
196 Node *insert_node(Node *node) noexcept override
197 {
198 return Graph::insert_node(node);
199 }
200
202 {
203 return insert_node(new Node(info));
204 }
205
206 Node *insert_node(const Point &position)
207 {
208 return insert_node(new Node(position));
209 }
210
211 Node *insert_node(const Node_Type &info, const Point &position)
212 {
213 return insert_node(new Node(info, position));
214 }
215
217 {
218 const Point &src_point = this->get_src_node(arc)->get_position();
219 const Point &tgt_point = this->get_tgt_node(arc)->get_position();
220 return src_point.distance_to(tgt_point);
221 }
222
230 {
231 if (this == &eg)
232 return *this;
233
235 copy.digraph = this->digraph;
236 copy.copy_from(eg);
237 copy.get_cookie() = this->get_cookie();
238 this->swap(copy);
239 return *this;
240 }
241
243 {
244 clear_graph(*this);
245 }
246
247 Node *search_node(const Point &);
248
249protected:
256 {
258 for (typename Graph::Node_Iterator it(src); it.has_curr(); it.next_ne())
259 {
260 Node *p = it.get_curr();
261 image.insert(p, insert_node(new Node(p))); // its info and position
262 }
263
264 for (typename Graph::Arc_Iterator it(src); it.has_curr(); it.next_ne())
265 {
266 Arc *a = it.get_curr();
267 Arc *copy = Graph::insert_arc(image.find(src.get_src_node(a)),
268 image.find(src.get_tgt_node(a)));
269 *copy = *a;
270 }
271 }
272}; // End class Euclidian_Graph
273
274template <class __Euclidian_Node, class __Euclidian_Arc>
275class Euclidian_Digraph : public Euclidian_Graph<__Euclidian_Node, __Euclidian_Arc>
276{
277public:
282
283 // Directed before the copy, as Digraph does, so that each arc goes only
284 // in the list of its source
286 : Euclidian_Graph<__Euclidian_Node, __Euclidian_Arc>()
287 {
289 this->copy_from(euclidian_digraph);
290 }
291
298}; // End class Euclidian_Digraph
299
300template <class Node, class Arc>
302{
303 for (typename Euclidian_Graph<Node, Arc>::Node_Iterator it(*this); it.has_curr(); it.next_ne())
304 if (auto *curr = it.get_curr(); curr->get_position() == point)
305 return curr;
306 return nullptr;
307}
308
309template <class __Euclidian_Graph>
311{
316
318
321
324
325public:
327 : ptr_east_point(nullptr), ptr_north_point(nullptr), ptr_west_point(nullptr),
329 {
330 // Empty
331 }
332
333 Abstract_Euclidian_Plane(__Euclidian_Graph &graph)
334 : ptr_east_point(nullptr), ptr_north_point(nullptr), ptr_west_point(nullptr),
336 {
337 if (graph.get_num_nodes() < 1)
338 return;
339
340 typename __Euclidian_Graph::Node_Iterator itor(graph);
341 points.append(itor.get_curr()->get_position());
343
344 for (int i = 1; itor.has_curr(); itor.next_ne(), ++i)
345 {
346 const Point &p = itor.get_curr()->get_position();
347 points.append(p);
348 if (p.get_x() < ptr_west_point->get_x())
349 ptr_west_point = &points.access(i);
350 if (p.get_y() > ptr_north_point->get_y())
351 ptr_north_point = &points.access(i);
352 if (p.get_x() > ptr_east_point->get_x())
353 ptr_east_point = &points.access(i);
354 if (p.get_y() < ptr_south_point->get_y())
355 ptr_south_point = &points.access(i);
356 }
357 }
358
360 {
361 /* Empty */
362 }
363
364 Point &add_point(typename __Euclidian_Graph::Node *node)
365 {
366 ah_domain_error_if(node == nullptr) << "node is nullptr";
367 points.append(node->get_position());
368 Point &p = points.top();
369 if (points.size() == 1)
371 else
372 {
373 if (p.get_x() < ptr_west_point->get_x())
374 ptr_west_point = &p;
375 if (p.get_y() > ptr_north_point->get_y())
376 ptr_north_point = &p;
377 if (p.get_x() > ptr_east_point->get_x())
378 ptr_east_point = &p;
379 if (p.get_y() < ptr_south_point->get_y())
380 ptr_south_point = &p;
381 }
382 return p;
383 }
384
385 const Point &get_west_point() const
386 {
387 ah_logic_error_if(points.size() < 1) << "There are no points on plane";
388 return *ptr_west_point;
389 }
390
391 const Point &get_north_point() const
392 {
393 ah_logic_error_if(points.size() < 1) << "There are no points on plane";
394 return *ptr_north_point;
395 }
396
397 const Point &get_east_point() const
398 {
399 ah_logic_error_if(points.size() < 1) << "There are no points on plane";
400 return *ptr_east_point;
401 }
402
403 const Point &get_south_point() const
404 {
405 ah_logic_error_if(points.size() < 1) << "There are no points on plane";
406 return *ptr_south_point;
407 }
408
410 {
411 if (points.size() < 1)
412 return Geom_Number(0);
414 }
415
417 {
418 if (points.size() < 1)
419 return Geom_Number(0);
421 }
422
424 {
425 return x_node_ratio;
426 }
427
428 void set_x_node_ratio(const Geom_Number &_x_node_ratio)
429 {
430 x_node_ratio = _x_node_ratio;
431 }
432
434 {
435 return y_node_ratio;
436 }
437
438 void set_y_node_ratio(const Geom_Number &_y_node_ratio)
439 {
440 y_node_ratio = _y_node_ratio;
441 }
442
444 {
445 return x_scale;
446 }
447
448 void set_x_scale(const Geom_Number &_x_scale)
449 {
450 x_scale = _x_scale;
451 }
452
454 {
455 return y_scale;
456 }
457
458 void set_y_scale(const Geom_Number &_y_scale)
459 {
460 y_scale = _y_scale;
461 }
462}; // End class Abstract_Euclidian_Plane
463} // End namespace Aleph
464
465#endif // TPL_EUCLIDIAN_GRAPH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
Definition ah-errors.H:474
WeightedDigraph::Node Node
const Geom_Number & get_x_node_ratio() const
Point & add_point(typename __Euclidian_Graph::Node *node)
void set_y_node_ratio(const Geom_Number &_y_node_ratio)
const Geom_Number & get_y_node_ratio() const
void set_x_scale(const Geom_Number &_x_scale)
void set_x_node_ratio(const Geom_Number &_x_node_ratio)
const Geom_Number & get_y_scale() const
const Geom_Number & get_x_scale() const
void set_y_scale(const Geom_Number &_y_scale)
Abstract_Euclidian_Plane(__Euclidian_Graph &graph)
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
Euclidian_Arc(void *src, void *tgt, const Arc_Info &info)
Euclidian_Arc(void *src, void *tgt)
Euclidian_Arc(const Arc_Info &info)
Euclidian_Digraph< __Euclidian_Node, __Euclidian_Arc > & operator=(const Euclidian_Digraph< __Euclidian_Node, __Euclidian_Arc > &eg)
Euclidian_Digraph(const Euclidian_Digraph< __Euclidian_Node, __Euclidian_Arc > &euclidian_digraph)
Euclidian_Graph(const Euclidian_Graph< Node, Arc > &euclidian_graph)
Node * insert_node(const Point &position)
void copy_from(const Euclidian_Graph< Node, Arc > &src)
Copies into this graph, which must be empty, the nodes of src, with their positions,...
Geom_Number get_distance(Arc *arc)
Node * insert_node(const Node_Type &info, const Point &position)
Node * insert_node(Node *node) noexcept override
Insertion of a node already allocated.
Node * search_node(const Point &)
Euclidian_Graph< Node, Arc > & operator=(const Euclidian_Graph< Node, Arc > &eg)
Replaces the contents of this graph with a copy of eg.
List_Graph< Node, Arc > Graph
Node * insert_node(const Node_Type &info)
Euclidian_Node(const Point &_position)
const Point & get_position() const
Euclidian_Node(const Node_Info &info, const Point &_position)
Euclidian_Node(Euclidian_Node *node)
Euclidian_Node(const Node_Info &info)
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition tpl_graph.H:1007
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:448
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:457
Geom_Number distance_to(const Point &p) const
Calculates the Euclidean distance to another point.
Definition point.H:1498
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
Definition graph-dry.H:931
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:113
2D point and geometric utilities.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:126
Generic graph and digraph implementations.