DGtal  1.2.0
PlaneProbingEstimatorHelper.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
19  * @author Jocelyn Meyron (\c jocelyn.meyron@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2020/09/15
23  *
24  * Implementation of inline methods defined in PlaneProbingEstimatorHelper.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline functions.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 // ------------------------------------------------------------------------
39 template < typename Point >
40 inline
41 typename Point::Coordinate
42 DGtal::detail::squaredNorm (Point const& aPoint)
43 {
44  using Integer = typename Point::Coordinate;
45  Integer res = DGtal::NumberTraits<Integer>::ZERO;
46 
47  for (typename Point::Dimension i = 0; i < aPoint.size(); ++i)
48  {
49  res += aPoint[i] * aPoint[i];
50  }
51 
52  return res;
53 }
54 
55 // ------------------------------------------------------------------------
56 template < int N, typename T >
57 inline
58 T
59 DGtal::detail::determinant (const T aMatrix[N][N])
60 {
61  DGtal::SimpleMatrix<T, N, N> m;
62 
63  for (int i = 0; i < N; ++i)
64  {
65  for (int j = 0; j < N; ++j)
66  {
67  m.setComponent(i, j, aMatrix[i][j]);
68  }
69  }
70 
71  return m.determinant();
72 }
73 
74 // ------------------------------------------------------------------------
75 template < typename Point >
76 inline
77 typename Point::Coordinate
78 DGtal::detail::distToSphere (std::array<Point, 5> const& aPoints)
79 {
80  using Integer = typename Point::Coordinate;
81  Integer one = DGtal::NumberTraits<Integer>::ONE,
82  zero = DGtal::NumberTraits<Integer>::ZERO;
83 
84  Integer M0[4][4] = { { aPoints[0][0], aPoints[0][1], aPoints[0][2], one },
85  { aPoints[1][0], aPoints[1][1], aPoints[1][2], one },
86  { aPoints[2][0], aPoints[2][1], aPoints[2][2], one },
87  { aPoints[3][0], aPoints[3][1], aPoints[3][2], one } };
88 
89  if ( DGtal::detail::determinant<4, Integer>(M0) == zero)
90  {
91  throw std::runtime_error("4 coplanar points in distToSphere");
92  }
93  Integer M[5][5] = { { aPoints[0][0], aPoints[0][1], aPoints[0][2], squaredNorm(aPoints[0]), one },
94  { aPoints[1][0], aPoints[1][1], aPoints[1][2], squaredNorm(aPoints[1]), one },
95  { aPoints[2][0], aPoints[2][1], aPoints[2][2], squaredNorm(aPoints[2]), one },
96  { aPoints[3][0], aPoints[3][1], aPoints[3][2], squaredNorm(aPoints[3]), one },
97  { aPoints[4][0], aPoints[4][1], aPoints[4][2], squaredNorm(aPoints[4]), one } };
98  return DGtal::detail::determinant<5, Integer>(M);
99 }
100 
101 // ------------------------------------------------------------------------
102 template < typename Point >
103 inline
104 bool
105 DGtal::detail::isBasisReduced (Point const& aU, Point const& aV)
106 {
107  Point w = aU + aV, x = aU - aV;
108  return (squaredNorm(aU) <= squaredNorm(w)) &&
109  (squaredNorm(aU) <= squaredNorm(x)) &&
110  (squaredNorm(aV) <= squaredNorm(w)) &&
111  (squaredNorm(aV) <= squaredNorm(x));
112 }
113 
114 ///////////////////////////////////////////////////////////////////////////////
115 // IMPLEMENTATION of inline methods.
116 ///////////////////////////////////////////////////////////////////////////////
117 
118 ///////////////////////////////////////////////////////////////////////////////
119 // ----------------------- Standard services ------------------------------
120 
121 // ------------------------------------------------------------------------
122 template < typename Integer >
123 inline
124 DGtal::detail::PointOnProbingRay<Integer>::
125 PointOnProbingRay (Permutation const& aSigma, Integer const& aIndex)
126  : mySigma(aSigma), myIndex(aIndex)
127 {
128 }
129 
130 
131 // ------------------------------------------------------------------------
132 template < typename Integer >
133 inline
134 DGtal::detail::PointOnProbingRay<Integer>
135 DGtal::detail::PointOnProbingRay<Integer>::getBase () const
136 {
137  return PointOnProbingRay(mySigma, 0);
138 }
139 
140 // ------------------------------------------------------------------------
141 template < typename Integer >
142 inline
143 typename DGtal::detail::PointOnProbingRay<Integer>::Permutation const&
144 DGtal::detail::PointOnProbingRay<Integer>::sigma () const
145 {
146  return mySigma;
147 }
148 
149 // ------------------------------------------------------------------------
150 template < typename Integer >
151 inline
152 int
153 DGtal::detail::PointOnProbingRay<Integer>::sigma (int aIndex) const
154 {
155  assert(aIndex >= 0 && aIndex <= 2);
156  return mySigma[aIndex];
157 }
158 
159 // ------------------------------------------------------------------------
160 template < typename Integer >
161 inline
162 Integer const&
163 DGtal::detail::PointOnProbingRay<Integer>::index () const
164 {
165  return myIndex;
166 }
167 
168 // ------------------------------------------------------------------------
169 template < typename Integer >
170 inline
171 bool
172 DGtal::detail::PointOnProbingRay<Integer>::operator== (PointOnProbingRay const& aRay) const
173 {
174  return (mySigma == aRay.mySigma) && (myIndex == aRay.index());
175 }
176 
177 
178 // ------------------------------------------------------------------------
179 template < typename Integer >
180 inline
181 bool
182 DGtal::detail::PointOnProbingRay<Integer>::operator!= (PointOnProbingRay const& aRay) const
183 {
184  return !(*this == aRay);
185 }
186 
187 // ------------------------------------------------------------------------
188 template < typename Integer >
189 inline
190 bool
191 DGtal::detail::PointOnProbingRay<Integer>::operator<= (PointOnProbingRay const& aRay) const
192 {
193  return (mySigma == aRay.mySigma) && (myIndex <= aRay.index());
194 }
195 
196 // ------------------------------------------------------------------------
197 template < typename Integer >
198 inline
199 DGtal::detail::PointOnProbingRay<Integer>
200 DGtal::detail::PointOnProbingRay<Integer>::next (Integer const& aInc) const
201 {
202  return PointOnProbingRay(mySigma, myIndex + aInc);
203 }
204 
205 // ------------------------------------------------------------------------
206 template < typename Integer >
207 inline
208 DGtal::detail::PointOnProbingRay<Integer>
209 DGtal::detail::PointOnProbingRay<Integer>::previous (Integer const& aDec) const
210 {
211  return PointOnProbingRay(mySigma, myIndex - aDec);
212 }
213 
214 // ------------------------------------------------------------------------
215 template < typename Integer >
216 inline
217 std::ostream&
218 DGtal::detail::operator<< (std::ostream& aOs, PointOnProbingRay<Integer> const& aRay)
219 {
220  aOs << "sigma=(" <<
221  aRay.sigma(0) << ", " <<
222  aRay.sigma(1) << ", " <<
223  aRay.sigma(2) << "); i=" << aRay.index();
224  return aOs;
225 }