Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tikz_funnel_beamer_handout_example.cc
Go to the documentation of this file.
1#include <fstream>
2#include <iomanip>
3#include <iostream>
4#include <sstream>
5#include <string>
6
8
9using namespace Aleph;
10
11namespace
12{
13
15{
16 Polygon p;
17 p.add_vertex(Point(0, 0));
18 p.add_vertex(Point(24, 0));
19 p.add_vertex(Point(24, 20));
20 p.add_vertex(Point(14, 20));
21 p.add_vertex(Point(14, 8));
22 p.add_vertex(Point(10, 8));
23 p.add_vertex(Point(10, 20));
24 p.add_vertex(Point(0, 20));
25 p.close();
26 return p;
27}
28
29std::string step_event(const FunnelTraceStep & step)
30{
31 if (step.emitted_left) return "emit-left";
32 if (step.emitted_right) return "emit-right";
33 if (step.tightened_left and step.tightened_right) return "tighten-both";
34 if (step.tightened_left) return "tighten-left";
35 if (step.tightened_right) return "tighten-right";
36 return "noop";
37}
38
39std::string fmt_point(const Point & p)
40{
41 std::ostringstream ss;
42 ss << "(" << std::fixed << std::setprecision(2)
43 << geom_number_to_double(p.get_x()) << ","
44 << geom_number_to_double(p.get_y()) << ")";
45 return ss.str();
46}
47
48std::string render_trace_step_tikz(const Polygon & polygon,
49 const Point & source,
50 const Point & target,
52 const size_t step_index)
53{
54 Tikz_Plane plane(178, 108, 0, 0);
55 plane.put_cartesian_axis();
56 plane.set_point_radius_mm(0.70);
57
59 plane,
60 polygon,
61 source,
62 target,
63 trace,
65 tikz_area_style("black", "gray!15", 0.22),
66 tikz_points_style("green!50!black"),
67 tikz_points_style("blue"),
68 tikz_wire_style("purple", true),
69 tikz_path_style("purple"),
70 tikz_path_style("orange!90!black"),
71 tikz_path_style("red"),
72 true,
73 tikz_points_style("red"));
74
75 std::ostringstream ss;
76 plane.draw(ss, true);
77 return ss.str();
78}
79
80std::string render_final_tikz(const Polygon & polygon,
81 const Point & source,
82 const Point & target)
83{
84 Tikz_Plane plane(178, 108, 0, 0);
85 plane.put_cartesian_axis();
86 plane.set_point_radius_mm(0.70);
87
89 plane, polygon, source, target, ShortestPathInPolygon());
90
91 std::ostringstream ss;
92 plane.draw(ss, true);
93 return ss.str();
94}
95
97 const size_t step_index)
98{
99 const FunnelTraceStep & step = trace.steps(step_index);
100
101 std::ostringstream ss;
102 ss << "\\small\\textbf{Step " << (step_index + 1) << "/" << trace.steps.size()
103 << "}\\\\\n"
104 << "\\footnotesize event: \\texttt{" << step_event(step) << "}\\\\\n"
105 << "portal: " << step.portal_index << "\\\\[1.2mm]\n"
106 << "\\begin{tabular}{@{}ll@{}}\n"
107 << "Apex & " << fmt_point(step.apex) << "\\\\\n"
108 << "Left & " << fmt_point(step.left_boundary) << "\\\\\n"
109 << "Right & " << fmt_point(step.right_boundary) << "\\\\\n"
110 << "L-portal & " << fmt_point(step.portal_left) << "\\\\\n"
111 << "R-portal & " << fmt_point(step.portal_right) << "\\\\\n"
112 << "Committed & " << step.committed_path.size() << " pts\\\\\n"
113 << "\\end{tabular}";
114
115 return ss.str();
116}
117
118std::string render_final_panel(const FunnelTraceResult & trace)
119{
120 std::ostringstream ss;
121 ss << "\\small\\textbf{Final Path}\\\\\n"
122 << "\\footnotesize frames: " << (trace.steps.size() + 1) << "\\\\\n"
123 << "portals: " << trace.portals.size() << "\\\\\n"
124 << "path nodes: " << trace.final_path.size() << "\\\\[1.2mm]\n"
125 << "\\begin{tabular}{@{}ll@{}}\n";
126
127 if (trace.final_path.is_empty())
128 {
129 ss << "Source & N/A\\\\\n"
130 << "Target & N/A\\\\\n";
131 }
132 else
133 {
134 ss << "Source & " << fmt_point(trace.final_path(0)) << "\\\\\n"
135 << "Target & " << fmt_point(trace.final_path(trace.final_path.size() - 1))
136 << "\\\\\n";
137 }
138
139 ss << "\\end{tabular}";
140
141 return ss.str();
142}
143
144void write_twocol_frame(std::ostream & out,
145 const std::string & title,
146 const std::string & left,
147 const std::string & right)
148{
149 out << "\\begin{frame}[t]{" << title << "}\n"
150 << "\\begin{columns}[T,totalwidth=\\textwidth]\n"
151 << "\\begin{column}{0.70\\textwidth}\n"
152 << left << '\n'
153 << "\\end{column}\n"
154 << "\\begin{column}{0.29\\textwidth}\n"
155 << right << '\n'
156 << "\\end{column}\n"
157 << "\\end{columns}\n"
158 << "\\end{frame}\n\n";
159}
160
161} // namespace
162
163int main(int argc, char * argv[])
164{
165 const std::string output_path =
166 argc > 1 ? argv[1] : "tikz_funnel_beamer_handout_example.tex";
167
168 std::ofstream out(output_path);
169 if (not out)
170 {
171 std::cerr << "Cannot open output file: " << output_path << '\n';
172 return 1;
173 }
174
175 const Polygon polygon = make_funnel_polygon();
176 const Point source(2, 16);
177 const Point target(22, 16);
178
180 compute_shortest_path_funnel_trace(polygon, source, target);
181
182 out << "\\documentclass[handout]{beamer}\n"
183 << "\\usepackage{tikz}\n"
184 << "\\setbeamertemplate{navigation symbols}{}\n"
185 << "\\begin{document}\n\n";
186
187 if (trace.steps.size() == 0)
188 {
190 out,
191 "Shortest Path Funnel Trace (Handout)",
192 render_final_tikz(polygon, source, target),
193 "\\small\\textbf{No funnel iterations}\\\\\\n"
194 "Direct/degenerate case");
195 }
196 else
197 {
198 for (size_t i = 0; i < trace.steps.size(); ++i)
199 {
200 const std::string title =
201 "Shortest Path Funnel Trace (Handout) - Step " +
202 std::to_string(i + 1);
204 out,
205 title,
206 render_trace_step_tikz(polygon, source, target, trace, i),
208 }
209
211 out,
212 "Shortest Path Funnel Trace (Handout) - Final",
213 render_final_tikz(polygon, source, target),
215 }
216
217 out << "\\end{document}\n";
218
219 std::cout << "Generated " << output_path << '\n'
220 << "Frames: " << (trace.steps.size() + 1) << '\n'
221 << "Compile with: pdflatex " << output_path << '\n';
222
223 return 0;
224}
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:457
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:466
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 shortest Euclidean path between two points inside a simple polygon.
2D TikZ canvas storing geometry objects and emitting LaTeX output.
Definition tikzgeom.H:184
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
void put_funnel_trace_step(Tikz_Plane &plane, const Polygon &polygon, const Point &source, const Point &target, const FunnelTraceResult &trace, size_t step_index, const Tikz_Style &polygon_style=tikz_area_style("black", "gray!15", 0.22), const Tikz_Style &source_style=tikz_points_style("green!50!black"), const Tikz_Style &target_style=tikz_points_style("blue"), const Tikz_Style &all_portals_style=tikz_wire_style("purple", true), const Tikz_Style &active_portal_style=tikz_path_style("purple"), const Tikz_Style &funnel_leg_style=tikz_path_style("orange!90!black"), const Tikz_Style &committed_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 highlight_layer=Tikz_Plane::Layer_Overlay)
Render one funnel-trace frame in a plane.
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.
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
Definition point.H:122
Tikz_Style tikz_path_style(const std::string &color="red", const bool with_arrow=false)
Creates a style optimized for polyline paths.
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.
FunnelTraceResult compute_shortest_path_funnel_trace(const Polygon &polygon, const Point &source, const Point &target)
Compute a full SSFA trace (portal-by-portal states).
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.
Full trace for shortest-path funnel processing.
One SSFA (funnel) iteration snapshot.
Helpers to visualize computational-geometry algorithm results in TikZ.