DGtal  0.9.2
LambdaMST3D.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 LambdaMST3D.ih
19  * @author Kacper Pluta (\c kacper.pluta@esiee.fr )
20  * Laboratoire d'Informatique Gaspard-Monge - LIGM, France
21  *
22  * @date 2014/10/06
23  *
24  * This file is part of the DGtal library.
25  */
26 
27 template < typename TSpace, typename TSegmentation, typename Functor >
28 inline
29 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::LambdaMST3DEstimator() :
30 myBegin(), myEnd(), dssSegments ( 0 ), myFunctor() {}
31 
32 
33 template < typename TSpace, typename TSegmentation, typename Functor >
34 inline
35 void
36 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::init ( const ConstIterator& itb, const ConstIterator& ite )
37 {
38  myBegin = itb;
39  myEnd = ite;
40 }
41 
42 template < typename TSpace, typename TSegmentation, typename Functor >
43 inline
44 void
45 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::attach ( Alias<TSegmentation> segmentComputer )
46 {
47  dssSegments = &segmentComputer;
48 }
49 
50 template < typename TSpace, typename TSegmentation, typename Functor >
51 inline
52 bool
53 DGtal::LambdaMST3DEstimator< TSpace, TSegmentation, Functor >::isValid () const
54 {
55  return ( dssSegments != 0 );
56 }
57 
58 template < typename TSpace, typename TSegmentation, typename Functor >
59 inline
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 )
63 {
64  assert ( isValid() );
65  typename TSegmentation::SegmentComputerIterator DSS = dssSegments->begin();
66  typename TSegmentation::SegmentComputerIterator lastDSS = dssSegments->end();
67  Value tangent, partial, prev;
68 
69  for ( ; DSS != lastDSS; ++DSS )
70  {
71  if ( DSS->isInDSS ( *it ) )
72  {
73  unsigned int dssLen = std::distance ( DSS.begin(), DSS.end() ) + 1;
74  prev = partial;
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;
80  tangent += partial;
81  }
82  }
83  if ( tangent.second != 0. )
84  return tangent.first / tangent.second;
85  else
86  return tangent.first;
87 }
88 
89 template < typename TSpace, typename TSegmentation, typename Functor >
90 template < typename OutputIterator >
91 inline
92 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 )
96 {
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();
102 
103  for(; DSS != lastDSS; ++DSS)
104  {
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 ) ) );
109  }
110  accumulate< OutputIterator >( outValues, itb, ite, result );
111  return result;
112 }
113 
114 template < typename TSpace, typename TSegmentation, typename Functor >
115 template <typename OutputIterator>
116 inline
117 void
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 )
122 {
123  Value prev = outValues.begin()->second;
124  for ( ConstIterator itt = itb; itt != ite; ++itt )
125  {
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 );
128  Value tangent;
129  for (; it != it2; it++ )
130  {
131  Value partial = it->second;
132  if ( prev.first.cosineSimilarity ( partial.first ) > M_PI_2 )
133  partial.first = -partial.first;
134  prev = partial;
135  tangent += partial;
136  }
137  outValues.erase ( *itt );
138  if ( tangent.second != 0 )
139  *result++ = ( tangent.first / tangent.second );
140  else
141  *result++ = tangent.first;
142  }
143 }