Commit 835cdd6a authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[CHANGE #157] Changed the metric infrastructure, so we may now easy use other...

[CHANGE #157] Changed the metric infrastructure, so we may now easy use other fiber similarity metrics.
parent c264d1eb
ADD_SUBDIRECTORY( fiberSimilarity )
FILE( GLOB MATH_SRC "*.[c,h]pp" )
FILE( GLOB FIBERSIMILARITY_SRC "fiberSimilarity/*.[c,h]pp" )
ADD_LIBRARY( math SHARED ${MATH_SRC} )
ADD_LIBRARY( math SHARED ${MATH_SRC} ${FIBERSIMILARITY_SRC} )
# Unit tests
IF( CXXTEST_FOUND )
......
......@@ -22,12 +22,9 @@
//
//---------------------------------------------------------------------------
#include <algorithm>
#include <utility>
#include <vector>
#include "WFiber.h"
#include "../common/WLimits.h"
namespace wmath
{
......@@ -35,63 +32,4 @@ namespace wmath
: WLine( points )
{
}
std::pair< double, double > WFiber::dXt_optimized( const WFiber &other,
const double thresholdSquare ) const
{
const WFiber &q = *this;
const WFiber &r = other;
const size_t qsize = q.size();
const size_t rsize = r.size();
double qr = 0.0;
double rq = 0.0;
std::vector< std::vector< double > > m( qsize, std::vector< double >( rsize, 0.0 ) );
for( size_t i = 0; i < qsize; ++i )
{
for( size_t j = 0; j < rsize; ++j )
{
m[i][j] = q[i].distanceSquare( r[j] );
}
}
double minSoFar;
for( size_t i = 0; i < qsize; ++i )
{
minSoFar = *( std::min_element( m[i].begin(), m[i].end() ) );
if( minSoFar > thresholdSquare )
{
qr += sqrt( minSoFar );
}
}
qr = qr / qsize;
for( size_t j = 0; j < rsize; ++j )
{
minSoFar = wlimits::MAX_DOUBLE;
for( size_t i = 0; i < qsize; ++i )
{
if( m[i][j] < minSoFar )
{
minSoFar = m[i][j];
}
}
if( minSoFar > thresholdSquare )
{
rq += sqrt( minSoFar );
}
}
rq = rq / rsize;
return std::make_pair( qr, rq );
}
double WFiber::dSt( const WFiber& other, const double thresholdSquare ) const
{
std::pair< double, double > result = dXt_optimized( other, thresholdSquare );
return std::min( result.first, result.second );
}
double WFiber::dLt( const WFiber& other, const double thresholdSquare ) const
{
std::pair< double, double > result = dXt_optimized( other, thresholdSquare );
return std::max( result.first, result.second );
}
}
......@@ -46,46 +46,6 @@ friend class WFiberTest;
public:
explicit WFiber( const std::vector< WPosition > &points );
/**
* Optimized version which computes dt measure from fibers Q and R.
* Since this is not symmetric it computes both: dt(Q,R) and dt(R,Q) at
* once. dt(Q,R) computes the mean euclidian distance of the closest
* points. See Paper from Zhang: doi.ieeecomputersociety.org/10.1109/TVCG.2008.52
*
* \param other The other fiber R
* \param thresholdSquare The threshold, where distances below will be
* ignored for dt() computation. Note that this parameter is given
* as square, since the distanceSquare is much faster to compute then
* just the distance.
* \return Both dt(Q,R) and dt(R,Q).
*/
std::pair< double, double > dXt_optimized( const WFiber &other,
const double thresholdSquare ) const;
/**
* Computes dt(Q, R, t) and dt(R, Q, t) and then the one which is smaller
* is returned. Note this distance metric is symmetric.
*
* \param other The other fiber "R", while this fiber is taken as "Q".
* \param thresholdSquare The threshold upto which the accumulated
* distances should be considered
*
* \return The minimum of dt(Q, R, t) and dt(R, Q, t)
*/
double dSt( const WFiber &other, const double thresholdSquare = 0.0 ) const;
/**
* Computes dt(Q, R, t) and dt(R, Q, t) and then the one which is smaller
* is returned. Note this distance metric is symmetric.
*
* \param other The other fiber "R", while this fiber is taken as "Q".
* \param thresholdSquare The threshold upto which the accumulated
* distances should be considered
*
* \return The maximum dt(Q, R, t) and dt(R, Q, t)
*/
double dLt( const WFiber &other, const double thresholdSquare = 0.0 ) const;
protected:
private:
};
......
FILE( GLOB FIBERSIMILARITY_SRC "*.[c,h]pp" )
# Unit tests
IF( CXXTEST_FOUND )
CXXTEST_ADD_TESTS_FROM_LIST( "${FIBERSIMILARITY_SRC}"
"math;common"
"WFiberSimilarity.cpp" # This is just an interface
)
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 <algorithm>
#include <utility>
#include "WDLTMetric.h"
WDLTMetric::WDLTMetric( double thresholdSquare )
: WZhangMetric( thresholdSquare )
{
}
double WDLTMetric::dist( const wmath::WFiber &q, const wmath::WFiber &r ) const
{
std::pair< double, double > result = dXt_optimized( q, r );
return std::max( result.first, result.second );
}
//---------------------------------------------------------------------------
//
// 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 WDLTMETRIC_H
#define WDLTMETRIC_H
#include "WZhangMetric.h"
#include "../WFiber.h"
/**
* This is the Larger thresholded distance as described by Zhang.
*/
class WDLTMetric : public WZhangMetric
{
public:
/**
* Constructs this metric with a certain threshold.
*/
explicit WDLTMetric( double thresholdSquare );
/**
* \return The maximum of dt(Q,R) and dt(R,Q)
*/
virtual double dist( const wmath::WFiber &q, const wmath::WFiber &r ) const;
private:
};
#endif // WDLTMETRIC_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 <algorithm>
#include <utility>
#include "WDSTMetric.h"
WDSTMetric::WDSTMetric( double thresholdSquare )
: WZhangMetric( thresholdSquare )
{
}
double WDSTMetric::dist( const wmath::WFiber &q, const wmath::WFiber &r ) const
{
std::pair< double, double > result = dXt_optimized( q, r );
return std::min( result.first, result.second );
}
//---------------------------------------------------------------------------
//
// 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 WDSTMETRIC_H
#define WDSTMETRIC_H
#include "WZhangMetric.h"
#include "../WFiber.h"
/**
* This is the Smaller thresholded distance as described by Zhang.
*/
class WDSTMetric : WZhangMetric
{
public:
/**
* Constructs this metric with a certain threshold.
*/
explicit WDSTMetric( double thresholdSquare );
/**
* \return The minimum of dt(Q, R, t) and dt(R, Q, t)
*/
virtual double dist( const wmath::WFiber &q, const wmath::WFiber &ri ) const;
private:
};
#endif // WDSTMETRIC_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 "WFiberSimilarity.h"
WFiberSimilarity::~WFiberSimilarity()
{
}
//---------------------------------------------------------------------------
//
// 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 WFIBERSIMILARITY_H
#define WFIBERSIMILARITY_H
namespace wmath
{
class WFiber;
}
/**
* Abstract interface to all fiber similarity measures. The term
* "fiber-similarity" is closely related to the term fiber-distance. Basically
* fibers that are very close to each other (low distance) are considered
* similar.
*/
class WFiberSimilarity
{
public:
/**
* Computes the similarity between two fibers Q and R. Values near 0
* indicate very strong similarity and low distance.
*/
virtual double dist( const wmath::WFiber &q, const wmath::WFiber &r ) const = 0;
/**
* Virtual destructor since there is a virtual member function.
*/
virtual ~WFiberSimilarity();
protected:
private:
};
#endif // WFIBERSIMILARITY_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 <algorithm>
#include <utility>
#include <vector>
#include "WZhangMetric.h"
#include "../../common/WLimits.h"
WZhangMetric::WZhangMetric( double thresholdSquare )
: m_thresholdSquare( thresholdSquare )
{
}
void WZhangMetric::setThreshold( double thresholdSquare )
{
m_thresholdSquare = thresholdSquare;
}
std::pair< double, double > WZhangMetric::dXt_optimized( const wmath::WFiber &q,
const wmath::WFiber &r ) const
{
const size_t qsize = q.size();
const size_t rsize = r.size();
double qr = 0.0;
double rq = 0.0;
// will contain every point-to-point square-distances
std::vector< std::vector< double > > m( qsize, std::vector< double >( rsize, 0.0 ) );
for( size_t i = 0; i < qsize; ++i )
{
for( size_t j = 0; j < rsize; ++j )
{
m[i][j] = q[i].distanceSquare( r[j] );
}
}
// compute dt(q,r)
double minSoFar;
for( size_t i = 0; i < qsize; ++i )
{
minSoFar = *( std::min_element( m[i].begin(), m[i].end() ) );
if( minSoFar > m_thresholdSquare )
{
qr += sqrt( minSoFar );
}
}
qr = qr / qsize;
// compute dt(r,q)
for( size_t j = 0; j < rsize; ++j )
{
minSoFar = wlimits::MAX_DOUBLE;
for( size_t i = 0; i < qsize; ++i )
{
if( m[i][j] < minSoFar )
{
minSoFar = m[i][j];
}
}
if( minSoFar > m_thresholdSquare )
{
rq += sqrt( minSoFar );
}
}
rq = rq / rsize;
return std::make_pair( qr, rq );
}
//---------------------------------------------------------------------------
//
// 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 WZHANGMETRIC_H
#define WZHANGMETRIC_H
#include <utility>
#include "WFiberSimilarity.h"
#include "../WFiber.h"
/**
* Abstract class which prepares the implementation of the thresholded mean min
* distance as introduced by Zhang etal: http://dx.doi.org/10.1109/TVCG.2008.52
*/
class WZhangMetric : public WFiberSimilarity
{
public:
/**
* Constructs new algorithm with its threshold for minimal square
* distances.
*/
explicit WZhangMetric( double thresholdSquare );
/**
* Resets the threshold for minimal point distances used in computation.
*/
void setThreshold( double thresholdSquare );
protected:
/**
* Optimized version which computes dt measure from fibers Q and R.
* Since this is not symmetric it computes both: dt(Q,R) and dt(R,Q) at
* once. dt(Q,R) computes the mean euclidian distance of the closest
* points.
*
* \param q The first fiber
* \param r The second fiber
* \return Both dt(Q,R) and dt(R,Q).
*/
std::pair< double, double > dXt_optimized( const wmath::WFiber &q, const wmath::WFiber &r ) const;
private:
/**
* Threshold upto this point-distances are threaded as zero-distance.
*/
double m_thresholdSquare;
};
#endif // WZHANGMETRIC_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 WDLTMETRIC_TEST_H
#define WDLTMETRIC_TEST_H
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../WDLTMetric.h"