bounding_box.hpp 3.91 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library 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 Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Geom
{

template <typename VEC>
BoundingBox<VEC>::BoundingBox(const VEC& p) :
m_pMin(p),
m_pMax(p)
{}


template <typename VEC>
VEC& BoundingBox<VEC>::min()
{
	return m_pMin ;
}

template <typename VEC>
const VEC& BoundingBox<VEC>::min() const
{
	return m_pMin ;
}

template <typename VEC>
VEC& BoundingBox<VEC>::max()
{
	return m_pMax ;
}

template <typename VEC>
const VEC& BoundingBox<VEC>::max() const
{
	return m_pMax ;
}

template <typename VEC>
typename VEC::DATA_TYPE BoundingBox<VEC>::size(unsigned int coord) const
{
	assert(coord < m_pMax.dimension()) ;
	return m_pMax[coord] - m_pMin[coord] ;
}

template <typename VEC>
VEC BoundingBox<VEC>::center() const
{
	VEC center = (m_pMax + m_pMin) / typename VEC::DATA_TYPE(2) ;
	return center ;
}

/**********************************************/
/*                 FUNCTIONS                  */
/**********************************************/

template <typename VEC>
void BoundingBox<VEC>::addPoint(const VEC& p)
{
	for(unsigned int i = 0; i < p.dimension(); ++i)
	{
		if(p[i] < m_pMin[i])
			m_pMin[i] = p[i] ;
		if(p[i] > m_pMax[i])
			m_pMax[i] = p[i] ;
	}
}

template <typename VEC>
bool BoundingBox<VEC>::intersects(const BoundingBox<VEC>& bb)
{
	VEC bbmin = bb.min() ;
	VEC bbmax = bb.max() ;
97
	for(unsigned int i = 0; i < bbmin.dimension(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	{
		if(m_pMax[i] < bbmin[i])
			return false ;
		if(m_pMin[i] > bbmax[i])
			return false ;
	}
	return true ;
}

template <typename VEC>
void BoundingBox<VEC>::fusion(const BoundingBox<VEC>& bb)
{
	VEC bbmin = bb.min() ;
	VEC bbmax = bb.max() ;
	for(unsigned int i = 0; i < bbmin.size(); ++i)
	{
		if(bbmin[i] < m_pMin[i])
			m_pMin[i] = bbmin[i] ;
		if(bbmax[i] > m_pMax[i])
			m_pMax[i] = bbmax[i] ;
	}
}


//template <typename VEC>
//friend std::ostream& BoundingBox<VEC>::operator<<(std::ostream& out, const BoundingBox<VEC>& bb)
//{
//	out << bb.min() << " " << bb.max() ;
//	return out ;
//}
//
//template <typename VEC>
//friend std::istream& BoundingBox<VEC>::operator>>(std::istream& in, BoundingBox<VEC>& bb)
//{
//	in >> bb.min() >> bb.max() ;
//	return in ;
//}

//end namespace
}
}