DGtal 1.4.0
Loading...
Searching...
No Matches
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// ------------------------------------------------------------------------
43template < typename TPredicate, DGtal::ProbingMode mode >
44inline
45DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::
46PlaneProbingParallelepipedEstimator (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// ------------------------------------------------------------------------
59template < typename TPredicate, DGtal::ProbingMode mode >
60inline
61DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
62NotAbovePredicate (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// ------------------------------------------------------------------------
71template < typename TPredicate, DGtal::ProbingMode mode >
72inline
73typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate&
74DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
75operator=(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// ------------------------------------------------------------------------
88template < typename TPredicate, DGtal::ProbingMode mode >
89inline
90bool
91DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
92inPlane (Point const& aPoint) const
93{
94 return (*myPredicate)(aPoint);
95}
96
97// ------------------------------------------------------------------------
98template < typename TPredicate, DGtal::ProbingMode mode >
99inline
100bool
101DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
102operator() (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// ------------------------------------------------------------------------
129template < typename TPredicate, DGtal::ProbingMode mode >
130inline
131typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
132DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
133q () const
134{
135 ASSERT(myParallelpipedEstimator != nullptr);
136 return myParallelpipedEstimator->q();
137}
138
139// ------------------------------------------------------------------------
140template < typename TPredicate, DGtal::ProbingMode mode >
141inline
142typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
143DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::q () const
144{
145 return myTetrahedronEstimator.q();
146}
147
148// ------------------------------------------------------------------------
149template < typename TPredicate, DGtal::ProbingMode mode >
150inline
151typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
152DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::m (int aIndex) const
153{
154 return myTetrahedronEstimator.m(aIndex);
155}
156
157// ------------------------------------------------------------------------
158template < typename TPredicate, DGtal::ProbingMode mode >
159inline
160typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
161DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getOrigin () const
162{
163 return myTetrahedronEstimator.q() - myTetrahedronEstimator.m(0) - myTetrahedronEstimator.m(1) - myTetrahedronEstimator.m(2);
164}
165
166// ------------------------------------------------------------------------
167template < typename TPredicate, DGtal::ProbingMode mode >
168inline
169bool
170DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isSeparating () const
171{
172 return myNotAbovePredicate.inPlane(getOrigin()) != myNotAbovePredicate.inPlane(q());
173}
174
175// ------------------------------------------------------------------------
176template < typename TPredicate, DGtal::ProbingMode mode >
177inline
178bool
179DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isInReverseState () const
180{
181 return myIsInReverseState;
182}
183
184// ------------------------------------------------------------------------
185template < typename TPredicate, DGtal::ProbingMode mode >
186bool
187DGtal::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// ------------------------------------------------------------------------
207template < typename TPredicate, DGtal::ProbingMode mode >
208std::vector<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::UpdateOperation>
209DGtal::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// ------------------------------------------------------------------------
259template < typename TPredicate, DGtal::ProbingMode mode >
260bool
261DGtal::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// ------------------------------------------------------------------------
311template < typename TPredicate, DGtal::ProbingMode mode >
312inline
313bool
314DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::advance ()
315{
316 return advance({});
317}
318
319// ------------------------------------------------------------------------
320template < typename TPredicate, DGtal::ProbingMode mode >
321inline
322typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
323DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute (std::vector<PointOnProbingRay> const& aNeighbors)
324{
325 while (advance(aNeighbors)) {}
326
327 return getNormal();
328}
329
330// ------------------------------------------------------------------------
331template < typename TPredicate, DGtal::ProbingMode mode >
332inline
333typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
334DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute ()
335{
336 return compute({});
337}
338
339// ------------------------------------------------------------------------
340template < typename TPredicate, DGtal::ProbingMode mode >
341inline
342typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::HexagonState
343DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::hexagonState () const
344{
345 return myTetrahedronEstimator.hexagonState();
346}
347
348// ------------------------------------------------------------------------
349template < typename TPredicate, DGtal::ProbingMode mode >
350inline
351typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
352DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getNormal () const
353{
354 return myTetrahedronEstimator.getNormal();
355}
356
357// ------------------------------------------------------------------------
358template < typename TPredicate, DGtal::ProbingMode mode >
359inline
360std::pair<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector,
361 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector>
362DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getBasis () const
363{
364 return myTetrahedronEstimator.getBasis();
365}
366
367template < typename TPredicate, DGtal::ProbingMode mode >
368inline
369bool
370DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isReduced () const
371{
372 auto basis = getBasis();
373 return DGtal::detail::isBasisReduced(basis.first, basis.second);
374}
375
376// ------------------------------------------------------------------------
377template < typename TPredicate, DGtal::ProbingMode mode >
378inline
379int
380DGtal::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// ------------------------------------------------------------------------
400template < typename TPredicate, DGtal::ProbingMode mode >
401inline
402bool
403DGtal::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 */
415template <typename TPredicate, DGtal::ProbingMode mode>
416inline
417void
418DGtal::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 */
427template <typename TPredicate, DGtal::ProbingMode mode>
428inline
429bool
430DGtal::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
443template <typename TPredicate, DGtal::ProbingMode mode>
444inline
445std::ostream&
446DGtal::operator<< ( std::ostream & out,
447 const PlaneProbingParallelepipedEstimator<TPredicate, mode> & object )
448{
449 object.selfDisplay( out );
450 return out;
451}
452
453// //
454///////////////////////////////////////////////////////////////////////////////
455
456