DGtal  0.9.2
KanungoNoise.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 KanungoNoise.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2013/03/06
23  *
24  * Implementation of inline methods defined in KanungoNoise.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <random>
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // -----------------------------------------------------
41 template <typename TP, typename TD, typename TS>
42 inline
43 DGtal::KanungoNoise<TP,TD, TS>::KanungoNoise(ConstAlias<TP> aPredicate, ConstAlias<Domain> aDomain, const double alpha):
44  myPredicate(aPredicate), myDomain(aDomain), myAlpha(alpha)
45 {
46  std::mt19937 rng;
47  std::uniform_real_distribution<double> dis(0.0, 1.0);
48 
49  ASSERT(alpha>0 && alpha < 1);
50  double p;
51 
52  //We copy the point set
53  mySet = new DigitalSet( new Domain( aDomain ) );
54 
55  typedef ExactPredicateLpSeparableMetric< typename Domain::Space, 2> L2;
56  typedef DistanceTransformation< typename Domain::Space, PointPredicate, L2> DTPredicate;
57  typedef DistanceTransformation< typename Domain::Space, functors::NotPointPredicate<PointPredicate> , L2> DTNotPredicate;
58 
59  //DT computation for l2metric
60  L2 l2;
61  functors::NotPointPredicate<PointPredicate> negPred(myPredicate);
62 
63  DTPredicate DTin(myDomain, myPredicate, l2);
64  DTNotPredicate DTout(myDomain, negPred, l2);
65 
66  for(typename Domain::ConstIterator it = myDomain.begin(), itend = myDomain.end();
67  it != itend; ++it)
68  {
69  p = dis(rng);
70  if ( myPredicate(*it) )
71  {
72  if ( p >= std::pow( alpha, 1.0+DTin(*it) ) )
73  mySet->insertNew( *it );
74  }
75  else
76  {
77  if ( p < std::pow( alpha, 1.0+DTout(*it) ) )
78  mySet->insertNew( *it );
79  }
80  }
81 }
82 // -----------------------------------------------------
83 template <typename TP, typename TD, typename TS>
84 inline
85 DGtal::KanungoNoise<TP,TD, TS>::~KanungoNoise()
86 {
87  delete mySet;
88 }
89 // -----------------------------------------------------
90 template <typename TP, typename TD, typename TS>
91 inline
92 DGtal::KanungoNoise<TP,TD, TS> &
93 DGtal::KanungoNoise<TP,TD, TS>::operator=( const KanungoNoise<TP,TD,TS> &other)
94 {
95  ASSERT( ( myDomain.lowerBound() <= other.myDomain.lowerBound() )
96  && ( myDomain.upperBound() >= other.myDomain.upperBound() )
97  && "This domain should include the domain of the other set in case of assignment." );
98 
99  //We do not copy the predicate
100 
101  myAlpha = other.myAlpha;
102  mySet = other.mySet;
103 
104  return *this;
105 }
106 // --------------------------------------------- --------
107 template <typename TP, typename TD, typename TS>
108 inline
109 bool
110 DGtal::KanungoNoise<TP,TD, TS>::operator()(const Point &aPoint) const
111 {
112  return mySet->operator()(aPoint);
113 }
114 // -----------------------------------------------------
115 template <typename TP, typename TD, typename TS>
116 inline
117 void
118 DGtal::KanungoNoise<TP,TD, TS>::selfDisplay ( std::ostream & out ) const
119 {
120  out << "[KanungoNoise] Alpha="<<myAlpha<<" Set "<< *mySet;
121 }
122 // -----------------------------------------------------
123 template <typename TP, typename TD, typename TS>
124 inline
125 bool
126 DGtal::KanungoNoise<TP,TD, TS>::isValid() const
127 {
128  return mySet->isValid();
129 }
130 // -----------------------------------------------------
131 template <typename TP, typename TD, typename TS>
132 inline
133 std::ostream&
134 DGtal::operator<< ( std::ostream & out,
135  const KanungoNoise<TP,TD, TS> & object )
136 {
137  object.selfDisplay( out );
138  return out;
139 }
140 
141 // //
142 ///////////////////////////////////////////////////////////////////////////////
143 
144