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

[ADD] enhanced the WLoaderFibers to read points, added endian tools in WIOTools

parent 21160f66
......@@ -3,7 +3,7 @@ ADD_SUBDIRECTORY( test )
ADD_SUBDIRECTORY( nifti )
ADD_SUBDIRECTORY( biosig )
FILE( GLOB DATAHANDLER_LOADERS_SRC "WLoader*.cpp" )
FILE( GLOB DATAHANDLER_LOADERS_SRC "WLoader*.cpp" "*.hpp" )
# Unit tests
IF( 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/>.
//
//---------------------------------------------------------------------------
#ifndef WIOTOOLS_HPP
#define WIOTOOLS_HPP
#include <stdint.h>
#include <cassert>
/**
* Namespaces for several tools we may need while doing IO
*/
namespace wiotools
{
/**
* Checks if you are on a big endian machine or not.
*/
inline bool isBigEndian()
{
union {
uint32_t i;
char c[4];
} some = {0x01020304}; // assigning an 32 bit unsigned integer // NOLINT
return some.c[0] == 1;
}
/**
* Transforms a value of type T into the opposite byte order.
*/
template< class T > T switchByteOrder( const T value )
{
size_t numBytes = sizeof( T );
T result = value;
if( numBytes == 1 )
return result;
assert( numBytes % 2 == 0 && numBytes > 0 );
char *s = reinterpret_cast< char* >( &result );
for( size_t i = 0; i < numBytes / 2; ++i ) {
std::swap( s[i], s[ ( numBytes - 1 ) - i ] );
}
return result;
}
/**
* Transform a whole array of elements (of type T and size of sizeof(T))
* into opposite byte order.
*/
template< class T > void switchByteOrderOfArray( T *array, const size_t arraySize )
{
for( size_t i = 0; i < arraySize; ++i ) {
array[i] = switchByteOrder< T >( array[i] );
}
}
}
#endif // WIOTOOLS_HPP
......@@ -22,6 +22,7 @@
//
//---------------------------------------------------------------------------
#include <stdint.h>
#include <cassert>
#include <fstream>
#include <string>
......@@ -31,6 +32,7 @@
#include <boost/lexical_cast.hpp>
#include "WLoaderFibers.h"
#include "WIOTools.hpp"
#include "../WDataHandler.h"
#include "../../common/WStringUtils.hpp"
#include "../../math/WPosition.h"
......@@ -90,8 +92,6 @@ void WLoaderFibers::readHeader() throw( WDHIOFailure, WDHException )
{
throw WDHIOFailure( "Reading first 4 lines of '" + m_fileName + "': " + e.what() );
}
std::cout << m_fileName << std::endl;
std::cout << m_header << std::endl;
// check if the header may be valid for the .fib format
if( m_header.at(0) != "# vtk DataFile Version 3.0" )
......@@ -126,24 +126,34 @@ void WLoaderFibers::readPoints()
{
throw WDHIOFailure( "Error reading POINTS declaration '" + m_fileName + "': " + e.what() );
}
// TODO(math): fix this since stylecheckers complains about long, and we want
// int64 not W_DT_INT64 so see dataHandler/WDataHandlerEnums.h:
// line 49: W_DT_INT64 = 1024, /* long long (64 bits) */
// namespace su = string_utils;
// long numPoints = 0;
// std::vector< std::string > tokens = su::tokenize( line );
// if( tokens.size() < 3 || su::toLower( tokens.at( 2 ) ) != "float" )
// {
// throw WDHException( "Invalid VTK POINTS declaration: " + line );
// }
// try
// {
// numPoints = boost::lexical_cast< long >( tokens.at( 1 ) );
// }
// catch( const boost::bad_lexical_cast &e )
// {
// throw WDHException( "Invalid number of points: " + tokens.at( 1 ) );
// }
//
// std::vector< wmath::WPosition > points( numPoints );
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" )
{
throw WDHException( "Invalid VTK POINTS declaration: " + line );
}
try
{
numPoints = boost::lexical_cast< size_t >( tokens.at( 1 ) );
}
catch( const boost::bad_lexical_cast &e )
{
throw WDHException( "Invalid number of points: " + tokens.at( 1 ) );
}
m_points.reserve( numPoints );
float *pointData = new float[ 3 * numPoints ];
m_ifs->read( reinterpret_cast< char* >( pointData ), 3 * sizeof( float ) * numPoints );
// all 4 bytes of each float are in wrong order we need to reorder them
wiotools::switchByteOrderOfArray( pointData, 3 * numPoints );
for( size_t i = 0; i < numPoints; ++i )
{
m_points.push_back( wmath::WPosition( pointData[i * 3],
pointData[i * 3 + 1],
pointData[i * 3 + 2] ) );
}
delete[] pointData;
}
......@@ -33,6 +33,7 @@
#include "../WLoader.h"
#include "../exceptions/WDHIOFailure.h"
#include "../../math/WPosition.h"
class WDataHandler;
......@@ -83,6 +84,11 @@ protected:
*/
std::vector< std::string > m_header;
/**
* Point vector for all fibers
*/
std::vector< wmath::WPosition > m_points;
/**
* Pointer to the input file stream reader.
*/
......
//---------------------------------------------------------------------------
//
// 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 WIOTOOLS_TEST_H
#define WIOTOOLS_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WIOTools.hpp"
/**
* Unit test helper functions in the wiotools namespace
*/
class WIOToolsTest : public CxxTest::TestSuite
{
public:
/**
* When switching byte order, the first and last bytes should be swapped
* and the bytes inbetween too.
*/
void testByteOrderSwitching( void )
{
uint32_t x = 1;
TS_ASSERT_EQUALS( x, 1 );
x = wiotools::switchByteOrder( x );
TS_ASSERT_EQUALS( x, 16777216 );
x = wiotools::switchByteOrder( x );
TS_ASSERT_EQUALS( x, 1 );
}
/**
* Since templates should work on multiple types we just use double here.
*/
void testByteOrderSwitchingOnFloats( void )
{
double x = 3.141592653589793238462643383279502884197169399375105820974;
double original = x;
x = wiotools::switchByteOrder( x );
TS_ASSERT_DIFFERS( x, original );
x = wiotools::switchByteOrder( x );
TS_ASSERT_EQUALS( x, original );
}
/**
* On single bytes we should do nothing.
*/
void testByteOrderSwitchingOnSingleBytes( void )
{
char x = 1;
TS_ASSERT_EQUALS( wiotools::switchByteOrder( x ), x );
}
/**
* When switching the byte order of an whole array every element should be
* switched.
*/
void testByteOrderSwitchOnArray( void )
{
uint32_t x[] = { 1, 16777216 };
wiotools::switchByteOrderOfArray( x, 2 );
TS_ASSERT_EQUALS( x[0], 16777216 );
TS_ASSERT_EQUALS( x[1], 1 );
}
};
#endif // WIOTOOLS_TEST_H
......@@ -182,7 +182,22 @@ public:
std::string( "Unexpected end of file: fixtures/Fibers/crippled_header.fib" ) );
}
/**
* After reading the points the point vector of WLoaderFibers should be
* filled with valid numbers.
*/
void testReadPoints( void )
{
WLoaderFibers loader( "fixtures/Fibers/valid_small_example.fib", m_dataHandler );
loader.readHeader();
loader.readPoints();
double delta = 0.0001;
TS_ASSERT_EQUALS( loader.m_points.size(), 1224 );
TS_ASSERT_DELTA( loader.m_points.at( 1223 )[2], 60.4135, delta );
TS_ASSERT_DELTA( loader.m_points.at( 0 )[0], 46.2582, delta );
TS_ASSERT_DELTA( loader.m_points.at( 0 )[1], 36.7184, delta );
TS_ASSERT_DELTA( loader.m_points.at( 0 )[2], 65.7245, delta );
}
private:
/**
......
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