Pangolin
Header-only C++20 plane computational geometry library
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "shape/shape.hpp"
4
12
13
14namespace pgl {
15
16// -----------------------------------------------------------------------------
17// Point
18
26template <class Number, class Label>
27std::ostream& operator<<(std::ostream& stream, const Point<Number, Label>& point) {
28 if constexpr (detail::has_label_v<Label>) {
29 stream << point.label() << ":(" << point.x() << ',' << point.y() << ')';
30 } else {
31 stream << '(' << point.x() << ',' << point.y() << ')';
32 }
33 return stream;
34}
35
36// -----------------------------------------------------------------------------
37// Segment
38
46template <class PointType, class LabelType>
47std::ostream& operator<<(std::ostream& stream, const Segment<PointType, LabelType>& segment) {
48 if constexpr (detail::has_label_v<LabelType>) {
49 stream << segment.label() << ":{" << segment.min() << "--" << segment.max() << "}";
50 } else {
51 stream << segment.min() << "--" << segment.max();
52 }
53 return stream;
54}
55
56// -----------------------------------------------------------------------------
57// OrientedSegment
58
68template <class PointType, class LabelType>
69std::ostream& operator<<(std::ostream& stream, const OrientedSegment<PointType, LabelType>& segment) {
70 if constexpr (detail::has_label_v<LabelType>) {
71 stream << segment.label() << ":{" << segment.source() << "->" << segment.target() << "}";
72 } else {
73 stream << segment.source() << "->" << segment.target();
74 }
75 return stream;
76}
77
78// -----------------------------------------------------------------------------
79// Line
80
88template <class PointType, class LabelType>
89std::ostream& operator<<(std::ostream& stream, const Line<PointType, LabelType>& line) {
90 if constexpr (detail::has_label_v<LabelType>) {
91 stream << line.label() << ":{-" << line.min() << "--" << line.max() << "-}";
92 } else {
93 stream << '-' << line.min() << "--" << line.max() << '-';
94 }
95 return stream;
96}
97
98// -----------------------------------------------------------------------------
99// OrientedLine
100
108template <class PointType, class LabelType>
109std::ostream& operator<<(std::ostream& stream, const OrientedLine<PointType, LabelType>& line) {
110 if constexpr (detail::has_label_v<LabelType>) {
111 stream << line.label() << ":{-" << line.source() << "--" << line.target() << "->}";
112 } else {
113 stream << '-' << line.source() << "--" << line.target() << "->";
114 }
115 return stream;
116}
117
118// -----------------------------------------------------------------------------
119// Ray
120
128template <class PointType, class LabelType>
129std::ostream& operator<<(std::ostream& stream, const Ray<PointType, LabelType>& ray) {
130 if constexpr (detail::has_label_v<LabelType>) {
131 stream << ray.label() << ":{" << ray.source() << "--" << ray.target() << "->}";
132 } else {
133 stream << ray.source() << "--" << ray.target() << "->";
134 }
135 return stream;
136}
137
138// -----------------------------------------------------------------------------
139// Rectangle
140
148template <class PointType, class LabelType>
149std::ostream& operator<<(std::ostream& stream, const Rectangle<PointType, LabelType>& rectangle) {
150 if constexpr (detail::has_label_v<LabelType>) {
151 stream << rectangle.label() << ':';
152 }
153 // The corners of an empty rectangle are inverted placeholders, so printing
154 // them would read as a rectangle it is not.
155 if (rectangle.empty()) {
156 return stream << "[]";
157 }
158 return stream << '[' << rectangle.min() << ',' << rectangle.max() << ']';
159}
160
161// -----------------------------------------------------------------------------
162// Triangle
163
171template <class PointType, class LabelType>
172std::ostream& operator<<(std::ostream& stream, const Triangle<PointType, LabelType>& triangle) {
173 if constexpr (detail::has_label_v<LabelType>) {
174 stream << triangle.label() << ":<" << triangle.a() << triangle.b() << triangle.c() << ">";
175 } else {
176 stream << '<' << triangle.a() << triangle.b() << triangle.c() << '>';
177 }
178 return stream;
179}
180
181// -----------------------------------------------------------------------------
182// Halfplane
183
191template <class PointType, class LabelType>
192std::ostream& operator<<(std::ostream& stream, const Halfplane<PointType, LabelType>& halfplane) {
193 if constexpr (detail::has_label_v<LabelType>) {
194 stream << halfplane.label() << ":^-" << halfplane.source() << "--" << halfplane.target() << "-^";
195 } else {
196 stream << "^-" << halfplane.source() << "--" << halfplane.target() << "-^";
197 }
198 return stream;
199}
200
201// -----------------------------------------------------------------------------
202// Disk
203
211template <class PointType, class LabelType>
212std::ostream& operator<<(std::ostream& stream, const Disk<PointType, LabelType>& disk) {
213 if constexpr (detail::has_label_v<LabelType>) {
214 stream << disk.label() << ":Disk(" << disk.a() << disk.b() << disk.c() << ")";
215 } else {
216 stream << "Disk(" << disk.a() << disk.b() << disk.c() << ')';
217 }
218 return stream;
219}
220
221
222// -----------------------------------------------------------------------------
223// Convex
231template <class PointType, class LabelType>
232std::ostream& operator<<(std::ostream& stream, const Convex<PointType, LabelType>& convex) {
233 if constexpr (detail::has_label_v<LabelType>) {
234 stream << convex.label() << ':';
235 }
236 stream << "Convex[";
237 for (size_t i = 0; i < convex.size(); ++i) {
238 if (i > 0) {
239 stream << ",";
240 }
241 stream << convex[i];
242 }
243 stream << "]";
244 return stream;
245}
246
247// -----------------------------------------------------------------------------
248// Polygon
256template <class PointType, class LabelType>
257std::ostream& operator<<(std::ostream& stream, const Polygon<PointType, LabelType>& polygon) {
258 stream << "Polygon[";
259 for (std::size_t i = 0; i < polygon.size(); ++i) {
260 if (i > 0) {
261 stream << ",";
262 }
263 stream << polygon[i];
264 }
265 stream << "]";
266 return stream;
267}
268
269// -----------------------------------------------------------------------------
270// PolygonWithHoles
282template <class PointType, class LabelType>
283std::ostream& operator<<(std::ostream& stream, const PolygonWithHoles<PointType, LabelType>& region) {
284 stream << "PolygonWithHoles[" << region.outer();
285 for (const auto& hole : region.holes()) {
286 stream << "," << hole;
287 }
288 stream << "]";
289 return stream;
290}
291
292// -----------------------------------------------------------------------------
293// PolygonSet
303template <class PointType, class LabelType>
304std::ostream& operator<<(std::ostream& stream, const PolygonSet<PointType, LabelType>& set) {
305 stream << "PolygonSet[";
306 bool first = true;
307 for (const auto& component : set) {
308 if (!first) {
309 stream << ",";
310 }
311 first = false;
312 stream << component;
313 }
314 stream << "]";
315 return stream;
316}
317
318// -----------------------------------------------------------------------------
319// MonotoneChain
327template <class PointType, class LabelType, class Storage>
328std::ostream& operator<<(std::ostream& stream, const MonotoneChain<PointType, LabelType, Storage>& chain) {
329 stream << "MonotoneChain[";
330 for (std::size_t i = 0; i < chain.size(); ++i) {
331 if (i > 0) {
332 stream << ",";
333 }
334 stream << chain[i];
335 }
336 stream << "]";
337 return stream;
338}
339
340// -----------------------------------------------------------------------------
341// Polyline
349template <class PointType, class LabelType>
350std::ostream& operator<<(std::ostream& stream, const Polyline<PointType, LabelType>& polyline) {
351 stream << "Polyline[";
352 for (std::size_t i = 0; i < polyline.size(); ++i) {
353 if (i > 0) {
354 stream << ",";
355 }
356 stream << polyline[i];
357 }
358 stream << "]";
359 return stream;
360}
361
362// ---------------------------------------------------------------------------
363// HalfplaneIntersection
364
375template <class PointType, class LabelType>
376std::ostream& operator<<(std::ostream& stream, const HalfplaneIntersection<PointType, LabelType>& region) {
377 if constexpr (detail::has_label_v<LabelType>) {
378 stream << region.label() << ":";
379 }
380 stream << "HalfplaneIntersection[";
381 if (region.empty()) {
382 stream << "empty";
383 } else if (region.isPlane()) {
384 stream << "plane";
385 } else {
386 for (std::size_t i = 0; i < region.size(); ++i) {
387 if (i > 0) {
388 stream << ",";
389 }
390 stream << region[i];
391 }
392 }
393 stream << "]";
394 return stream;
395}
396} // namespace pgl
Definition arrangement.hpp:67
std::ostream & operator<<(std::ostream &stream, const Point< Number, Label > &point)
Streams a point as (x,y) or label:(x,y).
Definition io.hpp:27
Runtime variant wrapper over the currently implemented shape types.
Closed convex polygon stored by its vertices.
Definition convex.hpp:170
constexpr A & label() const
Returns the convex-polygon label.
Definition convex.hpp:270
size_t size() const
Returns the number of vertices in the convex polygon.
Definition convex.hpp:840
Closed Euclidean disk stored by boundary points plus optional disk label.
Definition disk.hpp:66
constexpr const PointType & c() const
Returns the third boundary point in canonical order.
Definition disk.hpp:244
constexpr A & label() const
Returns the disk label.
Definition disk.hpp:183
constexpr const PointType & a() const
Returns the first boundary point (lexicographically smallest).
Definition disk.hpp:228
constexpr const PointType & b() const
Returns the second boundary point in canonical order.
Definition disk.hpp:235
Intersection of closed half-planes; convex but possibly unbounded or empty.
Definition halfplaneintersection.hpp:244
constexpr bool empty() const
Returns whether the region is the empty set.
Definition halfplaneintersection.hpp:649
constexpr A & label() const
Returns the label.
Definition halfplaneintersection.hpp:487
constexpr std::size_t size() const
Returns the number of stored (non-redundant) half-planes.
Definition halfplaneintersection.hpp:596
constexpr bool isPlane() const
Returns whether the region is the whole plane (no half-planes).
Definition halfplaneintersection.hpp:656
Closed half-plane defined by an oriented boundary line.
Definition halfplane.hpp:51
constexpr A & label() const
Returns the half-plane label.
Definition halfplane.hpp:302
constexpr const PointType & target() const
Returns the target boundary point.
Definition halfplane.hpp:193
constexpr const PointType & source() const
Returns the source boundary point.
Definition halfplane.hpp:181
Unoriented infinite line.
Definition line.hpp:52
constexpr const PointType & max() const
Returns the largest stored defining point.
Definition line.hpp:189
constexpr A & label() const
Returns the line label.
Definition line.hpp:262
constexpr const PointType & min() const
Returns the smallest stored defining point.
Definition line.hpp:180
Weakly x-monotone polyline stored by lexicographically sorted vertices.
Definition monotonechain.hpp:146
constexpr std::size_t size() const
Returns the number of vertices in the chain.
Definition monotonechain.hpp:393
Directed infinite line with left/right side semantics plus optional line label.
Definition orientedline.hpp:53
constexpr const PointType & target() const
Returns the target defining point.
Definition orientedline.hpp:195
constexpr A & label() const
Returns the line label.
Definition orientedline.hpp:305
constexpr const PointType & source() const
Returns the source defining point.
Definition orientedline.hpp:183
Directed segment preserving source-to-target order plus optional segment label.
Definition orientedsegment.hpp:44
constexpr const PointType & source() const
Returns the source endpoint.
Definition orientedsegment.hpp:178
constexpr A & label() const
Returns the segment label.
Definition orientedsegment.hpp:302
constexpr const PointType & target() const
Returns the target endpoint.
Definition orientedsegment.hpp:190
Two-dimensional point with optional label payload.
Definition point.hpp:129
constexpr A & label() const
Returns the stored label.
Definition point.hpp:223
constexpr const NumberType & x() const
Returns the x coordinate.
Definition point.hpp:193
constexpr const NumberType & y() const
Returns the y coordinate.
Definition point.hpp:205
Set of closed regions with pairwise disjoint interiors.
Definition polygonset.hpp:165
Closed region bounded by one outer simple polygon minus disjoint polygonal holes.
Definition polygonwithholes.hpp:89
constexpr const PolygonType & outer() const
Returns the outer boundary.
Definition polygonwithholes.hpp:178
constexpr const std::vector< PolygonType > & holes() const
Returns the holes in canonical order.
Definition polygonwithholes.hpp:202
Closed simple polygon stored by its vertices.
Definition polygon.hpp:59
constexpr std::size_t size() const
Returns the number of vertices in the polygon.
Definition polygon.hpp:259
Open polygonal chain stored in traversal order; may self-intersect.
Definition polyline.hpp:69
constexpr std::size_t size() const
Returns the number of vertices in the polyline.
Definition polyline.hpp:388
Half-infinite line starting from one source point plus optional ray label.
Definition ray.hpp:51
constexpr A & label() const
Returns the ray label.
Definition ray.hpp:303
constexpr const PointType & target() const
Returns the second stored point defining the direction.
Definition ray.hpp:193
constexpr const PointType & source() const
Returns the source point of the ray.
Definition ray.hpp:181
Axis-aligned rectangle stored by minimum and maximum corners.
Definition rectangle.hpp:75
constexpr const PointType & min() const
Returns the minimum corner (min x, min y).
Definition rectangle.hpp:347
constexpr bool empty() const
Returns whether the rectangle is the empty set of points.
Definition rectangle.hpp:290
constexpr A & label() const
Returns the rectangle label.
Definition rectangle.hpp:517
constexpr const PointType & max() const
Returns the maximum corner (max x, max y).
Definition rectangle.hpp:359
Unoriented closed segment between two endpoints plus optional segment label.
Definition segment.hpp:58
constexpr const PointType & max() const
Returns the largest stored endpoint.
Definition segment.hpp:199
constexpr const PointType & min() const
Returns the smallest stored endpoint.
Definition segment.hpp:190
constexpr A & label() const
Returns the segment label.
Definition segment.hpp:275
Closed triangle stored by three vertices.
Definition triangle.hpp:53
constexpr const PointType & b() const
Returns the second vertex.
Definition triangle.hpp:217
constexpr const PointType & a() const
Returns the first vertex.
Definition triangle.hpp:208
constexpr A & label() const
Returns the triangle label.
Definition triangle.hpp:295
constexpr const PointType & c() const
Returns the third vertex.
Definition triangle.hpp:226