bounding_box.hpp 4.93 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Geom
{

template <typename VEC>
32 33 34 35
BoundingBox<VEC>::BoundingBox() :
	m_initialized(false),
	m_pMin(0),
	m_pMax(0)
Pierre Kraemer's avatar
Pierre Kraemer committed
36 37
{}

38 39 40 41 42 43
template <typename VEC>
BoundingBox<VEC>::BoundingBox(const VEC& p) :
	m_initialized(true),
	m_pMin(p),
	m_pMax(p)
{}
Pierre Kraemer's avatar
Pierre Kraemer committed
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

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] ;
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
template <typename VEC>
typename VEC::DATA_TYPE BoundingBox<VEC>::maxSize() const
{
	typename VEC::DATA_TYPE max = m_pMax[0] - m_pMin[0] ;
	for(unsigned int i = 1; i < m_pMax.dimension(); ++i)
	{
		typename VEC::DATA_TYPE size = m_pMax[i] - m_pMin[i] ;
		if(size > max)
			max = size ;
	}
	return max ;
}

template <typename VEC>
typename VEC::DATA_TYPE BoundingBox<VEC>::minSize() const
{
	typename VEC::DATA_TYPE min = m_pMax[0] - m_pMin[0] ;
	for(unsigned int i = 1; i < m_pMax.dimension(); ++i)
	{
		typename VEC::DATA_TYPE size = m_pMax[i] - m_pMin[i] ;
		if(size < min)
			min = size ;
	}
	return min ;
}

template <typename VEC>
VEC BoundingBox<VEC>::diag() const
{
	return m_pMax - m_pMin ;
}

template <typename VEC>
typename VEC::DATA_TYPE BoundingBox<VEC>::diagSize() const
{
	return (m_pMax - m_pMin).norm() ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
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)
{
128 129 130 131 132 133
	if(!m_initialized)
	{
		m_pMin = p ;
		m_pMax = p ;
	}
	else
Pierre Kraemer's avatar
Pierre Kraemer committed
134
	{
135 136 137 138 139 140 141
		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] ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143 144 145 146 147 148 149
	}
}

template <typename VEC>
bool BoundingBox<VEC>::intersects(const BoundingBox<VEC>& bb)
{
	VEC bbmin = bb.min() ;
	VEC bbmax = bb.max() ;
150
	for(unsigned int i = 0; i < bbmin.dimension(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	{
		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 ;
//}

188
} // namespace Geom
Pierre Kraemer's avatar
Pierre Kraemer committed
189

190
} // namespace CGoGN