src/types/boundingbox.cpp

Sat, 05 Mar 2022 13:18:40 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:18:40 +0200
changeset 161
6fe485a84d06
parent 33
4c41bfe2ec6e
child 189
815fbaae9cb2
permissions
-rw-r--r--

Icons update

/*
 *  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 glm::vec3& vertex)
{
	this->consider(vertex);
	return *this;
}

void BoundingBox::consider(const glm::vec3& 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.
 */
float longestMeasure(const BoundingBox& box)
{
	if (box != emptyBoundingBox)
	{
		const float dx = std::abs(box.minimum.x - box.maximum.x);
		const float dy = std::abs(box.minimum.y - box.maximum.y);
		const float dz = std::abs(box.minimum.z - box.maximum.z);
		const float size = std::max(std::max(dx, dy), dz);
		return std::max(size / 2.0f, 1.0f);
	}
	else
	{
		return 0.0f;
	}
}


/*
 * Yields the center of the bounding box.
 */
glm::vec3 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 glm::vec3{0, 0, 0};
	}
}

/*
 * Returns the length of the bounding box's space diagonal.
 */
float spaceDiagonal(const BoundingBox& box)
{
	return glm::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