Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
polygon_intersection_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 <fstream>
48# include <iomanip>
49# include <iostream>
50
51using namespace Aleph;
52using namespace std;
53
54static void print_banner(const char * title)
55{
56 cout << "[Aleph Geometry Example] " << title << "\n";
57 cout << "============================================================\n";
58}
59
60static Geom_Number polygon_area(const Polygon & poly)
61{
62 Geom_Number acc = 0;
63 if (not poly.is_closed() or poly.size() < 3)
64 return 0;
65
67 v.reserve(poly.size());
68 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
69 v.append(it.get_current_vertex());
70
71 for (size_t i = 0; i < v.size(); ++i)
72 {
73 const Point & a = v(i);
74 const Point & b = v((i + 1) % v.size());
75 acc += a.get_x() * b.get_y() - a.get_y() * b.get_x();
76 }
77
78 if (acc < 0)
79 acc = -acc;
80 return acc / 2;
81}
82
83static void write_polygon_wkt(ostream & out, const Polygon & poly)
84{
85 out << "POLYGON((";
86
87 bool first = true;
89 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
90 {
91 const Point & p = it.get_current_vertex();
92 if (first)
93 {
94 first_point = p;
95 first = false;
96 }
97 else
98 out << ", ";
99
100 out << geom_number_to_double(p.get_x()) << " "
102 }
103
104 if (not first)
105 out << ", " << geom_number_to_double(first_point.get_x()) << " "
107
108 out << "))";
109}
110
111static bool export_csv(const char * path,
112 const Polygon & subject,
113 const Polygon & clip,
114 const Polygon & inter)
115{
116 ofstream file(path);
117 if (not file)
118 return false;
119
120 file << fixed << setprecision(8);
121 file << "name,vertex_count,area,wkt\n";
122
123 auto write_row = [&file](const char * name, const Polygon & p)
124 {
125 file << name << "," << p.size() << "," << polygon_area(p) << ",\"";
127 file << "\"\n";
128 };
129
130 write_row("subject", subject);
131 write_row("clip", clip);
132 write_row("intersection", inter);
133 return true;
134}
135
136static void print_polygon(const char * name, const Polygon & poly)
137{
138 cout << name << " (vertices=" << poly.size()
139 << ", closed=" << (poly.is_closed() ? "yes" : "no") << ")" << endl;
140 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
141 {
142 const Vertex & p = it.get_current_vertex();
143 cout << " (" << geom_number_to_double(p.get_x()) << ", "
144 << geom_number_to_double(p.get_y()) << ")" << endl;
145 }
146}
147
148int main(int argc, char ** argv)
149{
150 print_banner("Convex Polygon Intersection");
151
152 Polygon a;
153 a.add_vertex(Point(0, 0));
154 a.add_vertex(Point(7, 0));
155 a.add_vertex(Point(7, 5));
156 a.add_vertex(Point(0, 5));
157 a.close();
158
159 Polygon b;
160 b.add_vertex(Point(3, -1));
161 b.add_vertex(Point(9, 2));
162 b.add_vertex(Point(6, 7));
163 b.add_vertex(Point(2, 6));
164 b.close();
165
167 Polygon inter = intersector(a, b);
168
169 print_polygon("Subject", a);
170 print_polygon("Clip", b);
171 print_polygon("Intersection", inter);
172
174 cout << "Intersection area = " << area_inter << " (exact rational)" << endl;
175 assert(inter.is_closed());
176 assert(inter.size() >= 3);
177 assert(area_inter > 0);
178
179 Polygon c;
180 c.add_vertex(Point(20, 20));
181 c.add_vertex(Point(22, 20));
182 c.add_vertex(Point(22, 22));
183 c.add_vertex(Point(20, 22));
184 c.close();
186 assert(disjoint.size() == 0);
187
188 cout << "Disjoint case validated: intersection is empty." << endl;
189
190 const char * csv_path =
191 (argc > 1) ? argv[1] : "polygon_intersection_output.csv";
192 if (export_csv(csv_path, a, b, inter))
193 cout << "CSV/WKT exported to: " << csv_path << endl;
194 else
195 cout << "Warning: cannot export CSV to: " << csv_path << endl;
196
197 cout << "STATUS: OK" << endl;
198 return 0;
199}
int main()
size_t size_t int32_t * out
Definition ca-c-api.h:120
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:365
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:250
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:320
Basic exact intersection for closed convex polygons.
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:448
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:457
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:247
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:678
void close()
Close the polygon.
Definition polygon.H:843
const bool & is_closed() const
Check if the polygon is closed.
Definition polygon.H:474
const size_t & size() const
Get the number of vertices.
Definition polygon.H:478
A vertex in a polygon's doubly linked vertex list.
Definition polygon.H:120
Computational geometry algorithms.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
Definition point.H:120
STL namespace.
static void print_polygon(const char *name, const Polygon &poly)
static bool export_csv(const char *path, const Polygon &subject, const Polygon &clip, const Polygon &inter)
static Geom_Number polygon_area(const Polygon &poly)
static void write_polygon_wkt(ostream &out, const Polygon &poly)
static void print_banner(const char *title)
Iterator over the vertices of a polygon.
Definition polygon.H:490
ofstream file
Definition writeRb.C:51