2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file LambdaMST2D.ih
19 * @author Kacper Pluta (\c kacper.pluta@esiee.fr )
20 * Laboratoire d'Informatique Gaspard-Monge - LIGM, France
24 * This file is part of the DGtal library.
30 template < typename TSpace, typename TSegmentation, typename Functor >
32 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::LambdaMST2DEstimator() : dssSegments ( 0 ) {}
34 template < typename TSpace, typename TSegmentation, typename Functor >
37 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::init ( ConstIterator itb, ConstIterator ite )
43 template < typename TSpace, typename TSegmentation, typename Functor >
46 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::attach ( Alias<TSegmentation> segmentComputer )
48 dssSegments = &segmentComputer;
51 template < typename TSpace, typename TSegmentation, typename Functor >
54 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::isValid () const
56 return ( dssSegments != 0 );
59 template < typename TSpace, typename TSegmentation, typename Functor >
61 typename TSpace::RealVector
62 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::eval ( const Point & p )
64 assert ( dssSegments != 0 );
65 typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
66 typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
67 Value tangent, partial;
68 for ( ; DSS != lastDSS; ++DSS )
70 if ( DSS->isInDSS ( p ) )
72 size_t pos = std::distance ( DSS.begin(), std::find ( DSS.begin(), DSS.end(), p ) ) + 1;
73 size_t dssLen = std::distance ( DSS.begin(), DSS.end() ) + 1;
74 SegmentComputer comp ( *DSS );
75 partial = myFunctor ( comp, pos, dssLen );
79 if ( tangent.second != 0. )
80 return tangent.first / tangent.second;
85 template < typename TSpace, typename TSegmentation, typename Functor >
86 template <typename OutputIterator>
89 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::eval ( ConstIterator itb, ConstIterator ite,
90 OutputIterator result )
92 assert ( ( myBegin != myEnd ) && isValid ( ) && std::distance ( myBegin, itb ) >= 0 && std::distance ( myEnd, ite ) <= 0 && ( itb != ite ) );
93 dssSegments->setSubRange ( itb, ite );
94 std::vector < Value > outValues ( std::distance ( itb, ite ) );
95 typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
96 typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
97 for ( ; DSS != lastDSS; ++DSS )
99 size_t dssLen = std::distance ( DSS.begin(), DSS.end() );
100 SegmentComputer comp ( *DSS );
101 for ( unsigned int i = 0; i < dssLen; i++ )
102 outValues[ std::distance ( itb, DSS.begin() ) + i ] += myFunctor ( comp, i + 1, dssLen + 1 );
104 accumulate ( outValues, result );
108 template < typename TSpace, typename TSegmentation, typename Functor >
109 template <typename OutputIterator>
112 LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::accumulate ( std::vector < Value > & outValues, OutputIterator & result )
114 for ( unsigned int i = 0; i < outValues.size(); i++ )
116 Value & tangent = outValues[i];
117 if ( tangent.second != 0. )
118 *result++ = ( tangent.first / tangent.second );
120 *result++ = tangent.first;