4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
31 /** \file gameengine/SceneGraph/SG_BBox.cpp
47 SG_BBox::SG_BBox(const MT_Point3 &min, const MT_Point3 &max) :
53 SG_BBox::SG_BBox(const SG_BBox &other, const MT_Transform &world) :
54 m_min(world(other.m_min)),
55 m_max(world(other.m_max))
57 *this += world(MT_Point3(m_min[0], m_min[1], m_max[2]));
58 *this += world(MT_Point3(m_min[0], m_max[1], m_min[2]));
59 *this += world(MT_Point3(m_min[0], m_max[1], m_max[2]));
60 *this += world(MT_Point3(m_max[0], m_min[1], m_min[2]));
61 *this += world(MT_Point3(m_max[0], m_min[1], m_max[2]));
62 *this += world(MT_Point3(m_max[0], m_max[1], m_min[2]));
65 SG_BBox::SG_BBox(const SG_BBox &other) :
75 SG_BBox& SG_BBox::operator +=(const MT_Point3 &point)
77 if (point[0] < m_min[0])
79 else if (point[0] > m_max[0])
82 if (point[1] < m_min[1])
84 else if (point[1] > m_max[1])
87 if (point[2] < m_min[2])
89 else if (point[2] > m_max[2])
95 SG_BBox& SG_BBox::operator += (const SG_BBox &bbox)
103 SG_BBox SG_BBox::operator +(const SG_BBox &bbox2) const
110 MT_Scalar SG_BBox::volume() const
112 MT_Vector3 size = m_max - m_min;
113 return size[0]*size[1]*size[2];
116 void SG_BBox::translate(const MT_Vector3& dx)
122 void SG_BBox::scale(const MT_Vector3& size, const MT_Point3& point)
124 MT_Vector3 center = (m_max - m_min)/2. + point;
125 m_max = (m_max - center)*size;
126 m_min = (m_min - center)*size;
130 SG_BBox SG_BBox::transform(const MT_Transform &world) const
132 SG_BBox bbox(world(m_min), world(m_max));
133 bbox += world(MT_Point3(m_min[0], m_min[1], m_max[2]));
134 bbox += world(MT_Point3(m_min[0], m_max[1], m_min[2]));
135 bbox += world(MT_Point3(m_min[0], m_max[1], m_max[2]));
136 bbox += world(MT_Point3(m_max[0], m_min[1], m_min[2]));
137 bbox += world(MT_Point3(m_max[0], m_min[1], m_max[2]));
138 bbox += world(MT_Point3(m_max[0], m_max[1], m_min[2]));
142 bool SG_BBox::inside(const MT_Point3 &point) const
144 return point[0] >= m_min[0] && point[0] <= m_max[0] &&
145 point[1] >= m_min[1] && point[1] <= m_max[1] &&
146 point[2] >= m_min[2] && point[2] <= m_max[2];
149 bool SG_BBox::inside(const SG_BBox& other) const
151 return inside(other.m_min) && inside(other.m_max);
154 bool SG_BBox::intersects(const SG_BBox& other) const
156 return inside(other.m_min) != inside(other.m_max);
159 bool SG_BBox::outside(const SG_BBox& other) const
161 return !inside(other.m_min) && !inside(other.m_max);
164 SG_BBox::intersect SG_BBox::test(const SG_BBox& other) const
166 bool point1(inside(other.m_min)), point2(inside(other.m_max));
168 return point1?(point2?INSIDE:INTERSECT):(point2?INTERSECT:OUTSIDE);
171 void SG_BBox::get(MT_Point3 *box, const MT_Transform &world) const
173 *box++ = world(m_min);
174 *box++ = world(MT_Point3(m_min[0], m_min[1], m_max[2]));
175 *box++ = world(MT_Point3(m_min[0], m_max[1], m_min[2]));
176 *box++ = world(MT_Point3(m_min[0], m_max[1], m_max[2]));
177 *box++ = world(MT_Point3(m_max[0], m_min[1], m_min[2]));
178 *box++ = world(MT_Point3(m_max[0], m_min[1], m_max[2]));
179 *box++ = world(MT_Point3(m_max[0], m_max[1], m_min[2]));
180 *box++ = world(m_max);
183 void SG_BBox::getaa(MT_Point3 *box, const MT_Transform &world) const
185 const MT_Point3 min(world(m_min)), max(world(m_max));
187 *box++ = MT_Point3(min[0], min[1], max[2]);
188 *box++ = MT_Point3(min[0], max[1], min[2]);
189 *box++ = MT_Point3(min[0], max[1], max[2]);
190 *box++ = MT_Point3(max[0], min[1], min[2]);
191 *box++ = MT_Point3(max[0], min[1], max[2]);
192 *box++ = MT_Point3(max[0], max[1], min[2]);
196 void SG_BBox::getmm(MT_Point3 *box, const MT_Transform &world) const
198 const MT_Point3 min(world(m_min)), max(world(m_max));
203 void SG_BBox::split(SG_BBox &left, SG_BBox &right) const
205 MT_Scalar sizex = m_max[0] - m_min[0];
206 MT_Scalar sizey = m_max[1] - m_min[1];
207 MT_Scalar sizez = m_max[2] - m_min[2];
213 left.m_max[0] = m_max[0];
214 left.m_max[1] = m_min[1] + sizey/2.0;
215 left.m_max[2] = m_max[2];
217 right.m_min[0] = m_min[0];
218 right.m_min[1] = m_min[1] + sizey/2.0;
219 right.m_min[2] = m_min[2];
221 std::cout << "splity" << std::endl;
224 left.m_max[0] = m_max[0];
225 left.m_max[1] = m_max[1];
226 left.m_max[2] = m_min[2] + sizez/2.0;
228 right.m_min[0] = m_min[0];
229 right.m_min[1] = m_min[1];
230 right.m_min[2] = m_min[2] + sizez/2.0;
232 std::cout << "splitz" << std::endl;
238 left.m_max[0] = m_min[0] + sizex/2.0;
239 left.m_max[1] = m_max[1];
240 left.m_max[2] = m_max[2];
242 right.m_min[0] = m_min[0] + sizex/2.0;
243 right.m_min[1] = m_min[1];
244 right.m_min[2] = m_min[2];
246 std::cout << "splitx" << std::endl;
249 left.m_max[0] = m_max[0];
250 left.m_max[1] = m_max[1];
251 left.m_max[2] = m_min[2] + sizez/2.0;
253 right.m_min[0] = m_min[0];
254 right.m_min[1] = m_min[1];
255 right.m_min[2] = m_min[2] + sizez/2.0;
257 std::cout << "splitz" << std::endl;
261 //std::cout << "Left: " << left.m_min << " -> " << left.m_max << " Right: " << right.m_min << " -> " << right.m_max << std::endl;