Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
polygon_simplification_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
34
35# include <tikzgeom_algorithms.H>
36# include <cassert>
37# include <cmath>
38# include <random>
39# include <iostream>
40# include <fstream>
41
42using namespace std;
43using namespace Aleph;
44
45static Polygon make_noisy_circle(size_t n, double radius, double noise,
46 unsigned seed = 42)
47{
50
51 Polygon poly;
52 for (size_t i = 0; i < n; ++i)
53 {
54 double angle = 2.0 * M_PI * i / n;
55 double r = radius + dist(rng);
56 poly.add_vertex(Point(r * cos(angle), r * sin(angle)));
57 }
58 poly.close();
59 return poly;
60}
61
62int main()
63{
64 // 1. Generate noisy circle
65 Polygon original = make_noisy_circle(40, 10.0, 1.5);
67 cout << "Original: " << orig_verts.size() << " vertices" << endl;
68
69 // 2. Douglas-Peucker at two epsilon values
73
76 cout << "DP mild: " << dpv_mild.size() << " vertices" << endl;
77 cout << "DP aggressive: " << dpv_aggr.size() << " vertices" << endl;
78
79 // 3. Visvalingam-Whyatt at two thresholds
81 Polygon vw_mild = vw.simplify_polygon(original, Geom_Number(1));
82 Polygon vw_aggressive = vw.simplify_polygon(original, Geom_Number(10));
83
86 cout << "VW mild: " << vwv_mild.size() << " vertices" << endl;
87 cout << "VW aggressive: " << vwv_aggr.size() << " vertices" << endl;
88
89 // 4. Assert simplified vertices are subsets of original
90 auto is_subset = [&](const Array<Point> & sub)
91 {
92 for (size_t i = 0; i < sub.size(); ++i)
93 {
94 bool found = false;
95 for (size_t j = 0; j < orig_verts.size(); ++j)
96 if (orig_verts(j) == sub(i))
97 { found = true; break; }
98 assert(found && "Simplified vertex not in original");
99 }
100 };
105 cout << "All simplified vertices are subsets of original. OK" << endl;
106
107 // 5. Produce TikZ output with 4 sub-figures
108 ofstream tex("polygon_simplification_output.tex");
109 tex << "\\documentclass[border=5mm]{standalone}\n"
110 << "\\usepackage{tikz}\n"
111 << "\\usepackage{subcaption}\n"
112 << "\\begin{document}\n"
113 << "\\begin{figure}\n";
114
115 auto draw_subfig = [&](const char * caption, const Polygon & simplified)
116 {
117 Tikz_Plane plane(60, 60);
119 tex << "\\begin{minipage}{0.45\\textwidth}\\centering\n";
120 plane.draw(tex);
121 tex << "\\captionof{subfigure}{" << caption << "}\n"
122 << "\\end{minipage}\\hfill\n";
123 };
124
125 draw_subfig("Original (40 pts)", original);
126 draw_subfig("DP mild (eps=0.5)", dp_mild);
127 tex << "\n\\medskip\n\n";
128 draw_subfig("DP aggressive (eps=3)", dp_aggressive);
129 draw_subfig("VW aggressive (area=10)", vw_aggressive);
130
131 tex << "\\end{figure}\n"
132 << "\\end{document}\n";
133
134 cout << "TikZ output written to polygon_simplification_output.tex" << endl;
135
136 return 0;
137}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Douglas-Peucker polyline simplification.
Polygon simplify_polygon(const Polygon &poly, const Geom_Number &epsilon) const
Simplify a closed polygon.
static Array< Point > extract_vertices(const Polygon &poly)
Extract vertices from a polygon into an array for indexed access.
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
2D TikZ canvas storing geometry objects and emitting LaTeX output.
Definition tikzgeom.H:184
void draw(std::ostream &output, const bool squarize=true) const
Emit a complete tikzpicture with all inserted objects.
Definition tikzgeom.H:1383
Visvalingam-Whyatt polyline simplification.
static mt19937 rng
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_cos_function > > cos(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4069
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4070
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:115
void put_simplification_result(Tikz_Plane &plane, const Polygon &original, const Polygon &simplified, const Tikz_Style &original_style=tikz_wire_style("gray!50"), const Tikz_Style &simplified_style=tikz_wire_style("blue!80", 1.2), const bool draw_vertices=true, const Tikz_Style &vertex_style=tikz_points_style("red"))
Draw original and simplified polygons overlaid.
STL namespace.
static Polygon make_noisy_circle(size_t n, double radius, double noise, unsigned seed=42)
ValueArg< size_t > seed
Definition testHash.C:48
gsl_rng * r
Helpers to visualize computational-geometry algorithm results in TikZ.