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

[ADD][CHANGE]

 * all string helper functions (esp. the <<-operators) are now in string_utils since we need them here for direct access in template friend functions for templates
 * WLoaderFibers now loads fibers out of the fib file but no scalars at the moment. They are stored at the moment directly in the WLoaderFibers object which will change very soon
 * WLine and WFiber are new classes to play with in math
 * output operator for WValue (printing the components array)
 * brainlinter: shouldn't choke on missing whitespace aroung "<<" see ticket: #83
 * brainlinter: should now accept "//" comments after closing braces
parent 55a9251a
......@@ -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 );
}
......
......@@ -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;
}
......
......@@ -62,6 +62,7 @@ void WLoaderFibers::operator()() throw()
{
readHeader();
readPoints();
readLines();
}
catch( WDHException e )
{
......@@ -129,7 +130,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 +157,67 @@ void WLoaderFibers::readPoints()
pointData[i * 3 + 2] ) );
}
delete[] pointData;
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
}
void WLoaderFibers::readLines()
{
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 );
}
m_fibers.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;
m_fibers.push_back( wmath::WFiber( fib ) );
}
delete[] lineData;
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
}
......@@ -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.
*/
void readLines();
/**
* First four lines of ASCII text describing this file
*/
......@@ -89,6 +95,11 @@ protected:
*/
std::vector< wmath::WPosition > m_points;
/**
* All Fibers build up with their positions.
*/
std::vector< wmath::WFiber > m_fibers;
/**
* Pointer to the input file stream reader.
*/
......
......@@ -40,23 +40,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 +225,30 @@ 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 )
{
WLoaderFibers loader( "fixtures/Fibers/small_example_one_fiber.fib", m_dataHandler );
loader.readHeader();
loader.readPoints();
loader.readLines();
TS_ASSERT_EQUALS( loader.m_fibers.size(), 1 );
using wmath::WPosition;
using wmath::WFiber;
std::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, loader.m_fibers.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 )
{
......
......@@ -137,7 +137,7 @@ std::string WShader::getIncludeFileName( std::string line )
return 0;
}
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
boost::char_separator<char> sep( "\"" );
tokenizer tok( line, sep );
......
......@@ -5,6 +5,6 @@ ADD_LIBRARY( math SHARED ${MATH_SRC} )
# Unit tests
IF( CXXTEST_FOUND )
CXXTEST_ADD_TESTS_FROM_LIST( "${MATH_SRC}"
"math"
"math;common"
)
ENDIF( CXXTEST_FOUND )
//---------------------------------------------------------------------------
//
// 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 <vector>
#include "WFiber.h"
namespace wmath
{
WFiber::WFiber( const std::vector< WPosition > &points )
: WLine( points )
{
}
}
//---------------------------------------------------------------------------
//
// 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 WFIBER_H
#define WFIBER_H
#include <vector>
#include "WLine.h"
// we need this to find the WFiberTest class which is not inside wmath namespace
// Additionally I found no way to put the WFiberTest class into wmath and CXXTest
// finds it by it self. Hence this is the only solution that might work
class WFiberTest;
namespace wmath
{
/**
* Represents a neural pathway.
*/
class WFiber : public WLine
{
friend class WFiberTest;
public:
explicit WFiber( const std::vector< WPosition > &points );
protected:
private:
};
}
#endif // WFIBER_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 <iostream>
#include <vector>
#include "WLine.h"
#include "WPosition.h"
#include "../common/WStringUtils.hpp"
using wmath::WLine;
WLine::WLine( const std::vector< WPosition > &points )
: m_points( points )
{
}
bool WLine::operator==( const WLine &rhs ) const
{
return m_points == rhs.m_points;
}
bool WLine::operator!=( const WLine &rhs ) const
{
return m_points != rhs.m_points;
}
//---------------------------------------------------------------------------
//
// 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 WLINE_H
#define WLINE_H
#include <iostream>
#include <vector>
#include "WPosition.h"
#include "../common/WStringUtils.hpp"
// we need this to find the WLineTest class which is not inside wmath namespace
class WLineTest;
namespace wmath
{
/**
* A line is an ordered sequence of WPositions.
*/
class WLine
{
friend class ::WLineTest;
public:
/**
* Constructs a new line with the given points in the given order
*/
explicit WLine( const std::vector< WPosition > &points );
/**
* \return true if both lines have a same point vector
*/
bool operator==( const WLine &rhs ) const;
/**
* \return false if both lines have a same point vector
*/
bool operator!=( const WLine &rhs ) const;
/**
* Gives a meaningful representation of this object to the given
* output stream.
*/
friend std::ostream& operator<<( std::ostream& os, const WLine &rhs )
{
using string_utils::operator<<;
os << rhs.m_points;
return os;
}
protected:
private:
std::vector< WPosition > m_points;
};
}
#endif // WLINE_H
......@@ -25,10 +25,11 @@
#ifndef WVALUE_H
#define WVALUE_H
#include <vector>
#include <cassert>
#include <cmath>
#include <vector>
#include <cassert>
#include "../common/WStringUtils.hpp"
namespace wmath
{
......@@ -241,6 +242,15 @@ public:
return result;
}
/**
* Writes a meaningful representation of that object to the given stream
*/
friend std::ostream& operator<<( std::ostream& os, const WValue< T > &rhs )
{
// Caution: only directly referencing the operator<< template function will work here
return string_utils::operator<<( os, rhs.m_components );
}