src/types/boundingbox.cpp

Sun, 19 Jan 2020 14:25:43 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 14:25:43 +0200
changeset 24
1a0faaaceb84
parent 23
3387a84ddaba
child 33
4c41bfe2ec6e
permissions
-rw-r--r--

added license

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2019 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "boundingbox.h"

BoundingBox& BoundingBox::operator<<(const Point3D& vertex)
{
	this->consider(vertex);
	return *this;
}

void BoundingBox::consider(const Point3D& vertex)
{
	this->minimum.x = math::min(vertex.x, this->minimum.x);
	this->minimum.y = math::min(vertex.y, this->minimum.y);
	this->minimum.z = math::min(vertex.z, this->minimum.z);
	this->maximum.x = math::max(vertex.x, this->maximum.x);
	this->maximum.y = math::max(vertex.y, this->maximum.y);
	this->maximum.z = math::max(vertex.z, this->maximum.z);
}

/*
 * Returns the length of the bounding box on the longest measure.
 */
double longestMeasure(const BoundingBox& box)
{
	if (box != emptyBoundingBox)
	{
		const double dx = std::abs(box.minimum.x - box.maximum.x);
		const double dy = std::abs(box.minimum.y - box.maximum.y);
		const double dz = std::abs(box.minimum.z - box.maximum.z);
		const double size = std::max(std::max(dx, dy), dz);
		return std::max(size / 2.0, 1.0);
	}
	else
	{
		return 0.0;
	}
}


/*
 * Yields the center of the bounding box.
 */
Point3D boxCenter(const BoundingBox& box)
{
	if (box != emptyBoundingBox)
	{
		return {
			(box.minimum.x + box.maximum.x) / 2,
			(box.minimum.y + box.maximum.y) / 2,
			(box.minimum.z + box.maximum.z) / 2
		};
	}
	else
	{
		return origin;
	}
}

/*
 * Returns the length of the bounding box's space diagonal.
 */
double spaceDiagonal(const BoundingBox& box)
{
	return math::distance(box.minimum, box.maximum);
}

bool operator==(const BoundingBox &box_1, const BoundingBox &box_2)
{
	return box_1.minimum == box_2.minimum and box_1.maximum == box_2.maximum;
}

bool operator!=(const BoundingBox &box_1, const BoundingBox &box_2)
{
	return not (box_1 == box_2);
}

mercurial