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 #include "OSDLCircleBoundingBox.h"
00028 
00029 
00030 
00031 using namespace Ceylan::Maths::Linear ;
00032 
00033 using namespace OSDL::Engine ;
00034 
00035 using std::string ;
00036 
00037 
00038 
00039 #ifdef OSDL_USES_CONFIG_H
00040 #include <OSDLConfig.h>               
00041 #endif // OSDL_USES_CONFIG_H
00042 
00043 
00044 #if OSDL_DEBUG_BOUNDING_BOX
00045 
00046 using namespace Ceylan::Log ;
00047 #define OSDL_BOX_LOG(message) LogPlug::debug( message ) ;
00048 
00049 #else // OSDL_DEBUG_BOUNDING_BOX
00050 
00051 #define OSDL_BOX_LOG(message)
00052 
00053 #endif // OSDL_DEBUG_BOUNDING_BOX
00054 
00055 
00056 
00057 using Ceylan::Maths::Real ;
00058 
00059 
00060 
00061 CircleBoundingBox::CircleBoundingBox( Locatable2D & father, 
00062         const Bipoint & center, Real radius ) :
00063     BoundingBox2D( father, center ),
00064     _radius( radius )
00065 {
00066 
00067 }
00068 
00069 
00070 
00071 CircleBoundingBox::~CircleBoundingBox() throw()
00072 {
00073 
00074 }
00075 
00076 
00077 
00078 Real CircleBoundingBox::getRadius() const
00079 {
00080 
00081     return _radius ;
00082     
00083 }
00084 
00085 
00086 
00087 void CircleBoundingBox::setRadius( Real newRadius )
00088 {
00089 
00090     _radius = newRadius ;
00091     
00092 }
00093 
00094 
00095 
00096 const string CircleBoundingBox::toString( Ceylan::VerbosityLevels level ) const
00097 {   
00098 
00099     string res = "Circular 2D bounding box, whose center is " 
00100         + getCenter().toString( level )
00101         + " and whose radius is " + Ceylan::toString( _radius ) ;
00102     
00103     if ( level == Ceylan::low )
00104         return res ;
00105     
00106     return res + " From a referential point of view, this is a(n) " 
00107         + Locatable2D::toString( level ) ;  
00108             
00109 }
00110 
00111 
00112 
00113 IntersectionResult CircleBoundingBox::doesIntersectWith( BoundingBox & other ) 
00114 {
00115     
00116     BoundingBox2D & other2D = BoundingBox2D::CheckIs2D( other ) ;
00117     
00118     
00119     
00120     
00121 
00122 
00123 
00124 
00125     CircleBoundingBox * circleBox = dynamic_cast<CircleBoundingBox *>( 
00126         & other2D ) ;
00127     
00128     if ( circleBox == 0 )
00129         throw BoundingBoxException( "CircleBoundingBox::doesIntersectWith: "
00130             "intersection with specified 2D box ("
00131             +other.toString() + ") is not implemented for the moment." ) ;
00132     
00133     return compareWith( * circleBox ) ;
00134 
00135 }
00136 
00137 
00138 
00139 IntersectionResult CircleBoundingBox::compareWith( CircleBoundingBox & other )
00140 {
00141     
00142     
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151             
00152     
00153 
00154 
00155 
00156 
00157 
00158      
00159     OSDL_BOX_LOG( "Comparing " + toString() + " to " + other.toString() ) ;
00160     
00161     Matrix3 & firstMatrix = * dynamic_cast<Matrix3*>( 
00162         & getGlobalReferential() ) ;    
00163         
00164     Bipoint firstCenter( firstMatrix.getElementAt( 2, 0 ),
00165         firstMatrix.getElementAt( 2, 1 ) ) ;    
00166         
00167     OSDL_BOX_LOG( "First matrix: " + firstMatrix.toString() ) ;
00168     
00169     Matrix3 & secondMatrix = * dynamic_cast<Matrix3*>( 
00170         & other.getGlobalReferential() ) ;
00171         
00172     Bipoint secondCenter( secondMatrix.getElementAt( 2, 0 ),
00173         secondMatrix.getElementAt( 2, 1 ) ) ;
00174         
00175     OSDL_BOX_LOG( "Second matrix: " + secondMatrix.toString() ) ;
00176     
00177     
00178 
00179 
00180 
00181 
00182     
00183     Real centerDistance = Bipoint::Distance( firstCenter, secondCenter ) ;
00184     
00185     OSDL_BOX_LOG( "Distance between centers: " 
00186         + Ceylan::toString( centerDistance ) ) ;
00187            
00188     if ( centerDistance >= _radius + other.getRadius() )
00189         return isSeparate ;
00190     
00191     
00192     
00193     
00194 
00195 
00196  
00197     if ( _radius > centerDistance + other.getRadius() )
00198         return contains ;
00199         
00200     if ( other.getRadius() > centerDistance + _radius )
00201         return isContained ;
00202     
00203     
00204     if ( Ceylan::Maths::AreRelativelyEqual( centerDistance, 0.0f )
00205         && Ceylan::Maths::AreRelativelyEqual( _radius, other.getRadius() ) )    
00206         return isEqual ;
00207     
00208     
00209     return intersects ;
00210     
00211 }
00212 
00213 
00214     
00215 CircleBoundingBox & CircleBoundingBox::CheckIsCircle( BoundingBox & box ) 
00216 {
00217 
00218     
00219     
00220     CircleBoundingBox * circleBox = dynamic_cast<CircleBoundingBox *>( & box ) ;
00221     
00222     if ( circleBox == 0 )
00223         throw BoundingBoxException( 
00224             "CircleBoundingBox::CheckIsCircle: specified box ("
00225             + box.toString() + ") was not a circular (two dimensional) box." ) ;
00226     
00227     return * circleBox ;
00228         
00229 }
00230