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 from the surfel
119 ProbingFrame initialFrame = probingFrameFromSurfel(s);
120 // Compute a frame from the initial one using the pre-estimation
121 std::pair<bool, ProbingFrame> res =
122 probingFrameWithPreEstimation(initialFrame, preEstimation);
125 //If we have found a frame, we initialize the plane-probing algorithm
126 myProbingAlgorithm = myProbingFactory(res.second, myPredicate);
128 // We use slightly different versions depending on the number of zeros
129 // in the pre-estimation vector.
130 const auto zeros = findZeros(preEstimation);
133 if (zeros.size() == 0)
135 normal = myProbingAlgorithm->compute();
137 else if (zeros.size() == 1)
139 int index = zeros[0];
140 normal = myProbingAlgorithm->compute(getProbingRaysOneFlatDirection(index));
142 else if (zeros.size() == 2)
144 normal = res.second.normal;
147 delete myProbingAlgorithm;
148 myProbingAlgorithm = nullptr;
153 // If we have found no way to properly initialize the plane-probing estimator,
154 // we return the initial frame normal, i.e. the trivial normal of the surfel.
155 return initialFrame.normal;
159 // ------------------------------------------------------------------------
160 template < typename TSurface, typename TInternalProbingAlgorithm >
161 template < typename SurfelConstIterator, typename OutputIterator >
164 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
165 eval (SurfelConstIterator itb, SurfelConstIterator ite, OutputIterator out)
167 for (auto it = itb; it != ite; ++it)
175 // ------------------------------------------------------------------------
176 template < typename TSurface, typename TInternalProbingAlgorithm >
177 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::Scalar
179 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
185 // --------------- model of CDigitalSurfaceLocalEstimator ------------------
187 // ------------------------------------------------------------------------
188 template < typename TSurface, typename TInternalProbingAlgorithm >
190 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
191 attach (ConstAlias<Surface> aSurface)
193 mySurface = aSurface;
194 myPredicate = Predicate(mySurface);
195 myPreEstimationEstimator = PreEstimation(mySurface);
198 // ------------------------------------------------------------------------
199 template < typename TSurface, typename TInternalProbingAlgorithm >
201 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
202 setParams (ProbingFactory const& aProbingFactory,
203 std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
206 myProbingFactory = aProbingFactory;
207 myPreEstimations = aPreEstimations;
208 myVerbose = aVerbose;
211 // ------------------------- Internals ------------------------------------
213 // ------------------------------------------------------------------------
214 template < typename TSurface, typename TInternalProbingAlgorithm >
216 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame
217 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
218 probingFrameFromSurfel (Surfel const& aSurfel) const
220 ASSERT(mySurface != nullptr);
222 KSpace const& K = mySurface->container().space();
224 typename KSpace::DirIterator q1 = K.sDirs(aSurfel), q2 = q1;
228 Cell surfel = K.unsigns(aSurfel);
229 Cell linel1 = K.uIncident(surfel, *q1, false),
230 linel2 = K.uIncident(surfel, *q2, false);
231 Cell p1_1 = K.uIncident(linel1, *q2, false), p1_2 = K.uIncident(linel1, *q2, true),
232 p2_1 = K.uIncident(linel2, *q1, false), p2_2 = K.uIncident(linel2, *q1, true);
234 ASSERT(p1_1 == p2_1);
236 Point myP = K.uCoords(p1_1),
237 myB1 = K.uCoords(p2_2) - myP,
238 myB2 = K.uCoords(p1_2) - myP;
240 // Make the normal consistent with the exterior normal
241 Point n = myB1.crossProduct(myB2);
243 typename KSpace::DirIterator orth = K.sOrthDirs(aSurfel);
244 SCell voxel_direct = K.sDirectIncident(aSurfel, *orth),
245 voxel_indirect = K.sIndirectIncident(aSurfel, *orth);
246 Point n_interior = K.sCoords(voxel_direct) - K.sCoords(voxel_indirect); // oriented towards the interior (direct) side
248 if (n.dot(n_interior) > 0) {
253 Point myNormal = myB1.crossProduct(myB2);
254 ASSERT(myNormal.dot(n_interior) < 0);
256 return { myP, myB1, myB2, myNormal };
259 // ------------------------------------------------------------------------
260 template < typename TSurface, typename TInternalProbingAlgorithm >
262 std::pair<bool, typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame >
263 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
264 probingFrameWithPreEstimation (ProbingFrame const& aInitialFrame, RealPoint const& aPreEstimation) const
266 ProbingFrame frame = aInitialFrame;
268 for (int i = 0; i < 4; ++i)
270 Point shift = frame.shift();
271 // We search for a frame whose shift point is outside...
272 if (! myPredicate(frame.p + shift))
274 //... and that matches the pre-estimation
276 signs += int(signComponent(shift[0]) == signComponent(aPreEstimation[0]));
277 signs += int(signComponent(shift[1]) == signComponent(aPreEstimation[1]));
278 signs += int(signComponent(shift[2]) == signComponent(aPreEstimation[2]));
282 return std::make_pair(true, frame);
286 frame = frame.rotatedCopy();
289 // No frame that matches the pre-estimation found,
290 return std::make_pair(false, aInitialFrame);
293 ///////////////////////////////////////////////////////////////////////////////
294 // Interface - public :
297 * Writes/Displays the object on an output stream.
298 * @param out the output stream where the object is written.
300 template < typename TSurface, typename TInternalProbingAlgorithm >
303 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::selfDisplay ( std::ostream & out ) const
305 out << "[PlaneProbingDigitalSurfaceLocalEstimator]";
309 * Checks the validity/consistency of the object.
310 * @return 'true' if the object is valid, 'false' otherwise.
312 template < typename TSurface, typename TInternalProbingAlgorithm >
315 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::isValid() const
320 // ------------------------------------------------------------------------
321 template < typename TSurface, typename TInternalProbingAlgorithm >
322 template < typename SurfelConstIterator >
323 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::RealPoint
324 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
325 getPreEstimation (SurfelConstIterator it) const
327 Surfel const& s = *it;
329 if (myPreEstimations.count(s))
331 return myPreEstimations.at(s);
335 RealPoint preEstimation = myPreEstimationEstimator.eval(it);
336 myPreEstimations[s] = preEstimation; // cache the value for future calls
337 return preEstimation;
342 ///////////////////////////////////////////////////////////////////////////////
343 // Implementation of inline functions //
345 template < typename TSurface, typename TInternalProbingAlgorithm >
348 DGtal::operator<< ( std::ostream & out,
349 const PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm> & object )
351 object.selfDisplay( out );
356 ///////////////////////////////////////////////////////////////////////////////