DGtal  1.5.beta
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 namespace DGtal
28 {
29 
30  template < typename TSpace, typename TSegmentation, typename Functor >
31  inline
32  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::LambdaMST2DEstimator() : dssSegments ( 0 ) {}
33 
34  template < typename TSpace, typename TSegmentation, typename Functor >
35  inline
36  void
37  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::init ( ConstIterator itb, ConstIterator ite )
38  {
39  myBegin = itb;
40  myEnd = ite;
41  }
42 
43  template < typename TSpace, typename TSegmentation, typename Functor >
44  inline
45  void
46  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::attach ( Alias<TSegmentation> segmentComputer )
47  {
48  dssSegments = &segmentComputer;
49  }
50 
51  template < typename TSpace, typename TSegmentation, typename Functor >
52  inline
53  bool
54  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::isValid () const
55  {
56  return ( dssSegments != 0 );
57  }
58 
59  template < typename TSpace, typename TSegmentation, typename Functor >
60  inline
61  typename TSpace::RealVector
62  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::eval ( const Point & p )
63  {
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 )
69  {
70  if ( DSS->isInDSS ( p ) )
71  {
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 );
76  tangent += partial;
77  }
78  }
79  if ( tangent.second != 0. )
80  return tangent.first / tangent.second;
81  else
82  return tangent.first;
83  }
84 
85  template < typename TSpace, typename TSegmentation, typename Functor >
86  template <typename OutputIterator>
87  inline
88  OutputIterator
89  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::eval ( ConstIterator itb, ConstIterator ite,
90  OutputIterator result )
91  {
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 )
98  {
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 );
103  }
104  accumulate ( outValues, result );
105  return result;
106  }
107 
108  template < typename TSpace, typename TSegmentation, typename Functor >
109  template <typename OutputIterator>
110  inline
111  void
112  LambdaMST2DEstimator< TSpace, TSegmentation, Functor >::accumulate ( std::vector < Value > & outValues, OutputIterator & result )
113  {
114  for ( unsigned int i = 0; i < outValues.size(); i++ )
115  {
116  Value & tangent = outValues[i];
117  if ( tangent.second != 0. )
118  *result++ = ( tangent.first / tangent.second );
119  else
120  *result++ = tangent.first;
121  }
122  }
123 }