Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
constrained_delaunay_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
44# include <geom_algorithms.H>
45
46# include <cassert>
47# include <iomanip>
48# include <iostream>
49
50using namespace Aleph;
51using namespace std;
52
53static void print_point(const Point & p)
54{
55 cout << "(" << geom_number_to_double(p.get_x())
56 << ", " << geom_number_to_double(p.get_y()) << ")";
57}
58
59int main()
60{
61 cout << fixed << setprecision(3);
62 cout << "[Aleph Geometry Example] Constrained Delaunay Triangulation\n";
63 cout << "============================================================\n";
64
65 // Rectangular domain with interior points and crossing constraints.
66 DynList<Point> sites;
67 sites.append(Point(0, 0));
68 sites.append(Point(8, 0));
69 sites.append(Point(8, 6));
70 sites.append(Point(0, 6));
71 sites.append(Point(3, 2));
72 sites.append(Point(5, 4));
73 sites.append(Point(2, 5));
74 sites.append(Point(6, 1));
75
76 // Constraint edges: two crossing diagonals of the rectangle.
78 constraints.append(Segment(Point(0, 0), Point(8, 6)));
79 constraints.append(Segment(Point(8, 0), Point(0, 6)));
80
82 auto result = cdt(sites, constraints);
83
84 cout << "\nUnique sites: " << result.sites.size() << endl;
85 cout << "Triangles: " << result.triangles.size() << endl;
86 cout << "Constrained edges: " << result.constrained_edges.size() << endl;
87
88 assert(result.sites.size() >= 8);
89 assert(not result.triangles.is_empty());
90
91 cout << "\nTriangles:" << endl;
92 for (size_t i = 0; i < result.triangles.size(); ++i)
93 {
94 const auto & tr = result.triangles(i);
95 cout << " T" << i << ": [" << tr.i << ", " << tr.j << ", " << tr.k
96 << "] ";
97 print_point(result.sites(tr.i));
98 cout << " ";
99 print_point(result.sites(tr.j));
100 cout << " ";
101 print_point(result.sites(tr.k));
102 cout << endl;
103 }
104
105 cout << "\nConstrained edges:" << endl;
106 for (size_t i = 0; i < result.constrained_edges.size(); ++i)
107 {
108 const auto & e = result.constrained_edges(i);
109 cout << " E" << i << ": [" << e.u << ", " << e.v << "] ";
110 print_point(result.sites(e.u));
111 cout << " -> ";
112 print_point(result.sites(e.v));
113 cout << endl;
114 }
115
116 // Verify all constrained edges are in the triangulation.
117 auto has_edge = [&](size_t pu, size_t pv) -> bool
118 {
119 for (size_t t = 0; t < result.triangles.size(); ++t)
120 {
121 const auto & tri = result.triangles(t);
122 size_t vs[3] = {tri.i, tri.j, tri.k};
123 for (int e = 0; e < 3; ++e)
124 if ((vs[e] == pu and vs[(e + 1) % 3] == pv) or
125 (vs[e] == pv and vs[(e + 1) % 3] == pu))
126 return true;
127 }
128 return false;
129 };
130
131 for (size_t i = 0; i < result.constrained_edges.size(); ++i)
132 {
133 const auto & e = result.constrained_edges(i);
134 assert(has_edge(e.u, e.v));
135 }
136
137 // The two crossing diagonals intersect — CDT automatically splits them
138 // at the intersection point. Verify the split sub-edges exist.
139 assert(result.constrained_edges.size() >= 4);
140
141 // Convert to geometric triangles.
144 assert(geo_tris.size() == result.triangles.size());
145
146 cout << "\nValidation OK: all constraints present in CDT." << endl;
147 cout << "STATUS: OK" << endl;
148 return 0;
149}
Constrained Delaunay Triangulation via Sloan's flip-based method.
static DynList< Triangle > as_triangles(const Result &result)
Convert indexed triangulation to geometric triangles.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
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
Represents a line segment between two points.
Definition point.H:827
static void print_point(const Point &p)
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.