Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tikz_tree_structures_example.cc
Go to the documentation of this file.
1#include <fstream>
2#include <iostream>
3#include <string>
4
6
7using namespace Aleph;
8
9namespace
10{
11
13{
15 pts.append(Point(10, 10));
16 pts.append(Point(20, 30));
17 pts.append(Point(35, 12));
18 pts.append(Point(48, 42));
19 pts.append(Point(60, 18));
20 pts.append(Point(78, 36));
21 pts.append(Point(88, 8));
22 return pts;
23}
24
26{
28 pts.append(Point(2, 2));
29 pts.append(Point(6, 7));
30 pts.append(Point(9, 3));
31 pts.append(Point(13, 8));
32 pts.append(Point(16, 1));
33 pts.append(Point(19, 6));
34 return pts;
35}
36
38{
39 AABBTree tree;
41 entries.append({Rectangle(0, 0, 4, 4), 0});
42 entries.append({Rectangle(3, 2, 9, 7), 1});
43 entries.append({Rectangle(11, 1, 15, 5), 2});
44 entries.append({Rectangle(14, 4, 19, 9), 3});
45 entries.append({Rectangle(6, 8, 10, 12), 4});
46 tree.build(entries);
47 return tree;
48}
49
50} // namespace
51
52int main(int argc, char * argv[])
53{
54 const std::string output_path =
55 argc > 1 ? argv[1] : "tikz_tree_structures_example.tex";
56
57 std::ofstream out(output_path);
58 if (not out)
59 {
60 std::cerr << "Cannot open output file: " << output_path << '\n';
61 return 1;
62 }
63
64 Tikz_Plane kd_plane(200, 120, 6, 6);
65 kd_plane.put_cartesian_axis();
66 kd_plane.put_coordinate_grid(5, 5, true);
67 kd_plane.enable_auto_legend(true);
68 kd_plane.set_point_radius_mm(0.65);
69
70 const auto kd = KDTreePointSearch::build(make_kd_points(), 0, 0, 100, 50);
71 const auto kd_snap = visualize_kdtree_partitions(kd_plane, kd, true, true);
73 Text(Point(-2, 52),
74 "KD-tree partitions=" + std::to_string(kd_snap.partitions.size())),
75 make_tikz_draw_style("black"),
77
78 Tikz_Plane range_plane(200, 120, 6, 6);
79 range_plane.put_cartesian_axis();
80 range_plane.put_coordinate_grid(2, 2, true);
81 range_plane.enable_auto_legend(true);
82 range_plane.set_point_radius_mm(0.75);
83
86 const Rectangle range_query(5, 2, 15, 7);
89 Text(Point(0, 10),
90 "Range-tree nodes=" + std::to_string(rv.snapshot.nodes.size()) +
91 ", hits=" + std::to_string(rv.query_hits.size())),
92 make_tikz_draw_style("black"),
94
95 Tikz_Plane aabb_plane(200, 120, 6, 6);
96 aabb_plane.put_cartesian_axis();
97 aabb_plane.put_coordinate_grid(2, 2, true);
98 aabb_plane.enable_auto_legend(true);
99
101 const Rectangle aabb_query(2, 1, 8, 6);
104 Text(Point(-1, 13),
105 "AABB nodes=" + std::to_string(av.snapshot.nodes.size()) +
106 ", hits=" + std::to_string(av.query_hit_ids.size())),
107 make_tikz_draw_style("black"),
109
110 out << "\\documentclass[tikz,border=8pt]{standalone}\n"
111 << "\\usepackage{tikz}\n"
112 << "\\begin{document}\n\n";
113
114 kd_plane.draw(out, true);
115 out << "\n\\vspace{4mm}\n\n";
116 range_plane.draw(out, true);
117 out << "\n\\vspace{4mm}\n\n";
118 aabb_plane.draw(out, true);
119 out << "\n\\end{document}\n";
120
121 std::cout << "Generated " << output_path << '\n';
122 std::cout << "Compile with: pdflatex " << output_path << '\n';
123 return 0;
124}
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.
Definition tpl_array.H:139
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
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.
Definition point.H:229
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.
Definition point.H:1737
Represents a text string positioned at a 2D point.
Definition point.H:2739
2D TikZ canvas storing geometry objects and emitting LaTeX output.
Definition tikzgeom.H:184
static constexpr int Layer_Overlay
Definition tikzgeom.H:189
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
void put_in_plane(Tikz_Plane &plane, const Geom &geom_obj)
Insert any supported geometry type in a Tikz_Plane.
Definition tikzgeom.H:1456
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.
Tikz_Style make_tikz_draw_style(const std::string &draw_color)
Create a basic draw style with a custom color.
Definition tikzgeom.H:156
Helpers to visualize computational-geometry algorithm results in TikZ.