Commit 660d8a15 authored by schurade's avatar schurade
Browse files

[MERGE] with a91a10e34a7a306811c1f6ce3814d7a0ddaa1a9c

parents 7bbafaed 8e0db2f3
......@@ -50,7 +50,7 @@ int WSegmentationFault::getSignalNumber() throw()
void WSegmentationFault::installSignalHandler() throw()
{
#ifdef __linux__
SignalTranslator<WSegmentationFault> segmentationFaultTranslator;
SignalTranslator< WSegmentationFault > segmentationFaultTranslator;
#endif // __linux__
}
......@@ -22,11 +22,14 @@
//
//---------------------------------------------------------------------------
#ifndef WSTRINGUTILS_H
#define WSTRINGUTILS_H
#ifndef WSTRINGUTILS_HPP
#define WSTRINGUTILS_HPP
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <list>
#include <sstream>
#include <string>
#include <vector>
......@@ -121,29 +124,41 @@ namespace string_utils {
std::vector< std::string > tokenize( const std::string& source,
bool compress = true,
const std::string& t = WHITESPACE );
}
/**
* Writes the given vector to an output stream such as cout, if and only if
* its elements have an output operator defined.
*
* \param os The output stream where the elements are written to
* \param v Vector containing the elements
* \return The output stream again.
*/
template< class T > std::ostream& operator<<( std::ostream& os,
const std::vector< T >& v )
{
std::stringstream result;
result << "[";
// typename, since const_iterator is a so called "dependent name"
typename std::vector< T >::const_iterator citer;
for( citer = v.begin(); citer != v.end(); ++citer )
/**
* Writes every vector to an output stream such as cout, if its elements
* have an output operator defined.
*
* \param os The output stream where the elements are written to
* \param v Vector containing the elements
* \return The output stream again.
*/
template< class T > std::ostream& operator<<( std::ostream& os, const std::vector< T >& v )
{
result << *citer << ", ";
std::stringstream result;
result << "[" << std::scientific << std::setprecision(16);
std::copy( v.begin(), v.end(), std::ostream_iterator< T >(result, ", ") );
os << rTrim( result.str(), ", " ) << "]";
return os;
}
os << string_utils::rTrim( result.str(), ", " ) << "]";
return os;
}
/**
* Writes every list to an output stream such as cout, if its elements have
* an output operator defined.
*
* \param os The output stream where the elements are written to
* \param l List containing the elements
* \return The output stream again.
*/
template< class T > std::ostream& operator<<( std::ostream& os, const std::list< T >& l )
{
std::stringstream result;
result << "<" << std::scientific;
std::copy( l.begin(), l.end(), std::ostream_iterator< T >(result, ", ") );
os << rTrim( result.str(), ", " ) << ">";
return os;
}
} // end of namespace
#endif // WSTRINGUTILS_H
#endif // WSTRINGUTILS_HPP
......@@ -48,13 +48,14 @@ public:
{
std::stringstream ss;
std::vector< double > pansen;
using string_utils::operator<<;
ss << pansen;
TS_ASSERT_EQUALS( ss.str(), "[]" );
ss.str( "" );
ss.clear();
pansen = std::vector< double >( 4, 3.1415 );
pansen[2] = 1.414;
std::string expected( "[3.1415, 3.1415, 1.414, 3.1415]" );
pansen = std::vector< double >( 2, 3.1415 );
pansen[1] = 1.414;
std::string expected( "[3.1415000000000002e+00, 1.4139999999999999e+00]" );
ss << pansen;
TS_ASSERT_EQUALS( ss.str(), expected );
}
......
//---------------------------------------------------------------------------
//
// 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 "WDataSetFibers.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/>.
//
//---------------------------------------------------------------------------
#ifndef WDATASETFIBERS_H
#define WDATASETFIBERS_H
#include <vector>
#include <boost/shared_ptr.hpp>
#include "WDataSet.h"
#include "../math/WFiber.h"
/**
* Represents a simple set of WFibers.
*/
class WDataSetFibers : public WDataSet
{
public:
/**
* Constructs a new set of WFibers
*/
explicit WDataSetFibers( boost::shared_ptr< std::vector< wmath::WFiber > > fibs ) : m_fibers( fibs )
{
}
/**
* Get number of fibers in this data set.
*/
size_t size()
{
return m_fibers->size();
}
/**
* \return The i'th fiber.
*/
const wmath::WFiber& operator[]( const size_t index ) const
{
assert( index < m_fibers->size() );
return (*m_fibers)[index];
}
protected:
private:
boost::shared_ptr< std::vector< wmath::WFiber > > m_fibers;
};
#endif // WDATASETFIBERS_H
......@@ -42,7 +42,7 @@ namespace wiotools
union {
uint32_t i;
char c[4];
} some = {0x01020304}; // assigning an 32 bit unsigned integer // NOLINT
} some = {0x01020304}; // assigning an 32 bit unsigned integer
return some.c[0] == 1;
}
......
......@@ -34,6 +34,8 @@
#include "WLoaderFibers.h"
#include "WIOTools.hpp"
#include "../WDataHandler.h"
#include "../WDataSetFibers.h"
#include "../WSubject.h"
#include "../../common/WStringUtils.hpp"
#include "../../math/WPosition.h"
......@@ -58,10 +60,16 @@ WLoaderFibers::~WLoaderFibers() throw()
void WLoaderFibers::operator()() throw()
{
using boost::shared_ptr;
using std::vector;
using wmath::WFiber;
shared_ptr< vector< WFiber > > data;
try
{
readHeader();
readPoints();
data = readLines();
}
catch( WDHException e )
{
......@@ -71,6 +79,21 @@ void WLoaderFibers::operator()() throw()
// could be thousands of them
std::cerr << "Error :: DataHandler :: Abort loading VTK file due to: " << e.what() << std::endl;
}
assert( !data->empty() && "loaded empty vector of fibers" );
shared_ptr< WDataSetFibers > fibers = shared_ptr< WDataSetFibers >( new WDataSetFibers( data ) );
fibers->setFileName( m_fileName );
boost::shared_ptr< WSubject > subject;
if( m_dataHandler->getNumberOfSubjects() == 0 )
{
subject = boost::shared_ptr< WSubject >( new WSubject );
m_dataHandler->addSubject( subject );
}
else
{
subject = m_dataHandler->getSubject( 0 );
}
subject->addDataSet( fibers );
}
void WLoaderFibers::readHeader() throw( WDHIOFailure, WDHException )
......@@ -129,7 +152,7 @@ void WLoaderFibers::readPoints()
namespace su = string_utils;
size_t numPoints = 0;
std::vector< std::string > tokens = su::tokenize( line );
if( tokens.size() < 3 || su::toLower( tokens.at( 2 ) ) != "float" )
if( tokens.size() != 3 || su::toLower( tokens.at( 2 ) ) != "float" )
{
throw WDHException( "Invalid VTK POINTS declaration: " + line );
}
......@@ -156,4 +179,74 @@ void WLoaderFibers::readPoints()
pointData[i * 3 + 2] ) );
}
delete[] pointData;
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
}
boost::shared_ptr< std::vector< wmath::WFiber > > WLoaderFibers::readLines()
{
using wmath::WFiber;
using std::vector;
using boost::shared_ptr;
shared_ptr< vector< WFiber > > result = shared_ptr< vector< WFiber > >( new vector< WFiber > );
std::string line;
try
{
std::getline( *m_ifs, line );
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( "Error reading LINES declaration '" + m_fileName + "': " + e.what() );
}
namespace su = string_utils;
size_t numLines = 0;
size_t linesSize = 0;
std::vector< std::string > tokens = su::tokenize( line );
if( tokens.size() != 3 || su::toUpper( tokens.at( 0 ) ) != "LINES" )
{
throw WDHException( "Invalid VTK LINES declaration: " + line );
}
try
{
numLines = boost::lexical_cast< size_t >( tokens.at( 1 ) );
linesSize = boost::lexical_cast< size_t >( tokens.at( 2 ) );
}
catch( const boost::bad_lexical_cast &e )
{
throw WDHException( "Invalid number of lines or size of lines: " + line );
}
result->reserve( numLines );
uint32_t *lineData = new uint32_t[ linesSize ];
m_ifs->read( reinterpret_cast<char*>( lineData ), linesSize * sizeof( uint32_t ) );
wiotools::switchByteOrderOfArray( lineData, linesSize );
// now convert lines with point numbers to real fibers
size_t linesSoFar = 0;
size_t pos = 0;
while( linesSoFar < numLines )
{
std::vector< wmath::WPosition > fib;
size_t fiberLength = lineData[pos];
++pos;
for( size_t i = 0; i < fiberLength; ++i )
{
fib.push_back( m_points[ lineData[pos] ] );
++pos;
}
++linesSoFar;
result->push_back( wmath::WFiber( fib ) );
}
delete[] lineData;
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
return result;
}
......@@ -34,6 +34,7 @@
#include "../WLoader.h"
#include "../exceptions/WDHIOFailure.h"
#include "../../math/WPosition.h"
#include "../../math/WFiber.h"
class WDataHandler;
......@@ -75,10 +76,15 @@ protected:
void readHeader() throw( WDHIOFailure, WDHException );
/**
* Read points from file while starting at the given position.
* Read points from input stream.
*/
void readPoints();
/**
* Read lines from input stream and use the points for constructing fibers.
*/
boost::shared_ptr< std::vector< wmath::WFiber > > readLines();
/**
* First four lines of ASCII text describing this file
*/
......
......@@ -25,6 +25,7 @@
#ifndef WLOADEREEGASCII_TEST_H
#define WLOADEREEGASCII_TEST_H
#include <ctime>
#include <string>
#include <cxxtest/TestSuite.h>
#include <boost/thread.hpp>
......@@ -51,7 +52,16 @@ public:
WLoaderEEGASCII eegASCIILoader( fileName, dataHandler );
boost::thread loaderThread( eegASCIILoader );
sleep( 3 );
std::clock_t startTime;
startTime = std::clock();
std::clock_t elapsedTime = 0;
while( dataHandler->getNumberOfSubjects() == 0 && ( elapsedTime / static_cast< double >( CLOCKS_PER_SEC ) ) < 4 )
{
elapsedTime = std::clock() - startTime;
}
// TODO(wiebel): we need to change this because loading, in the end,
// should not always increase the number of subjects.
TS_ASSERT_EQUALS( dataHandler->getNumberOfSubjects(), 1 );
......
......@@ -30,6 +30,7 @@
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/shared_ptr.hpp>
#include <cxxtest/TestSuite.h>
#include <cxxtest/ValueTraits.h>
......@@ -40,23 +41,49 @@
#ifdef CXXTEST_RUNNING
namespace CxxTest
{
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits< std::streampos >
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits< std::streampos >
{
private:
char _s[256];
public:
explicit ValueTraits( const std::streampos &pos )
{
std::stringstream ss;
ss << pos;
snprintf( _s, ss.str().size(), "%s", ss.str().c_str() );
}
const char *asString() const
{
return _s;
}
};
}
#endif // CXXTEST_RUNNING
// New value trait for wmath::WFiber
#ifdef CXXTEST_RUNNING
namespace CxxTest
{
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits< wmath::WFiber >
{
private:
char _s[10240];
public:
explicit ValueTraits( const wmath::WFiber &fib )
{
char _s[256];
public: // NOLINT
explicit ValueTraits( const std::streampos &pos )
{
std::stringstream ss;
ss << pos;
snprintf( _s, ss.str().size(), "%s", ss.str().c_str() );
}
const char *asString() const
{
return _s;
}
};
std::stringstream ss;
ss << fib;
snprintf( _s, ss.str().size(), "%s", ss.str().c_str() );
}
const char *asString() const
{
return _s;
}
};
}
#endif // CXXTEST_RUNNING
......@@ -199,6 +226,32 @@ public:
TS_ASSERT_DELTA( loader.m_points.at( 0 )[2], 65.7245, delta );
}
/**
* Every line is stored in fib files with
* 1. Its number of points (length)
* 2. All references to the points its using
*/
void testReadAllLines( void )
{
using boost::shared_ptr;
using std::vector;
using wmath::WFiber;
using wmath::WPosition;
WLoaderFibers loader( "fixtures/Fibers/small_example_one_fiber.fib", m_dataHandler );
loader.readHeader();
loader.readPoints();
shared_ptr< vector< WFiber > > actual = loader.readLines();
TS_ASSERT_EQUALS( actual->size(), 1 );
vector< WPosition > pointList;
// CAUTION: we use here floats since the positions are stored with
// floats as well. If we would use doubles here, the both vectors are
// different then.
pointList.push_back( WPosition( 1.2f, 3.4f, 5.6f ) );
pointList.push_back( WPosition( 7.8f, 9.0f, -1.2f ) );
WFiber expected( pointList );
TS_ASSERT_EQUALS( expected, actual->back() );
}
private:
/**
* Dummy DataHandler instance
......
......@@ -30,13 +30,13 @@
#include "../WLoaderNIfTI.h"
/**
* TODO(math): Document this!
* TODO(schurade): Document this!
*/
class WLoaderNIfTITest : public CxxTest::TestSuite
{
public:
/**
* TODO(math): Document this!
* TODO(schurade): Document this!
*/
void testSomething( void )
{
......
//---------------------------------------------------------------------------
//
// 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 WDATASETFIBERS_TEST_H
#define WDATASETFIBERS_TEST_H
#include <vector>
#include <boost/shared_ptr.hpp>
#include <cxxtest/TestSuite.h>
#include "../WDataSetFibers.h"
#include "../../math/WPosition.h"
/**
* Unit tests our fiber dataset class
*/
class WDataSetFibersTest : public CxxTest::TestSuite
{
public:
/**
* Generates the test evironment
*/
void setUp( void )
{
using wmath::WPosition;
using wmath::WFiber;
using std::vector;
m_somefibs = boost::shared_ptr< vector< WFiber > >( new vector< WFiber > );
vector< WPosition > points;
points.push_back( WPosition( 0., 0., 1. ) );
m_somefibs->push_back( WFiber( points ) );
points.push_back( WPosition( 0., 0., 2. ) );
m_somefibs->push_back( WFiber( points ) );
points.push_back( WPosition( 0., 0., 3. ) );
m_somefibs->push_back( WFiber( points ) );
}
/**
* With a vector of WFiber a valid DataSetFibers should be possible
*/
void testBasicAccessAndCreation( void )
{
using wmath::WPosition;
using wmath::WFiber;
std::vector< WPosition > points;
points.push_back( WPosition( 0., 0., 1. ) );
points.push_back( WPosition( 0., 0., 2. ) );
points.push_back( WPosition( 0., 0., 3. ) );
WFiber expected( points );
WDataSetFibers d( m_somefibs );
TS_ASSERT_EQUALS( d[2], expected );
}
private:
boost::shared_ptr< std::vector< wmath::WFiber > > m_somefibs;
};
#endif // WDATASETFIBERS_TEST_H
......@@ -25,10 +25,12 @@
#ifndef WLOADERMANAGER_TEST_H
#define WLOADERMANAGER_TEST_H
#include <ctime>
#include <string>