reworked bounding box

Mon, 02 Apr 2018 10:59:38 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 02 Apr 2018 10:59:38 +0300
changeset 1370
c6d5ba08c62c
parent 1369
1e2391b78d17
child 1371
b8df4748d04e

reworked bounding box

src/basics.h file | annotate | diff | comparison | revisions
src/types/boundingbox.cpp file | annotate | diff | comparison | revisions
src/types/boundingbox.h file | annotate | diff | comparison | revisions
src/types/vertex.cpp file | annotate | diff | comparison | revisions
src/types/vertex.h file | annotate | diff | comparison | revisions
--- a/src/basics.h	Mon Apr 02 10:33:17 2018 +0300
+++ b/src/basics.h	Mon Apr 02 10:59:38 2018 +0300
@@ -78,6 +78,7 @@
 }
 
 static const double pi = 3.14159265358979323846;
+static const double inf = std::numeric_limits<double>::infinity();
 
 /*
  * Returns the norm of a vector.
--- a/src/types/boundingbox.cpp	Mon Apr 02 10:33:17 2018 +0300
+++ b/src/types/boundingbox.cpp	Mon Apr 02 10:59:38 2018 +0300
@@ -18,82 +18,83 @@
 
 #include "boundingbox.h"
 
-// =============================================================================
-//
-BoundingBox::BoundingBox()
+BoundingBox::BoundingBox() {}
+
+BoundingBox& BoundingBox::operator<<(const Vertex& vertex)
 {
-	reset();
-}
-
-// =============================================================================
-//
-BoundingBox& BoundingBox::operator<< (const Vertex& v)
-{
-	calcVertex (v);
+	consider(vertex);
 	return *this;
 }
 
-// =============================================================================
-//
-void BoundingBox::calcVertex (const Vertex& vertex)
+void BoundingBox::consider(const Vertex& vertex)
 {
-	m_vertex0.x = qMin(vertex.x, m_vertex0.x);
-	m_vertex0.y = qMin(vertex.y, m_vertex0.y);
-	m_vertex0.z = qMin(vertex.z, m_vertex0.z);
-	m_vertex1.x = qMax(vertex.x, m_vertex1.x);
-	m_vertex1.y = qMax(vertex.y, m_vertex1.y);
-	m_vertex1.z = qMax(vertex.z, m_vertex1.z);
-	m_isEmpty = false;
+	this->minimum.x = min(vertex.x, this->minimum.x);
+	this->minimum.y = min(vertex.y, this->minimum.y);
+	this->minimum.z = min(vertex.z, this->minimum.z);
+	this->maximum.x = max(vertex.x, this->maximum.x);
+	this->maximum.y = max(vertex.y, this->maximum.y);
+	this->maximum.z = max(vertex.z, this->maximum.z);
+	this->storedIsEmpty = false;
 }
 
-// =============================================================================
-//
-// Clears the bounding box
-//
-void BoundingBox::reset()
+/*
+ * Clears the bounding box
+ */
+void BoundingBox::clear()
 {
-	m_vertex0 = {10000.0, 10000.0, 10000.0};
-	m_vertex1 = {-10000.0, -10000.0, -10000.0};
-	m_isEmpty = true;
+	(*this)	= {};
 }
 
-// =============================================================================
-//
-// Returns the length of the bounding box on the longest measure.
-//
-double BoundingBox::longestMeasurement() const
+/*
+ * Returns the length of the bounding box on the longest measure.
+ */
+double BoundingBox::longestMeasure() const
 {
-	double xscale = m_vertex0.x - m_vertex1.x;
-	double yscale = m_vertex0.y - m_vertex1.y;
-	double zscale = m_vertex0.z - m_vertex1.z;
-	double size = qMax(xscale, qMax(yscale, zscale));
-	return qMax(qAbs(size / 2.0), 1.0);
+	double dx = this->minimum.x - this->maximum.x;
+	double dy = this->minimum.y - this->maximum.y;
+	double dz = this->minimum.z - this->maximum.z;
+	double size = max(dx, dy, dz);
+	return max(abs(size / 2.0), 1.0);
 }
 
-// =============================================================================
-//
-// Yields the center of the bounding box.
-//
+
+/*
+ * Yields the center of the bounding box.
+ */
 Vertex BoundingBox::center() const
 {
 	return {
-		(m_vertex0.x + m_vertex1.x) / 2,
-		(m_vertex0.y + m_vertex1.y) / 2,
-		(m_vertex0.z + m_vertex1.z) / 2
+		(this->minimum.x + this->maximum.x) / 2,
+		(this->minimum.y + this->maximum.y) / 2,
+		(this->minimum.z + this->maximum.z) / 2
 	};
 }
 
 bool BoundingBox::isEmpty() const
 {
-	return m_isEmpty;
+	return this->storedIsEmpty;
+}
+
+/*
+ * Returns the minimum vertex, the -X, -Y, -Z corner.
+ */
+const Vertex& BoundingBox::minimumVertex() const
+{
+	return this->minimum;
 }
 
-const Vertex& BoundingBox::vertex0() const
+/*
+ * Returns the maximum vertex, the +X, +Y, +Z corner.
+ */
+const Vertex& BoundingBox::maximumVertex() const
 {
-	return m_vertex0;
+	return this->maximum;
 }
 
-const Vertex& BoundingBox::vertex1() const
+/*
+ * Returns the length of the bounding box's space diagonal.
+ */
+double BoundingBox::spaceDiagonal() const
 {
-	return m_vertex1;
+	return distance(this->minimumVertex(), this->maximumVertex());
 }
--- a/src/types/boundingbox.h	Mon Apr 02 10:33:17 2018 +0300
+++ b/src/types/boundingbox.h	Mon Apr 02 10:59:38 2018 +0300
@@ -28,18 +28,19 @@
 public:
 	BoundingBox();
 
-	void calcVertex(const Vertex& vertex);
+	void consider(const Vertex& vertex);
 	Vertex center() const;
 	bool isEmpty() const;
-	double longestMeasurement() const;
-	void reset();
-	const Vertex& vertex0() const;
-	const Vertex& vertex1() const;
+	double longestMeasure() const;
+	void clear();
+	const Vertex& minimumVertex() const;
+	const Vertex& maximumVertex() const;
+	double spaceDiagonal() const;
 
 	BoundingBox& operator<<(const Vertex& v);
 
 private:
-	bool m_isEmpty;
-	Vertex m_vertex0;
-	Vertex m_vertex1;
+	bool storedIsEmpty = true;
+	Vertex minimum {inf, inf, inf};
+	Vertex maximum {-inf, -inf, -inf};
 };
--- a/src/types/vertex.cpp	Mon Apr 02 10:33:17 2018 +0300
+++ b/src/types/vertex.cpp	Mon Apr 02 10:59:38 2018 +0300
@@ -191,11 +191,26 @@
 	};
 }
 
+/*
+ * Returns the distance from one vertex to another.
+ */
+qreal distance(const Vertex& one, const Vertex& other)
+{
+	return (one - other).length();
+}
+
+/*
+ * Inserts this vertex into a data stream. This is needed for vertices to be
+ * stored in QSettings.
+ */
 QDataStream& operator<<(QDataStream& out, const Vertex& vertex)
 {
 	return out << vertex.x << vertex.y << vertex.z;
 }
 
+/*
+ * Takes a vertex from a data stream.
+ */
 QDataStream& operator>>(QDataStream& in, Vertex& vertex)
 {
 	return in >> vertex.x >> vertex.y >> vertex.z;
--- a/src/types/vertex.h	Mon Apr 02 10:33:17 2018 +0300
+++ b/src/types/vertex.h	Mon Apr 02 10:59:38 2018 +0300
@@ -59,3 +59,5 @@
 unsigned int qHash(const Vertex& key);
 QDataStream& operator<<(QDataStream& out, const Vertex& vertex);
 QDataStream& operator>>(QDataStream& in, Vertex& vertex);
+
+qreal distance(const Vertex& one, const Vertex& other);

mercurial