Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
voronoi_clipped_cells_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
45# include <geom_algorithms.H>
46
47# include <cassert>
48# include <fstream>
49# include <iomanip>
50# include <iostream>
51
52using namespace Aleph;
53using namespace std;
54
55static void write_polygon_wkt(ostream & out, const Polygon & poly)
56{
57 out << "POLYGON((";
58
59 bool first = true;
61
62 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
63 {
64 const Point & p = it.get_current_vertex();
65 if (first)
66 {
67 first_point = p;
68 first = false;
69 }
70 else
71 {
72 out << ", ";
73 }
74
75 out << geom_number_to_double(p.get_x()) << " "
77 }
78
79 if (not first)
80 {
81 out << ", "
82 << geom_number_to_double(first_point.get_x()) << " "
84 }
85
86 out << "))";
87}
88
89static void export_cells_csv(
90 ostream & out,
92{
93 out << "site_index,site_x,site_y,vertex_count,wkt\n";
94
95 for (size_t i = 0; i < cells.size(); ++i)
96 {
97 const auto & c = cells(i);
98 out << c.site_index << ","
99 << geom_number_to_double(c.site.get_x()) << ","
100 << geom_number_to_double(c.site.get_y()) << ","
101 << c.polygon.size() << ",\"";
102
103 write_polygon_wkt(out, c.polygon);
104 out << "\"\n";
105 }
106}
107
108int main(int argc, char ** argv)
109{
111
112 // Site set (deterministic, includes interior and near-hull points).
113 auto vor = voronoi({
114 Point(0, 0), Point(6, 0), Point(8, 4), Point(5, 8), Point(1, 7),
115 Point(3, 3), Point(5, 4)
116 });
117
118 // Convex clipping window for bounded output.
120 clip.add_vertex(Point(-2, -2));
121 clip.add_vertex(Point(10, -2));
122 clip.add_vertex(Point(10, 10));
123 clip.add_vertex(Point(-2, 10));
124 clip.close();
125
127 voronoi.clipped_cells_indexed(vor, clip);
128
129 cout << fixed << setprecision(4);
130 cout << "Voronoi clipped cells (site-indexed)\n";
131 cout << "===================================\n";
132 cout << "Sites: " << vor.sites.size() << "\n";
133 cout << "Cells: " << cells.size() << "\n\n";
134
135 assert(cells.size() == vor.sites.size());
136
138 for (size_t i = 0; i < cells.size(); ++i)
139 {
140 const auto & c = cells(i);
141 cout << "Cell #" << c.site_index
142 << " site=(" << geom_number_to_double(c.site.get_x())
143 << ", " << geom_number_to_double(c.site.get_y()) << ")"
144 << " vertices=" << c.polygon.size() << "\n";
145
146 assert(c.polygon.is_closed());
147 assert(c.polygon.size() >= 3);
148 assert(pip.contains(c.polygon, c.site));
149 }
150
151 const char * path = (argc > 1) ? argv[1] : "voronoi_clipped_cells_output.csv";
152 ofstream file(path);
153 if (not file)
154 {
155 cerr << "Cannot open output file: " << path << "\n";
156 return 1;
157 }
158
159 file << fixed << setprecision(8);
160 export_cells_csv(file, cells);
161 file.close();
162
163 cout << "\nCSV/WKT exported to: " << path << "\n";
164 cout << "Format: site_index, site_x, site_y, vertex_count, WKT polygon\n";
165
166 return 0;
167}
168
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
Exact point-in-polygon classification via winding number.
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
Voronoi diagram derived as the dual of a Delaunay triangulation.
Computational geometry algorithms.
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.
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
Definition point.H:122
STL namespace.
Iterator over the vertices of a polygon.
Definition polygon.H:489
static void export_cells_csv(ostream &out, const Array< VoronoiDiagramFromDelaunay::ClippedCell > &cells)
static void write_polygon_wkt(ostream &out, const Polygon &poly)
ofstream file
Definition writeRb.C:46