56struct QuadTreeSnapshot
63void collect_quadtree_rec(
QuadNode * node,
const size_t depth, QuadTreeSnapshot & snap)
72 snap.leaf_depths.append(depth);
73 for (
const Point & p : node->get_points_set())
74 snap.points.append(p);
78 collect_quadtree_rec(node->
get_nw_child(), depth + 1, snap);
79 collect_quadtree_rec(node->
get_ne_child(), depth + 1, snap);
80 collect_quadtree_rec(node->
get_sw_child(), depth + 1, snap);
81 collect_quadtree_rec(node->
get_se_child(), depth + 1, snap);
84QuadTreeSnapshot collect_quadtree(
QuadTree & tree)
86 QuadTreeSnapshot snap;
87 collect_quadtree_rec(tree.
get_root(), 0, snap);
93std::string depth_color(
const size_t depth)
95 static const char * colors[] =
96 {
"blue!55",
"orange!70!black",
"green!55!black",
"red!60",
"violet!65",
"brown!60"};
97 constexpr size_t num_colors =
sizeof(colors) /
sizeof(colors[0]);
98 return colors[depth % num_colors];
131 for (
int i = 0; i < 24; ++i)
133 const int col = i % 6;
134 const int row = i / 6;
144int main(
int argc,
char * argv[])
146 const std::string output_path =
147 argc > 1 ? argv[1] :
"tikz_tree_structures_example.tex";
149 std::ofstream
out(output_path);
152 std::cerr <<
"Cannot open output file: " << output_path <<
'\n';
166 "KD-tree partitions=" + std::to_string(kd_snap.partitions.size())),
177 range_tree.
build(make_range_points());
178 const Rectangle range_query(5, 2, 15, 7);
182 "Range-tree nodes=" + std::to_string(rv.snapshot.nodes.size()) +
183 ", hits=" + std::to_string(rv.query_hits.size())),
192 const AABBTree aabb_tree = make_aabb_tree();
197 "AABB nodes=" + std::to_string(av.snapshot.nodes.size()) +
198 ", hits=" + std::to_string(av.query_hit_ids.size())),
212 for (
const Point & p : make_quadtree_points())
215 const QuadTreeSnapshot qsnap = collect_quadtree(quadtree);
216 for (
size_t i = 0; i < qsnap.leaf_regions.size(); ++i)
223 "QuadTree leaves=" + std::to_string(qsnap.leaf_regions.size()) +
224 ", points=" + std::to_string(qsnap.points.size())),
240 for (
size_t i = 0; i < rects.
size(); ++i)
242 rtree.
insert(rects(i),
static_cast<int>(i));
243 rstar_tree.
insert(rects(i),
static_cast<int>(i));
246 const Rectangle rtree_query(8, 5, 28, 20);
250 "R-tree (Guttman) nodes=" + std::to_string(rt_result.snapshot.nodes.size()) +
251 ", hits=" + std::to_string(rt_result.query_hit_boxes.size())),
262 "R*-tree nodes=" + std::to_string(rstar_result.snapshot.nodes.size()) +
263 ", hits=" + std::to_string(rstar_result.query_hit_boxes.size())),
267 out <<
"\\documentclass[tikz,border=8pt]{standalone}\n"
268 <<
"\\usepackage{tikz}\n"
269 <<
"\\begin{document}\n\n";
272 out <<
"\n\\vspace{4mm}\n\n";
274 out <<
"\n\\vspace{4mm}\n\n";
276 out <<
"\n\\vspace{4mm}\n\n";
277 quadtree_plane.
draw(
out,
true);
278 out <<
"\n\\vspace{4mm}\n\n";
280 out <<
"\n\\vspace{4mm}\n\n";
282 out <<
"\n\\end{document}\n";
284 std::cout <<
"Generated " << output_path <<
'\n';
285 std::cout <<
"Compile with: pdflatex " << output_path <<
'\n';
size_t size_t int32_t * out
Axis-aligned bounding box tree for spatial queries.
size_t build(Array< size_t > &idx, const size_t lo, const size_t hi)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
static KDTreePointSearch build(const Array< Point > &points, const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Build a balanced KD-tree from a point array.
Represents a point with rectangular coordinates in a 2D plane.
Dynamic R-tree indexing axis-aligned rectangles by payload.
void insert(const Rectangle &bbox, const Payload &value)
Insert a (bbox, value) entry, copying value.
Static 2D range tree for orthogonal range queries.
void build(const DynList< Point > &points)
Build the range tree from a point set.
An axis-aligned rectangle.
Represents a text string positioned at a 2D point.
2D TikZ canvas storing geometry objects and emitting LaTeX output.
void draw(std::ostream &output, const bool squarize=true) const
Emit a complete tikzpicture with all inserted objects.
void put_cartesian_axis()
Enable Cartesian axes drawing (only when 0 lies in range).
static constexpr int Layer_Default
void enable_auto_legend(const bool enabled=true)
Enable auto-legend generation from style colors.
void put_coordinate_grid(const double step_x=1.0, const double step_y=1.0, const bool draw_ticks=true)
Enable coordinate grid/ticks with user step sizes.
void set_point_radius_mm(const double &radius_mm)
Configure point marker radius.
static constexpr int Layer_Overlay
Node for QuadTree spatial data structure.
QuadNode *& get_sw_child() noexcept
Get reference to SW child (for reading/writing via SW_CHILD macro).
QuadNode *& get_se_child() noexcept
Get reference to SE child (for reading/writing via SE_CHILD macro).
QuadNode *& get_ne_child() noexcept
Get reference to NE child (for reading/writing via NE_CHILD macro).
const Geom_Number & get_min_y() const noexcept
Get minimum Y coordinate of this region.
const Geom_Number & get_max_y() const noexcept
Get maximum Y coordinate of this region.
const Geom_Number & get_min_x() const noexcept
Get minimum X coordinate of this region.
QuadNode *& get_nw_child() noexcept
Get reference to NW child (for reading/writing via NW_CHILD macro).
bool is_leaf() const noexcept
Check if this node is a leaf (has no children).
const Geom_Number & get_max_x() const noexcept
Get maximum X coordinate of this region.
QuadTree - Hierarchical spatial index for 2D points.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Node * get_root() noexcept
Get the root node.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y0_function > > y0(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
AABBTreeQueryVizResult visualize_aabb_tree_query(Tikz_Plane &plane, const AABBTree &tree, const Rectangle &query_rect, const Tikz_Style &node_bbox_style=tikz_wire_style("teal!70!black"), const Tikz_Style &leaf_bbox_style=tikz_wire_style("blue!70"), const Tikz_Style &query_rect_style=tikz_wire_style("red", true), const Tikz_Style &query_hit_style=tikz_wire_style("red"))
Visualize AABB tree with a rectangle query overlay.
RangeTreeQueryVizResult visualize_range_tree_query(Tikz_Plane &plane, const RangeTree2D &tree, const Rectangle &query_rect, const bool draw_points=true, const Tikz_Style &split_style=tikz_wire_style("purple"), const Tikz_Style &point_style=tikz_points_style("black"), const Tikz_Style &query_rect_style=tikz_wire_style("red", true), const Tikz_Style &query_hit_style=tikz_points_style("red"))
Visualize range-tree plus a query rectangle and matching points.
void put_in_plane(Tikz_Plane &plane, const Geom &geom_obj)
Insert any supported geometry type in a Tikz_Plane.
KDTreePointSearch::DebugSnapshot visualize_kdtree_partitions(Tikz_Plane &plane, const KDTreePointSearch &kd_tree, const bool draw_partition_boxes=false, const bool draw_points=true, const Tikz_Style &partition_style=tikz_wire_style("gray!55", true), const Tikz_Style &split_style=tikz_wire_style("blue!70"), const Tikz_Style &point_style=tikz_points_style("red"))
Visualize KD-tree recursive space partitions.
void put_points(Tikz_Plane &plane, const Array< Point > &pts, const Tikz_Style &style=tikz_points_style(), const int layer=Tikz_Plane::Layer_Default)
Inserts all points from an Array<Point> into the plane.
mpq_class Geom_Number
Numeric type used by the geometry module.
RTreeQueryVizResult< Payload, MaxEntries, MinEntries, Variant > visualize_rtree_query(Tikz_Plane &plane, const RTree< Payload, MaxEntries, MinEntries, Variant > &tree, const Rectangle &query_rect, const Tikz_Style &node_bbox_style=tikz_wire_style("teal!70!black"), const Tikz_Style &leaf_bbox_style=tikz_wire_style("blue!70"), const Tikz_Style &entry_bbox_style=tikz_wire_style("gray!55"), const Tikz_Style &query_rect_style=tikz_wire_style("red", true), const Tikz_Style &query_hit_style=tikz_wire_style("red"))
Visualize an R-tree/R*-tree with a rectangle query overlay, highlighting every entry whose bbox inter...
Tikz_Style make_tikz_draw_style(const std::string &draw_color)
Create a basic draw style with a custom color.
Tikz_Style tikz_wire_style(const std::string &color="black", const bool dashed=false, const bool with_arrow=false)
Creates a style optimized for wireframe segments and polygons.
Tikz_Style tikz_points_style(const std::string &color="black", const double opacity=-1.0)
Creates a style optimized for point clouds.
QuadTree spatial data structure for efficient 2D point indexing.
Helpers to visualize computational-geometry algorithm results in TikZ.
R*-tree: an R-tree tuned with the Beckmann-Kriegel-Schneider-Seeger heuristics.