DGtal  1.5.beta
PlaneProbingParallelepipedEstimator.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/12/07
23  *
24  * Implementation of inline methods defined in PlaneProbingParallelepipedEstimator.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 methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 
42 // ------------------------------------------------------------------------
43 template < typename TPredicate, DGtal::ProbingMode mode >
44 inline
45 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::
46 PlaneProbingParallelepipedEstimator (Point const& aPoint, Triangle const& aM,
47  Predicate const& aPredicate, Integer const& aBound)
48  : myNotAbovePredicate(aPredicate, aBound, this),
49  myTetrahedronEstimator(aPoint, aM, myNotAbovePredicate)
50 {
51  ASSERT(isValid());
52  myIsInReverseState = getState() < 4;
53 }
54 
55 ///////////////////////////////////////////////////////////////////////////////
56 // ----------------------- Plane Probing services ------------------------------
57 
58 // ------------------------------------------------------------------------
59 template < typename TPredicate, DGtal::ProbingMode mode >
60 inline
61 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
62 NotAbovePredicate (Predicate const& aPredicate, Integer const& aBound,
63  Self* aParallelepipedEstimator)
64  : myPredicate(&aPredicate), myBound(aBound), myParallelpipedEstimator(aParallelepipedEstimator)
65 {
66  ASSERT(myPredicate != nullptr);
67  ASSERT(myParallelpipedEstimator != nullptr);
68 }
69 
70 // ------------------------------------------------------------------------
71 template < typename TPredicate, DGtal::ProbingMode mode >
72 inline
73 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate&
74 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
75 operator=(const NotAbovePredicate & other)
76 {
77  if (this != &other)
78  {
79  myPredicate = other.myPredicate;
80  myBound = other.myBound;
81  myParallelpipedEstimator = other.myParallelpipedEstimator;
82  }
83 
84  return *this;
85 }
86 
87 // ------------------------------------------------------------------------
88 template < typename TPredicate, DGtal::ProbingMode mode >
89 inline
90 bool
91 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
92 inPlane (Point const& aPoint) const
93 {
94  return (*myPredicate)(aPoint);
95 }
96 
97 // ------------------------------------------------------------------------
98 template < typename TPredicate, DGtal::ProbingMode mode >
99 inline
100 bool
101 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
102 operator() (Point const& aPoint) const
103 {
104  Point u = aPoint - q(), s = q();
105 
106  ASSERT(! inPlane(s));
107 
108  Integer l = DGtal::NumberTraits<Integer>::ONE;
109 
110  while (l < myBound)
111  {
112  if (inPlane(s + u * l))
113  {
114  return true;
115  }
116 
117  if (inPlane(s - u * l))
118  {
119  return false;
120  }
121 
122  l *= 2;
123  }
124 
125  return false;
126 }
127 
128 // ------------------------------------------------------------------------
129 template < typename TPredicate, DGtal::ProbingMode mode >
130 inline
131 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
132 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
133 q () const
134 {
135  ASSERT(myParallelpipedEstimator != nullptr);
136  return myParallelpipedEstimator->q();
137 }
138 
139 // ------------------------------------------------------------------------
140 template < typename TPredicate, DGtal::ProbingMode mode >
141 inline
142 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
143 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::q () const
144 {
145  return myTetrahedronEstimator.q();
146 }
147 
148 // ------------------------------------------------------------------------
149 template < typename TPredicate, DGtal::ProbingMode mode >
150 inline
151 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
152 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::m (int aIndex) const
153 {
154  return myTetrahedronEstimator.m(aIndex);
155 }
156 
157 // ------------------------------------------------------------------------
158 template < typename TPredicate, DGtal::ProbingMode mode >
159 inline
160 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
161 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getOrigin () const
162 {
163  return myTetrahedronEstimator.q() - myTetrahedronEstimator.m(0) - myTetrahedronEstimator.m(1) - myTetrahedronEstimator.m(2);
164 }
165 
166 // ------------------------------------------------------------------------
167 template < typename TPredicate, DGtal::ProbingMode mode >
168 inline
169 bool
170 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isSeparating () const
171 {
172  return myNotAbovePredicate.inPlane(getOrigin()) != myNotAbovePredicate.inPlane(q());
173 }
174 
175 // ------------------------------------------------------------------------
176 template < typename TPredicate, DGtal::ProbingMode mode >
177 inline
178 bool
179 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isInReverseState () const
180 {
181  return myIsInReverseState;
182 }
183 
184 // ------------------------------------------------------------------------
185 template < typename TPredicate, DGtal::ProbingMode mode >
186 bool
187 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::translateIf (UpdateOperation const& aOp)
188 {
189  if (getState() < 4)
190  {
191  Point translation = aOp.coeffs[1] * m(aOp.sigma[1]) + aOp.coeffs[2] * m(aOp.sigma[2]);
192  myIsInReverseState = !myIsInReverseState;
193  myTetrahedronEstimator.translateQ(translation);
194  }
195 
196  // We also test for separability and stop if the current parallelepiped is not separable:
197  // for analytical digital planes it is impossible but it can happen on digital surfaces.
198  if (! isSeparating())
199  {
200  return false;
201  }
202 
203  return true;
204 }
205 
206 // ------------------------------------------------------------------------
207 template < typename TPredicate, DGtal::ProbingMode mode >
208 std::vector<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::UpdateOperation>
209 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::geometricalDecomposition (UpdateOperation const& aOp) const
210 {
211  const auto& sigma = aOp.sigma;
212  const auto& coeffs = aOp.coeffs;
213 
214  assert(coeffs[0] == 1);
215 
216  if (coeffs[1] + coeffs[2] == -1)
217  {
218  // aOp is already elementary
219  return { aOp };
220  }
221 
222  Point e10 = m(sigma[1]) - m(sigma[0]), e12 = m(sigma[2]) - m(sigma[1]);
223 
224  std::vector<UpdateOperation> operations;
225  Integer c1(0), c2(0);
226 
227  while (c1 != -coeffs[1] && c2 != -coeffs[2])
228  {
229  Point v = e10 + (c1 + 1) * m(sigma[1]) + c2 * m(sigma[2]);
230 
231  if (e10.dot(v) >= 0)
232  {
233  operations.push_back({ sigma, { 1, -1, 0 } });
234  c1++;
235  }
236  else
237  {
238  operations.push_back({ sigma, { 1, 0, -1 } });
239  c2++;
240  }
241  }
242 
243  while (c1 != -coeffs[1])
244  {
245  operations.push_back({ sigma, { 1, -1, 0 } });
246  c1++;
247  }
248 
249  while (c2 != -coeffs[2])
250  {
251  operations.push_back({ sigma, { 1, 0, -1 } });
252  c2++;
253  }
254 
255  return operations;
256 }
257 
258 // ------------------------------------------------------------------------
259 template < typename TPredicate, DGtal::ProbingMode mode >
260 bool
261 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::advance (std::vector<PointOnProbingRay> const& aNeighbors)
262 {
263  bool flag;
264  UpdateOperation op;
265  std::tie(flag, op) = myTetrahedronEstimator.advance(aNeighbors);
266 
267  if (flag)
268  {
269  Integer lambda = -op.coeffs[2];
270 
271  // Decomposition of operations to ensure separability
272  if (lambda > 0)
273  {
274  assert(op.coeffs[0] == 1);
275 
276  // Undo the last operation
277  UpdateOperation undoOp;
278  undoOp.sigma = op.sigma;
279  undoOp.coeffs = { Integer(1), -op.coeffs[1], -op.coeffs[2] };
280  myTetrahedronEstimator.applyOperation(undoOp);
281 
282  // Iteratively apply elementary operations
283  const auto& operations = geometricalDecomposition(op);
284  for (const auto& iterOp: operations)
285  {
286  myTetrahedronEstimator.applyOperation(iterOp);
287  if (! translateIf(iterOp))
288  {
289  return false;
290  }
291  }
292  }
293  else
294  {
295  if (! translateIf(op))
296  {
297  return false;
298  }
299  }
300  }
301 
302  if (! isValid())
303  {
304  return false;
305  }
306 
307  return flag;
308 }
309 
310 // ------------------------------------------------------------------------
311 template < typename TPredicate, DGtal::ProbingMode mode >
312 inline
313 bool
314 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::advance ()
315 {
316  return advance({});
317 }
318 
319 // ------------------------------------------------------------------------
320 template < typename TPredicate, DGtal::ProbingMode mode >
321 inline
322 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
323 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute (std::vector<PointOnProbingRay> const& aNeighbors)
324 {
325  while (advance(aNeighbors)) {}
326 
327  return getNormal();
328 }
329 
330 // ------------------------------------------------------------------------
331 template < typename TPredicate, DGtal::ProbingMode mode >
332 inline
333 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
334 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute ()
335 {
336  return compute({});
337 }
338 
339 // ------------------------------------------------------------------------
340 template < typename TPredicate, DGtal::ProbingMode mode >
341 inline
342 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::HexagonState
343 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::hexagonState () const
344 {
345  return myTetrahedronEstimator.hexagonState();
346 }
347 
348 // ------------------------------------------------------------------------
349 template < typename TPredicate, DGtal::ProbingMode mode >
350 inline
351 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
352 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getNormal () const
353 {
354  return myTetrahedronEstimator.getNormal();
355 }
356 
357 // ------------------------------------------------------------------------
358 template < typename TPredicate, DGtal::ProbingMode mode >
359 inline
360 std::pair<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector,
361  typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector>
362 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getBasis () const
363 {
364  return myTetrahedronEstimator.getBasis();
365 }
366 
367 template < typename TPredicate, DGtal::ProbingMode mode >
368 inline
369 bool
370 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isReduced () const
371 {
372  auto basis = getBasis();
373  return DGtal::detail::isBasisReduced(basis.first, basis.second);
374 }
375 
376 // ------------------------------------------------------------------------
377 template < typename TPredicate, DGtal::ProbingMode mode >
378 inline
379 int
380 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getState () const
381 {
382  Point p = getOrigin();
383  int inside = 0;
384  std::vector<Point> parallelepiped = { p, q(),
385  p + m(0), p + m(1), p + m(2),
386  q() - m(0), q() - m(1), q() - m(2) };
387 
388  for (int i = 0; i < 8; ++i)
389  {
390  if (inPlane(parallelepiped[i]))
391  {
392  ++inside;
393  }
394  }
395 
396  return inside;
397 }
398 
399 // ------------------------------------------------------------------------
400 template < typename TPredicate, DGtal::ProbingMode mode >
401 inline
402 bool
403 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::inPlane (const Point& aPoint) const
404 {
405  return myNotAbovePredicate.inPlane(aPoint);
406 }
407 
408 ///////////////////////////////////////////////////////////////////////////////
409 // Interface - public :
410 
411 /**
412  * Writes/Displays the object on an output stream.
413  * @param out the output stream where the object is written.
414  */
415 template <typename TPredicate, DGtal::ProbingMode mode>
416 inline
417 void
418 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::selfDisplay ( std::ostream & out ) const
419 {
420  out << "[PlaneProbingParallelepipedEstimator]";
421 }
422 
423 /**
424  * Checks the validity/consistency of the object.
425  * @return 'true' if the object is valid, 'false' otherwise.
426  */
427 template <typename TPredicate, DGtal::ProbingMode mode>
428 inline
429 bool
430 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isValid() const
431 {
432  return ( ( ! inPlane( myTetrahedronEstimator.q() ) )
433  && inPlane( myTetrahedronEstimator.getOrigin() )
434  && myTetrahedronEstimator.isUnimodular()
435  && myTetrahedronEstimator.isProjectedInside() );
436 }
437 
438 
439 
440 ///////////////////////////////////////////////////////////////////////////////
441 // Implementation of inline functions //
442 
443 template <typename TPredicate, DGtal::ProbingMode mode>
444 inline
445 std::ostream&
446 DGtal::operator<< ( std::ostream & out,
447  const PlaneProbingParallelepipedEstimator<TPredicate, mode> & object )
448 {
449  object.selfDisplay( out );
450  return out;
451 }
452 
453 // //
454 ///////////////////////////////////////////////////////////////////////////////
455 
456