SWMMVis  6.0.0-alpha.4
Qt6/C++ GIS-based graphical user interface for the SWMMVis engine (6.0.0-alpha.4)
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{
10class Point2
11{
12 std::array<double, 2> xy_{};
13
14public:
15 constexpr Point2() = default;
16 constexpr Point2(double x, double y) : xy_{x, y} {}
17 constexpr double x() const noexcept { return xy_[0]; }
18 constexpr double y() const noexcept { return xy_[1]; }
19 double *data() noexcept { return xy_.data(); }
20 const double *data() const noexcept { return xy_.data(); }
21 constexpr bool isNull() const { return x() == 0 && y() == 0; }
22 static constexpr double dotProduct(Point2 a, Point2 b) { return a.x() * b.x() + a.y() * b.y(); }
24 {
25 xy_[0] *= s;
26 xy_[1] *= s;
27 return *this;
28 }
30 {
31 xy_[0] /= s;
32 xy_[1] /= s;
33 return *this;
34 }
36 {
37 xy_[0] += p.x();
38 xy_[1] += p.y();
39 return *this;
40 }
42 {
43 xy_[0] -= p.x();
44 xy_[1] -= p.y();
45 return *this;
46 }
47 friend constexpr Point2 operator+(Point2 a, Point2 b) { return {a.x() + b.x(), a.y() + b.y()}; }
48 friend constexpr Point2 operator-(Point2 a, Point2 b) { return {a.x() - b.x(), a.y() - b.y()}; }
49 friend constexpr Point2 operator*(Point2 a, double s) { return {a.x() * s, a.y() * s}; }
50 friend constexpr Point2 operator*(double s, Point2 a) { return a * s; }
51 friend constexpr Point2 operator/(Point2 a, double s) { return {a.x() / s, a.y() / s}; }
52 friend constexpr bool operator==(Point2 a, Point2 b)
53 {
54 return a.x() == b.x() && a.y() == b.y();
55 }
56 friend constexpr bool operator!=(Point2 a, Point2 b) { return !(a == b); }
57};
58class Box2
59{
60 double x0_ = 0, y0_ = 0, w_ = 0, h_ = 0;
61
62public:
63 constexpr Box2() = default;
64 constexpr Box2(Point2 a, Point2 b)
65 : x0_(a.x()), y0_(a.y()), w_(b.x() - a.x()), h_(b.y() - a.y())
66 {
67 }
68 constexpr double left() const { return x0_; }
69 constexpr double right() const { return x0_ + w_; }
70 constexpr double top() const { return y0_; }
71 constexpr double bottom() const { return y0_ + h_; }
72 constexpr double width() const { return w_; }
73 constexpr double height() const { return h_; }
74 Box2(double x, double y, double w, double h) : x0_(x), y0_(y), w_(w), h_(h) {}
76 {
77 return {{std::min(left(), right()), std::min(top(), bottom())},
78 {std::max(left(), right()), std::max(top(), bottom())}};
79 }
80 bool isValid() const { return w_ > 0 && h_ > 0; }
81 bool isEmpty() const { return w_ <= 0 || h_ <= 0; }
82 bool isNull() const { return w_ == 0 && h_ == 0; }
83 Point2 bottomRight() const { return {right(), bottom()}; }
84 Point2 topLeft() const { return {left(), top()}; }
85 bool contains(double x, double y) const { return contains({x, y}); }
86 bool contains(Point2 p) const
87 {
88 return p.x() >= left() && p.x() <= right() && p.y() >= top() && p.y() <= bottom();
89 }
90 bool intersects(Box2 b) const
91 {
92 return left() < b.right() && right() > b.left() && top() < b.bottom() && bottom() > b.top();
93 }
95 {
96 double x = std::max(left(), b.left()), y = std::max(top(), b.top()),
97 r = std::min(right(), b.right()), t = std::min(bottom(), b.bottom());
98 return r < x || t < y ? Box2{} : Box2{{x, y}, {r, t}};
99 }
100 Box2 adjusted(double l, double t, double r, double b) const
101 {
102 return {{left() + l, top() + t}, {right() + r, bottom() + b}};
103 }
104 constexpr Point2 center() const { return {x0_ + w_ * .5, y0_ + h_ * .5}; }
106 {
107 return {{std::min(x0_, b.x0_), std::min(y0_, b.y0_)},
108 {std::max(right(), b.right()), std::max(bottom(), b.bottom())}};
109 }
110};
111struct Point3 {
112 double x = 0, y = 0, z = 0;
113};
114static_assert(std::is_standard_layout<Point3>::value && sizeof(Point3) == 3 * sizeof(double));
115static_assert(std::is_standard_layout<Point2>::value && sizeof(Point2) == 2 * sizeof(double));
116}
Definition geometry.hpp:59
bool contains(double x, double y) const
Definition geometry.hpp:85
constexpr double left() const
Definition geometry.hpp:68
constexpr Point2 center() const
Definition geometry.hpp:104
Box2(double x, double y, double w, double h)
Definition geometry.hpp:74
bool isValid() const
Definition geometry.hpp:80
bool isEmpty() const
Definition geometry.hpp:81
Box2 adjusted(double l, double t, double r, double b) const
Definition geometry.hpp:100
Box2 normalized() const
Definition geometry.hpp:75
bool intersects(Box2 b) const
Definition geometry.hpp:90
constexpr double bottom() const
Definition geometry.hpp:71
bool contains(Point2 p) const
Definition geometry.hpp:86
constexpr double width() const
Definition geometry.hpp:72
bool isNull() const
Definition geometry.hpp:82
Box2 intersected(Box2 b) const
Definition geometry.hpp:94
Box2 united(Box2 b) const
Definition geometry.hpp:105
constexpr Box2()=default
Point2 topLeft() const
Definition geometry.hpp:84
constexpr double right() const
Definition geometry.hpp:69
constexpr double top() const
Definition geometry.hpp:70
constexpr Box2(Point2 a, Point2 b)
Definition geometry.hpp:64
constexpr double height() const
Definition geometry.hpp:73
Point2 bottomRight() const
Definition geometry.hpp:83
Definition geometry.hpp:11
constexpr Point2(double x, double y)
Definition geometry.hpp:16
friend constexpr bool operator!=(Point2 a, Point2 b)
Definition geometry.hpp:56
static constexpr double dotProduct(Point2 a, Point2 b)
Definition geometry.hpp:22
friend constexpr Point2 operator/(Point2 a, double s)
Definition geometry.hpp:51
const double * data() const noexcept
Definition geometry.hpp:20
friend constexpr Point2 operator-(Point2 a, Point2 b)
Definition geometry.hpp:48
Point2 & operator+=(Point2 p)
Definition geometry.hpp:35
constexpr double y() const noexcept
Definition geometry.hpp:18
friend constexpr Point2 operator*(double s, Point2 a)
Definition geometry.hpp:50
friend constexpr Point2 operator+(Point2 a, Point2 b)
Definition geometry.hpp:47
friend constexpr bool operator==(Point2 a, Point2 b)
Definition geometry.hpp:52
Point2 & operator*=(double s)
Definition geometry.hpp:23
constexpr double x() const noexcept
Definition geometry.hpp:17
Point2 & operator-=(Point2 p)
Definition geometry.hpp:41
friend constexpr Point2 operator*(Point2 a, double s)
Definition geometry.hpp:49
double * data() noexcept
Definition geometry.hpp:19
constexpr bool isNull() const
Definition geometry.hpp:21
constexpr Point2()=default
Point2 & operator/=(double s)
Definition geometry.hpp:29
DiagramType t
Definition diagramspec.cpp:23
double s
Scene px per model length unit.
Definition inletdrawingview.cpp:95
ArrowPlacement p
Definition linesymbollayer.cpp:29
int h
Definition mesh2dresultsexport.cpp:477
int w
Definition mesh2dresultsexport.cpp:477
Definition qtcdtstorage.h:9
Definition geometry.hpp:111
double x
Definition geometry.hpp:112
double z
Definition geometry.hpp:112
double y
Definition geometry.hpp:112
int y
Definition terrainerrorfield.cpp:60
double a
Definition terrainerrorfield.cpp:42
double b
Definition terrainerrorfield.cpp:42
int x
Definition terrainerrorfield.cpp:60