Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
quadnode.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
32
44# ifndef QUADNODE_H
45# define QUADNODE_H
46
47# include <htlist.H>
48# include <point.H>
49# include <ah-errors.H>
50
51using namespace Aleph;
52
53# define PARENT(p) ((p)->get_parent())
54# define NW_CHILD(p) ((p)->get_nw_child())
55# define NE_CHILD(p) ((p)->get_ne_child())
56# define SW_CHILD(p) ((p)->get_sw_child())
57# define SE_CHILD(p) ((p)->get_se_child())
58// As that of rbNode.H, so that the two can be included together
59# define COLOR(p) ((p)->getColor())
60# define LEVEL(p) ((p)->get_level())
61
95{
96public:
97
105 enum class Color
106 {
107 White,
108 Gray,
109 Black,
111 };
112
121 enum class Quad
122 {
123 NW,
124 NE,
125 SW,
126 SE,
128 };
129
134 enum class Side
135 {
136 North,
137 South,
138 East,
139 West,
141 };
142
143private:
145
151
153 unsigned long level;
154
155 // Region boundaries
160
162 static QuadNode * get_north_neighbor(QuadNode * v) noexcept
163 {
164 if (PARENT(v) == nullptr)
165 return nullptr;
166
167 if (v->is_sw_child())
168 return NW_CHILD(PARENT(v));
169
170 if (v->is_se_child())
171 return NE_CHILD(PARENT(v));
172
174
175 if (u == nullptr or u->is_leaf())
176 return u;
177 else if (v->is_nw_child())
178 return SW_CHILD(u);
179 else
180 return SE_CHILD(u);
181 }
182
184 static QuadNode * get_south_neighbor(QuadNode * v) noexcept
185 {
186 if (PARENT(v) == nullptr)
187 return nullptr;
188
189 if (v->is_nw_child())
190 return SW_CHILD(PARENT(v));
191
192 if (v->is_ne_child())
193 return SE_CHILD(PARENT(v));
194
196
197 if (u == nullptr or u->is_leaf())
198 return u;
199 else if (v->is_sw_child())
200 return NW_CHILD(u);
201 else
202 return NE_CHILD(u);
203 }
204
206 static QuadNode * get_east_neighbor(QuadNode * v) noexcept
207 {
208 if (PARENT(v) == nullptr)
209 return nullptr;
210
211 if (v->is_nw_child())
212 return NE_CHILD(PARENT(v));
213
214 if (v->is_sw_child())
215 return SE_CHILD(PARENT(v));
216
218
219 if (u == nullptr or u->is_leaf())
220 return u;
221 else if (v->is_ne_child())
222 return NW_CHILD(u);
223 else
224 return SW_CHILD(u);
225 }
226
228 static QuadNode * get_west_neighbor(QuadNode * v) noexcept
229 {
230 if (PARENT(v) == nullptr)
231 return nullptr;
232
233 if (v->is_ne_child())
234 return NW_CHILD(PARENT(v));
235
236 if (v->is_se_child())
237 return SW_CHILD(PARENT(v));
238
240
241 if (u == nullptr or u->is_leaf())
242 return u;
243 else if (v->is_nw_child())
244 return NE_CHILD(u);
245 else
246 return SE_CHILD(u);
247 }
248
250 static void get_neighbors_by_side(QuadNode * node,
251 const Side & side,
252 DynList<QuadNode *> & neighbors)
253 {
254 if (node == nullptr)
255 return;
256
257 if (node->is_leaf())
258 {
259 neighbors.append(node);
260 return;
261 }
262
263 switch(side)
264 {
265 case Side::West:
266 get_neighbors_by_side(NW_CHILD(node), side, neighbors);
267 get_neighbors_by_side(SW_CHILD(node), side, neighbors);
268 break;
269 case Side::East:
270 get_neighbors_by_side(NE_CHILD(node), side, neighbors);
271 get_neighbors_by_side(SE_CHILD(node), side, neighbors);
272 break;
273 case Side::North:
274 get_neighbors_by_side(NW_CHILD(node), side, neighbors);
275 get_neighbors_by_side(NE_CHILD(node), side, neighbors);
276 break;
277 case Side::South:
278 get_neighbors_by_side(SW_CHILD(node), side, neighbors);
279 get_neighbors_by_side(SE_CHILD(node), side, neighbors);
280 break;
281 default:
282 ah_domain_error_if(true) << "Invalid side value";
283 }
284}
285
287static size_t count_points(QuadNode * node) noexcept
288{
289 if (node == nullptr)
290 return 0;
291
292 if (node->is_leaf())
293 return node->points.size();
294
295 size_t num_points = 0;
296
297 num_points += count_points(NW_CHILD(node));
298 num_points += count_points(NE_CHILD(node));
299 num_points += count_points(SW_CHILD(node));
300 num_points += count_points(SE_CHILD(node));
301
302 return num_points;
303}
304
305public:
306
308 QuadNode() noexcept
309 : points(), parent(nullptr), nw_child(nullptr), ne_child(nullptr),
310 sw_child(nullptr), se_child(nullptr), color(Color::White), level(0),
311 min_x(0), max_x(0), min_y(0), max_y(0)
312 {
313 // Empty
314 }
315
325 QuadNode(const Geom_Number & _min_x, const Geom_Number & _max_x,
326 const Geom_Number & _min_y, const Geom_Number & _max_y,
327 QuadNode * _parent = nullptr)
328 : points(), parent(_parent), nw_child(nullptr), ne_child(nullptr),
329 sw_child(nullptr), se_child(nullptr), color(Color::White), level(0),
330 min_x(_min_x), max_x(_max_x), min_y(_min_y), max_y(_max_y)
331 {
333 << "QuadNode bounds must define a non-empty region";
334 }
335
336 QuadNode(const QuadNode &) = delete;
337
346 void set_region(const Geom_Number & _min_x,
347 const Geom_Number & _max_x,
348 const Geom_Number & _min_y,
349 const Geom_Number & _max_y)
350 {
351 ah_domain_error_if(_min_x >= _max_x or _min_y >= _max_y)
352 << "QuadNode bounds must define a non-empty region";
353
354 min_x = _min_x;
355 max_x = _max_x;
356 min_y = _min_y;
357 max_y = _max_y;
358 }
359
361 [[nodiscard]] QuadNode *& get_parent() noexcept { return parent; }
362
364 [[nodiscard]] QuadNode *& get_nw_child() noexcept { return nw_child; }
365
367 [[nodiscard]] QuadNode *& get_ne_child() noexcept { return ne_child; }
368
370 [[nodiscard]] QuadNode *& get_sw_child() noexcept { return sw_child; }
371
373 [[nodiscard]] QuadNode *& get_se_child() noexcept { return se_child; }
374
376 [[nodiscard]] Color & get_color() noexcept { return color; }
377
379 [[nodiscard]] Color & getColor() noexcept { return color; }
380
382 [[nodiscard]] unsigned long & get_level() noexcept { return level; }
383
385 [[nodiscard]] bool is_leaf() const noexcept { return color != Color::Gray; }
386
388 [[nodiscard]] bool is_nw_child() const noexcept
389 {
390 if (parent == nullptr)
391 return false;
392 return NW_CHILD(parent) == this;
393 }
394
396 [[nodiscard]] bool is_ne_child() const noexcept
397 {
398 if (parent == nullptr)
399 return false;
400 return NE_CHILD(parent) == this;
401 }
402
404 [[nodiscard]] bool is_sw_child() const noexcept
405 {
406 if (parent == nullptr)
407 return false;
408 return SW_CHILD(parent) == this;
409 }
410
412 [[nodiscard]] bool is_se_child() const noexcept
413 {
414 if (parent == nullptr)
415 return false;
416 return SE_CHILD(parent) == this;
417 }
418
427 [[nodiscard]] bool contains(const Point & p) const noexcept
428 {
429 return p.get_x() >= min_x and p.get_x() < max_x and
430 p.get_y() >= min_y and p.get_y() < max_y;
431 }
432
442 [[nodiscard]] QuadNode * get_child_to(const Point & p) const
443 {
444 assert(nw_child != nullptr);
445 assert(ne_child != nullptr);
446 assert(sw_child != nullptr);
447 assert(se_child != nullptr);
448
450 << "This node does not contain that point";
451
452 if (nw_child->contains(p))
453 return nw_child;
454
455 if (ne_child->contains(p))
456 return ne_child;
457
458 if (sw_child->contains(p))
459 return sw_child;
460
461 if (se_child->contains(p))
462 return se_child;
463
464 return nullptr; // To avoid compiler warnings
465 }
466
475 Point & add_point(const Point & p)
476 {
477 color = Color::Black;
478 return points.append(p);
479 }
480
482 [[nodiscard]] size_t get_num_points() noexcept
483 {
484 return count_points(this);
485 }
486
488 [[nodiscard]] const Geom_Number & get_min_x() const noexcept { return min_x; }
489
491 [[nodiscard]] const Geom_Number & get_max_x() const noexcept { return max_x; }
492
494 [[nodiscard]] const Geom_Number & get_min_y() const noexcept { return min_y; }
495
497 [[nodiscard]] const Geom_Number & get_max_y() const noexcept { return max_y; }
498
500 [[nodiscard]] Geom_Number get_width() const noexcept { return max_x - min_x; }
501
503 [[nodiscard]] Geom_Number get_height() const noexcept { return max_y - min_y; }
504
506 [[nodiscard]] Geom_Number get_mid_x() const noexcept { return (min_x + max_x) / 2; }
507
509 [[nodiscard]] Geom_Number get_mid_y() const noexcept { return (min_y + max_y) / 2; }
510
518 [[nodiscard]] Point * search_point(const Point & p) noexcept
519 {
520 for (Point & point : points)
521 {
522 if (point == p)
523 return &point;
524 }
525 return nullptr;
526 }
527
533 bool remove_point(const Point & p)
534 {
535 for (DynList<Point>::iterator it(points); it.has_curr(); it.next_ne())
536 {
537 const Point & curr = it.get_curr_ne();
538
539 if (curr == p)
540 {
541 it.del();
542 if (points.is_empty())
543 color = Color::White;
544 return true;
545 }
546 }
547 return false;
548 }
549
551 void empty() noexcept
552 {
553 points.empty();
554 color = Color::White;
555 }
556
565 {
566 DynList<QuadNode *> neighbors;
567
568 QuadNode * north_neighbor = get_north_neighbor(this);
569 get_neighbors_by_side(north_neighbor, Side::South, neighbors);
570
571 QuadNode * south_neighbor = get_south_neighbor(this);
572 get_neighbors_by_side(south_neighbor, Side::North, neighbors);
573
574 QuadNode * east_neighbor = get_east_neighbor(this);
575 get_neighbors_by_side(east_neighbor, Side::West, neighbors);
576
577 QuadNode * west_neighbor = get_west_neighbor(this);
578 get_neighbors_by_side(west_neighbor, Side::East, neighbors);
579
580 return neighbors;
581 }
582
584 [[nodiscard]] DynList<Point> & get_points_set() noexcept { return points; }
585
591 template <class Op>
592 void for_each_point(Op & op)
593 {
594 for (Point & point : points)
595 op(point);
596 };
597
599 template <class Op>
600 void for_each_point(Op && op = Op())
601 {
602 for_each_point<Op>(op);
603 }
604
605 QuadNode & operator = (const QuadNode &) = delete;
606};
607
608# endif // QUADNODE_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
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
Node for QuadTree spatial data structure.
Definition quadnode.H:95
DynList< QuadNode * > get_neighbors()
Find all adjacent leaf nodes.
Definition quadnode.H:564
Color & get_color() noexcept
Get reference to color.
Definition quadnode.H:376
Side
Cardinal directions for neighbor navigation.
Definition quadnode.H:135
@ South
Southern neighbor.
@ North
Northern neighbor.
@ East
Eastern neighbor.
@ West
Western neighbor.
bool is_sw_child() const noexcept
Check if this node is the SW child of its parent.
Definition quadnode.H:404
static QuadNode * get_east_neighbor(QuadNode *v) noexcept
Find the eastern neighbor of a node.
Definition quadnode.H:206
QuadNode(const Geom_Number &_min_x, const Geom_Number &_max_x, const Geom_Number &_min_y, const Geom_Number &_max_y, QuadNode *_parent=nullptr)
Construct a node with specified region bounds.
Definition quadnode.H:325
static size_t count_points(QuadNode *node) noexcept
Recursively count all points in a subtree.
Definition quadnode.H:287
QuadNode *& get_sw_child() noexcept
Get reference to SW child (for reading/writing via SW_CHILD macro).
Definition quadnode.H:370
Geom_Number min_y
Minimum Y coordinate.
Definition quadnode.H:158
QuadNode *& get_se_child() noexcept
Get reference to SE child (for reading/writing via SE_CHILD macro).
Definition quadnode.H:373
static QuadNode * get_west_neighbor(QuadNode *v) noexcept
Find the western neighbor of a node.
Definition quadnode.H:228
static void get_neighbors_by_side(QuadNode *node, const Side &side, DynList< QuadNode * > &neighbors)
Recursively collect leaf neighbors on a given side.
Definition quadnode.H:250
bool contains(const Point &p) const noexcept
Check if a point is contained in this node's region.
Definition quadnode.H:427
unsigned long & get_level() noexcept
Get reference to level (for reading/writing via LEVEL macro).
Definition quadnode.H:382
Geom_Number get_height() const noexcept
Get height of this region.
Definition quadnode.H:503
Geom_Number max_x
Maximum X coordinate.
Definition quadnode.H:157
static QuadNode * get_south_neighbor(QuadNode *v) noexcept
Find the southern neighbor of a node.
Definition quadnode.H:184
bool is_ne_child() const noexcept
Check if this node is the NE child of its parent.
Definition quadnode.H:396
Geom_Number max_y
Maximum Y coordinate.
Definition quadnode.H:159
QuadNode *& get_ne_child() noexcept
Get reference to NE child (for reading/writing via NE_CHILD macro).
Definition quadnode.H:367
QuadNode() noexcept
Default constructor - creates a white (empty) node.
Definition quadnode.H:308
const Geom_Number & get_min_y() const noexcept
Get minimum Y coordinate of this region.
Definition quadnode.H:494
bool is_se_child() const noexcept
Check if this node is the SE child of its parent.
Definition quadnode.H:412
const Geom_Number & get_max_y() const noexcept
Get maximum Y coordinate of this region.
Definition quadnode.H:497
void for_each_point(Op &&op=Op())
Apply operation to each point (rvalue reference version).
Definition quadnode.H:600
QuadNode * ne_child
Northeast child.
Definition quadnode.H:148
Geom_Number min_x
Minimum X coordinate.
Definition quadnode.H:156
void empty() noexcept
Remove all points from this node and mark it as White.
Definition quadnode.H:551
Geom_Number get_mid_x() const noexcept
Get midpoint X coordinate.
Definition quadnode.H:506
QuadNode * parent
Parent node (nullptr for root)
Definition quadnode.H:146
QuadNode * get_child_to(const Point &p) const
Find which child node should contain a given point.
Definition quadnode.H:442
DynList< Point > & get_points_set() noexcept
Get reference to the points list (for direct manipulation).
Definition quadnode.H:584
Geom_Number get_mid_y() const noexcept
Get midpoint Y coordinate.
Definition quadnode.H:509
Quad
Quadrant directions for child nodes.
Definition quadnode.H:122
@ SW
Southwest quadrant.
@ NW
Northwest quadrant.
@ NE
Northeast quadrant.
@ SE
Southeast quadrant.
Color & getColor() noexcept
Get reference to color (for reading/writing via COLOR macro).
Definition quadnode.H:379
const Geom_Number & get_min_x() const noexcept
Get minimum X coordinate of this region.
Definition quadnode.H:488
QuadNode * sw_child
Southwest child.
Definition quadnode.H:149
Geom_Number get_width() const noexcept
Get width of this region.
Definition quadnode.H:500
bool is_nw_child() const noexcept
Check if this node is the NW child of its parent.
Definition quadnode.H:388
static QuadNode * get_north_neighbor(QuadNode *v) noexcept
Find the northern neighbor of a node.
Definition quadnode.H:162
QuadNode *& get_nw_child() noexcept
Get reference to NW child (for reading/writing via NW_CHILD macro).
Definition quadnode.H:364
QuadNode & operator=(const QuadNode &)=delete
Point & add_point(const Point &p)
Add a point to this node.
Definition quadnode.H:475
void set_region(const Geom_Number &_min_x, const Geom_Number &_max_x, const Geom_Number &_min_y, const Geom_Number &_max_y)
Set the region boundaries for this node.
Definition quadnode.H:346
unsigned long level
Depth in the tree (0 for root)
Definition quadnode.H:153
Point * search_point(const Point &p) noexcept
Search for a point in this node.
Definition quadnode.H:518
void for_each_point(Op &op)
Apply operation to each point in this node.
Definition quadnode.H:592
QuadNode(const QuadNode &)=delete
QuadNode * se_child
Southeast child.
Definition quadnode.H:150
bool is_leaf() const noexcept
Check if this node is a leaf (has no children).
Definition quadnode.H:385
DynList< Point > points
Points stored in this node (only for leaves)
Definition quadnode.H:144
bool remove_point(const Point &p)
Remove a point from this node.
Definition quadnode.H:533
Color color
Node state (White/Gray/Black)
Definition quadnode.H:152
size_t get_num_points() noexcept
Get total number of points in this subtree.
Definition quadnode.H:482
const Geom_Number & get_max_x() const noexcept
Get maximum X coordinate of this region.
Definition quadnode.H:491
Color
Node color states for quadtree nodes.
Definition quadnode.H:106
@ White
Empty leaf node.
@ Gray
Internal node with children.
@ Black
Leaf node with points.
QuadNode *& get_parent() noexcept
Get reference to parent node (for reading/writing via PARENT macro).
Definition quadnode.H:361
QuadNode * nw_child
Northwest child.
Definition quadnode.H:147
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
2D point and geometric utilities.
#define PARENT(p)
Definition quadnode.H:53
#define SE_CHILD(p)
Definition quadnode.H:57
#define NE_CHILD(p)
Definition quadnode.H:55
#define NW_CHILD(p)
Definition quadnode.H:54
#define SW_CHILD(p)
Definition quadnode.H:56