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

[CHANGE] Renamed WDXtLookUpTable to WMatrixSym, since it really is a symmetric...

[CHANGE] Renamed WDXtLookUpTable to WMatrixSym, since it really is a symmetric matrix, but what you use it for should not be part of documentation or filename..
parent b69f57d2
......@@ -22,56 +22,4 @@
//
//---------------------------------------------------------------------------
#include <cassert>
#include <algorithm>
#include <vector>
#include "WDXtLookUpTable.h"
WDXtLookUpTable::WDXtLookUpTable( size_t dim )
: m_data( ( dim * ( dim - 1 ) ) / 2, 0.0 ),
m_dim( dim )
{
}
double& WDXtLookUpTable::operator()( size_t i, size_t j ) throw( WOutOfBounds )
{
if( i == j )
{
std::stringstream ss;
ss << "Invalid Element Access ( " << i << ", " << j
<< " ). Main Diagonal Elements are forbidden in this table.";
throw WOutOfBounds( ss.str() );
}
if( i > j )
{
std::swap( i, j );
}
return m_data[( i * m_dim + j - ( i + 1 ) * ( i + 2 ) / 2 )];
}
size_t WDXtLookUpTable::size() const
{
return m_data.size();
}
size_t WDXtLookUpTable::dim() const
{
return m_dim;
}
const std::vector< double >& WDXtLookUpTable::getData() const
{
return m_data;
}
void WDXtLookUpTable::setData( const std::vector< double > &data ) throw( WOutOfBounds )
{
if( m_dim * ( m_dim - 1 ) / 2 != data.size() )
{
std::stringstream ss;
ss << "Data vector length: " << data.size() << " doesn't fit to dimension: " << m_dim;
throw WOutOfBounds( ss.str() );
}
m_data = std::vector< double >( data ); // copy content
}
#include "WMatrixSym.h"
......@@ -22,74 +22,140 @@
//
//---------------------------------------------------------------------------
#ifndef WDXTLOOKUPTABLE_H
#define WDXTLOOKUPTABLE_H
#ifndef WMATRIXSYM_H
#define WMATRIXSYM_H
#include <algorithm>
#include <cassert>
#include <vector>
#include "../exceptions/WOutOfBounds.h"
#include "../WExportCommon.h"
/**
* Represents a symmetric matrix-like look up table, meaning it stores only
* the elements inside the triangular matrix without the main diagonal.
* Symmetric square matrix, storing only the elements of the triangular matrix without the main
* diagonal. So in case of a NxN matrix there are only (N^2-N)/2 elements stored.
*
* So in case of a NxN matrix there are only (N^2-N)/2 elements to store.
*
* The reason why this is named DXt look up table is, since is it used as
* look up table for dSt and dLt fiber distance metrics.
* \note There exists also a WWriter and WReader for storing/reading the matrix in VTK file format.
*/
class OWCOMMON_EXPORT WDXtLookUpTable
template< typename T >
class OWCOMMON_EXPORT WMatrixSymImpl
{
friend class WDXtLookUpTableTest;
friend class WMatrixSymTest;
public:
/**
* Generates new look up table.
* Type of stored elements.
*/
typedef T value_type;
/**
* Generates new symmetric matrix.
*
* \param dim the dimension of the square matrix
* \param n number of rows and cols
*/
explicit WDXtLookUpTable( size_t dim );
explicit WMatrixSymImpl( size_t n );
/**
* Element acces operator as if the elements where stored as a matrix.
* Element acces operator as if the elements where stored as a normal matrix.
*
* \warning Acessing elements of the main diagonal is forbidden!
*
* \param i The i'th row
* \param j The j'th column
*
* \return reference to the (i,j) element of the table
*/
double& operator()( size_t i, size_t j ) throw( WOutOfBounds );
T& operator()( size_t i, size_t j ) throw( WOutOfBounds );
/**
* \return the number of distances inside this table.
* Returns the number of elements stored inside this matrix.
*/
size_t size() const;
size_t numElements() const;
/**
* \return the dimensionality of this table
* Returns the number of rows and cols.
*/
size_t dim() const;
size_t size() const;
/**
* \return readonly access to the internal data
* Returns the elements stored inside of this container.
*
* \return Read-only reference to the elements stored inside this container.
*/
const std::vector< double >& getData() const;
const std::vector< T >& getData() const;
/**
* Resets the internal data (e.g. loaded from file).
* Resets the internal data to the given vector of elements.
*
* \param data Vector containing the new elements
* \param data new data in row major arrangement
*/
void setData( const std::vector< double > &data ) throw( WOutOfBounds );
void setData( const std::vector< T > &data ) throw( WOutOfBounds );
private:
/**
* Internal data structure to store the elements. The order is line major
* meaning first element (0,0)...(0,n-1),(1,0)...(1, n-1)...
* Internal data structure to store the elements. The order is row major.
*/
std::vector< double > m_data;
std::vector< T > m_data;
size_t m_dim; //!< Matrix dimension
/**
* Number of rows and cols.
*/
size_t m_n;
};
#endif // WDXTLOOKUPTABLE_H
template< typename T >
inline WMatrixSymImpl< T >::WMatrixSymImpl( size_t n )
: m_data( ( n * ( n - 1 ) ) / 2, 0.0 ),
m_n( n )
{
}
template< typename T >
inline T& WMatrixSymImpl< T >::operator()( size_t i, size_t j ) throw( WOutOfBounds )
{
if( i == j )
{
std::stringstream ss;
ss << "Invalid Element Access ( " << i << ", " << j << " ). Main Diagonal Elements are forbidden in this table.";
throw WOutOfBounds( ss.str() );
}
if( i > j )
{
std::swap( i, j );
}
return m_data[( i * m_n + j - ( i + 1 ) * ( i + 2 ) / 2 )];
}
template< typename T >
inline size_t WMatrixSymImpl< T >::numElements() const
{
return m_data.size();
}
template< typename T >
inline size_t WMatrixSymImpl< T >::size() const
{
return m_n;
}
template< typename T >
inline const typename std::vector< T >& WMatrixSymImpl< T >::getData() const
{
return m_data;
}
template< typename T >
inline void WMatrixSymImpl< T >::setData( const std::vector< T > &data ) throw( WOutOfBounds )
{
if( m_n * ( m_n - 1 ) / 2 != data.size() )
{
std::stringstream ss;
ss << "Data vector length: " << data.size() << " doesn't fit to number of rows and cols: " << m_n;
throw WOutOfBounds( ss.str() );
}
m_data = std::vector< T >( data ); // copy content
}
typedef WMatrixSymImpl< double > WMatrixSym;
#endif // WMATRIXSYM_H
......@@ -22,21 +22,21 @@
//
//---------------------------------------------------------------------------
#ifndef WDXTLOOKUPTABLE_TEST_H
#define WDXTLOOKUPTABLE_TEST_H
#ifndef WMATRIXSYM_TEST_H
#define WMATRIXSYM_TEST_H
#include <string>
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../WDXtLookUpTable.h"
#include "../../exceptions/WOutOfBounds.h"
#include "../WMatrixSym.h"
/**
* Unit test this LookUp table class
*/
class WDXtLookUpTableTest : public CxxTest::TestSuite
class WMatrixSymTest : public CxxTest::TestSuite
{
public:
/**
......@@ -44,7 +44,7 @@ public:
*/
void testOperatorOn3x3Matrix( void )
{
WDXtLookUpTable t( 3 );
WMatrixSym t( 3 );
TS_ASSERT_EQUALS( t.m_data.size(), 3 );
}
......@@ -54,7 +54,7 @@ public:
*/
void testAccessOn3x3Matrix( void )
{
WDXtLookUpTable t( 3 );
WMatrixSym t( 3 );
double mydata[] = { 1.6, 0.2, 7.7 }; // NOLINT
std::vector< double > data( mydata, mydata + sizeof( mydata ) / sizeof( double ) );
t.setData( data );
......@@ -69,10 +69,10 @@ public:
*/
void testSetDataWithInvalidLengthForDimension( void )
{
WDXtLookUpTable t( 4 );
WMatrixSym t( 4 );
double mydata[] = { 1.6, 0.2, 7.7 }; // NOLINT
std::vector< double > data( mydata, mydata + sizeof( mydata ) / sizeof( double ) );
TS_ASSERT_THROWS_EQUALS( t.setData( data ), WOutOfBounds &e, std::string( e.what() ), "Data vector length: 3 doesn't fit to dimension: 4" );
TS_ASSERT_THROWS_EQUALS( t.setData( data ), WOutOfBounds &e, std::string( e.what() ), "Data vector length: 3 doesn't fit to number of rows and cols: 4" ); // NOLINT line length
}
/**
......@@ -80,7 +80,7 @@ public:
*/
void testInvalidAccessOnMainDiagonal( void )
{
WDXtLookUpTable t( 4 );
WMatrixSym t( 4 );
double mydata[] = { 1.6, 0.2, 7.7 }; // NOLINT
std::vector< double > data( mydata, mydata + sizeof( mydata ) / sizeof( double ) );
TS_ASSERT_THROWS_EQUALS( t( 0, 0 ), WOutOfBounds &e, std::string( e.what() ),
......@@ -88,4 +88,4 @@ public:
}
};
#endif // WDXTLOOKUPTABLE_TEST_H
#endif // WMATRIXSYM_TEST_H
......@@ -36,14 +36,14 @@
#include "../exceptions/WDHException.h"
#include "../exceptions/WDHIOFailure.h"
#include "WReader.h"
#include "WReaderLookUpTableVTK.h"
#include "WReaderMatrixSymVTK.h"
WReaderLookUpTableVTK::WReaderLookUpTableVTK( std::string fname )
WReaderMatrixSymVTK::WReaderMatrixSymVTK( std::string fname )
: WReader( fname )
{
}
void WReaderLookUpTableVTK::readTable( boost::shared_ptr< std::vector< double > > table ) const
void WReaderMatrixSymVTK::readTable( boost::shared_ptr< std::vector< double > > table ) const
{
WAssert( table->size() == 0, "Non-zero size indicates an error, since the vector will be filled IN HERE." );
......@@ -71,10 +71,20 @@ void WReaderLookUpTableVTK::readTable( boost::shared_ptr< std::vector< double >
{
throw WDHIOFailure( std::string( "Reading first 4 lines of '" + m_fname + "': " + e.what() ) );
}
WAssert( header[0] == "# vtk DataFile Version 3.0", "Wrong string in file header found." );
WAssert( header[1] == "DXtLookUpTable from OpenWalnut", "Wrong string in file header found." );
WAssert( header[2] == "BINARY", "Wrong string in file header found." );
WAssert( header[3] == "FIELD DXtLookUpTable 1", "Wrong string in file header found." );
if( header[0] != "# vtk DataFile Version 3.0" )
{
wlog::warning( "WReaderMatrixSymVTK" ) << "Wrong version string in file header found, expected: \"# vtk DataFile Version 3.0\" but got: " << header[0];
}
if( header[2] != "BINARY" )
{
wlog::error( "WReaderMatrixSymVTK" ) << "Wrong data format: BINARY expected but got: " << header[2];
throw WDHIOFailure( "Error reading file '" + m_fname + " invalid binary format." );
}
if( header[3] != "FIELD WMatrixSym 1" )
{
wlog::error( "WReaderMatrixSymVTK" ) << "Wrong field desc in file header found: " << header[3] << " but expected: \"FIELD WMatrixSym 1\"";
throw WDHIOFailure( "Error reading file '" + m_fname + " invalid VTK field name." );
}
try
{
......@@ -82,14 +92,14 @@ void WReaderLookUpTableVTK::readTable( boost::shared_ptr< std::vector< double >
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( std::string( "Error reading DISTANCES field '" + m_fname + "': " + e.what() ) );
throw WDHIOFailure( std::string( "Error reading ELEMENTS field '" + m_fname + "': " + e.what() ) );
}
namespace su = string_utils;
size_t numDistances = 0;
std::vector< std::string > tokens = su::tokenize( line );
if( tokens.size() != 4 || su::toLower( tokens.at( 3 ) ) != "float" )
{
throw WDHException( std::string( "Invalid DISTANCES declaration: " + line ) );
throw WDHException( std::string( "Invalid ELEMENTS declaration: " + line ) );
}
try
{
......@@ -97,7 +107,7 @@ void WReaderLookUpTableVTK::readTable( boost::shared_ptr< std::vector< double >
}
catch( const boost::bad_lexical_cast &e )
{
throw WDHException( std::string( "Invalid number of distances: " + tokens.at( 1 ) ) );
throw WDHException( std::string( "Invalid number of elements: " + tokens.at( 1 ) ) );
}
float *data = new float[ numDistances ];
......@@ -107,7 +117,7 @@ void WReaderLookUpTableVTK::readTable( boost::shared_ptr< std::vector< double >
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( std::string( "Error reading distances in VTK DISTANCES field '" + m_fname + "': " + e.what() ) );
throw WDHIOFailure( std::string( "Error reading elements in VTK ELEMENTS field '" + m_fname + "': " + e.what() ) );
}
// all 4 bytes of each float are in wrong order we need to reorder them
......
......@@ -22,8 +22,8 @@
//
//---------------------------------------------------------------------------
#ifndef WREADERLOOKUPTABLEVTK_H
#define WREADERLOOKUPTABLEVTK_H
#ifndef WREADERMATRIXSYMVTK_H
#define WREADERMATRIXSYMVTK_H
#include <string>
#include <vector>
......@@ -36,7 +36,7 @@
/**
* Can read a look up table from a file in VTK format.
*/
class OWDATAHANDLER_EXPORT WReaderLookUpTableVTK : public WReader // NOLINT
class OWDATAHANDLER_EXPORT WReaderMatrixSymVTK : public WReader // NOLINT
{
public:
/**
......@@ -45,7 +45,7 @@ public:
*
* \param fname file name
*/
explicit WReaderLookUpTableVTK( std::string fname );
explicit WReaderMatrixSymVTK( std::string fname );
/**
* Perform reading from the file.
......@@ -58,4 +58,4 @@ protected:
private:
};
#endif // WREADERLOOKUPTABLEVTK_H
#endif // WREADERMATRIXSYMVTK_H
......@@ -30,14 +30,14 @@
#include "../../common/WIOTools.h"
#include "../exceptions/WDHIOFailure.h"
#include "WWriterLookUpTableVTK.h"
#include "WWriterMatrixSymVTK.h"
WWriterLookUpTableVTK::WWriterLookUpTableVTK( std::string fname, bool overwrite )
WWriterMatrixSymVTK::WWriterMatrixSymVTK( std::string fname, bool overwrite )
: WWriter( fname, overwrite )
{
}
void WWriterLookUpTableVTK::writeTable( const std::vector< double > &table, size_t dim ) const
void WWriterMatrixSymVTK::writeTable( const std::vector< double > &table, size_t dim ) const
{
using std::fstream;
fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
......@@ -46,12 +46,12 @@ void WWriterLookUpTableVTK::writeTable( const std::vector< double > &table, size
throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) );
}
out << "# vtk DataFile Version 3.0" << std::endl;
out << "DXtLookUpTable from OpenWalnut" << std::endl;
out << "WMatrixSym from OpenWalnut" << std::endl;
out << "BINARY" << std::endl;
out << "FIELD DXtLookUpTable 1" << std::endl;
out << "FIELD WMatrixSym 1" << std::endl;
unsigned int numDistances = table.size() + 1;
out << "DISTANCES " << numDistances << " 1 float" << std::endl;
out << "ELEMENTS " << numDistances << " 1 float" << std::endl;
float *data = new float[numDistances];
for( size_t i = 0; i < table.size() ; ++i )
......
......@@ -22,31 +22,30 @@
//
//---------------------------------------------------------------------------
#ifndef WWRITERLOOKUPTABLEVTK_H
#define WWRITERLOOKUPTABLEVTK_H
#ifndef WWRITERMATRIXSYMVTK_H
#define WWRITERMATRIXSYMVTK_H
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "WWriter.h"
#include "../WExportDataHandler.h"
#include "WWriter.h"
/**
* Can write a look up table to a file in VTK format.
*/
class OWDATAHANDLER_EXPORT WWriterLookUpTableVTK : public WWriter // NOLINT
class OWDATAHANDLER_EXPORT WWriterMatrixSymVTK : public WWriter // NOLINT
{
public:
/**
* Creates a writer object for FiberVTK file writing.
* Creates a writer object for FiberVTK file writing.
*
* \param fname path to the target file where stuff will be written to
* \param overwrite If true existing files will be overwritten
*/
WWriterLookUpTableVTK( std::string fname, bool overwrite = false );
WWriterMatrixSymVTK( std::string fname, bool overwrite = false );
/**
* Actually perform writing to file.
......@@ -60,4 +59,4 @@ protected:
private:
};
#endif // WWRITERLOOKUPTABLEVTK_H
#endif // WWRITERMATRIXSYMVTK_H
......@@ -33,8 +33,8 @@
#include <osg/Geode>
#include <osg/Geometry>
#include "../../common/datastructures/WDXtLookUpTable.h"
#include "../../common/datastructures/WFiber.h"
#include "../../common/math/WMatrixSym.h"
#include "../../common/WAssert.h"
#include "../../common/WColor.h"
#include "../../common/WIOTools.h"
......@@ -44,14 +44,14 @@
#include "../../common/WThreadedFunction.h"
#include "../../dataHandler/datastructures/WFiberCluster.h"
#include "../../dataHandler/exceptions/WDHIOFailure.h"
#include "../../dataHandler/io/WReaderLookUpTableVTK.h"
#include "../../dataHandler/io/WWriterLookUpTableVTK.h"
#include "../../dataHandler/io/WReaderMatrixSymVTK.h"
#include "../../dataHandler/io/WWriterMatrixSymVTK.h"
#include "../../dataHandler/WDataSetFiberVector.h"
#include "../../dataHandler/WSubject.h"
#include "../../graphicsEngine/WGEUtils.h"
#include "../../kernel/WKernel.h"
#include "WMDetTractClustering.xpm"
#include "WMDetTractClustering.h"
#include "WMDetTractClustering.xpm"
#ifdef CUDA_FOUND
#include "WMDetTractClusteringCudaInterface.h"
......@@ -182,7 +182,7 @@ void WMDetTractClustering::update()
if( !( m_dLtTableExists = dLtTableExists() ) )
{
debugLog() << "Consider old table as invalid.";
m_dLtTable.reset( new WDXtLookUpTable( m_tracts->size() ) );
m_dLtTable.reset( new WMatrixSym( m_tracts->size() ) );
}
cluster();
......@@ -191,7 +191,7 @@ void WMDetTractClustering::update()
m_progress->addSubProgress( saveProgress );
if( !wiotools::fileExists( lookUpTableFileName() ) )
{
WWriterLookUpTableVTK w( lookUpTableFileName(), true );
WWriterMatrixSymVTK w( lookUpTableFileName(), true );
try
{
w.writeTable( m_dLtTable->getData(), m_lastTractsSize );
......@@ -233,10 +233,10 @@ bool WMDetTractClustering::dLtTableExists()
try
{
debugLog() << "trying to read table from: " << dLtFileName;
WReaderLookUpTableVTK r( dLtFileName );
WReaderMatrixSymVTK r( dLtFileName );
boost::shared_ptr< std::vector< double > > data( new std::vector< double >() );
r.readTable( data );
m_dLtTable.reset( new WDXtLookUpTable( static_cast< size_t >( data->back() ) ) );
m_dLtTable.reset( new WMatrixSym( static_cast< size_t >( data->back() ) ) );
m_lastTractsSize = static_cast< size_t >( data->back() );
// remove the dimension from data array since it's not representing any distance
......@@ -452,7 +452,7 @@ bool WMDetTractClustering::OutputIDBound::accept( boost::shared_ptr< WPropertyVa
}
WMDetTractClustering::SimilarityMatrixComputation::SimilarityMatrixComputation(
boost::shared_ptr< WDXtLookUpTable > dLtTable,
boost::shared_ptr< WMatrixSym > dLtTable,
boost::shared_ptr< WDataSetFiberVector > tracts,
double proxSquare,
const WBoolFlag& shutdownFlag )
......
......@@ -32,8 +32,8 @@
#include <osg/Geode>
#include "../../common/datastructures/WDXtLookUpTable.h"
#include "../../common/datastructures/WFiber.h"
#include "../../common/math/WMatrixSym.h"
#include "../../dataHandler/datastructures/WFiberCluster.h"
#include "../../dataHandler/WDataSetFiberVector.h"
#include "../../graphicsEngine/WGEManagedGroupNode.h"
......@@ -187,7 +187,7 @@ private:
boost::shared_ptr< WDataSetFibers > m_rawTracts; //!< Reference to the WDataSetFibers object
boost::shared_ptr< WModuleInputData< WDataSetFibers > > m_tractInput; //!< Input connector for a tract dataset.
boost::shared_ptr< WModuleOutputData< WFiberCluster > > m_output; //!< Output connector for the first cluster.
boost::shared_ptr< WDXtLookUpTable > m_dLtTable; //!< Distance matrix lookUpTable
boost::shared_ptr< WMatrixSym > m_dLtTable; //!< Distance matrix lookUpTable
boost::shared_ptr< WCondition > m_update; //!< Used for register properties indicating a rerun of the moduleMain loop
......@@ -240,7 +240,7 @@ private:
* the boost::function instance
* \param shutdownFlag a bool flag indicating an abort.
*/
SimilarityMatrixComputation( const boost::shared_ptr< WDXtLookUpTable > dLtTable,
SimilarityMatrixComputation( const boost::shared_ptr< WMatrixSym > dLtTable,
boost::shared_ptr< WDataSetFiberVector > tracts,
double proxSquare,
const WBoolFlag& shutdownFlag );
......@@ -259,7 +259,7 @@ private:
/**
* The table where the similarity computation results should be saved.
*/
boost::shared_ptr< WDXtLookUpTable > m_table;
boost::shared_ptr< WMatrixSym > m_table;
/**
* Reference to the dataset of the tracts.
......
......@@ -22,8 +22,8 @@
//
//---------------------------------------------------------------------------
#include "../../common/datastructures/WDXtLookUpTable.h"
#include "../../common/datastructures/WFiber.h"
#include "../../common/math/WMatrixSym.h"
#include "../../common/WProgressCombiner.h"
#include "../../dataHandler/WDataSetFiberVector.h"
#include "WMDetTractClusteringCudaInterface.h"
......@@ -31,7 +31,7 @@
#include "WProgressWrapper.h"
#include "WProgressWrapperData.h"
bool initDLtTableCuda( boost::shared_ptr< WDXtLookUpTable > dLtTable,
bool initDLtTableCuda( boost::shared_ptr< WMatrixSym > dLtTable,
const boost::shared_ptr< WDataSetFiberVector > tracts,
double proximity_threshold,
boost::shared_ptr< WProgressCombiner >