Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
trapezoidal_map_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
33
34# include <iostream>
35# include <fstream>
36# include <tikzgeom_algorithms.H>
37
38using namespace std;
39using namespace Aleph;
40
43
44static const char * location_name(LT t)
45{
46 switch (t)
47 {
48 case LT::TRAPEZOID: return "TRAPEZOID";
49 case LT::ON_SEGMENT: return "ON_SEGMENT";
50 case LT::ON_POINT: return "ON_POINT";
51 }
52 return "UNKNOWN";
53}
54
55int main()
56{
57 // Build an L-shaped polygon (6 vertices).
58 // (0,4)---(2,4)
59 // | |
60 // | (2,2)--(4,2)
61 // | |
62 // (0,0)------(4,0)
63 Polygon L;
64 L.add_vertex(Point(0, 0));
65 L.add_vertex(Point(4, 0));
66 L.add_vertex(Point(4, 2));
67 L.add_vertex(Point(2, 2));
68 L.add_vertex(Point(2, 4));
69 L.add_vertex(Point(0, 4));
70 L.close();
71
72 cout << "=== Trapezoidal Map Point Location Example ===\n\n";
73 cout << "L-shaped polygon with 6 vertices, 6 edges.\n\n";
74
75 // Construct the trapezoidal map.
76 TM tm;
77 auto result = tm(L);
78
79 // Count active trapezoids.
80 size_t active = 0;
81 for (size_t i = 0; i < result.trapezoids.size(); ++i)
82 if (result.trapezoids(i).active) ++active;
83 cout << "Trapezoidal map: " << active << " active trapezoids, "
84 << result.dag.size() << " DAG nodes.\n\n";
85
86 // Query several points.
87 struct QueryCase
88 {
89 Point p;
90 const char * desc;
91 };
92
94 {
95 {Point(1, 1), "inside (lower part)"},
96 {Point(1, 3), "inside (upper part)"},
97 {Point(3, 1), "inside (right part)"},
98 {Point(3, 3), "outside (reflex area)"},
99 {Point(5, 1), "outside (far right)"},
100 {Point(0, 0), "vertex"},
101 {Point(2, 0), "edge midpoint"},
102 {Point(2, 2), "reflex vertex"},
103 };
104
105 cout << "Point queries:\n";
106 for (const auto & q : queries)
107 {
108 auto loc = result.locate(q.p);
109 bool inside = result.contains(q.p);
110 cout << " (" << q.p.get_x() << ", " << q.p.get_y() << ") "
111 << q.desc << " => " << location_name(loc.type)
112 << (inside ? " [INSIDE]" : " [OUTSIDE]") << "\n";
113 }
114
115 // Generate TikZ output.
116 const char * tex_file = "trapezoidal_map_example_output.tex";
117 cout << "\nWriting TikZ output to " << tex_file << " ...\n";
118
119 Tikz_Plane plane(80, 80);
120 put_trapezoidal_map_result(plane, result);
121
122 // Add query points color-coded.
123 for (const auto & q : queries)
124 {
125 bool inside = result.contains(q.p);
126 auto loc = result.locate(q.p);
127 std::string color = "red"; // outside
128 if (loc.type == LT::ON_SEGMENT or loc.type == LT::ON_POINT)
129 color = "blue"; // boundary
130 else if (inside)
131 color = "green!70!black"; // inside
132
135 }
136
137 ofstream out(tex_file);
138 plane.draw(out);
139 out.close();
140
141 cout << "Done. Compile with: pdflatex " << tex_file << "\n";
142 return 0;
143}
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
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
2D TikZ canvas storing geometry objects and emitting LaTeX output.
Definition tikzgeom.H:184
void draw(std::ostream &output, const bool squarize=true) const
Emit a complete tikzpicture with all inserted objects.
Definition tikzgeom.H:1383
static constexpr int Layer_Overlay
Definition tikzgeom.H:189
O(log n) point location via trapezoidal map with DAG search.
LocationType
Classification of a point location query result.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void put_in_plane(Tikz_Plane &plane, const Geom &geom_obj)
Insert any supported geometry type in a Tikz_Plane.
Definition tikzgeom.H:1456
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 put_trapezoidal_map_result(Tikz_Plane &plane, const TrapezoidalMapPointLocation::Result &res, const Tikz_Style &segment_style=tikz_path_style("red"), const Tikz_Style &extension_style=tikz_wire_style("gray!60", true), const bool draw_trapezoids=true, const double trapezoid_opacity=0.3, const int trap_layer=Tikz_Plane::Layer_Background, const int seg_layer=Tikz_Plane::Layer_Foreground, const int ext_layer=Tikz_Plane::Layer_Default)
Draw the trapezoidal map result into a Tikz_Plane.
Tikz_Style tikz_points_style(const std::string &color="black", const double opacity=-1.0)
Creates a style optimized for point clouds.
static long & color(typename GT::Node *p)
STL namespace.
Style descriptor for TikZ primitives.
Definition tikzgeom.H:86
Helpers to visualize computational-geometry algorithm results in TikZ.
static const char * location_name(LT t)
fstream tex_file
Definition writeTreap.C:53