16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 |
18 |
19 #include "boundingbox.h" |
19 #include "boundingbox.h" |
20 |
20 |
21 // ============================================================================= |
21 BoundingBox::BoundingBox() {} |
22 // |
22 |
23 BoundingBox::BoundingBox() |
23 BoundingBox& BoundingBox::operator<<(const Vertex& vertex) |
24 { |
24 { |
25 reset(); |
25 consider(vertex); |
26 } |
|
27 |
|
28 // ============================================================================= |
|
29 // |
|
30 BoundingBox& BoundingBox::operator<< (const Vertex& v) |
|
31 { |
|
32 calcVertex (v); |
|
33 return *this; |
26 return *this; |
34 } |
27 } |
35 |
28 |
36 // ============================================================================= |
29 void BoundingBox::consider(const Vertex& vertex) |
37 // |
|
38 void BoundingBox::calcVertex (const Vertex& vertex) |
|
39 { |
30 { |
40 m_vertex0.x = qMin(vertex.x, m_vertex0.x); |
31 this->minimum.x = min(vertex.x, this->minimum.x); |
41 m_vertex0.y = qMin(vertex.y, m_vertex0.y); |
32 this->minimum.y = min(vertex.y, this->minimum.y); |
42 m_vertex0.z = qMin(vertex.z, m_vertex0.z); |
33 this->minimum.z = min(vertex.z, this->minimum.z); |
43 m_vertex1.x = qMax(vertex.x, m_vertex1.x); |
34 this->maximum.x = max(vertex.x, this->maximum.x); |
44 m_vertex1.y = qMax(vertex.y, m_vertex1.y); |
35 this->maximum.y = max(vertex.y, this->maximum.y); |
45 m_vertex1.z = qMax(vertex.z, m_vertex1.z); |
36 this->maximum.z = max(vertex.z, this->maximum.z); |
46 m_isEmpty = false; |
37 this->storedIsEmpty = false; |
47 } |
38 } |
48 |
39 |
49 // ============================================================================= |
40 /* |
50 // |
41 * Clears the bounding box |
51 // Clears the bounding box |
42 */ |
52 // |
43 void BoundingBox::clear() |
53 void BoundingBox::reset() |
|
54 { |
44 { |
55 m_vertex0 = {10000.0, 10000.0, 10000.0}; |
45 (*this) = {}; |
56 m_vertex1 = {-10000.0, -10000.0, -10000.0}; |
|
57 m_isEmpty = true; |
|
58 } |
46 } |
59 |
47 |
60 // ============================================================================= |
48 /* |
61 // |
49 * Returns the length of the bounding box on the longest measure. |
62 // Returns the length of the bounding box on the longest measure. |
50 */ |
63 // |
51 double BoundingBox::longestMeasure() const |
64 double BoundingBox::longestMeasurement() const |
|
65 { |
52 { |
66 double xscale = m_vertex0.x - m_vertex1.x; |
53 double dx = this->minimum.x - this->maximum.x; |
67 double yscale = m_vertex0.y - m_vertex1.y; |
54 double dy = this->minimum.y - this->maximum.y; |
68 double zscale = m_vertex0.z - m_vertex1.z; |
55 double dz = this->minimum.z - this->maximum.z; |
69 double size = qMax(xscale, qMax(yscale, zscale)); |
56 double size = max(dx, dy, dz); |
70 return qMax(qAbs(size / 2.0), 1.0); |
57 return max(abs(size / 2.0), 1.0); |
71 } |
58 } |
72 |
59 |
73 // ============================================================================= |
60 |
74 // |
61 /* |
75 // Yields the center of the bounding box. |
62 * Yields the center of the bounding box. |
76 // |
63 */ |
77 Vertex BoundingBox::center() const |
64 Vertex BoundingBox::center() const |
78 { |
65 { |
79 return { |
66 return { |
80 (m_vertex0.x + m_vertex1.x) / 2, |
67 (this->minimum.x + this->maximum.x) / 2, |
81 (m_vertex0.y + m_vertex1.y) / 2, |
68 (this->minimum.y + this->maximum.y) / 2, |
82 (m_vertex0.z + m_vertex1.z) / 2 |
69 (this->minimum.z + this->maximum.z) / 2 |
83 }; |
70 }; |
84 } |
71 } |
85 |
72 |
86 bool BoundingBox::isEmpty() const |
73 bool BoundingBox::isEmpty() const |
87 { |
74 { |
88 return m_isEmpty; |
75 return this->storedIsEmpty; |
89 } |
76 } |
90 |
77 |
91 const Vertex& BoundingBox::vertex0() const |
78 /* |
|
79 * Returns the minimum vertex, the -X, -Y, -Z corner. |
|
80 */ |
|
81 const Vertex& BoundingBox::minimumVertex() const |
92 { |
82 { |
93 return m_vertex0; |
83 return this->minimum; |
94 } |
84 } |
95 |
85 |
96 const Vertex& BoundingBox::vertex1() const |
86 /* |
|
87 * Returns the maximum vertex, the +X, +Y, +Z corner. |
|
88 */ |
|
89 const Vertex& BoundingBox::maximumVertex() const |
97 { |
90 { |
98 return m_vertex1; |
91 return this->maximum; |
99 } |
92 } |
|
93 |
|
94 /* |
|
95 * Returns the length of the bounding box's space diagonal. |
|
96 */ |
|
97 double BoundingBox::spaceDiagonal() const |
|
98 { |
|
99 return distance(this->minimumVertex(), this->maximumVertex()); |
|
100 } |