DGtal  0.9.2
LambdaMST2D.ih
1 /**
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.
6  *
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.
11  *
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/>.
14  *
15  **/
16 
17 /**
18  * @file LambdaMST2D.ih
19  * @author Kacper Pluta (\c kacper.pluta@esiee.fr )
20  * Laboratoire d'Informatique Gaspard-Monge - LIGM, France
21  *
22  * @date 2014/10/03
23  *
24  * This file is part of the DGtal library.
25  */
26 
27 template < typename TSpace, typename TSegmentation, typename Functor >
28 inline
29 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::LambdaMST2DEstimator() : dssSegments ( 0 ) {}
30 
31 template < typename TSpace, typename TSegmentation, typename Functor >
32 inline
33 void
34 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::init ( const ConstIterator & itb, const ConstIterator & ite )
35 {
36  myBegin = itb;
37  myEnd = ite;
38 }
39 
40 template < typename TSpace, typename TSegmentation, typename Functor >
41 inline
42 void
43 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::attach ( Alias<TSegmentation> segmentComputer )
44 {
45  dssSegments = &segmentComputer;
46 }
47 
48 template < typename TSpace, typename TSegmentation, typename Functor >
49 inline
50 bool
51 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::isValid () const
52 {
53  return ( dssSegments != 0 );
54 }
55 
56 template < typename TSpace, typename TSegmentation, typename Functor >
57 inline
58 typename TSpace::RealVector
59 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::eval( const typename DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & it )
60 {
61  assert ( dssSegments != 0 );
62  typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
63  typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
64  Value tangent, partial;
65  for ( ; DSS != lastDSS; ++DSS )
66  {
67  if ( DSS->isInDSS ( *it ) )
68  {
69  unsigned int pos = std::distance ( DSS.begin(), std::find ( DSS.begin(), DSS.end(), *it ) );
70  unsigned int dssLen = std::distance ( DSS.begin(), DSS.end() );
71  SegmentComputer comp ( *DSS );
72  partial = myFunctor ( comp, pos, dssLen );
73  tangent += partial;
74  }
75  }
76  if ( tangent.second != 0. )
77  return tangent.first / tangent.second;
78  else
79  return tangent.first;
80 }
81 
82 template < typename TSpace, typename TSegmentation, typename Functor >
83 template <typename OutputIterator>
84 inline
85 OutputIterator
86 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::eval ( const typename DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & itb,
87  const typename DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::ConstIterator & ite,
88  OutputIterator result )
89 {
90  assert ( myBegin != myEnd && isValid() && myBegin <= itb && ite <= myEnd && itb != ite );
91  dssSegments->setSubRange ( itb, ite );
92  std::vector < Value > outValues ( std::distance ( itb, ite ) );
93  typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
94  typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
95  for ( ; DSS != lastDSS; ++DSS )
96  {
97  unsigned int dssLen = std::distance ( DSS.begin(), DSS.end() );
98  SegmentComputer comp ( *DSS );
99  for ( unsigned int i = 0; i < dssLen; i++ )
100  outValues[ std::distance ( itb, DSS.begin() ) + i ] += myFunctor ( comp, i, dssLen );
101  }
102  accumulate ( outValues, result );
103  return result;
104 }
105 
106 template < typename TSpace, typename TSegmentation, typename Functor >
107 template <typename OutputIterator>
108 inline
109 void
110 DGtal::LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::accumulate ( std::vector < Value > & outValues, OutputIterator & result )
111 {
112  for ( unsigned int i = 0; i < outValues.size(); i++ )
113  {
114  Value & tangent = outValues[i];
115  if ( tangent.second != 0. )
116  *result++ = ( tangent.first / tangent.second );
117  else
118  *result++ = tangent.first;
119  }
120 }