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 LambdaMST3D.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.
27 template < typename TSpace, typename TSegmentation, typename Functor >
29 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::LambdaMST3DEstimator() :
30 myBegin(), myEnd(), dssSegments ( 0 ), myFunctor() {}
33 template < typename TSpace, typename TSegmentation, typename Functor >
36 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::init ( const ConstIterator& itb, const ConstIterator& ite )
42 template < typename TSpace, typename TSegmentation, typename Functor >
45 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::attach ( Alias<TSegmentation> segmentComputer )
47 dssSegments = &segmentComputer;
50 template < typename TSpace, typename TSegmentation, typename Functor >
53 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::isValid () const
55 return ( dssSegments != 0 );
58 template < typename TSpace, typename TSegmentation, typename Functor >
60 typename DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::RealVector
61 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::eval
62 ( const typename DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & it )
65 typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
66 typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
67 Value tangent, partial, prev;
69 for ( ; DSS != lastDSS; ++DSS )
71 if ( DSS->isInDSS ( *it ) )
73 unsigned int dssLen = std::distance ( DSS.begin(), DSS.end() ) + 1;
75 unsigned int pos = std::distance ( DSS.begin(), std::find ( DSS.begin(), DSS.end(), *it ) ) + 1;
76 SegmentComputer comp ( *DSS );
77 partial = myFunctor ( comp, pos, dssLen );
78 if ( prev.first.cosineSimilarity ( partial.first ) > M_PI_2 )
79 partial.first = -partial.first;
83 if ( tangent.second != 0. )
84 return tangent.first / tangent.second;
89 template < typename TSpace, typename TSegmentation, typename Functor >
90 template < typename OutputIterator >
93 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::eval ( const typename DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & itb,
94 const typename DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & ite,
95 OutputIterator result )
97 assert ( myBegin != myEnd && isValid() && myBegin <= itb && ite <= myEnd && itb != ite );
98 std::multimap < Point, Value > outValues;
99 dssSegments->setSubRange ( itb, ite );
100 typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
101 typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
103 for(; DSS != lastDSS; ++DSS)
105 unsigned int dssLen = std::distance ( DSS.begin(), DSS.end() );
106 SegmentComputer comp ( *DSS );
107 for ( unsigned int indexOfPointInDSS = 0; indexOfPointInDSS < dssLen; indexOfPointInDSS++ )
108 outValues.insert ( std::make_pair ( *(DSS.begin() + indexOfPointInDSS), myFunctor ( comp, indexOfPointInDSS+1, dssLen+1 ) ) );
110 accumulate< OutputIterator >( outValues, itb, ite, result );
114 template < typename TSpace, typename TSegmentation, typename Functor >
115 template <typename OutputIterator>
118 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::accumulate
119 ( std::multimap < Point, Value > & outValues, const typename DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & itb,
120 const typename DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & ite,
121 OutputIterator & result )
123 Value prev = outValues.begin()->second;
124 for ( ConstIterator itt = itb; itt != ite; ++itt )
126 typename std::multimap< Point, Value >::const_iterator it = outValues.lower_bound ( *itt );
127 typename std::multimap< Point, Value >::const_iterator it2 = outValues.upper_bound ( *itt );
129 for (; it != it2; it++ )
131 Value partial = it->second;
132 if ( prev.first.cosineSimilarity ( partial.first ) > M_PI_2 )
133 partial.first = -partial.first;
137 outValues.erase ( *itt );
138 if ( tangent.second != 0 )
139 *result++ = ( tangent.first / tangent.second );
141 *result++ = tangent.first;