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.
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.
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/>.
18 * @file ArithmeticalDSLKernel.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
24 * Implementation of inline methods defined in ArithmeticalDSLKernel.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////////////////////////
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------------------------------------------------------------
40template <typename TCoordinate, unsigned short adjacency>
41template <typename TInteger>
43typename DGtal::ArithmeticalDSLKernel<TCoordinate,adjacency>::Vector
44DGtal::ArithmeticalDSLKernel<TCoordinate,adjacency>
45::shift(const TInteger& a, const TInteger& b)
48 if (b == NumberTraits<TInteger>::ZERO)
50 if (a == NumberTraits<TInteger>::ZERO) //no octant
51 res = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
52 else if (a > NumberTraits<TInteger>::ZERO) //octant 2
53 res = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
54 else // (a < 0) //octant 6
55 res = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
57 else if (b > NumberTraits<TInteger>::ZERO)
59 if (a == NumberTraits<TInteger>::ZERO) //octant 0
60 res = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
61 else if (a > NumberTraits<TInteger>::ZERO)
64 res = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
66 res = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
70 if (b >= -a) //octant 7
71 res = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
73 res = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
78 if (a == NumberTraits<TInteger>::ZERO) //octant 4
79 res = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
80 else if (a > NumberTraits<TInteger>::ZERO)
82 if (-b >= a) //octant 3
83 res = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
85 res = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
89 if (-b > -a) //octant 4
90 res = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
92 res = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
98// ----------------------------------------------------------------------------
99template <typename TCoordinate, unsigned short adjacency>
100template <typename TInteger>
102typename DGtal::ArithmeticalDSLKernel<TCoordinate,adjacency>::Steps
103DGtal::ArithmeticalDSLKernel<TCoordinate,adjacency>
104::steps(const TInteger& a, const TInteger& b)
107 if (b == NumberTraits<TInteger>::ZERO)
109 if (a == NumberTraits<TInteger>::ZERO)
111 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
112 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
114 else if (a > NumberTraits<TInteger>::ZERO)
116 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
117 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
121 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
122 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
125 else if (b > NumberTraits<TInteger>::ZERO)
127 if (a == NumberTraits<TInteger>::ZERO)
129 steps.first = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
130 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
132 else if (a > NumberTraits<TInteger>::ZERO)
136 steps.first = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
137 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
141 steps.first = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
142 steps.second = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
146 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
147 steps.first = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
154 steps.first = Vector(NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
155 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
159 steps.second = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
160 steps.first = Vector(NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
164 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
165 steps.second = Vector(NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
171 if (a == NumberTraits<TInteger>::ZERO)
173 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
174 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
176 else if (a > NumberTraits<TInteger>::ZERO)
180 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
181 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
185 steps.second = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
186 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
190 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
191 steps.second = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
198 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
199 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
203 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
204 steps.second = Vector(-NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
208 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
209 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
216// ----------------------------------------------------------------------------
217template <typename TCoordinate, unsigned short adjacency>
218template <typename TInteger>
221DGtal::ArithmeticalDSLKernel<TCoordinate,adjacency>
222::norm(const TInteger& a, const TInteger& b)
224 if (b >= NumberTraits<TInteger>::ZERO)
226 if (a >= NumberTraits<TInteger>::ZERO)
243 if (a >= NumberTraits<TInteger>::ZERO)
260// ----------------------------------------------------------------------------
261template <typename TCoordinate>
262template <typename TInteger>
264typename DGtal::ArithmeticalDSLKernel<TCoordinate,4>::Vector
265DGtal::ArithmeticalDSLKernel<TCoordinate,4>
266::shift(const TInteger& a, const TInteger& b)
269 if (b == NumberTraits<TInteger>::ZERO)
271 if (a == NumberTraits<TInteger>::ZERO)
273 res = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
275 else if (a > NumberTraits<TInteger>::ZERO)
277 res = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
281 res = Vector(-NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
284 else if (b > NumberTraits<TInteger>::ZERO)
286 if (a >= NumberTraits<TInteger>::ZERO)
288 res = Vector(NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
292 res = Vector(-NumberTraits<TCoordinate>::ONE,-NumberTraits<TCoordinate>::ONE);
297 if (a > NumberTraits<TInteger>::ZERO)
299 res = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
303 res = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ONE);
309// ----------------------------------------------------------------------------
310template <typename TCoordinate>
311template <typename TInteger>
313typename DGtal::ArithmeticalDSLKernel<TCoordinate,4>::Steps
314DGtal::ArithmeticalDSLKernel<TCoordinate,4>
315::steps(const TInteger& a, const TInteger& b)
318 if (b == NumberTraits<TInteger>::ZERO)
320 if (a == NumberTraits<TInteger>::ZERO)
322 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
323 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
325 else if (a > NumberTraits<TInteger>::ZERO)
327 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
328 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
332 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
333 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
336 else if (b > NumberTraits<TInteger>::ZERO)
338 if (a == NumberTraits<TInteger>::ZERO)
340 steps.first = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
341 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
343 else if (a > NumberTraits<TInteger>::ZERO)
345 steps.first = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
346 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
350 steps.second = Vector(NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
351 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
356 if (a == NumberTraits<TInteger>::ZERO)
358 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
359 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ZERO);
361 else if (a > NumberTraits<TInteger>::ZERO)
363 steps.second = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
364 steps.first = Vector(NumberTraits<TCoordinate>::ZERO,NumberTraits<TCoordinate>::ONE);
368 steps.first = Vector(-NumberTraits<TCoordinate>::ONE,NumberTraits<TCoordinate>::ZERO);
369 steps.second = Vector(NumberTraits<TCoordinate>::ZERO,-NumberTraits<TCoordinate>::ONE);
375// ----------------------------------------------------------------------------
376template <typename TCoordinate>
377template <typename TInteger>
380DGtal::ArithmeticalDSLKernel<TCoordinate,4>
381::norm(const TInteger& a, const TInteger& b)
383 if (b >= NumberTraits<TInteger>::ZERO)
385 if (a >= NumberTraits<TInteger>::ZERO)
392 if (a >= NumberTraits<TInteger>::ZERO)