Loading...
Searching...
No Matches
planmodel.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
4#include <sstream>
5#include <iomanip>
6#include <cmath>
7namespace meshkit::plan
8{
9using detail::Vector;
10using detail::Map;
11using detail::Set;
13inline constexpr double pi = 3.141592653589793238462643383279502884;
20class Text : public std::string
21{
22public:
23 using std::string::string;
24 Text() = default;
29 Text(const std::string &v) : std::string(v) {};
31 bool isEmpty() const { return empty(); }
41 template <class T> Text arg(const T &value) const
42 {
43 std::ostringstream out;
44 out << value;
45 Text result = *this;
46 std::size_t at = std::string::npos;
47 int lowest = 100;
48 for (std::size_t i = 0; i + 1 < size(); ++i)
49 if ((*this)[i] == '%' && (*this)[i + 1] >= '1' && (*this)[i + 1] <= '9') {
50 int n = (*this)[i + 1] - '0';
51 if (n < lowest) {
52 lowest = n;
53 at = i;
54 }
55 }
56 if (at != std::string::npos) {
57 const auto key = "%" + std::to_string(lowest);
58 std::size_t p = 0;
59 while ((p = result.find(key, p)) != std::string::npos) {
60 result.replace(p, key.size(), out.str());
61 p += out.str().size();
62 }
63 }
64 return result;
65 }
66
72 template <class T, class U> Text arg(const T &a, const U &b) const { return arg(a).arg(b); }
73};
74
75using Bytes = std::string;
82class Polygon : public Vector<Point2>
83{
84public:
86 Polygon() = default;
91 Polygon(const Vector<Point2> &v) : Vector<Point2>(v) {};
96 Polygon(const std::vector<Point2> &v) : Vector<Point2>(v) {}
103 {
104 if (empty())
105 return {};
106 double x0 = front().x(), x1 = x0, y0 = front().y(), y1 = y0;
107 for (auto p : *this) {
108 x0 = std::min(x0, p.x());
109 x1 = std::max(x1, p.x());
110 y0 = std::min(y0, p.y());
111 y1 = std::max(y1, p.y());
112 }
113 return {{x0, y0}, {x1, y1}};
114 }
115};
116
117struct Line2 {
128 Line2(Point2 x, Point2 y) : a(x), b(y) {}
130 double length() const { return std::hypot(b.x() - a.x(), b.y() - a.y()); }
131};
132
135 double z = 0;
136};
137
154 int v0 = 0, v1 = 0, v2 = 0, v3 = -1;
157 bool isQuad() const { return v3 >= 0; }
159 int vertexCount() const { return isQuad() ? 4 : 3; }
166 int vertex(int k) const { return k == 0 ? v0 : k == 1 ? v1 : k == 2 ? v2 : k == 3 ? v3 : -1; }
173 void setVertex(int k, int v)
174 {
175 if (k == 0)
176 v0 = v;
177 else if (k == 1)
178 v1 = v;
179 else if (k == 2)
180 v2 = v;
181 else if (k == 3)
182 v3 = v;
183 }
184};
185
186struct MeshEdge {
193 int v0 = 0, v1 = 0, marker = 0;
195};
196
208template <class T> class ReadArray : public Vector<T>
209{
210 const void *owner_ = nullptr;
211 T (*read_)(const void *, std::size_t) = nullptr;
212 std::ptrdiff_t count_ = 0;
213
214public:
215 using Vector<T>::Vector;
224 void borrow(const void *owner, std::ptrdiff_t count, T (*read)(const void *, std::size_t))
225 {
226 owner_ = owner;
227 count_ = count;
228 read_ = read;
229 }
230
231 std::ptrdiff_t size() const { return owner_ ? count_ : Vector<T>::size(); }
233 bool isEmpty() const { return size() == 0; }
239 T operator[](std::size_t i) const
240 {
241 return owner_ ? read_(owner_, i) : Vector<T>::operator[](i);
242 }
243
247 struct Iterator {
249 std::size_t i;
251 T operator*() const { return (*array)[i]; }
257 {
258 ++i;
259 return *this;
260 }
261
266 bool operator!=(const Iterator &b) const { return i != b.i; }
267 };
268
269 Iterator begin() const { return {this, 0}; }
271 Iterator end() const { return {this, std::size_t(size())}; }
272};
273
289
303
306 int marker = 0;
308 double z = 0;
311 bool hasZ = false;
312};
313
321inline bool cellIsConvex(const Vector<MeshVertex> &v, const MeshTriangle &c)
322{
323 if (!c.isQuad())
324 return true;
325 double before = 0;
326 for (int i = 0; i < 4; ++i) {
327 auto a = v[c.vertex(i)].xy, b = v[c.vertex((i + 1) % 4)].xy,
328 d = v[c.vertex((i + 2) % 4)].xy;
329 auto ab = b - a, bd = d - b;
330 double turn = ab.x() * bd.y() - ab.y() * bd.x();
331 if (!std::isfinite(turn) || turn == 0 || (before && (turn > 0) != (before > 0)))
332 return false;
333 before = turn;
334 }
335 return true;
336}
337}
Axis-aligned rectangle in the XY plane, stored as an origin corner and a signed size.
Definition geometry.hpp:167
Point or displacement vector in the horizontal (XY) plane.
Definition geometry.hpp:21
std::vector<T> with the QVector-style member names the CDT kernel and plan model use.
Definition storage.hpp:31
Vector()=default
Empty vector.
Polygon(const Vector< Point2 > &v)
Copy a point vector.
Definition planmodel.hpp:91
Polygon(const std::vector< Point2 > &v)
Copy a std::vector of points.
Definition planmodel.hpp:96
Box2 boundingRect() const
Axis-aligned bounding box of all points.
Definition planmodel.hpp:102
Read-only array that either owns its elements or reads them from borrowed storage.
Definition planmodel.hpp:209
T operator[](std::size_t i) const
Element i, from the borrowed storage or the owned elements.
Definition planmodel.hpp:239
void borrow(const void *owner, std::ptrdiff_t count, T(*read)(const void *, std::size_t))
View external storage instead of the owned elements, which are left unchanged.
Definition planmodel.hpp:224
Iterator end() const
Iterator at index size().
Definition planmodel.hpp:271
bool isEmpty() const
Return true when size() is 0.
Definition planmodel.hpp:233
std::ptrdiff_t size() const
Borrowed count while borrowing, otherwise the owned element count.
Definition planmodel.hpp:231
Iterator begin() const
Iterator at index 0.
Definition planmodel.hpp:269
Owned byte string with the QString-style helpers used for tags and diagnostics.
Definition planmodel.hpp:21
Text(const std::string &v)
Copy a std::string; also serves as an implicit conversion.
Definition planmodel.hpp:29
Text arg(const T &a, const U &b) const
Substitute two values in turn; exactly arg(a).arg(b).
Definition planmodel.hpp:72
Text arg(const T &value) const
Replace the lowest-numbered %1..%9 placeholder with a formatted value.
Definition planmodel.hpp:41
bool isEmpty() const
Return true when the string holds no characters.
Definition planmodel.hpp:31
std::ptrdiff_t size() const noexcept
Number of elements as a signed std::ptrdiff_t.
Definition storage.hpp:107
Planning algorithms on standard containers: quad patch planning and placement, size fields,...
constexpr double pi
The constant pi as a double.
Definition planmodel.hpp:13
bool cellIsConvex(const Vector< MeshVertex > &v, const MeshTriangle &c)
Test whether a cell is a strictly convex quadrilateral; triangles always pass.
Definition planmodel.hpp:321
std::string Bytes
Raw byte buffer held in a std::string; the planner uses it for exact binary keys.
Definition planmodel.hpp:75
Constraint polyline with marker, tag and optional strip width.
Definition planmodel.hpp:295
Vector< Point2 > path
Polyline vertices, XY units; each consecutive pair is one segment.
Definition planmodel.hpp:296
double stripWidth
Width (XY units) of a swept quad strip requested along an open path; a value that is not > 0 means a ...
Definition planmodel.hpp:301
int marker
Source marker kept on the generated edges; 0 means none.
Definition planmodel.hpp:297
Text tag
Caller-resolved label for the marker; not interpreted here.
Definition planmodel.hpp:298
double length() const
Euclidean length |b - a| via std::hypot, in XY units; finiteness is not checked.
Definition planmodel.hpp:130
Point2 a
Start point, XY units.
Definition planmodel.hpp:122
Point2 b
End point, XY units.
Definition planmodel.hpp:122
Line2(Point2 x, Point2 y)
Construct the segment from x to y.
Definition planmodel.hpp:128
Mesh edge between two vertex indices, carrying a source marker.
Definition planmodel.hpp:186
int v0
First endpoint index into the owning vertex array.
Definition planmodel.hpp:193
int v1
Second endpoint index into the owning vertex array.
Definition planmodel.hpp:193
int marker
Marker of the source constraint the edge came from; 0 (default) means none.
Definition planmodel.hpp:193
Text tag
Optional label; the native planning and audit code does not read it.
Definition planmodel.hpp:194
Mesh as read by the native audits: vertices, cells, marked edges and a status.
Definition planmodel.hpp:279
bool ok
True for a successful mesh (default false); the constraint audit refuses a mesh with ok false.
Definition planmodel.hpp:286
ReadArray< MeshEdge > boundaryEdges
Constraint and boundary edges with their source markers, indexing vertices.
Definition planmodel.hpp:283
ReadArray< MeshTriangle > triangles
Every cell, triangles and quads, indexing vertices.
Definition planmodel.hpp:281
ReadArray< MeshVertex > vertices
Positions (XY units) and elevations (Z units).
Definition planmodel.hpp:280
Text errorMsg
Failure description that accompanies ok == false.
Definition planmodel.hpp:287
Mesh cell: a triangle, or a quadrilateral when v3 is nonnegative.
Definition planmodel.hpp:144
int vertex(int k) const
Corner index stored in slot k.
Definition planmodel.hpp:166
Text tag
Cell label (for example the patch or region tag); empty when untagged.
Definition planmodel.hpp:155
int v1
Second corner index.
Definition planmodel.hpp:154
bool isQuad() const
Return true when v3 is nonnegative (a quadrilateral); indices are not checked.
Definition planmodel.hpp:157
int v2
Third corner index.
Definition planmodel.hpp:154
int v3
Fourth corner index of a quadrilateral; a negative value (default -1) marks a triangle.
Definition planmodel.hpp:154
int vertexCount() const
Return 4 for a quadrilateral and 3 for a triangle.
Definition planmodel.hpp:159
int v0
First corner index.
Definition planmodel.hpp:154
void setVertex(int k, int v)
Store a corner index in one slot, leaving the other fields unchanged.
Definition planmodel.hpp:173
Mesh vertex: planar position plus elevation.
Definition planmodel.hpp:133
Point2 xy
Planar position, XY units.
Definition planmodel.hpp:134
double z
Elevation, Z units; defaults to 0 (there is no separate "unset" flag).
Definition planmodel.hpp:135
Minimal iterator for range-based for loops: supports only *, prefix ++ and !=, and yields elements by...
Definition planmodel.hpp:247
Iterator & operator++()
Advance to the next index.
Definition planmodel.hpp:256
T operator*() const
Current element, by value.
Definition planmodel.hpp:251
bool operator!=(const Iterator &b) const
Compare positions only.
Definition planmodel.hpp:266
const ReadArray * array
Array being iterated; not owned.
Definition planmodel.hpp:248
std::size_t i
Current element index.
Definition planmodel.hpp:249
Mandatory mesh point with marker, tag and optional pre-sampled elevation.
Definition planmodel.hpp:304
Point2 xy
Position, XY units.
Definition planmodel.hpp:305
bool hasZ
True when z holds a pre-sampled elevation.
Definition planmodel.hpp:311
double z
Elevation in Z units; meaningful only when hasZ is true.
Definition planmodel.hpp:308
int marker
Point marker; 0 means none. The native size field seeds only nonzero ones.
Definition planmodel.hpp:306
Text tag
Caller-resolved label; not interpreted by the native code.
Definition planmodel.hpp:307