Commit fe51ec0e authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[ADD #428] Now minDistance may throw an exception when used with invalid boxes...

[ADD #428] Now minDistance may throw an exception when used with invalid boxes and also added an output operator
parent c17b24de
......@@ -25,8 +25,11 @@
#ifndef WBOUNDINGBOX_H
#define WBOUNDINGBOX_H
#include <ostream>
#include <osg/BoundingBox>
#include "exceptions/WInvalidBoundingBox.h"
#include "math/WVector3D.h"
/**
......@@ -145,6 +148,7 @@ public:
using osg::BoundingBoxImpl< VT >::contains;
protected:
private:
};
......@@ -202,8 +206,21 @@ inline bool WBoundingBoxImpl< VT >::intersects( const WBoundingBoxImpl< VT > &bb
return osg::BoundingBoxImpl< VT >::intersects( bb );
}
/**
* Anonymous namespace, just to be DRY in minDistance.
*/
namespace
{
/**
* Checks if the two given intervals intersect and computes the distance between them.
*
* \param a0 lower bound of the first interval
* \param a1 upper bound of the first interval
* \param b0 lower bound of the second interval
* \param b1 upper bound if the second interval
*
* \return The distance between those intervals if they don't overlap, zero otherwise
*/
inline double intervalDistance( double a0, double a1, double b0, double b1 )
{
if( a1 < b0 )
......@@ -221,15 +238,11 @@ namespace
template< class VT >
inline typename WBoundingBoxImpl< VT >::value_type WBoundingBoxImpl< VT >::minDistance( const WBoundingBoxImpl< VT > &bb ) const
{
// TODO(math): implement the exception used below!
//
// // test if they are valid
// if( !valid() || !bb.valid() )
// {
// throw WInvalidBoundingBoxes( "One of the both bounding boxes inside minDistance computation is not valid." );
// }
//TODO(math): test if they are axis parallel
// test if they are valid
if( !valid() || !bb.valid() )
{
throw WInvalidBoundingBox( "One of the both bounding boxes inside minDistance computation is not valid." );
}
double dx = intervalDistance( xMin(), xMax(), bb.xMin(), bb.xMax() );
double dy = intervalDistance( yMin(), yMax(), bb.yMin(), bb.yMax() );
......@@ -241,6 +254,23 @@ inline typename WBoundingBoxImpl< VT >::value_type WBoundingBoxImpl< VT >::minDi
return std::sqrt( dx * dx + dy * dy + dz * dz );
}
/**
* Output operator for the WBoundingBoxImpl class.
*
* \param out Output stream operator
* \param bb The box which should be streamed out
*
* \return reference to the output stream
*/
template< class VT >
inline std::ostream& operator<<( std::ostream& out, const WBoundingBoxImpl< VT >& bb )
{
out << std::scientific << std::setprecision( 16 );
out << "AABB( min: " << bb.xMin() << ", " << bb.yMin() << ", " << bb.zMin();
out << " max: " << bb.xMax() << ", " << bb.yMax() << ", " << bb.zMax() << " )";
return out;
}
typedef WBoundingBoxImpl< wmath::WVector3D > WBoundingBox;
#endif // WBOUNDINGBOX_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WInvalidBoundingBox.h"
WInvalidBoundingBox::WInvalidBoundingBox( const std::string& msg )
: WException( msg )
{
// init members
}
WInvalidBoundingBox::~WInvalidBoundingBox() throw()
{
// clean up
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WINVALIDBOUNDINDBOX_H
#define WINVALIDBOUNDINDBOX_H
#include <string>
#include "../WException.h"
#include "../WExportCommon.h"
/**
* Indicates that a bounding box is not valid, meaning its valid() member function delivers false.
*/
class OWCOMMON_EXPORT WInvalidBoundingBox : public WException
{
public:
/**
* Default constructor.
* \param msg the exception message.
*/
explicit WInvalidBoundingBox( const std::string& msg = "A bounding box is not valid" );
/**
* Destructor.
*/
virtual ~WInvalidBoundingBox() throw();
protected:
private:
};
#endif // WINVALIDBOUNDINDBOX_H
......@@ -67,6 +67,7 @@ public:
}
/**
* The distance should not depend on the order in which the boxes are given.
*/
void testCommutativeIntervalDistance( void )
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment