OpenWalnut  1.4.0
WFiber.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <algorithm>
00026 #include <cmath>
00027 #include <utility>
00028 #include <vector>
00029 
00030 #include "../WLimits.h"
00031 #include "WFiber.h"
00032 
00033 namespace
00034 {
00035     std::pair< double, double > dXt_optimized( double thresholdSquare, const WFiber &q, const WFiber &r )
00036     {
00037         const size_t qsize = q.size();
00038         const size_t rsize = r.size();
00039         double qr = 0.0;
00040         double rq = 0.0;
00041 
00042         // will contain every point-to-point square-distances
00043         std::vector< std::vector< double > > m( qsize, std::vector< double >( rsize, 0.0 ) );
00044 
00045         // double **m = new double*[qsize];
00046 
00047         for( size_t i = 0; i < qsize; ++i )
00048         {
00049             // m[i] = new double[rsize];
00050             for( size_t j = 0; j < rsize; ++j )
00051             {
00052                 m[i][j] = length2( q[i] - r[j] );
00053             }
00054         }
00055         // compute dt(q,r)
00056         double minSoFar;
00057         for( size_t i = 0; i < qsize; ++i )
00058         {
00059             minSoFar = *( std::min_element( m[i].begin(), m[i].end() ) );
00060             // minSoFar = *( std::min_element( &m[i][0], &m[i][rsize] ) );
00061             if( minSoFar > thresholdSquare )
00062             {
00063                 qr += std::sqrt( minSoFar );
00064             }
00065         }
00066         qr = qr / qsize;
00067         // compute dt(r,q)
00068         for( size_t j = 0; j < rsize; ++j )
00069         {
00070             minSoFar = wlimits::MAX_DOUBLE;
00071             for( size_t i = 0; i < qsize; ++i )
00072             {
00073                 if( m[i][j] < minSoFar )
00074                 {
00075                     minSoFar = m[i][j];
00076                 }
00077             }
00078             if( minSoFar > thresholdSquare )
00079             {
00080                 rq += std::sqrt( minSoFar );
00081             }
00082         }
00083         rq = rq / rsize;
00084         return std::make_pair( qr, rq );
00085     }
00086 }
00087 
00088 WFiber::WFiber( const std::vector< WPosition > &points )
00089     : WLine( points )
00090 {
00091 }
00092 
00093 WFiber::WFiber()
00094     : WLine()
00095 {
00096 }
00097 
00098 double WFiber::distDST( double thresholdSquare, const WFiber &q, const WFiber &r )
00099 {
00100     std::pair< double, double > result = ::dXt_optimized( thresholdSquare, q, r );
00101     return std::min( result.first, result.second );
00102 }
00103 
00104 double WFiber::distDLT( double thresholdSquare, const WFiber &q, const WFiber &r )
00105 {
00106     std::pair< double, double > result = ::dXt_optimized( thresholdSquare, q, r );
00107     return std::max( result.first, result.second );
00108 }