Commit 93e92791 authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[ADD] Midpoint of fibers

parent 2a9159c7
......@@ -28,6 +28,7 @@
#include "../common/WLimits.h"
#include "../common/WStringUtils.h"
#include "../common/exceptions/WOutOfBounds.h"
#include "WLine.h"
#include "WPosition.h"
......@@ -100,4 +101,13 @@ void WLine::resample( size_t numPoints )
assert( size() == numPoints && "Resampling of a line has failed! Is your fiber of length 0 or even the new sample rate??" );
}
const wmath::WPosition& WLine::midPoint() const
{
if( empty() )
{
throw WOutOfBounds( "There is no midpoint for an empty line." );
}
return at( ( size() - 1 ) / 2 );
}
} // end of namespace
......@@ -76,6 +76,17 @@ namespace wmath
* \return Sum of all line segment lengths
*/
double pathLength() const;
/**
* Returns the point in the middle of the line. In case of an even sized
* line the mid point is the same as if there were only size()-1 many
* elements present.
*
* \throws WOutOfBounds In case its called on an empty line
*
* \return Const reference to the midpoint element.
*/
const wmath::WPosition& midPoint() const;
};
} // end of namespace
#endif // WLINE_H
......@@ -31,8 +31,10 @@
#include <cxxtest/TestSuite.h>
#include "../../common/WLimits.h"
#include "../../common/exceptions/WOutOfBounds.h"
#include "../WLine.h"
#include "WLineTraits.h"
#include "WVector3DTraits.h"
/**
* Unit tests the WLine class
......@@ -226,6 +228,51 @@ public:
// assert_SAMELINES( expected, line );
// }
/**
* The mid point of a WLine is just the point in the middle of the line.
* For lines with even numbered size the element before that non existing
* midPoint is selected.
*/
void testMidPointOnEvenSize( void )
{
using wmath::WPosition;
wmath::WLine line;
line.push_back( WPosition( 0, 0, 0 ) );
line.push_back( WPosition( 1, 1, 0 ) );
line.push_back( WPosition( 2, 0, 0 ) );
line.push_back( WPosition( 3, 1, 0 ) );
WPosition expected( 1, 1, 0 );
TS_ASSERT_EQUALS( expected, WPosition( line.midPoint() ) );
}
/**
* When a line has uneven numbered size, the mid point is unique.
*/
void testMidPointOnUnevenSize( void )
{
using wmath::WPosition;
wmath::WLine line;
line.push_back( WPosition( 0, 0, 0 ) );
line.push_back( WPosition( 1, 1, 0 ) );
line.push_back( WPosition( 2, 0, 0 ) );
WPosition expected( 1, 1, 0 );
TS_ASSERT_EQUALS( expected, WPosition( line.midPoint() ) );
}
/**
* When calling midPoint on empty lines => there is no point to return
* hence an exception is generated.
*/
void testMidPointOnEmptyLine( void )
{
using wmath::WPosition;
wmath::WLine line;
WPosition expected( 1, 1, 0 );
TS_ASSERT_THROWS_EQUALS( line.midPoint(), WOutOfBounds &e, std::string( e.what() ), "There is no midpoint for an empty line." );
}
private:
/**
* TS_ASSERT_DELTA needs the operator+, operator- and operator< to be implemented especially for WPositions the operator< and operator +
......
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