Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
chaikin_smoothing_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
33
34# include <tikzgeom_algorithms.H>
35# include <cassert>
36# include <cmath>
37# include <iostream>
38# include <fstream>
39
40using namespace std;
41using namespace Aleph;
42
44static Polygon make_star(double outer_r = 10.0, double inner_r = 4.0)
45{
46 Polygon poly;
47 for (int i = 0; i < 10; ++i)
48 {
49 double angle = M_PI / 2.0 + 2.0 * M_PI * i / 10.0;
50 double r = (i % 2 == 0) ? outer_r : inner_r;
51 poly.add_vertex(Point(r * cos(angle), r * sin(angle)));
52 }
53 poly.close();
54 return poly;
55}
56
59{
60 Polygon poly;
61 poly.add_vertex(Point(0, 0));
62 poly.add_vertex(Point(6, 0));
63 poly.add_vertex(Point(6, 3));
64 poly.add_vertex(Point(3, 3));
65 poly.add_vertex(Point(3, 8));
66 poly.add_vertex(Point(0, 8));
67 poly.close();
68 return poly;
69}
70
71int main()
72{
73 // 1. Star polygon
76 cout << "Star: " << star_verts.size() << " vertices" << endl;
77
78 // Smooth at 1..4 iterations
79 for (size_t k = 1; k <= 4; ++k)
80 {
83 cout << " iter " << k << ": " << sv.size() << " vertices" << endl;
84 }
85
86 // 2. L-shaped polygon
89 cout << "L-shape: " << l_verts.size() << " vertices" << endl;
90
91 for (size_t k = 1; k <= 3; ++k)
92 {
95 cout << " iter " << k << ": " << sv.size() << " vertices" << endl;
96 }
97
98 // 3. TikZ output
99 ofstream tex("chaikin_smoothing_output.tex");
100 tex << "\\documentclass[border=5mm]{standalone}\n"
101 << "\\usepackage{tikz}\n"
102 << "\\usepackage{subcaption}\n"
103 << "\\begin{document}\n"
104 << "\\begin{figure}\n";
105
106 auto draw_subfig = [&](const char * caption, const Polygon & original,
107 const Polygon & smoothed)
108 {
109 Tikz_Plane plane(60, 60);
111 tex << "\\begin{minipage}{0.45\\textwidth}\\centering\n";
112 plane.draw(tex);
113 tex << "\\captionof{subfigure}{" << caption << "}\n"
114 << "\\end{minipage}\\hfill\n";
115 };
116
117 // Star: original, 1, 2, 4 iterations
118 draw_subfig("Star original", star, star);
120 draw_subfig("Star iter=1", star, star1);
121 tex << "\n\\medskip\n\n";
123 draw_subfig("Star iter=2", star, star2);
125 draw_subfig("Star iter=4", star, star4);
126
127 tex << "\n\\bigskip\n\n";
128
129 // L-shape: original, 1, 2, 3 iterations
130 draw_subfig("L-shape original", lshape, lshape);
132 draw_subfig("L-shape iter=1", lshape, l1);
133 tex << "\n\\medskip\n\n";
135 draw_subfig("L-shape iter=2", lshape, l2);
137 draw_subfig("L-shape iter=3", lshape, l3);
138
139 tex << "\\end{figure}\n"
140 << "\\end{document}\n";
141
142 cout << "TikZ output written to chaikin_smoothing_output.tex" << endl;
143
144 return 0;
145}
static Polygon make_L_shape()
Build an L-shaped polygon.
static Polygon smooth_polygon(const Polygon &poly, size_t iterations, const Geom_Number &ratio=Geom_Number(1, 4))
Smooth 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
static Polygon make_star()
Star-shaped polygon with 5 reflex vertices.
__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
void put_smoothing_result(Tikz_Plane &plane, const Polygon &original, const Polygon &smoothed, const Tikz_Style &original_style=tikz_wire_style("gray!50"), const Tikz_Style &smoothed_style=tikz_wire_style("violet!80", 1.2), const bool draw_vertices=true, const Tikz_Style &vertex_style=tikz_points_style("red"))
Draw original and smoothed polygons overlaid.
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.
STL namespace.
DynList< char > l3
DynList< int > l1
DynList< int > l2
static int * k
gsl_rng * r
Helpers to visualize computational-geometry algorithm results in TikZ.