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/>.
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
24 * Implementation of inline methods defined in PlaneProbingParallelepipedEstimator.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////////////////////////
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
42// ------------------------------------------------------------------------
43template < typename TPredicate, DGtal::ProbingMode mode >
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)
52 myIsInReverseState = getState() < 4;
55///////////////////////////////////////////////////////////////////////////////
56// ----------------------- Plane Probing services ------------------------------
58// ------------------------------------------------------------------------
59template < typename TPredicate, DGtal::ProbingMode mode >
61DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
62NotAbovePredicate (Predicate const& aPredicate, Integer const& aBound,
63 Self* aParallelepipedEstimator)
64 : myPredicate(&aPredicate), myBound(aBound), myParallelpipedEstimator(aParallelepipedEstimator)
66 ASSERT(myPredicate != nullptr);
67 ASSERT(myParallelpipedEstimator != nullptr);
70// ------------------------------------------------------------------------
71template < typename TPredicate, DGtal::ProbingMode mode >
73typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate&
74DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
75operator=(const NotAbovePredicate & other)
79 myPredicate = other.myPredicate;
80 myBound = other.myBound;
81 myParallelpipedEstimator = other.myParallelpipedEstimator;
87// ------------------------------------------------------------------------
88template < typename TPredicate, DGtal::ProbingMode mode >
91DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
92inPlane (Point const& aPoint) const
94 return (*myPredicate)(aPoint);
97// ------------------------------------------------------------------------
98template < typename TPredicate, DGtal::ProbingMode mode >
101DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
102operator() (Point const& aPoint) const
104 Point u = aPoint - q(), s = q();
106 ASSERT(! inPlane(s));
108 Integer l = DGtal::NumberTraits<Integer>::ONE;
112 if (inPlane(s + u * l))
117 if (inPlane(s - u * l))
128// ------------------------------------------------------------------------
129template < typename TPredicate, DGtal::ProbingMode mode >
131typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
132DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
135 ASSERT(myParallelpipedEstimator != nullptr);
136 return myParallelpipedEstimator->q();
139// ------------------------------------------------------------------------
140template < typename TPredicate, DGtal::ProbingMode mode >
142typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
143DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::q () const
145 return myTetrahedronEstimator.q();
148// ------------------------------------------------------------------------
149template < typename TPredicate, DGtal::ProbingMode mode >
151typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
152DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::m (int aIndex) const
154 return myTetrahedronEstimator.m(aIndex);
157// ------------------------------------------------------------------------
158template < typename TPredicate, DGtal::ProbingMode mode >
160typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
161DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getOrigin () const
163 return myTetrahedronEstimator.q() - myTetrahedronEstimator.m(0) - myTetrahedronEstimator.m(1) - myTetrahedronEstimator.m(2);
166// ------------------------------------------------------------------------
167template < typename TPredicate, DGtal::ProbingMode mode >
170DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isSeparating () const
172 return myNotAbovePredicate.inPlane(getOrigin()) != myNotAbovePredicate.inPlane(q());
175// ------------------------------------------------------------------------
176template < typename TPredicate, DGtal::ProbingMode mode >
179DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isInReverseState () const
181 return myIsInReverseState;
184// ------------------------------------------------------------------------
185template < typename TPredicate, DGtal::ProbingMode mode >
187DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::translateIf (UpdateOperation const& aOp)
191 Point translation = aOp.coeffs[1] * m(aOp.sigma[1]) + aOp.coeffs[2] * m(aOp.sigma[2]);
192 myIsInReverseState = !myIsInReverseState;
193 myTetrahedronEstimator.translateQ(translation);
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())
206// ------------------------------------------------------------------------
207template < typename TPredicate, DGtal::ProbingMode mode >
208std::vector<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::UpdateOperation>
209DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::geometricalDecomposition (UpdateOperation const& aOp) const
211 const auto& sigma = aOp.sigma;
212 const auto& coeffs = aOp.coeffs;
214 assert(coeffs[0] == 1);
216 if (coeffs[1] + coeffs[2] == -1)
218 // aOp is already elementary
222 Point e10 = m(sigma[1]) - m(sigma[0]), e12 = m(sigma[2]) - m(sigma[1]);
224 std::vector<UpdateOperation> operations;
225 Integer c1(0), c2(0);
227 while (c1 != -coeffs[1] && c2 != -coeffs[2])
229 Point v = e10 + (c1 + 1) * m(sigma[1]) + c2 * m(sigma[2]);
233 operations.push_back({ sigma, { 1, -1, 0 } });
238 operations.push_back({ sigma, { 1, 0, -1 } });
243 while (c1 != -coeffs[1])
245 operations.push_back({ sigma, { 1, -1, 0 } });
249 while (c2 != -coeffs[2])
251 operations.push_back({ sigma, { 1, 0, -1 } });
258// ------------------------------------------------------------------------
259template < typename TPredicate, DGtal::ProbingMode mode >
261DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::advance (std::vector<PointOnProbingRay> const& aNeighbors)
265 std::tie(flag, op) = myTetrahedronEstimator.advance(aNeighbors);
269 Integer lambda = -op.coeffs[2];
271 // Decomposition of operations to ensure separability
274 assert(op.coeffs[0] == 1);
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);
282 // Iteratively apply elementary operations
283 const auto& operations = geometricalDecomposition(op);
284 for (const auto& iterOp: operations)
286 myTetrahedronEstimator.applyOperation(iterOp);
287 if (! translateIf(iterOp))
295 if (! translateIf(op))
310// ------------------------------------------------------------------------
311template < typename TPredicate, DGtal::ProbingMode mode >
314DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::advance ()
319// ------------------------------------------------------------------------
320template < typename TPredicate, DGtal::ProbingMode mode >
322typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
323DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute (std::vector<PointOnProbingRay> const& aNeighbors)
325 while (advance(aNeighbors)) {}
330// ------------------------------------------------------------------------
331template < typename TPredicate, DGtal::ProbingMode mode >
333typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
334DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute ()
339// ------------------------------------------------------------------------
340template < typename TPredicate, DGtal::ProbingMode mode >
342typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::HexagonState
343DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::hexagonState () const
345 return myTetrahedronEstimator.hexagonState();
348// ------------------------------------------------------------------------
349template < typename TPredicate, DGtal::ProbingMode mode >
351typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
352DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getNormal () const
354 return myTetrahedronEstimator.getNormal();
357// ------------------------------------------------------------------------
358template < typename TPredicate, DGtal::ProbingMode mode >
360std::pair<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector,
361 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector>
362DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getBasis () const
364 return myTetrahedronEstimator.getBasis();
367template < typename TPredicate, DGtal::ProbingMode mode >
370DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isReduced () const
372 auto basis = getBasis();
373 return DGtal::detail::isBasisReduced(basis.first, basis.second);
376// ------------------------------------------------------------------------
377template < typename TPredicate, DGtal::ProbingMode mode >
380DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getState () const
382 Point p = getOrigin();
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) };
388 for (int i = 0; i < 8; ++i)
390 if (inPlane(parallelepiped[i]))
399// ------------------------------------------------------------------------
400template < typename TPredicate, DGtal::ProbingMode mode >
403DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::inPlane (const Point& aPoint) const
405 return myNotAbovePredicate.inPlane(aPoint);
408///////////////////////////////////////////////////////////////////////////////
409// Interface - public :
412 * Writes/Displays the object on an output stream.
413 * @param out the output stream where the object is written.
415template <typename TPredicate, DGtal::ProbingMode mode>
418DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::selfDisplay ( std::ostream & out ) const
420 out << "[PlaneProbingParallelepipedEstimator]";
424 * Checks the validity/consistency of the object.
425 * @return 'true' if the object is valid, 'false' otherwise.
427template <typename TPredicate, DGtal::ProbingMode mode>
430DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isValid() const
432 return ( ( ! inPlane( myTetrahedronEstimator.q() ) )
433 && inPlane( myTetrahedronEstimator.getOrigin() )
434 && myTetrahedronEstimator.isUnimodular()
435 && myTetrahedronEstimator.isProjectedInside() );
440///////////////////////////////////////////////////////////////////////////////
441// Implementation of inline functions //
443template <typename TPredicate, DGtal::ProbingMode mode>
446DGtal::operator<< ( std::ostream & out,
447 const PlaneProbingParallelepipedEstimator<TPredicate, mode> & object )
449 object.selfDisplay( out );
454///////////////////////////////////////////////////////////////////////////////