DGtal  1.5.beta
ArithmeticalDSSCheck.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 ArithmeticalDSSCheck.ih
19  * @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2013/10/28
23  *
24  * Implementation of inline functions defined in ArithmeticalDSSCheck.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 
33 #include "ArithmeticalDSLKernel.h"
34 #include "DGtal/kernel/NumberTraits.h"
35 #include "DGtal/arithmetic/IntegerComputer.h"
36 //////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // Implementation of inline functions //
40 
41 
42 //-----------------------------------------------------------------------------
43 template <typename TCoordinate, typename TInteger, unsigned short adjacency>
44 inline
45 bool
46 DGtal::functions::
47 checkOnePoint(const ArithmeticalDSS<TCoordinate,TInteger,adjacency>& aDSS)
48 {
49  ASSERT(aDSS.front()==aDSS.back());
50 
51  //Vector type redefinition
52  typedef typename ArithmeticalDSS<TCoordinate,TInteger,adjacency>::Vector V;
53 
54  return ( (aDSS.a()==NumberTraits<TCoordinate>::ZERO)
55  &&(aDSS.b()==NumberTraits<TCoordinate>::ZERO)
56  &&(aDSS.mu()==NumberTraits<TInteger>::ZERO)
57  &&(aDSS.omega()==NumberTraits<TInteger>::ZERO)
58  &&(aDSS.Uf()==aDSS.back())
59  &&(aDSS.Lf()==aDSS.back())
60  &&(aDSS.Ul()==aDSS.front())
61  &&(aDSS.Ll()==aDSS.front())
62  &&(aDSS.shift()==V(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO))
63  &&(aDSS.steps().first==V(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO))
64  &&(aDSS.steps().second==V(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO)) );
65 }
66 
67 //-----------------------------------------------------------------------------
68 template <typename TCoordinate, typename TInteger, unsigned short adjacency>
69 inline
70 bool
71 DGtal::functions::
72 checkPointsRemainder(const ArithmeticalDSS<TCoordinate,TInteger,adjacency>& aDSS)
73 {
74  ASSERT(aDSS.front()!=aDSS.back());
75 
76  //'true' if valid, 'false' otherwise
77  bool isValid;
78 
79  if (aDSS.remainder(aDSS.Uf()) != aDSS.mu())
80  isValid = false;
81  else if (aDSS.remainder(aDSS.Ul()) != aDSS.mu())
82  isValid = false;
83  else if (aDSS.remainder(aDSS.Lf()) != aDSS.mu()+aDSS.omega()-1)
84  { isValid = false; std::cout << aDSS.remainder(aDSS.Lf()) << " " << aDSS.mu()+aDSS.omega()-1 << std::endl;}
85  else if (aDSS.remainder(aDSS.Ll()) != aDSS.mu()+aDSS.omega()-1)
86  { isValid = false; std::cout << aDSS.remainder(aDSS.Ll()) << " " << aDSS.mu()+aDSS.omega()-1 << std::endl;}
87  else if (aDSS.isInDSL(aDSS.front()))
88  isValid = true;
89  else if (aDSS.isInDSL(aDSS.back()))
90  isValid = true;
91  else
92  isValid = false;
93 
94  return isValid;
95 }
96 
97 //-----------------------------------------------------------------------------
98 template <typename TCoordinate, typename TInteger, unsigned short adjacency>
99 inline
100 bool
101 DGtal::functions::
102 checkPointsPosition(const ArithmeticalDSS<TCoordinate,TInteger,adjacency>& aDSS)
103 {
104  ASSERT(aDSS.front()!=aDSS.back());
105 
106  //Vector type redefinition
107  typedef typename ArithmeticalDSS<TCoordinate,TInteger,adjacency>::Vector V;
108  V v(aDSS.b(), aDSS.a());
109  //Length of direction vector
110  TInteger lv = aDSS.position(v);
111 
112  //Position of end points and leaning points
113  TInteger posBack = aDSS.position(aDSS.back());
114  TInteger posUf = aDSS.position(aDSS.Uf());
115  TInteger posLf = aDSS.position(aDSS.Lf());
116  TInteger posUl = aDSS.position(aDSS.Ul());
117  TInteger posLl = aDSS.position(aDSS.Ll());
118  TInteger posFront = aDSS.position(aDSS.front());
119 
120  bool isValid = ( ((posUf - posBack) < lv)
121  && ((posLf - posBack) < lv)
122  && ((posFront - posUl) < lv)
123  && ((posFront - posLl) < lv)
124  && ((posUl - posUf)%lv == 0)
125  && ( ( ((posUl - posUf) >= 0) && (lv >= 0) )
126  || ( ((posUl - posUf) < 0) && (lv < 0) ) )
127  && ((posLl - posLf)%lv == 0)
128  && ( ( ((posLl - posLf) >= 0) && (lv >= 0) )
129  || ( ((posLl - posLf) < 0) && (lv < 0) ) )
130  );
131 
132  return isValid;
133 }
134 
135 //-----------------------------------------------------------------------------
136 template <typename TCoordinate, typename TInteger, unsigned short adjacency>
137 inline
138 bool
139 DGtal::functions::
140 checkAll(const ArithmeticalDSS<TCoordinate,TInteger,adjacency>& aDSS)
141 {
142  //'true' if valid, 'false' otherwise
143  bool isValid;
144 
145  //special case with one point
146  if (aDSS.front() == aDSS.back())
147  isValid = checkOnePoint(aDSS);
148 
149  //otherwise
150  else
151  {
152 
153  if (!aDSS.dsl().isValid())
154  isValid = false;
155  else if (!checkPointsRemainder(aDSS))
156  isValid = false;
157  else if (!checkPointsPosition(aDSS))
158  isValid = false;
159  else
160  isValid = true;
161 
162  }
163 
164  return isValid;
165 }
166 
167 // //
168 ///////////////////////////////////////////////////////////////////////////////
169 
170