00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "OSGConfig.h"
00045
00046 #include <OSGLog.h>
00047
00048 #include <assert.h>
00049
00050 #include <OSGLine.h>
00051 #include <OSGMatrix.h>
00052
00053 #include "OSGBoxVolume.h"
00054
00055
00064 OSG_USING_NAMESPACE
00065
00066
00068
00069 void BoxVolume::getCenter(Pnt3f ¢er) const
00070 {
00071 if(isEmpty() == true)
00072 {
00073 center.setValues(0.0f, 0.0f, 0.0f);
00074 }
00075 else
00076 {
00077 center = _min + (_max - _min) * .5f;
00078 }
00079 }
00080
00082
00083 Real32 BoxVolume::getScalarVolume() const
00084 {
00085 return (isEmpty() == true) ? 0.0f : (_max[0] - _min[0]) *
00086 (_max[1] - _min[1]) *
00087 (_max[2] - _min[2]);
00088 }
00089
00090
00091 void BoxVolume::getBounds(Pnt3f &min, Pnt3f &max) const
00092 {
00093 min = _min;
00094 max = _max;
00095 }
00096
00097
00099 void BoxVolume::setBoundsByCenterAndSize(const Pnt3f ¢er,
00100 const Vec3f &size)
00101 {
00102 _min.setValues(center.x() - size.x() / 2.0f,
00103 center.y() - size.y() / 2.0f,
00104 center.z() - size.z() / 2.0f);
00105 _max.setValues(center.x() + size.x() / 2.0f,
00106 center.y() + size.y() / 2.0f,
00107 center.z() + size.z() / 2.0f);
00108
00109 Volume::setValid (true);
00110 Volume::setEmpty (size.isZero());
00111 Volume::setInfinite(false);
00112 }
00113
00114
00115
00116
00118
00119 void BoxVolume::extendBy(const Pnt3f &pt)
00120 {
00121 if(isUntouchable() == true)
00122 return;
00123
00124 if(isEmpty() == true)
00125 {
00126 _min[0] = _max[0] = pt[0];
00127 _min[1] = _max[1] = pt[1];
00128 _min[2] = _max[2] = pt[2];
00129
00130 setEmpty(false);
00131
00132 return;
00133 }
00134
00135 if(pt[0] < _min[0])
00136 {
00137 _min[0] = pt[0];
00138 }
00139 else
00140 {
00141 if(pt[0] > _max[0])
00142 _max[0] = pt[0];
00143 }
00144
00145 if(pt[1] < _min[1])
00146 {
00147 _min[1] = pt[1];
00148 }
00149 else
00150 {
00151 if(pt[1] > _max[1])
00152 _max[1] = pt[1];
00153 }
00154
00155 if(pt[2] < _min[2])
00156 {
00157 _min[2] = pt[2];
00158 }
00159 else
00160 {
00161 if(pt[2] > _max[2])
00162 _max[2] = pt[2];
00163 }
00164 }
00165
00166
00167 void BoxVolume::extendBy(const Volume &volume)
00168 {
00169 OSG::extend(*this,volume);
00170 }
00171
00172
00173
00175
00176 bool BoxVolume::intersect(const Pnt3f &pt) const
00177 {
00178 return
00179 (!isEmpty() &&
00180 (_min[0] < pt[0] && _max[0] > pt[0]) &&
00181 (_min[1] < pt[1] && _max[1] > pt[1]) &&
00182 (_min[2] < pt[2] && _max[2] > pt[2]));
00183 }
00184
00187 bool BoxVolume::intersect(const Line &line) const
00188 {
00189 Real32 enter;
00190 Real32 exit;
00191
00192 return line.intersect(*this, enter, exit);
00193 }
00194
00195
00196
00199 bool BoxVolume::intersect(const Line &line, Real32 &min, Real32 &max) const
00200 {
00201 return line.intersect(*this, min, max);
00202 }
00203
00204
00205 bool BoxVolume::intersect(const Volume &volume) const
00206 {
00207 return OSG::intersect(*this, volume);
00208 }
00209
00210
00211 bool BoxVolume::intersect(const BoxVolume &volume) const
00212 {
00213 return OSG::intersect(*this, volume);
00214 }
00215
00216
00217 bool BoxVolume::isOnSurface (const Pnt3f &point) const
00218 {
00219 if(((osgabs(point[0] - _min[0]) < Eps ||
00220 osgabs(point[0] - _max[0]) < Eps ) &&
00221 (point[1] >= _min[1] && point[1] <= _max[1] &&
00222 point[2] >= _min[2] && point[2] <= _max[2] ) ) ||
00223
00224 ((osgabs(point[1] - _min[1]) < Eps ||
00225 osgabs(point[1] - _max[1]) < Eps ) &&
00226 (point[0] >= _min[0] && point[0] <= _max[1] &&
00227 point[2] >= _min[2] && point[2] <= _max[2] ) ) ||
00228
00229 ((osgabs(point[2] - _min[2]) < Eps ||
00230 osgabs(point[2] - _max[2]) < Eps ) &&
00231 (point[1] >= _min[1] && point[1] <= _max[1] &&
00232 point[0] >= _min[0] && point[0] <= _max[0] ) ) )
00233 {
00234 return true;
00235 }
00236 else
00237 {
00238 return false;
00239 }
00240 }
00241
00242
00244
00245 void BoxVolume::transform(const Matrix &m)
00246 {
00247 Real32 xmin;
00248 Real32 ymin;
00249 Real32 zmin;
00250 Real32 xmax;
00251 Real32 ymax;
00252 Real32 zmax;
00253 Real32 a;
00254 Real32 b;
00255
00256 if(isEmpty() == true)
00257 return;
00258
00259 xmin = xmax = m[3][0];
00260 ymin = ymax = m[3][1];
00261 zmin = zmax = m[3][2];
00262
00263
00264
00265
00266
00267 a = _max[0] * m[0][0];
00268 b = _min[0] * m[0][0];
00269
00270 if(a >= b)
00271 {
00272 xmax += a;
00273 xmin += b;
00274 }
00275 else
00276 {
00277 xmax += b;
00278 xmin += a;
00279 }
00280
00281 a = _max[1] * m[1][0];
00282 b = _min[1] * m[1][0];
00283
00284 if(a >= b)
00285 {
00286 xmax += a;
00287 xmin += b;
00288 }
00289 else
00290 {
00291 xmax += b;
00292 xmin += a;
00293 }
00294
00295 a = _max[2] * m[2][0];
00296 b = _min[2] * m[2][0];
00297
00298 if(a >= b)
00299 {
00300 xmax += a;
00301 xmin += b;
00302 }
00303 else
00304 {
00305 xmax += b;
00306 xmin += a;
00307 }
00308
00309
00310
00311
00312
00313 a = _max[0] * m[0][1];
00314 b = _min[0] * m[0][1];
00315
00316 if(a >= b)
00317 {
00318 ymax += a;
00319 ymin += b;
00320 }
00321 else
00322 {
00323 ymax += b;
00324 ymin += a;
00325 }
00326
00327 a = _max[1] * m[1][1];
00328 b = _min[1] * m[1][1];
00329
00330 if(a >= b)
00331 {
00332 ymax += a;
00333 ymin += b;
00334 }
00335 else
00336 {
00337 ymax += b;
00338 ymin += a;
00339 }
00340
00341 a = _max[2] * m[2][1];
00342 b = _min[2] * m[2][1];
00343
00344 if(a >= b)
00345 {
00346 ymax += a;
00347 ymin += b;
00348 }
00349 else
00350 {
00351 ymax += b;
00352 ymin += a;
00353 }
00354
00355
00356
00357
00358
00359 a = _max[0] * m[0][2];
00360 b = _min[0] * m[0][2];
00361
00362 if(a >= b)
00363 {
00364 zmax += a;
00365 zmin += b;
00366 }
00367 else
00368 {
00369 zmax += b;
00370 zmin += a;
00371 }
00372
00373 a = _max[1] * m[1][2];
00374 b = _min[1] * m[1][2];
00375
00376 if(a >= b)
00377 {
00378 zmax += a;
00379 zmin += b;
00380 }
00381 else
00382 {
00383 zmax += b;
00384 zmin += a;
00385 }
00386
00387 a = _max[2] * m[2][2];
00388 b = _min[2] * m[2][2];
00389
00390 if(a >= b)
00391 {
00392 zmax += a;
00393 zmin += b;
00394 }
00395 else
00396 {
00397 zmax += b;
00398 zmin += a;
00399 }
00400
00401 _min.setValues(xmin, ymin, zmin);
00402 _max.setValues(xmax, ymax, zmax);
00403 }
00404
00406
00407 const BoxVolume &BoxVolume::operator =(const BoxVolume &b1)
00408 {
00409 if(&b1 == this)
00410 return *this;
00411
00412 _min = b1._min;
00413 _max = b1._max;
00414 _state = b1._state;
00415
00416 return *this;
00417 }
00418
00420
00421 void BoxVolume::dump( UInt32 OSG_CHECK_ARG(uiIndent),
00422 const BitVector OSG_CHECK_ARG(bvFlags )) const
00423 {
00424 PLOG << "Box(" << _min << "|" << _max << ")";
00425 }
00426
00427
00428 OSG_BEGIN_NAMESPACE
00429
00431
00432 bool operator ==(const BoxVolume &b1, const BoxVolume &b2)
00433 {
00434 return ((b1._min[0] == b2._min[0]) &&
00435 (b1._min[1] == b2._min[1]) &&
00436 (b1._min[2] == b2._min[2]) &&
00437 (b1._max[0] == b2._max[0]) &&
00438 (b1._max[1] == b2._max[1]) &&
00439 (b2._max[2] == b2._max[2]) );
00440 }
00441
00442
00443 OSG_END_NAMESPACE