src/boundingbox.cpp

Sat, 29 Aug 2015 16:30:56 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 29 Aug 2015 16:30:56 +0300
changeset 951
54d90af8542d
parent 941
f895379d7fab
permissions
-rw-r--r--

Closed old branch

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2015 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"
#include "ldDocument.h"

// =============================================================================
//
BoundingBox::BoundingBox()
{
	reset();
}

// =============================================================================
//
void BoundingBox::calculateFromCurrentDocument()
{
	reset();

	if (CurrentDocument() == nullptr)
		return;

	for (LDObjectPtr obj : CurrentDocument()->objects())
		calcObject (obj);
}

// =============================================================================
//
void BoundingBox::calcObject (LDObjectPtr obj)
{
	switch (obj->type())
	{
		case OBJ_Line:
		case OBJ_Triangle:
		case OBJ_Quad:
		case OBJ_CondLine:
		{
			for (int i = 0; i < obj->numVertices(); ++i)
				calcVertex (obj->vertex (i));
		} break;

		case OBJ_Subfile:
		{
			LDSubfilePtr ref = obj.staticCast<LDSubfile>();
			LDObjectList objs = ref->inlineContents (true, false);

			for (LDObjectPtr obj : objs)
			{
				calcObject (obj);
				obj->destroy();
			}
		}
		break;

		default:
			break;
	}
}

// =============================================================================
//
BoundingBox& BoundingBox::operator<< (const Vertex& v)
{
	calcVertex (v);
	return *this;
}

// =============================================================================
//
BoundingBox& BoundingBox::operator<< (LDObjectPtr obj)
{
	calcObject (obj);
	return *this;
}

// =============================================================================
//
void BoundingBox::calcVertex (const Vertex& vertex)
{
	m_vertex0.setX (min (vertex.x(), m_vertex0.x()));
	m_vertex0.setY (min (vertex.y(), m_vertex0.y()));
	m_vertex0.setZ (min (vertex.z(), m_vertex0.z()));
	m_vertex1.setX (max (vertex.x(), m_vertex1.x()));
	m_vertex1.setY (max (vertex.y(), m_vertex1.y()));
	m_vertex1.setZ (max (vertex.z(), m_vertex1.z()));
	setEmpty (false);
}

// =============================================================================
//
void BoundingBox::reset()
{
	m_vertex0 = Vertex (10000.0, 10000.0, 10000.0);
	m_vertex1 = Vertex (-10000.0, -10000.0, -10000.0);
	setEmpty (true);
}

// =============================================================================
//
double BoundingBox::longestMeasurement() 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 = zscale;

	if (xscale > yscale)
	{
		if (xscale > zscale)
			size = xscale;
	}
	else if (yscale > zscale)
		size = yscale;

	if (abs (size) >= 2.0)
		return abs (size / 2);

	return 1.0;
}

// =============================================================================
//
Vertex BoundingBox::center() const
{
	return Vertex (
		(m_vertex0.x() + m_vertex1.x()) / 2,
		(m_vertex0.y() + m_vertex1.y()) / 2,
		(m_vertex0.z() + m_vertex1.z()) / 2);
}

mercurial