Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tikz_advanced_algorithms_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{
14 Array<Segment> segments;
15 segments.append(Segment(Point(-28, 0), Point(28, 0)));
16 segments.append(Segment(Point(0, -24), Point(0, 24)));
17 segments.append(Segment(Point(-24, -18), Point(24, 18)));
18 segments.append(Segment(Point(-24, 18), Point(24, -18)));
19 return segments;
20}
21
23{
24 Polygon p;
25 p.add_vertex(Point(0, 0));
26 p.add_vertex(Point(24, 0));
27 p.add_vertex(Point(24, 20));
28 p.add_vertex(Point(14, 20));
29 p.add_vertex(Point(14, 8));
30 p.add_vertex(Point(10, 8));
31 p.add_vertex(Point(10, 20));
32 p.add_vertex(Point(0, 20));
33 p.close();
34 return p;
35}
36
38{
39 DynList<Point> points;
40 points.append(Point(-18, -10));
41 points.append(Point(-14, 12));
42 points.append(Point(-4, 17));
43 points.append(Point(8, 15));
44 points.append(Point(17, 8));
45 points.append(Point(20, -8));
46 points.append(Point(10, -16));
47 points.append(Point(-2, -18));
48 points.append(Point(2, 2));
49 points.append(Point(-6, 4));
50 return points;
51}
52
54{
55 Polygon p;
56 p.add_vertex(Point(0, 0));
57 p.add_vertex(Point(16, 0));
58 p.add_vertex(Point(16, 12));
59 p.add_vertex(Point(10, 12));
60 p.add_vertex(Point(10, 5));
61 p.add_vertex(Point(6, 5));
62 p.add_vertex(Point(6, 12));
63 p.add_vertex(Point(0, 12));
64 p.close();
65 return p;
66}
67
68} // namespace
69
70int main(int argc, char * argv[])
71{
72 const std::string output_path =
73 argc > 1 ? argv[1] : "tikz_advanced_algorithms_example.tex";
74
75 std::ofstream out(output_path);
76 if (not out)
77 {
78 std::cerr << "Cannot open output file: " << output_path << '\n';
79 return 1;
80 }
81
82 // Figure 1: Segment arrangement.
83 Tikz_Plane arrangement_plane(210, 115, 6, 6);
84 arrangement_plane.put_cartesian_axis();
85 arrangement_plane.set_point_radius_mm(0.65);
86
91 true,
92 true,
93 false,
94 tikz_area_style("teal!60!black", "teal!12", 0.35),
95 tikz_wire_style("teal!70!black"),
96 tikz_points_style("teal!80!black"),
97 true);
98
100 Text(Point(-30, 26),
101 "Segment Arrangement: V=" + std::to_string(arrangement.vertices.size()) +
102 ", E=" + std::to_string(arrangement.edges.size())),
103 make_tikz_draw_style("black"),
105
106 // Figure 2: Shortest path in polygon.
107 Tikz_Plane shortest_plane(210, 115, 6, 6);
108 shortest_plane.put_cartesian_axis();
109 shortest_plane.set_point_radius_mm(0.75);
110
112 const Point source(2, 16);
113 const Point target(22, 16);
114
117 corridor,
118 source,
119 target,
121 tikz_area_style("black", "gray!15", 0.28),
122 tikz_points_style("green!50!black"), // source
123 tikz_points_style("blue"), // target
124 tikz_wire_style("purple", true), // portals
125 tikz_path_style("red"), // final path
126 true,
127 tikz_points_style("red"));
128
129 size_t path_nodes = 0;
130 for (DynList<Point>::Iterator it(shortest_debug.path); it.has_curr(); it.next_ne())
131 ++path_nodes;
132
134 Text(Point(-1, 22),
135 "Shortest Path + Portals: path nodes=" +
136 std::to_string(path_nodes) +
137 ", portals=" + std::to_string(shortest_debug.portals.size())),
138 make_tikz_draw_style("black"),
140
141 // Figure 3: Convex decomposition.
142 Tikz_Plane decomp_plane(210, 115, 6, 6);
143 decomp_plane.put_cartesian_axis();
144 decomp_plane.set_point_radius_mm(0.70);
145
151 true,
152 tikz_wire_style("black", true),
153 true,
154 tikz_area_style("blue!60!black", "blue!15", 0.38));
155
157 Text(Point(-2, 14),
158 "Convex Decomposition: parts=" +
159 std::to_string(decomp_parts.size())),
160 make_tikz_draw_style("black"),
162
163 // Figure 4: Alpha shape.
164 Tikz_Plane alpha_plane(210, 115, 6, 6);
165 alpha_plane.put_cartesian_axis();
166 alpha_plane.set_point_radius_mm(0.75);
167
173 AlphaShape(),
174 true,
175 tikz_wire_style("gray!55"),
176 tikz_path_style("orange!90!black"),
177 true,
178 tikz_points_style("black"));
179
181 Text(Point(-22, 22),
182 "Alpha Shape: boundary edges=" +
183 std::to_string(alpha_shape.boundary_edges.size())),
184 make_tikz_draw_style("black"),
186
187 out << "\\documentclass[tikz,border=8pt]{standalone}\n"
188 << "\\usepackage{tikz}\n"
189 << "\\begin{document}\n\n";
190
191 arrangement_plane.draw(out, true);
192 out << "\n\\vspace{5mm}\n\n";
193 shortest_plane.draw(out, true);
194 out << "\n\\vspace{5mm}\n\n";
195 decomp_plane.draw(out, true);
196 out << "\n\\vspace{5mm}\n\n";
197 alpha_plane.draw(out, true);
198
199 out << "\n\\end{document}\n";
200
201 std::cout << "Generated " << output_path << '\n'
202 << "Compile with: pdflatex " << output_path << '\n';
203
204 return 0;
205}
Alpha shape of a point set.
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
Decompose a simple polygon into convex parts using Hertel-Mehlhorn.
Iterator on the items of list.
Definition htlist.H:1420
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:246
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:677
void close()
Close the polygon.
Definition polygon.H:840
Compute the full planar subdivision induced by a set of segments.
Represents a line segment between two points.
Definition point.H:827
Compute the shortest Euclidean path between two points inside a simple polygon.
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
void put_in_plane(Tikz_Plane &plane, const Geom &geom_obj)
Insert any supported geometry type in a Tikz_Plane.
Definition tikzgeom.H:1456
AlphaShape::Result visualize_alpha_shape(Tikz_Plane &plane, const DynList< Point > &points, const Geom_Number &alpha_squared, const AlphaShape &algorithm={}, const bool draw_kept_triangles=false, const Tikz_Style &triangle_style=tikz_wire_style("gray!55"), const Tikz_Style &boundary_style=tikz_path_style("orange!90!black"), const bool draw_sites=true, const Tikz_Style &site_style=tikz_points_style("black"))
Compute and insert alpha-shape for input 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.
ShortestPathDebugResult visualize_shortest_path_with_portals(Tikz_Plane &plane, const Polygon &polygon, const Point &source, const Point &target, const ShortestPathInPolygon &algorithm={}, const Tikz_Style &polygon_style=tikz_area_style("black", "gray!15", 0.25), const Tikz_Style &source_style=tikz_points_style("green!50!black"), const Tikz_Style &target_style=tikz_points_style("blue"), const Tikz_Style &portal_style=tikz_wire_style("purple", true), const Tikz_Style &path_style=tikz_path_style("red"), const bool draw_waypoints=true, const Tikz_Style &waypoint_style=tikz_points_style("red"), const int polygon_layer=Tikz_Plane::Layer_Default, const int portal_layer=Tikz_Plane::Layer_Foreground, const int path_layer=Tikz_Plane::Layer_Overlay)
Visualize the shortest path plus funnel portals.
Tikz_Style tikz_path_style(const std::string &color="red", const bool with_arrow=false)
Creates a style optimized for polyline paths.
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:115
Tikz_Style make_tikz_draw_style(const std::string &draw_color)
Create a basic draw style with a custom color.
Definition tikzgeom.H:156
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.
SegmentArrangement::Result visualize_segment_arrangement(Tikz_Plane &plane, const Array< Segment > &segments, const SegmentArrangement &algorithm={}, const bool draw_faces=true, const bool draw_vertices=true, const bool draw_unbounded_face=false, const Tikz_Style &face_style=tikz_area_style("teal!60!black", "teal!12", 0.30), const Tikz_Style &edge_style=tikz_wire_style("teal!70!black"), const Tikz_Style &vertex_style=tikz_points_style("teal!70!black"), const bool color_faces_by_index=false)
Compute and insert arrangement for input segments.
Tikz_Style tikz_area_style(const std::string &draw_color="black", const std::string &fill_color="gray!25", const double opacity=0.6)
Creates a style for drawing filled polygons.
Tikz_Style tikz_points_style(const std::string &color="black", const double opacity=-1.0)
Creates a style optimized for point clouds.
Array< Polygon > visualize_convex_decomposition(Tikz_Plane &plane, const Polygon &polygon, const ConvexPolygonDecomposition &algorithm={}, const bool draw_input_polygon=true, const Tikz_Style &input_style=tikz_wire_style("black", true), const bool color_parts_by_index=true, const Tikz_Style &part_style=tikz_area_style("blue!60!black", "blue!15", 0.40), const int input_layer=Tikz_Plane::Layer_Default, const int part_layer=Tikz_Plane::Layer_Foreground)
Compute and insert convex decomposition for a polygon.
Result of an alpha-shape computation.
Result bundle for shortest-path + funnel portal visualization.
Helpers to visualize computational-geometry algorithm results in TikZ.