OSGSphereVolume.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042
00043 #include "OSGConfig.h"
00044
00045 #include <cassert>
00046
00047 #include "OSGSphereVolume.h"
00048
00049 #include "OSGQuaternion.h"
00050 #include "OSGLine.h"
00051 #include "OSGBoxVolume.h"
00052 #include "OSGLog.h"
00053 #include "OSGMatrix.h"
00054
00055 OSG_BEGIN_NAMESPACE
00056
00057 #if 0
00058 
00059 void SphereVolume::circumscribe(const BoxVolume &box)
00060 {
00061     float radius = 0.5 * (box.getMax() - box.getMin()).length();
00062     Vec3f center;
00063
00064     box.getCenter(center);
00065
00066     setValue(center, radius);
00067 }
00068 #endif
00069 
00072 void SphereVolume::getCenter(Pnt3r &center) const
00073 {
00074     center = _center;
00075 }
00076
00077
00078 Real SphereVolume::getScalarVolume (void) const
00079 {
00080     return isEmpty() ? 0.0f : (4.f / 3.f * Pi * _radius * _radius * _radius);
00081 }
00082
00083
00084 void SphereVolume::getBounds(Pnt3r &min, Pnt3r &max) const
00085 {
00086     min.setValues(_center[0] - _radius,
00087                   _center[1] - _radius,
00088                   _center[2] - _radius);
00089     max.setValues(_center[0] + _radius,
00090                   _center[1] + _radius,
00091                   _center[2] + _radius);
00092 }
00093
00094
00095 void SphereVolume::extendBy(const Pnt3r &pt)
00096 {
00097     if(isUntouchable() == true)
00098         return;
00099
00100     if(isEmpty() == true)
00101     {
00102         _center = pt;
00103         _radius = 0.f;
00104
00105         setEmpty(false);
00106     }
00107     else
00108     {
00109         Real d = (_center - pt).length();
00110
00111         if(d > _radius)
00112             _radius = d;
00113     }
00114 }
00115
00116
00117 void SphereVolume::extendBy(const Volume &volume)
00118 {
00119     OSG::extend(*this, volume);
00120 }
00121
00122 /*------------------------- intersection ------------------------------*/
00123
00126 bool SphereVolume::intersect(const Pnt3r &point) const
00127 {
00128     Real d = (_center - point).length();
00129
00130     if(d <= _radius)
00131         return true;
00132     else
00133         return false;
00134 }
00135
00138 bool SphereVolume::intersect(const Line &line) const
00139 {
00140     return line.intersect(*this);
00141 }
00142
00145 bool SphereVolume::intersect(const Line &line,
00146                                    Real &enter,
00147                                    Real &exit ) const
00148 {
00149     return line.intersect(*this, enter, exit);
00150 }
00151
00152
00153 bool SphereVolume::intersect(const Volume &volume) const
00154 {
00155     return OSG::intersect(*this, volume);
00156 }
00157
00158
00159 bool SphereVolume::isOnSurface (const Pnt3r &point) const
00160 {
00161     if(osgAbs((point - _center).length() - _radius) < Eps)
00162         return true;
00163     else
00164         return false;
00165 }
00166
00167
00168 /*-------------------------- transformation -------------------------------*/
00169
00170 void SphereVolume::transform(const Matrixr &mat)
00171 {
00172     // assume uniform scaling, otherways we get an ellipsoid
00173     Pnt3r hull(_center);
00174
00175     hull += Vec3r(0.f, _radius, 0.f);
00176
00177     mat.mult(_center, _center);
00178     mat.mult(hull,    hull   );
00179
00180     _radius = (hull - _center).length();
00181
00182 /*
00183     Vec3f translation, scaleFactor;
00184     Quaternion rotation, scaleOrientation;
00185     
00186     mat.mult(_center, _center);
00187     mat.getTransform(translation, rotation, scaleFactor, scaleOrientation);
00188     _radius *= scaleFactor[0];
00189 */
00190 }
00191
00192 /*---------------------------------------------------------------------------*/
00193 /* Operators                                                                 */
00194
00195 SphereVolume &SphereVolume::operator =(const SphereVolume &rhs)
00196 {
00197     if(this == &rhs)
00198         return *this;
00199
00200     _center = rhs._center;
00201     _radius = rhs._radius;
00202
00203     return *this;
00204 }
00205
00206 bool SphereVolume::operator ==(const SphereVolume &rhs) const
00207 {
00208     return (_center == rhs._center) && (_radius == rhs._radius);
00209 }
00210
00212 void SphereVolume::dump(      UInt32    OSG_CHECK_ARG(uiIndent),
00213                         const BitVector OSG_CHECK_ARG(bvFlags)) const
00214 {
00215     print(PLOG);
00216 }
00217
00218 void SphereVolume::print(std::ostream &os) const
00219 {
00220     os << "Sphere (" << _center << "|" << _radius << ") ";
00221     printState(os);
00222 }
00223
00224 OSG_END_NAMESPACE