Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
convex_hull_comparison_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 <chrono>
50# include <iomanip>
51# include <iostream>
52# include <sstream>
53
54using namespace Aleph;
55using namespace std;
56
57static void print_banner(const char * title)
58{
59 cout << "[Aleph Geometry Example] " << title << "\n";
60 cout << "============================================================\n";
61}
62
64{
66 pts.append(Point(0, 0));
67 pts.append(Point(6, 1));
68 pts.append(Point(10, 5));
69 pts.append(Point(8, 10));
70 pts.append(Point(3, 12));
71 pts.append(Point(-1, 8));
72 pts.append(Point(-2, 3));
73 pts.append(Point(4, 4)); // interior
74 pts.append(Point(5, 6)); // interior
75 pts.append(Point(2, 7)); // interior
76 pts.append(Point(7, 7)); // interior
77 pts.append(Point(1, 2)); // interior
78 return pts;
79}
80
81static string hull_signature(const Polygon & poly)
82{
83 if (poly.size() == 0)
84 return "[]";
85
87 verts.reserve(poly.size());
88 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
89 verts.append(it.get_current_vertex());
90
91 quicksort_op(verts, [](const Point & a, const Point & b)
92 {
93 if (a.get_x() < b.get_x())
94 return true;
95 if (b.get_x() < a.get_x())
96 return false;
97 return a.get_y() < b.get_y();
98 });
99
101 unique.reserve(verts.size());
102 for (size_t i = 0; i < verts.size(); ++i)
103 if (unique.is_empty() or unique.get_last() != verts(i))
104 unique.append(verts(i));
105
106 ostringstream out;
107 out << "[";
108 for (size_t i = 0; i < unique.size(); ++i)
109 {
110 if (i > 0)
111 out << ";";
112 out << unique(i).get_x() << "," << unique(i).get_y();
113 }
114 out << "]";
115 return out.str();
116}
117
118template <typename Algo>
119static Polygon timed_hull(const char * name, Algo & algo,
120 const DynList<Point> & pts, double & micros)
121{
122 const auto t0 = chrono::high_resolution_clock::now();
123 Polygon h = algo(pts);
124 const auto t1 = chrono::high_resolution_clock::now();
125 micros = chrono::duration<double, std::micro>(t1 - t0).count();
126
127 cout << " " << setw(28) << left << name
128 << " vertices=" << setw(3) << h.size()
129 << " time=" << fixed << setprecision(2) << micros << " us" << endl;
130 return h;
131}
132
133int main()
134{
135 print_banner("Convex Hull Comparison");
136
138 cout << "Input points: " << pts.size() << endl;
139
145
146 double t_brute = 0;
147 double t_gift = 0;
148 double t_quick = 0;
149 double t_andrew = 0;
150 double t_graham = 0;
151
152 const Polygon h_brute = timed_hull("BruteForceConvexHull", brute, pts, t_brute);
153 const Polygon h_gift = timed_hull("GiftWrappingConvexHull", gift, pts, t_gift);
154 const Polygon h_quick = timed_hull("QuickHull", quick, pts, t_quick);
155 const Polygon h_andrew = timed_hull("AndrewMonotonicChainConvexHull", andrew, pts, t_andrew);
156 const Polygon h_graham = timed_hull("GrahamScanConvexHull", graham, pts, t_graham);
157
158 const string ref = hull_signature(h_andrew);
159 cout << "\nReference signature (Andrew): " << ref << endl;
160
165
166 cout << "All 5 algorithms produced the same hull vertex set." << endl;
167 cout << "STATUS: OK" << endl;
168 return 0;
169}
long double h
Definition btreepic.C:154
Andrew's monotonic chain convex hull algorithm.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:315
Brute force convex hull algorithm.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
Gift wrapping (Jarvis march) convex hull algorithm.
Graham scan convex hull algorithm.
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
const size_t & size() const
Get the number of vertices.
Definition polygon.H:477
QuickHull convex hull algorithm.
static Polygon timed_hull(const char *name, Algo &algo, const DynList< Point > &pts, double &micros)
static DynList< Point > build_point_set()
static string hull_signature(const Polygon &poly)
static void print_banner(const char *title)
Computational geometry algorithms.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
Definition ahAlgo.H:1058
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.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
STL namespace.
Iterator over the vertices of a polygon.
Definition polygon.H:489