src/types/boundingbox.cpp

Mon, 27 Jun 2022 01:49:20 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 27 Jun 2022 01:49:20 +0300
changeset 268
fb319526ba6c
parent 264
76a025db4948
permissions
-rw-r--r--

Define application name and version in CMakeLists

/*
 *  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 "src/types/boundingbox.h"

/**
 * @brief Resizes @c box so that @c vertex fits inside
 * @param box
 * @param vertex
 */
void addPointToBox(BoundingBox &box, const glm::vec3 &vertex)
{
	box.minimum.x = std::min(vertex.x, box.minimum.x);
	box.minimum.y = std::min(vertex.y, box.minimum.y);
	box.minimum.z = std::min(vertex.z, box.minimum.z);
	box.maximum.x = std::max(vertex.x, box.maximum.x);
	box.maximum.y = std::max(vertex.y, box.maximum.y);
	box.maximum.z = std::max(vertex.z, box.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