Loading...
Searching...
No Matches
geometry.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3#include <algorithm>
4#include <array>
5#include <cstdint>
6#include <type_traits>
7
8namespace meshkit
9{
20class Point2
21{
22 std::array<double, 2> xy_{};
23
24public:
26 constexpr Point2() = default;
32 constexpr Point2(double x, double y) : xy_{x, y} {}
34 constexpr double x() const noexcept { return xy_[0]; }
36 constexpr double y() const noexcept { return xy_[1]; }
42 double *data() noexcept { return xy_.data(); }
47 const double *data() const noexcept { return xy_.data(); }
49 constexpr bool isNull() const { return x() == 0 && y() == 0; }
56 static constexpr double dotProduct(Point2 a, Point2 b) { return a.x() * b.x() + a.y() * b.y(); }
62 Point2 &operator*=(double s)
63 {
64 xy_[0] *= s;
65 xy_[1] *= s;
66 return *this;
67 }
68
73 Point2 &operator/=(double s)
74 {
75 xy_[0] /= s;
76 xy_[1] /= s;
77 return *this;
78 }
79
85 {
86 xy_[0] += p.x();
87 xy_[1] += p.y();
88 return *this;
89 }
90
96 {
97 xy_[0] -= p.x();
98 xy_[1] -= p.y();
99 return *this;
100 }
101
107 friend constexpr Point2 operator+(Point2 a, Point2 b) { return {a.x() + b.x(), a.y() + b.y()}; }
114 friend constexpr Point2 operator-(Point2 a, Point2 b) { return {a.x() - b.x(), a.y() - b.y()}; }
121 friend constexpr Point2 operator*(Point2 a, double s) { return {a.x() * s, a.y() * s}; }
128 friend constexpr Point2 operator*(double s, Point2 a) { return a * s; }
135 friend constexpr Point2 operator/(Point2 a, double s) { return {a.x() / s, a.y() / s}; }
143 friend constexpr bool operator==(Point2 a, Point2 b)
144 {
145 return a.x() == b.x() && a.y() == b.y();
146 }
147
153 friend constexpr bool operator!=(Point2 a, Point2 b) { return !(a == b); }
154};
155
166class Box2
167{
168 double x0_ = 0, y0_ = 0, w_ = 0, h_ = 0;
169
170public:
172 constexpr Box2() = default;
181 constexpr Box2(Point2 a, Point2 b)
182 : x0_(a.x()), y0_(a.y()), w_(b.x() - a.x()), h_(b.y() - a.y())
183 {
184 }
185
186 constexpr double left() const { return x0_; }
188 constexpr double right() const { return x0_ + w_; }
190 constexpr double top() const { return y0_; }
192 constexpr double bottom() const { return y0_ + h_; }
194 constexpr double width() const { return w_; }
196 constexpr double height() const { return h_; }
204 Box2(double x, double y, double w, double h) : x0_(x), y0_(y), w_(w), h_(h) {}
211 {
212 return {{std::min(left(), right()), std::min(top(), bottom())},
213 {std::max(left(), right()), std::max(top(), bottom())}};
214 }
215
216 bool isValid() const { return w_ > 0 && h_ > 0; }
222 bool isEmpty() const { return w_ <= 0 || h_ <= 0; }
224 bool isNull() const { return w_ == 0 && h_ == 0; }
226 Point2 bottomRight() const { return {right(), bottom()}; }
228 Point2 topLeft() const { return {left(), top()}; }
235 bool contains(double x, double y) const { return contains({x, y}); }
243 bool contains(Point2 p) const
244 {
245 return p.x() >= left() && p.x() <= right() && p.y() >= top() && p.y() <= bottom();
246 }
247
253 bool intersects(Box2 b) const
254 {
255 return left() < b.right() && right() > b.left() && top() < b.bottom() && bottom() > b.top();
256 }
257
265 {
266 double x = std::max(left(), b.left()), y = std::max(top(), b.top()),
267 r = std::min(right(), b.right()), t = std::min(bottom(), b.bottom());
268 return r < x || t < y ? Box2{} : Box2{{x, y}, {r, t}};
269 }
270
280 Box2 adjusted(double l, double t, double r, double b) const
281 {
282 return {{left() + l, top() + t}, {right() + r, bottom() + b}};
283 }
284
285 constexpr Point2 center() const { return {x0_ + w_ * .5, y0_ + h_ * .5}; }
294 Box2 united(Box2 b) const
295 {
296 return {{std::min(x0_, b.x0_), std::min(y0_, b.y0_)},
297 {std::max(right(), b.right()), std::max(bottom(), b.bottom())}};
298 }
299};
300
308struct Point3 {
320 double x = 0, y = 0, z = 0;
321};
322static_assert(std::is_standard_layout<Point3>::value && sizeof(Point3) == 3 * sizeof(double));
323static_assert(std::is_standard_layout<Point2>::value && sizeof(Point2) == 2 * sizeof(double));
324}
bool contains(double x, double y) const
Inclusive point-in-box test on coordinates; same as contains(Point2(x, y)).
Definition geometry.hpp:235
constexpr double left() const
X of the origin corner; the minimum x when width() >= 0.
Definition geometry.hpp:186
constexpr Point2 center() const
Midpoint (left() + width() / 2, top() + height() / 2).
Definition geometry.hpp:285
Box2(double x, double y, double w, double h)
Box from an origin corner and a size, all stored as given.
Definition geometry.hpp:204
bool isValid() const
True when width and height are both > 0.
Definition geometry.hpp:216
bool isEmpty() const
True when width <= 0 or height <= 0.
Definition geometry.hpp:222
Box2 adjusted(double l, double t, double r, double b) const
Copy with each edge moved by an offset, in XY plane units.
Definition geometry.hpp:280
Box2 normalized() const
Copy with non-negative width and height covering the same two corners.
Definition geometry.hpp:210
bool intersects(Box2 b) const
Strict overlap test.
Definition geometry.hpp:253
constexpr double bottom() const
top() + height(); the maximum y when height() >= 0.
Definition geometry.hpp:192
bool contains(Point2 p) const
Inclusive point-in-box test.
Definition geometry.hpp:243
constexpr double width() const
Signed extent along x in XY plane units; negative when not normalized.
Definition geometry.hpp:194
bool isNull() const
True when width and height are both exactly 0 (one zero side is empty, not null).
Definition geometry.hpp:224
Box2 intersected(Box2 b) const
Overlap of this box and b.
Definition geometry.hpp:264
Box2 united(Box2 b) const
Bounding box of this box and b.
Definition geometry.hpp:294
constexpr Box2()=default
Null box at the origin: width and height 0, so isNull() and isEmpty() hold.
Point2 topLeft() const
Corner (left(), top()), the origin corner.
Definition geometry.hpp:228
constexpr double right() const
left() + width(); the maximum x when width() >= 0.
Definition geometry.hpp:188
constexpr double top() const
Y of the origin corner; the minimum y when height() >= 0.
Definition geometry.hpp:190
constexpr Box2(Point2 a, Point2 b)
Box spanning from corner a to corner b.
Definition geometry.hpp:181
constexpr double height() const
Signed extent along y in XY plane units; negative when not normalized.
Definition geometry.hpp:196
Point2 bottomRight() const
Corner (right(), bottom()).
Definition geometry.hpp:226
Point or displacement vector in the horizontal (XY) plane.
Definition geometry.hpp:21
constexpr Point2(double x, double y)
Construct a point from its coordinates.
Definition geometry.hpp:32
friend constexpr bool operator!=(Point2 a, Point2 b)
Negation of operator==.
Definition geometry.hpp:153
static constexpr double dotProduct(Point2 a, Point2 b)
Dot product a.x * b.x + a.y * b.y.
Definition geometry.hpp:56
friend constexpr Point2 operator/(Point2 a, double s)
Divide a point by a divisor.
Definition geometry.hpp:135
const double * data() const noexcept
Read-only pointer to the two contiguous coordinates {x, y}.
Definition geometry.hpp:47
friend constexpr Point2 operator-(Point2 a, Point2 b)
Component-wise difference, the displacement from b to a.
Definition geometry.hpp:114
Point2 & operator+=(Point2 p)
Add p component-wise in place.
Definition geometry.hpp:84
constexpr double y() const noexcept
Y coordinate in XY plane units.
Definition geometry.hpp:36
friend constexpr Point2 operator*(double s, Point2 a)
Scale a point by a factor, factor first; same result as a * s.
Definition geometry.hpp:128
friend constexpr Point2 operator+(Point2 a, Point2 b)
Component-wise sum.
Definition geometry.hpp:107
friend constexpr bool operator==(Point2 a, Point2 b)
Exact component-wise equality, with no tolerance.
Definition geometry.hpp:143
Point2 & operator*=(double s)
Multiply both coordinates by s in place.
Definition geometry.hpp:62
constexpr double x() const noexcept
X coordinate in XY plane units.
Definition geometry.hpp:34
Point2 & operator-=(Point2 p)
Subtract p component-wise in place.
Definition geometry.hpp:95
friend constexpr Point2 operator*(Point2 a, double s)
Scale a point by a factor.
Definition geometry.hpp:121
double * data() noexcept
Pointer to the two contiguous coordinates {x, y}.
Definition geometry.hpp:42
constexpr bool isNull() const
True when both coordinates compare equal to 0 (-0.0 counts, NaN does not).
Definition geometry.hpp:49
constexpr Point2()=default
Construct the origin (0, 0).
Point2 & operator/=(double s)
Divide both coordinates by s in place.
Definition geometry.hpp:73
The Qt-free MeshKit library: job events and progress (meshkit::runtime), the neutral mesh model,...
Plain x, y, z coordinate triple; the vertex type of meshkit::Mesh.
Definition geometry.hpp:308
double x
Horizontal x coordinate in XY plane units; default 0.
Definition geometry.hpp:320
double z
Vertical coordinate (elevation) in Z units; default 0.
Definition geometry.hpp:320
double y
Horizontal y coordinate in XY plane units; default 0.
Definition geometry.hpp:320