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 PlaneProbingDigitalSurfaceLocalEstimator.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
41 // ------------------------------------------------------------------------
42 template < typename TSurface, typename TInternalProbingAlgorithm >
44 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
45 PlaneProbingDigitalSurfaceLocalEstimator ()
49 // ------------------------------------------------------------------------
50 template < typename TSurface, typename TInternalProbingAlgorithm >
52 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
53 PlaneProbingDigitalSurfaceLocalEstimator (ConstAlias<Surface> aSurface)
54 : mySurface(aSurface), myPredicate(mySurface), myPreEstimationEstimator(mySurface)
58 // ------------------------------------------------------------------------
59 template < typename TSurface, typename TInternalProbingAlgorithm >
61 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
62 PlaneProbingDigitalSurfaceLocalEstimator(ProbingFactory const& aProbingFactory,
63 std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
65 : myProbingFactory(aProbingFactory), myPreEstimations(aPreEstimations), myVerbose(aVerbose)
69 // ------------------------------------------------------------------------
70 template < typename TSurface, typename TInternalProbingAlgorithm >
72 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
73 PlaneProbingDigitalSurfaceLocalEstimator(ConstAlias<Surface> aSurface,
74 ProbingFactory const& aProbingFactory,
75 std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
77 : mySurface(aSurface), myPredicate(mySurface), myPreEstimationEstimator(mySurface),
78 myProbingFactory(aProbingFactory), myPreEstimations(aPreEstimations), myVerbose(aVerbose)
82 // ------------------------------------------------------------------------
83 template < typename TSurface, typename TInternalProbingAlgorithm >
85 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
86 ~PlaneProbingDigitalSurfaceLocalEstimator ()
88 myProbingAlgorithm = nullptr;
91 // ----------------- model of CSurfelLocalEstimator -----------------------
93 // ------------------------------------------------------------------------
94 template < typename TSurface, typename TInternalProbingAlgorithm >
95 template < typename SurfelConstIterator >
97 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
98 init (Scalar const& h, SurfelConstIterator /* itb */, SurfelConstIterator /* ite */)
103 // ------------------------------------------------------------------------
104 template < typename TSurface, typename TInternalProbingAlgorithm >
105 template < typename SurfelConstIterator >
107 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::Quantity
108 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
109 eval (SurfelConstIterator it)
111 ASSERT(mySurface != nullptr);
112 ASSERT(myProbingFactory);
114 // If no pre-estimation is given, we make one using maximal segments
115 RealPoint preEstimation = getPreEstimation(it);
117 // Compute an initial frame using the pre-estimation
119 ProbingFrame initialFrame = probingFrameFromSurfel(s);
120 ProbingFrame frame = probingFrameWithPreEstimation(initialFrame, preEstimation);
122 // If the constructor of the probing estimator throws, we return the normal of the frame
123 // (this happens for instance when using a tetrahedron estimator on a digital surface,
124 // the initial frame will be considered invalid since not all the points of the upper
125 // triangle belong to the surface)
128 myProbingAlgorithm = myProbingFactory(frame, myPredicate);
130 catch (std::runtime_error const& e)
135 // We use slightly different versions depending on the number of zeros
136 // in the pre-estimation vector.
137 const auto zeros = findZeros(preEstimation);
140 if (zeros.size() == 0)
142 normal = myProbingAlgorithm->compute();
144 else if (zeros.size() == 1)
146 int index = zeros[0];
147 normal = myProbingAlgorithm->compute(getProbingRaysOneFlatDirection(index));
148 // normal = getNormalOneFlatDirection(index);
150 else if (zeros.size() == 2)
152 normal = frame.normal;
155 delete myProbingAlgorithm;
156 myProbingAlgorithm = nullptr;
161 // ------------------------------------------------------------------------
162 template < typename TSurface, typename TInternalProbingAlgorithm >
163 template < typename SurfelConstIterator, typename OutputIterator >
166 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
167 eval (SurfelConstIterator itb, SurfelConstIterator ite, OutputIterator out)
169 for (auto it = itb; it != ite; ++it)
177 // ------------------------------------------------------------------------
178 template < typename TSurface, typename TInternalProbingAlgorithm >
179 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::Scalar
181 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
187 // --------------- model of CDigitalSurfaceLocalEstimator ------------------
189 // ------------------------------------------------------------------------
190 template < typename TSurface, typename TInternalProbingAlgorithm >
192 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
193 attach (ConstAlias<Surface> aSurface)
195 mySurface = aSurface;
196 myPredicate = Predicate(mySurface);
197 myPreEstimationEstimator = PreEstimation(mySurface);
200 // ------------------------------------------------------------------------
201 template < typename TSurface, typename TInternalProbingAlgorithm >
203 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
204 setParams (ProbingFactory const& aProbingFactory,
205 std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
208 myProbingFactory = aProbingFactory;
209 myPreEstimations = aPreEstimations;
210 myVerbose = aVerbose;
213 // ------------------------- Internals ------------------------------------
215 // ------------------------------------------------------------------------
216 template < typename TSurface, typename TInternalProbingAlgorithm >
218 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame
219 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
220 probingFrameFromSurfel (Surfel const& aSurfel) const
222 ASSERT(mySurface != nullptr);
224 KSpace const& K = mySurface->container().space();
226 typename KSpace::DirIterator q1 = K.sDirs(aSurfel), q2 = q1;
230 Cell surfel = K.unsigns(aSurfel);
231 Cell linel1 = K.uIncident(surfel, *q1, false),
232 linel2 = K.uIncident(surfel, *q2, false);
233 Cell p1_1 = K.uIncident(linel1, *q2, false), p1_2 = K.uIncident(linel1, *q2, true),
234 p2_1 = K.uIncident(linel2, *q1, false), p2_2 = K.uIncident(linel2, *q1, true);
236 ASSERT(p1_1 == p2_1);
238 Point myP = K.uCoords(p1_1),
239 myB1 = K.uCoords(p2_2) - myP,
240 myB2 = K.uCoords(p1_2) - myP;
242 // Make the normal consistent with the exterior normal
243 Point n = myB1.crossProduct(myB2);
245 typename KSpace::DirIterator orth = K.sOrthDirs(aSurfel);
246 SCell voxel_direct = K.sDirectIncident(aSurfel, *orth),
247 voxel_indirect = K.sIndirectIncident(aSurfel, *orth);
248 Point n_interior = K.sCoords(voxel_direct) - K.sCoords(voxel_indirect); // oriented towards the interior (direct) side
250 if (n.dot(n_interior) > 0) {
255 Point myNormal = myB1.crossProduct(myB2);
256 ASSERT(myNormal.dot(n_interior) < 0);
258 return { myP, myB1, myB2, myNormal };
261 // ------------------------------------------------------------------------
262 template < typename TSurface, typename TInternalProbingAlgorithm >
264 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame
265 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
266 probingFrameWithPreEstimation (ProbingFrame const& aInitialFrame, RealPoint const& aPreEstimation) const
268 ProbingFrame frame = aInitialFrame;
269 ProbingFrame frameQExt;
271 for (int i = 0; i < 4; ++i)
273 Point shift = frame.shift();
274 if (! myPredicate(frame.p + shift))
279 signs += int(signComponent(shift[0]) == signComponent(aPreEstimation[0]));
280 signs += int(signComponent(shift[1]) == signComponent(aPreEstimation[1]));
281 signs += int(signComponent(shift[2]) == signComponent(aPreEstimation[2]));
289 frame = frame.rotatedCopy();
295 ///////////////////////////////////////////////////////////////////////////////
296 // Interface - public :
299 * Writes/Displays the object on an output stream.
300 * @param out the output stream where the object is written.
302 template < typename TSurface, typename TInternalProbingAlgorithm >
305 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::selfDisplay ( std::ostream & out ) const
307 out << "[PlaneProbingDigitalSurfaceLocalEstimator]";
311 * Checks the validity/consistency of the object.
312 * @return 'true' if the object is valid, 'false' otherwise.
314 template < typename TSurface, typename TInternalProbingAlgorithm >
317 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::isValid() const
322 // ------------------------------------------------------------------------
323 template < typename TSurface, typename TInternalProbingAlgorithm >
324 template < typename SurfelConstIterator >
325 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::RealPoint
326 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
327 getPreEstimation (SurfelConstIterator it) const
329 Surfel const& s = *it;
331 if (myPreEstimations.count(s))
333 return myPreEstimations.at(s);
337 RealPoint preEstimation = myPreEstimationEstimator.eval(it);
338 myPreEstimations[s] = preEstimation; // cache the value for future calls
339 return preEstimation;
344 ///////////////////////////////////////////////////////////////////////////////
345 // Implementation of inline functions //
347 template < typename TSurface, typename TInternalProbingAlgorithm >
350 DGtal::operator<< ( std::ostream & out,
351 const PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm> & object )
353 object.selfDisplay( out );
358 ///////////////////////////////////////////////////////////////////////////////