DGtal 1.3.0
Loading...
Searching...
No Matches
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// -----------------------------------------------------
41template <typename TP, typename TD, typename TS>
42inline
43DGtal::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// -----------------------------------------------------
83template <typename TP, typename TD, typename TS>
84inline
85DGtal::KanungoNoise<TP,TD, TS>::~KanungoNoise()
86{
87 delete mySet;
88}
89// -----------------------------------------------------
90template <typename TP, typename TD, typename TS>
91inline
92DGtal::KanungoNoise<TP,TD, TS> &
93DGtal::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// --------------------------------------------- --------
107template <typename TP, typename TD, typename TS>
108inline
109bool
110DGtal::KanungoNoise<TP,TD, TS>::operator()(const Point &aPoint) const
111{
112 return mySet->operator()(aPoint);
113}
114// -----------------------------------------------------
115template <typename TP, typename TD, typename TS>
116inline
117void
118DGtal::KanungoNoise<TP,TD, TS>::selfDisplay ( std::ostream & out ) const
119{
120 out << "[KanungoNoise] Alpha="<<myAlpha<<" Set "<< *mySet;
121}
122// -----------------------------------------------------
123template <typename TP, typename TD, typename TS>
124inline
125bool
126DGtal::KanungoNoise<TP,TD, TS>::isValid() const
127{
128 return mySet->isValid();
129}
130// -----------------------------------------------------
131template <typename TP, typename TD, typename TS>
132inline
133std::ostream&
134DGtal::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