DGtal  1.5.beta
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, typename Index >
123 inline
124 DGtal::detail::PointOnProbingRay<Integer,Index>::
125 PointOnProbingRay (Permutation const& aSigma, Integer const& aPosition)
126  : mySigma(aSigma), myPosition(aPosition)
127 {
128 }
129 
130 
131 // ------------------------------------------------------------------------
132 template < typename Integer, typename Index >
133 inline
134 DGtal::detail::PointOnProbingRay<Integer,Index>
135 DGtal::detail::PointOnProbingRay<Integer,Index>::getBase () const
136 {
137  return PointOnProbingRay(mySigma, 0);
138 }
139 
140 // ------------------------------------------------------------------------
141 template < typename Integer, typename Index >
142 inline
143 typename DGtal::detail::PointOnProbingRay<Integer,Index>::Permutation const&
144 DGtal::detail::PointOnProbingRay<Integer,Index>::sigma () const
145 {
146  return mySigma;
147 }
148 
149 // ------------------------------------------------------------------------
150 template < typename Integer, typename Index >
151 inline
152 Index
153 DGtal::detail::PointOnProbingRay<Integer,Index>::sigma (Index const& aIndex) const
154 {
155  assert(aIndex >= 0 && aIndex <= 2);
156  return mySigma[aIndex];
157 }
158 
159 // ------------------------------------------------------------------------
160 template < typename Integer, typename Index >
161 inline
162 Integer const&
163 DGtal::detail::PointOnProbingRay<Integer,Index>::position () const
164 {
165  return myPosition;
166 }
167 
168 // ------------------------------------------------------------------------
169 template < typename Integer, typename Index >
170 template < typename Point >
171 inline
172 Point
173 DGtal::detail::PointOnProbingRay<Integer,Index>::relativePoint (std::array<Point, 3> const& aM) const {
174  return -aM[mySigma[0]] + aM[mySigma[1]] + aM[mySigma[2]] * myPosition;
175 }
176 
177 // ------------------------------------------------------------------------
178 template < typename Integer, typename Index >
179 inline
180 bool
181 DGtal::detail::PointOnProbingRay<Integer,Index>::operator== (PointOnProbingRay const& aRay) const
182 {
183  return (mySigma == aRay.mySigma) && (myPosition == aRay.position());
184 }
185 
186 // ------------------------------------------------------------------------
187 template < typename Integer, typename Index >
188 inline
189 bool
190 DGtal::detail::PointOnProbingRay<Integer,Index>::operator!= (PointOnProbingRay const& aRay) const
191 {
192  return !(*this == aRay);
193 }
194 
195 // ------------------------------------------------------------------------
196 template < typename Integer, typename Index >
197 inline
198 bool
199 DGtal::detail::PointOnProbingRay<Integer,Index>::operator<= (PointOnProbingRay const& aRay) const
200 {
201  return (mySigma == aRay.mySigma) && (myPosition <= aRay.position());
202 }
203 
204 // ------------------------------------------------------------------------
205 template < typename Integer, typename Index >
206 inline
207 DGtal::detail::PointOnProbingRay<Integer,Index>
208 DGtal::detail::PointOnProbingRay<Integer,Index>::next (Integer const& aInc) const
209 {
210  return PointOnProbingRay(mySigma, myPosition + aInc);
211 }
212 
213 // ------------------------------------------------------------------------
214 template < typename Integer, typename Index >
215 inline
216 DGtal::detail::PointOnProbingRay<Integer,Index>
217 DGtal::detail::PointOnProbingRay<Integer,Index>::previous (Integer const& aDec) const
218 {
219  return PointOnProbingRay(mySigma, myPosition - aDec);
220 }
221 
222 // ------------------------------------------------------------------------
223 template < typename Integer, typename Index >
224 inline
225 std::ostream&
226 DGtal::detail::operator<< (std::ostream& aOs, PointOnProbingRay<Integer,Index> const& aRay)
227 {
228  aOs << "sigma=(" <<
229  aRay.sigma(0) << ", " <<
230  aRay.sigma(1) << ", " <<
231  aRay.sigma(2) << "); i=" << aRay.position();
232  return aOs;
233 }