Commit 164c5b25 authored by schurade's avatar schurade
Browse files

[ADD] missing files for last commit

parent 7bca8f3c
//---------------------------------------------------------------------------
//
// 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 <string>
#include <algorithm>
#include <vector>
#include "WDataSet.h"
#include "WDataSetFibers2.h"
// prototype instance as singleton
boost::shared_ptr< WPrototyped > WDataSetFibers2::m_prototype = boost::shared_ptr< WPrototyped >();
WDataSetFibers2::WDataSetFibers2()
: WDataSet()
{
// default constructor used by the prototype mechanism
}
WDataSetFibers2::WDataSetFibers2( boost::shared_ptr< std::vector< float > >vertices,
boost::shared_ptr< std::vector< unsigned int > > lineStartIndexes,
boost::shared_ptr< std::vector< unsigned int > > lineLengths,
boost::shared_ptr< std::vector< unsigned int > > verticesReverse )
: WDataSet(),
m_vertices( vertices ),
m_lineStartIndexes( lineStartIndexes ),
m_lineLengths( lineLengths ),
m_verticesReverse( verticesReverse )
{
for ( int i = 0; i < m_vertices->size(); ++i )
{
m_vertices->at( i ) = 160 - m_vertices->at( i );
++i;
m_vertices->at( i ) = 200 - m_vertices->at( i );
++i;
//m_pointArray[i] = m_dh->frames - m_pointArray[i];
}
}
void WDataSetFibers2::sortDescLength()
{
//std::sort( m_fibers->begin(), m_fibers->end(), wmath::hasGreaterLengthThen );
}
bool WDataSetFibers2::isTexture() const
{
return false;
}
size_t WDataSetFibers2::size() const
{
return m_lineStartIndexes->size();
//return m_fibers->size();
}
//const wmath::WFiber& WDataSetFibers2::operator[]( const size_t index ) const
//{
// assert( index < m_fibers->size() );
// return (*m_fibers)[index];
//}
const std::string WDataSetFibers2::getName() const
{
return "WDataSetFibers";
}
const std::string WDataSetFibers2::getDescription() const
{
return "Contains tracked fiber data.";
}
boost::shared_ptr< WPrototyped > WDataSetFibers2::getPrototype()
{
if ( !m_prototype )
{
m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetFibers2() );
}
return m_prototype;
}
boost::shared_ptr< std::vector< float > > WDataSetFibers2::getVertices() const
{
return m_vertices;
}
boost::shared_ptr< std::vector< unsigned int > > WDataSetFibers2::getLineStartIndexes() const
{
return m_lineStartIndexes;
}
boost::shared_ptr< std::vector< unsigned int > > WDataSetFibers2::getLineLengths() const
{
return m_lineLengths;
}
boost::shared_ptr< std::vector< unsigned int > > WDataSetFibers2::getVerticesReverse() const
{
return m_verticesReverse;
}
wmath::WPosition WDataSetFibers2::getPosition( unsigned int fiber, unsigned int vertex ) const
{
unsigned int index = m_lineStartIndexes->at( fiber ) * 3;
index += vertex * 3;
return wmath::WPosition( m_vertices->at( index ), m_vertices->at( index + 1 ), m_vertices->at( index + 2 ) );
}
//---------------------------------------------------------------------------
//
// 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 WDATASETFIBERS2_H
#define WDATASETFIBERS2_H
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "../math/WPosition.h"
#include "WDataSet.h"
/**
* Represents a simple set of WFibers.
*/
class WDataSetFibers2 : public WDataSet
{
public:
/**
* Constructs a new set of fibers, usage of WFiber here is for backward compatibility and should be removed
*
* \param fibs Fiber vector to store in this data set
* \param vertices
* \param lineStartIndexes
* \param lineLengths
* \param verticesReverse
*/
WDataSetFibers2( boost::shared_ptr< std::vector< float > >vertices,
boost::shared_ptr< std::vector< unsigned int > > lineStartIndexes,
boost::shared_ptr< std::vector< unsigned int > > lineLengths,
boost::shared_ptr< std::vector< unsigned int > > verticesReverse );
/**
* Constructs a new set of WFibers. The constructed instance is not usable.
*/
WDataSetFibers2();
/**
* Get number of fibers in this data set.
*/
size_t size() const;
/**
* \param index The index number of the fiber which should be returned
* \return The i'th fiber.
*/
//const wmath::WFiber& operator[]( const size_t index ) const;
/**
* Sort fibers descending on their length.
*/
void sortDescLength();
/**
* Determines whether this dataset can be used as a texture.
*
* \return true if usable as texture.
*/
virtual bool isTexture() const;
/**
* Gets the name of this prototype.
*
* \return the name.
*/
virtual const std::string getName() const;
/**
* Gets the description for this prototype.
*
* \return the description
*/
virtual const std::string getDescription() const;
/**
* Returns a prototype instantiated with the true type of the deriving class.
*
* \return the prototype.
*/
static boost::shared_ptr< WPrototyped > getPrototype();
/**
* Getter for m_vertices
*/
boost::shared_ptr< std::vector< float > > getVertices() const;
/**
* Getter
*/
boost::shared_ptr< std::vector< unsigned int > > getLineStartIndexes() const;
/**
* Getter
*/
boost::shared_ptr< std::vector< unsigned int > > getLineLengths() const;
/**
* Getter
*/
boost::shared_ptr< std::vector< unsigned int > > getVerticesReverse() const;
/**
*
*/
wmath::WPosition getPosition( unsigned int fiber, unsigned int vertex ) const;
protected:
/**
* The prototype as singleton.
*/
static boost::shared_ptr< WPrototyped > m_prototype;
private:
/**
* Point vector for all fibers that is actually usable for what we want to do
*/
boost::shared_ptr< std::vector< float > > m_vertices;
/**
* Line vector that contains the start index for each line
*/
boost::shared_ptr< std::vector< unsigned int > > m_lineStartIndexes;
/**
* Line vector that contains the number of vertices for each line
*/
boost::shared_ptr< std::vector< unsigned int > > m_lineLengths;
/**
* Reverse lookup table for which point belongs to which fiber
*/
boost::shared_ptr< std::vector< unsigned int > > m_verticesReverse;
};
#endif // WDATASETFIBERS2_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 <stdint.h>
#include <cassert>
#include <fstream>
#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include "../../common/WLogger.h"
#include "../../math/WPosition.h"
#include "../../common/WIOTools.h"
#include "../../common/WStringUtils.h"
#include "../WDataSetFibers2.h"
#include "../WSubject.h"
#include "WLoaderFibers2.h"
WLoaderFibers2::WLoaderFibers2( std::string fname ) throw( WDHIOFailure )
: WLoader( fname )
{
m_ifs = boost::shared_ptr< std::ifstream >( new std::ifstream() );
m_ifs->open( m_fileName.c_str(), std::ifstream::in | std::ifstream::binary );
if( !m_ifs || m_ifs->bad() )
{
throw WDHIOFailure( "Load broken file '" + m_fileName + "'" );
}
}
WLoaderFibers2::~WLoaderFibers2() throw()
{
}
boost::shared_ptr< WDataSet > WLoaderFibers2::load()
{
using boost::shared_ptr;
using std::vector;
try
{
readHeader();
readPoints();
readLines();
}
catch( WDHException e )
{
m_ifs->close();
// TODO(math): we should print the file name also, since knowing that
// the file was malformated, doesn't give you the hint that there
// could be thousands of them
wlog::error( "DataHandler" ) << "Abort loading Fib-VTK file due to: " << e.what();
}
shared_ptr< WDataSetFibers2 > fibers = shared_ptr< WDataSetFibers2 >( new WDataSetFibers2( m_vertices, m_lineStartIndexes, m_lineLengths, m_verticesReverse ) );
fibers->setFileName( m_fileName );
assert( !m_ifs->is_open() );
return fibers;
}
void WLoaderFibers2::readHeader() throw( WDHIOFailure, WDHException )
{
std::string line;
try
{
for( int i = 0; i < 4; ++i ) // strip first four lines
{
std::getline( *m_ifs, line );
if( !m_ifs->good() )
{
throw WDHException( "Unexpected end of file: " + m_fileName );
}
m_header.push_back( line );
}
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( "Reading first 4 lines of '" + m_fileName + "': " + e.what() );
}
// check if the header may be valid for the .fib format
if( m_header.at(0) != "# vtk DataFile Version 3.0" )
{
throw WDHIOFailure( "Unsupported format version string: " + m_header.at( 0 ) );
}
if( m_header.at(1).size() > 256 )
{
// TODO(math): This should be just a warning
throw WDHException( "VTK header too big: " + boost::lexical_cast< std::string >( m_header.at( 1 ).size() ) );
}
namespace su = string_utils;
if( su::toUpper( su::trim( m_header.at( 2 ) ) ) != "BINARY" )
{
throw WDHException( "VTK files in '" + m_header.at( 2 ) + "' format are not yet supported" );
}
if( su::tokenize( m_header.at( 3 ) ).size() < 2 ||
su::toUpper( su::tokenize( m_header.at( 3 ) )[1] ) != "POLYDATA" )
{
throw WDHException( "Invalid VTK DATASET type: " + su::tokenize( m_header.back() )[1] );
}
}
void WLoaderFibers2::readPoints()
{
std::string line;
try
{
std::getline( *m_ifs, line );
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( "Error reading POINTS declaration '" + m_fileName + "': " + e.what() );
}
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_vertices = boost::shared_ptr< std::vector< float > >( new std::vector< float > );
m_vertices->resize( numPoints * 3 );
m_verticesReverse = boost::shared_ptr< std::vector< unsigned int > >( new std::vector< unsigned int > ) ;
m_verticesReverse->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 * 3; ++i )
{
m_vertices->at( i ) = pointData[ i ];
}
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
}
void WLoaderFibers2::readLines()
{
using std::vector;
using boost::shared_ptr;
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 );
}
uint32_t *lineData = new uint32_t[ linesSize ];
m_ifs->read( reinterpret_cast<char*>( lineData ), linesSize * sizeof( uint32_t ) );
wiotools::switchByteOrderOfArray( lineData, linesSize );
m_lineStartIndexes = boost::shared_ptr< std::vector< unsigned int > >( new std::vector< unsigned int > ) ;
m_lineLengths = boost::shared_ptr< std::vector< unsigned int > >( new std::vector< unsigned int > ) ;
m_lineStartIndexes->reserve( numLines );
m_lineLengths->reserve( numLines );
// now convert lines with point numbers to real fibers
size_t linesSoFar = 0;
size_t pos = 0;
size_t posInVerts = 0;
while( linesSoFar < numLines )
{
m_lineStartIndexes->push_back( posInVerts );
size_t fiberLength = lineData[pos];
m_lineLengths->push_back( fiberLength );
++pos;
for( size_t i = 0; i < fiberLength; ++i )
{
m_verticesReverse->push_back( linesSoFar );
++posInVerts;
++pos;
}
++linesSoFar;
}
delete[] lineData;
// also eat the remaining newline
std::getline( *m_ifs, line );
assert( std::string( "" ) == line );
m_ifs->close(); // we can't close it in the destructor since then the copy constructor won't work
}
//---------------------------------------------------------------------------
//
// 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 WLOADERFIBERS2_H
#define WLOADERFIBERS2_H
#include <fstream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "../WLoader.h"
#include "../exceptions/WDHIOFailure.h"
#include "../../math/WPosition.h"
#include "../../math/WFiber.h"
/**
* Loader for the VTK file formats. For VTK just see http://www.vtk.org.
* Currently only a subset of the legacy format is supported: MedInria's
* Fib-VTK format. The byte order of the files is assumed to be big endian by
* default.
*
* \ingroup dataHandler
*/
class WLoaderFibers2 : public WLoader
{
public:
/**
* Constructs and makes a new VTK loader for separate thread start.
*
* \param fname File name where to load data from
* \throws WDHIOFailure
*/
explicit WLoaderFibers2( std::string fname ) throw( WDHIOFailure );
/**
* Destroys this instance and closes the file.
*/
virtual ~WLoaderFibers2() throw();
/**
* Loads the dataset.
*
* \return the dataset loaded.
*/
virtual boost::shared_ptr< WDataSet > load();
protected:
/**
* Read header from file.
*
* \return The offset where header ends, so we may skip this next operation.
*/
void readHeader() throw( WDHIOFailure, WDHException );