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 // ------------------------------------------------------------------------
43 template < typename TPredicate, DGtal::ProbingMode mode >
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)
52 myIsInReverseState = getState() < 4;
55 ///////////////////////////////////////////////////////////////////////////////
56 // ----------------------- Plane Probing services ------------------------------
58 // ------------------------------------------------------------------------
59 template < typename TPredicate, DGtal::ProbingMode mode >
61 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
62 NotAbovePredicate (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 // ------------------------------------------------------------------------
71 template < typename TPredicate, DGtal::ProbingMode mode >
73 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate&
74 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
75 operator=(const NotAbovePredicate & other)
79 myPredicate = other.myPredicate;
80 myBound = other.myBound;
81 myParallelpipedEstimator = other.myParallelpipedEstimator;
87 // ------------------------------------------------------------------------
88 template < typename TPredicate, DGtal::ProbingMode mode >
91 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
92 inPlane (Point const& aPoint) const
94 return (*myPredicate)(aPoint);
97 // ------------------------------------------------------------------------
98 template < typename TPredicate, DGtal::ProbingMode mode >
101 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
102 operator() (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 // ------------------------------------------------------------------------
129 template < typename TPredicate, DGtal::ProbingMode mode >
131 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
132 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::NotAbovePredicate::
135 ASSERT(myParallelpipedEstimator != nullptr);
136 return myParallelpipedEstimator->q();
139 // ------------------------------------------------------------------------
140 template < typename TPredicate, DGtal::ProbingMode mode >
142 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
143 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::q () const
145 return myTetrahedronEstimator.q();
148 // ------------------------------------------------------------------------
149 template < typename TPredicate, DGtal::ProbingMode mode >
151 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
152 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::m (int aIndex) const
154 return myTetrahedronEstimator.m(aIndex);
157 // ------------------------------------------------------------------------
158 template < typename TPredicate, DGtal::ProbingMode mode >
160 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Point
161 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getOrigin () const
163 return myTetrahedronEstimator.q() - myTetrahedronEstimator.m(0) - myTetrahedronEstimator.m(1) - myTetrahedronEstimator.m(2);
166 // ------------------------------------------------------------------------
167 template < typename TPredicate, DGtal::ProbingMode mode >
170 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isSeparating () const
172 return myNotAbovePredicate.inPlane(getOrigin()) != myNotAbovePredicate.inPlane(q());
175 // ------------------------------------------------------------------------
176 template < typename TPredicate, DGtal::ProbingMode mode >
179 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isInReverseState () const
181 return myIsInReverseState;
184 // ------------------------------------------------------------------------
185 template < typename TPredicate, DGtal::ProbingMode mode >
187 DGtal::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 // ------------------------------------------------------------------------
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
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 // ------------------------------------------------------------------------
259 template < typename TPredicate, DGtal::ProbingMode mode >
261 DGtal::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 // ------------------------------------------------------------------------
311 template < typename TPredicate, DGtal::ProbingMode mode >
314 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::advance ()
319 // ------------------------------------------------------------------------
320 template < typename TPredicate, DGtal::ProbingMode mode >
322 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
323 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute (std::vector<PointOnProbingRay> const& aNeighbors)
325 while (advance(aNeighbors)) {}
330 // ------------------------------------------------------------------------
331 template < typename TPredicate, DGtal::ProbingMode mode >
333 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Quantity
334 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::compute ()
339 // ------------------------------------------------------------------------
340 template < typename TPredicate, DGtal::ProbingMode mode >
342 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::HexagonState
343 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::hexagonState () const
345 return myTetrahedronEstimator.hexagonState();
348 // ------------------------------------------------------------------------
349 template < typename TPredicate, DGtal::ProbingMode mode >
351 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector
352 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getNormal () const
354 return myTetrahedronEstimator.getNormal();
357 // ------------------------------------------------------------------------
358 template < typename TPredicate, DGtal::ProbingMode mode >
360 std::pair<typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector,
361 typename DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::Vector>
362 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::getBasis () const
364 return myTetrahedronEstimator.getBasis();
367 template < typename TPredicate, DGtal::ProbingMode mode >
370 DGtal::PlaneProbingParallelepipedEstimator<TPredicate, mode>::isReduced () const
372 auto basis = getBasis();
373 return DGtal::detail::isBasisReduced(basis.first, basis.second);
376 // ------------------------------------------------------------------------
377 template < typename TPredicate, DGtal::ProbingMode mode >
380 DGtal::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 // ------------------------------------------------------------------------
400 template < typename TPredicate, DGtal::ProbingMode mode >
403 DGtal::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.
415 template <typename TPredicate, DGtal::ProbingMode mode>
418 DGtal::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.
427 template <typename TPredicate, DGtal::ProbingMode mode>
430 DGtal::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 //
443 template <typename TPredicate, DGtal::ProbingMode mode>
446 DGtal::operator<< ( std::ostream & out,
447 const PlaneProbingParallelepipedEstimator<TPredicate, mode> & object )
449 object.selfDisplay( out );
454 ///////////////////////////////////////////////////////////////////////////////