-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuboid.cpp
More file actions
227 lines (198 loc) · 5.66 KB
/
cuboid.cpp
File metadata and controls
227 lines (198 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "classes/cuboid.h"
#include <QMatrix4x4>
using namespace Mildred;
//! Construct a new Cuboid from the supplied positions
Cuboid::Cuboid(QVector3D v1, QVector3D v2) : v1x_(v1.x()), v1y_(v1.y()), v1z_(v1.z()), v2x_(v2.x()), v2y_(v2.y()), v2z_(v2.z())
{
}
//! Reset the cuboid to null size
/*!
* Set all extreme coordinates to null.
*/
void Cuboid::reset()
{
v1x_ = std::nullopt;
v1y_ = std::nullopt;
v1z_ = std::nullopt;
v2x_ = std::nullopt;
v2y_ = std::nullopt;
v2z_ = std::nullopt;
}
//! Set x extent
void Cuboid::setXExtent(double extentMin, double extentMax)
{
v1x_ = extentMin;
v2x_ = extentMax;
}
//! Set y extent
void Cuboid::setYExtent(double extentMin, double extentMax)
{
v1y_ = extentMin;
v2y_ = extentMax;
}
//! Set z extent
void Cuboid::setZExtent(double extentMin, double extentMax)
{
v1z_ = extentMin;
v2z_ = extentMax;
}
//! Return the lower left back coordinate
/*!
* Return the lower left back coordinate of the cuboid as a QVector3D. Any as-yet undefined coordinates will be set to zero.
*/
QVector3D Cuboid::lowerLeftBack() const
{
return {float(v1x_.value_or(0.0)), float(v1y_.value_or(0.0)), float(v1z_.value_or(0.0))};
}
//! Return the upper right front coordinate
/*!
* Return the upper right front coordinate of the cuboid as a QVector3D. Any as-yet undefined coordinates will be set to zero.
*/
QVector3D Cuboid::upperRightFront() const
{
return {float(v2x_.value_or(0.0)), float(v2y_.value_or(0.0)), float(v2z_.value_or(0.0))};
}
//! Expand cuboid to encompass supplied (optional) coordinates
void Cuboid::expand(std::optional<double> x, std::optional<double> y, std::optional<double> z)
{
if (x)
{
if (!v1x_ || x < v1x_.value())
v1x_ = x;
if (!v2x_ || x > v2x_.value())
v2x_ = x;
}
if (y)
{
if (!v1y_ || y < v1y_.value())
v1y_ = y;
if (!v2y_ || y > v2y_.value())
v2y_ = y;
}
if (z)
{
if (!v1z_ || z < v1z_.value())
v1z_ = z;
if (!v2z_ || z > v2z_.value())
v2z_ = z;
}
}
//! Expand cuboid to encompass supplied point
/*!
* Expand the current extrema so as to encompass the supplied point @param v.
*/
void Cuboid::expand(const QVector3D &v)
{
if (!v1x_ || v.x() < v1x_.value())
v1x_ = v.x();
if (!v1y_ || v.y() < v1y_.value())
v1y_ = v.y();
if (!v1z_ || v.z() < v1z_.value())
v1z_ = v.z();
if (!v2x_ || v.x() > v2x_.value())
v2x_ = v.x();
if (!v2y_ || v.y() > v2y_.value())
v2y_ = v.y();
if (!v2z_ || v.z() > v2z_.value())
v2z_ = v.z();
}
//! Expand cuboid to encompass supplied cuboid
/*!
* Expand the current extrema so as to completely encompass the supplied @param cuboid.
*/
void Cuboid::expand(const Cuboid &cuboid)
{
expand(cuboid.v1x_, cuboid.v1y_, cuboid.v1z_);
expand(cuboid.v2x_, cuboid.v2y_, cuboid.v2z_);
}
//! Translate the cuboid by the supplied vector
void Cuboid::translate(QVector3D v)
{
if (v1x_)
*v1x_ += v.x();
if (v1y_)
*v1y_ += v.y();
if (v1z_)
*v1z_ += v.z();
if (v2x_)
*v2x_ += v.x();
if (v2y_)
*v2y_ += v.y();
if (v2z_)
*v2z_ += v.z();
}
//! Expand x extent of cuboid by given amount
void Cuboid::expandX(double delta)
{
v1x_ = v1x_.value_or(0.0) - delta * 0.5;
v2x_ = v2x_.value_or(0.0) + delta * 0.5;
}
//! Expand y extent of cuboid by given amount
void Cuboid::expandY(double delta)
{
v1y_ = v1y_.value_or(0.0) - delta * 0.5;
v2y_ = v2y_.value_or(0.0) + delta * 0.5;
}
//! Expand z extent of cuboid by given amount
void Cuboid::expandZ(double delta)
{
v1z_ = v1z_.value_or(0.0) - delta * 0.5;
v2z_ = v2z_.value_or(0.0) + delta * 0.5;
}
//! Return whether a valid extent in x exists
bool Cuboid::validXExtent() const { return v1x_ && v2x_; }
//! Return whether a valid extent in y exists
bool Cuboid::validYExtent() const { return v1y_ && v2y_; }
//! Return whether a valid extent in z exists
bool Cuboid::validZExtent() const { return v1z_ && v2z_; }
//! Return x extent of cuboid
float Cuboid::xExtent() const
{
if (v1x_ && v2x_)
return *v2x_ - *v1x_;
else
return 0.0;
}
//! Return y extent of cuboid
float Cuboid::yExtent() const
{
if (v1y_ && v2y_)
return *v2y_ - *v1y_;
else
return 0.0;
}
//! Return z extent of cuboid
float Cuboid::zExtent() const
{
if (v1z_ && v2z_)
return *v2z_ - *v1z_;
else
return 0.0;
}
//! Return vector of extents
QVector3D Cuboid::extents() const { return {xExtent(), yExtent(), zExtent()}; }
//! Return bounding box cuboid required for rotated original cuboid (in degrees, around z-axis)
/*!
* Returns the bounding cuboid required to contain the original cuboid rotated @param rotation
* @return
*/
Cuboid Cuboid::zRotatedBoundingCuboid(QVector3D rotationOrigin, double thetaZ) const
{
QMatrix4x4 m;
m.rotate(thetaZ, QVector3D{0.0, 0.0, 1.0});
std::array<QPointF, 4> corners;
corners[0] = QPointF(v1x_.value_or(0.0) - rotationOrigin.x(), v1y_.value_or(0.0) - rotationOrigin.y());
corners[1] = QPointF(v2x_.value_or(0.0) - rotationOrigin.x(), v1y_.value_or(0.0) - rotationOrigin.y());
corners[2] = QPointF(v1x_.value_or(0.0) - rotationOrigin.x(), v2y_.value_or(0.0) - rotationOrigin.y());
corners[3] = QPointF(v2x_.value_or(0.0) - rotationOrigin.x(), v2y_.value_or(0.0) - rotationOrigin.y());
Cuboid rotated;
for (const auto &p : corners)
{
auto newP = m.map(p);
rotated.expand(newP.x() + rotationOrigin.x(), newP.y() + rotationOrigin.y(), {});
}
// Set Z extents
rotated.v1z_ = v1z_;
rotated.v2z_ = v2z_;
return rotated;
}