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

[ADD] Put the data from WLoaderFibers into the DataHandler via a new DataSetFibers data set.

parent 826a2675
//---------------------------------------------------------------------------
//
// 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
......@@ -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,11 +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();
readLines();
data = readLines();
}
catch( WDHException e )
{
......@@ -72,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 )
......@@ -163,8 +185,13 @@ void WLoaderFibers::readPoints()
assert( std::string( "" ) == line );
}
void WLoaderFibers::readLines()
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
{
......@@ -192,7 +219,7 @@ void WLoaderFibers::readLines()
throw WDHException( "Invalid number of lines or size of lines: " + line );
}
m_fibers.reserve( numLines );
result->reserve( numLines );
uint32_t *lineData = new uint32_t[ linesSize ];
m_ifs->read( reinterpret_cast<char*>( lineData ), linesSize * sizeof( uint32_t ) );
......@@ -212,7 +239,7 @@ void WLoaderFibers::readLines()
++pos;
}
++linesSoFar;
m_fibers.push_back( wmath::WFiber( fib ) );
result->push_back( wmath::WFiber( fib ) );
}
delete[] lineData;
......@@ -220,4 +247,6 @@ void WLoaderFibers::readLines()
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
return result;
}
......@@ -83,7 +83,7 @@ protected:
/**
* Read lines from input stream and use the points for constructing fibers.
*/
void readLines();
boost::shared_ptr< std::vector< wmath::WFiber > > readLines();
/**
* First four lines of ASCII text describing this file
......@@ -95,11 +95,6 @@ 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.
*/
......
......@@ -30,6 +30,7 @@
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/shared_ptr.hpp>
#include <cxxtest/TestSuite.h>
#include <cxxtest/ValueTraits.h>
......@@ -232,21 +233,23 @@ public:
*/
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();
loader.readLines();
TS_ASSERT_EQUALS( loader.m_fibers.size(), 1 );
using wmath::WPosition;
using wmath::WFiber;
std::vector< WPosition > pointList;
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, loader.m_fibers.back() );
TS_ASSERT_EQUALS( expected, actual->back() );
}
private:
......
//---------------------------------------------------------------------------
//
// 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
......@@ -22,27 +22,4 @@
//
//---------------------------------------------------------------------------
#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;
}
......@@ -36,6 +36,7 @@ class WLineTest;
namespace wmath
{
// Please notice that many small member functions are inlined for performance issues
/**
* A line is an ordered sequence of WPositions.
*/
......@@ -46,17 +47,51 @@ public:
/**
* Constructs a new line with the given points in the given order
*/
explicit WLine( const std::vector< WPosition > &points );
explicit WLine( const std::vector< WPosition > &points )
: m_points( points )
{
}
/**
* Copy constructor
*/
WLine( const WLine& other ) : m_points( other.m_points )
{
}
/**
* \return true if both lines have a same point vector
*/
bool operator==( const WLine &rhs ) const;
bool operator==( const WLine &rhs ) const
{
return m_points == rhs.m_points;
}
/**
* \return false if both lines have a same point vector
*/
bool operator!=( const WLine &rhs ) const;
bool operator!=( const WLine &rhs ) const
{
return m_points != rhs.m_points;
}
/**
* Get number of points (length) the value consists of.
*/
size_t size()
{
return m_points.size();
}
/**
* \return Const reference to the i'th position. This is const since
* we want an read only access.
*/
const WPosition& operator[]( size_t index ) const
{
assert( index < m_points.size() );
return m_points[index];
}
/**
* Gives a meaningful representation of this object to the given
......
......@@ -40,7 +40,7 @@ namespace wmath
template< typename T > class WValue
{
public:
/*
/**
* Create a WValue with the given number of components.
* The components will be set to zero if T is a type representing numbers.
*/
......@@ -49,7 +49,7 @@ public:
{
}
/*
/**
* Create a WValue as copy of the one given as parameter.
*/
WValue( const WValue& newValue )
......
......@@ -107,5 +107,21 @@ public:
line2.m_points.back()[1] += 0.0000000001;
TS_ASSERT_DIFFERS( line1, line2 );
}
/**
* When accessing an item within 0..length-1 a const reference to the
* WPosition object should be returned.
*/
void testAccessOperatorWithinValidBounds( void )
{
using wmath::WPosition;
std::vector< WPosition > lineData;
lineData.push_back( WPosition( 1., 1., 3.1415 ) );
lineData.push_back( WPosition( 0., 0., .44 ) );
lineData.push_back( WPosition( 1., 1., 1. ) );
wmath::WLine line( lineData );
WPosition expected( 1., 1., 1. );
TS_ASSERT_EQUALS( expected, line[2] );
}
};
#endif // WLINE_TEST_H
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