Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
delaunay_voronoi_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
46# include <geom_algorithms.H>
47
48# include <cassert>
49# include <iomanip>
50# include <iostream>
51
52using namespace Aleph;
53using namespace std;
54
55static void print_banner(const char * title)
56{
57 cout << "[Aleph Geometry Example] " << title << "\n";
58 cout << "============================================================\n";
59}
60
61static void print_point(const Point & p)
62{
63 cout << "(" << geom_number_to_double(p.get_x())
64 << ", " << geom_number_to_double(p.get_y()) << ")";
65}
66
68{
70 clip.add_vertex(Point(-2, -2));
71 clip.add_vertex(Point(10, -2));
72 clip.add_vertex(Point(10, 10));
73 clip.add_vertex(Point(-2, 10));
74 clip.close();
75 return clip;
76}
77
78int main()
79{
80 cout << fixed << setprecision(3);
81 print_banner("Delaunay + Voronoi");
82
83 DynList<Point> sites;
84 sites.append(Point(0, 0));
85 sites.append(Point(6, 0));
86 sites.append(Point(8, 4));
87 sites.append(Point(5, 8));
88 sites.append(Point(1, 7));
89 sites.append(Point(3, 3));
90 sites.append(Point(5, 4));
91
94
95 cout << "\nUnique sites: " << dt.sites.size() << endl;
96 cout << "Delaunay triangles: " << dt.triangles.size() << endl;
97 assert(dt.sites.size() >= 3);
98 assert(not dt.triangles.is_empty());
99
100 for (size_t i = 0; i < dt.triangles.size(); ++i)
101 {
102 const auto & tr = dt.triangles(i);
103 cout << " T" << i << ": [" << tr.i << ", " << tr.j << ", " << tr.k << "] ";
104 print_point(dt.sites(tr.i));
105 cout << " ";
106 print_point(dt.sites(tr.j));
107 cout << " ";
108 print_point(dt.sites(tr.k));
109 cout << endl;
110 }
111
114 cout << "\nVoronoi vertices: " << vor.vertices.size() << endl;
115 cout << "Voronoi edges: " << vor.edges.size() << endl;
116 cout << "Voronoi cells (raw): " << vor.cells.size() << endl;
117 assert(vor.sites.size() == dt.sites.size());
118
122
123 cout << "Voronoi cells (clipped): " << clipped.size() << endl;
124 assert(clipped.size() == dt.sites.size());
125
127 for (size_t i = 0; i < clipped.size(); ++i)
128 {
129 const auto & c = clipped(i);
130 assert(c.site_index == i);
131 assert(c.polygon.is_closed());
132 assert(c.polygon.size() >= 3);
133 assert(pip.contains(c.polygon, c.site));
134 }
135
136 cout << "\nFirst 3 clipped cells:" << endl;
137 for (size_t i = 0; i < clipped.size() and i < 3; ++i)
138 {
139 const auto & c = clipped(i);
140 cout << " cell[" << c.site_index << "] site=";
141 print_point(c.site);
142 cout << " vertices=" << c.polygon.size() << endl;
143 }
144
145 cout << "\nValidation OK: clipped cells are indexed and contain their site." << endl;
146 cout << "STATUS: OK" << endl;
147 return 0;
148}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Exact Delaunay triangulation using the Bowyer-Watson incremental algorithm.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
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.
static Array< ClippedCell > clipped_cells_indexed(const Array< Point > &sites, const Polygon &clip)
Clip Voronoi cells and return explicit site-indexed records.
static Polygon make_clip_window()
static void print_point(const Point &p)
static void print_banner(const char *title)
Computational geometry algorithms.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
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.
The result of a Delaunay triangulation.
The complete result of a Voronoi diagram computation.