Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
geom_algorithms_test_common.h
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
31#ifndef ALEPH_TEST_GEOM_ALGORITHMS_TEST_COMMON_H
32#define ALEPH_TEST_GEOM_ALGORITHMS_TEST_COMMON_H
33
34#include <gtest/gtest.h>
35#include <geom_algorithms.H>
36#include <ah-errors.H>
38
39namespace
40{
48 [[maybe_unused]] bool polygon_contains_vertex(const Polygon & poly, const Point & p)
49 {
50 if (poly.size() == 0)
51 return false;
52
53 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
54 if (it.get_current_vertex() == p)
55 return true;
56
57 return false;
58 }
59
69 [[maybe_unused]] bool matches_unordered_pair(const Point & a, const Point & b,
70 const Point & u, const Point & v)
71 {
72 return (a == u and b == v) or (a == v and b == u);
73 }
74
78 struct TriKey
79 {
80 size_t a;
81 size_t b;
82 size_t c;
83 };
84
94 [[maybe_unused]] void sort3(size_t & a, size_t & b, size_t & c)
95 {
96 if (a > b)
97 {
98 const size_t t = a;
99 a = b;
100 b = t;
101 }
102 if (b > c)
103 {
104 const size_t t = b;
105 b = c;
106 c = t;
107 }
108 if (a > b)
109 {
110 const size_t t = a;
111 a = b;
112 b = t;
113 }
114 }
115
127 {
129 out.reserve(r.triangles.size());
130 for (size_t i = 0; i < r.triangles.size(); ++i)
131 {
132 size_t a = r.triangles(i).i;
133 size_t b = r.triangles(i).j;
134 size_t c = r.triangles(i).k;
135 sort3(a, b, c);
136 out.append(TriKey{a, b, c});
137 }
138
139 quicksort_op(out, [](const TriKey & x, const TriKey & y)
140 {
141 if (x.a != y.a)
142 return x.a < y.a;
143 if (x.b != y.b)
144 return x.b < y.b;
145 return x.c < y.c;
146 });
147 return out;
148 }
149
159 [[maybe_unused]] Point circumcenter_of(const Point & a, const Point & b, const Point & c)
160 {
161 const Geom_Number & ax = a.get_x();
162 const Geom_Number & ay = a.get_y();
163 const Geom_Number & bx = b.get_x();
164 const Geom_Number & by = b.get_y();
165 const Geom_Number & cx = c.get_x();
166 const Geom_Number & cy = c.get_y();
167
168 const Geom_Number a2 = ax * ax + ay * ay;
169 const Geom_Number b2 = bx * bx + by * by;
170 const Geom_Number c2 = cx * cx + cy * cy;
171
172 const Geom_Number d = ax * (by - cy) + bx * (cy - ay) + cx * (ay - by);
173 const Geom_Number den = d + d;
174
176 << "circumcenter_of: collinear triangle (zero area)";
177
178 return {
179 (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / den,
180 (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / den
181 };
182 }
183
191 {
193 }
194
202 {
204 if (a < 0) { a = -a; }
205 return a;
206 }
207
214 [[maybe_unused]] bool is_ccw(const Polygon & poly)
215 {
217 }
218
226 {
227 Geom_Number a = t.area();
228 if (a < 0) { a = -a; }
229 return a;
230 }
231
239 [[maybe_unused]] bool all_points_inside_or_on(const DynList<Point> & points, const Polygon & hull)
240 {
241 for (auto it = points.get_it(); it.has_curr(); it.next_ne())
242 if (const auto loc = PointInPolygonWinding::locate(hull, it.get_curr());
243 loc == PointInPolygonWinding::Location::Outside)
244 return false;
245 return true;
246 }
247
254 [[maybe_unused]] bool polygon_is_convex(const Polygon & poly)
255 {
257 verts.reserve(poly.size());
258 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
259 verts.append(it.get_current_vertex());
261 }
262} // namespace
263
264class GeomAlgorithmsTest : public ::testing::Test
265{
266protected:
267 void SetUp() override {}
268 void TearDown() override {}
269};
270
271// Basic triangulation tests
272
273#endif // ALEPH_TEST_GEOM_ALGORITHMS_TEST_COMMON_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
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
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
static bool is_convex(const Array< Point > &verts)
Check if a vertex array forms a convex polygon.
static Geom_Number signed_double_area(const Array< Point > &verts)
Compute twice the signed area (shoelace formula without division).
static Location locate(const Polygon &poly, const Point &p)
Classify point location with respect to a polygon.
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
A non-degenerate triangle defined by three points.
Definition point.H:1478
Geom_Number area() const
Calculates the unsigned area of the triangle.
Definition point.H:1532
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:222
Computational geometry algorithms.
double triangle_area(const Triangle &t)
Calculate area of a triangle using cross product.
static mpfr_t y
Definition mpfr_mul_d.c:3
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.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
static Geom_Number polygon_area(const Polygon &poly)
The result of a Delaunay triangulation.
Iterator over the vertices of a polygon.
Definition polygon.H:489
gsl_rng * r