DGtal  1.4.beta
PlaneProbingDigitalSurfaceLocalEstimator.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 PlaneProbingDigitalSurfaceLocalEstimator.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 template < typename TSurface, typename TInternalProbingAlgorithm >
43 inline
44 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
45 PlaneProbingDigitalSurfaceLocalEstimator ()
46 {
47 }
48 
49 // ------------------------------------------------------------------------
50 template < typename TSurface, typename TInternalProbingAlgorithm >
51 inline
52 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
53 PlaneProbingDigitalSurfaceLocalEstimator (ConstAlias<Surface> aSurface)
54  : mySurface(aSurface), myPredicate(mySurface), myPreEstimationEstimator(mySurface)
55 {
56 }
57 
58 // ------------------------------------------------------------------------
59 template < typename TSurface, typename TInternalProbingAlgorithm >
60 inline
61 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
62 PlaneProbingDigitalSurfaceLocalEstimator(ProbingFactory const& aProbingFactory,
63  std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
64  bool aVerbose)
65  : myProbingFactory(aProbingFactory), myPreEstimations(aPreEstimations), myVerbose(aVerbose)
66 {
67 }
68 
69 // ------------------------------------------------------------------------
70 template < typename TSurface, typename TInternalProbingAlgorithm >
71 inline
72 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
73 PlaneProbingDigitalSurfaceLocalEstimator(ConstAlias<Surface> aSurface,
74  ProbingFactory const& aProbingFactory,
75  std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
76  bool aVerbose)
77  : mySurface(aSurface), myPredicate(mySurface), myPreEstimationEstimator(mySurface),
78  myProbingFactory(aProbingFactory), myPreEstimations(aPreEstimations), myVerbose(aVerbose)
79 {
80 }
81 
82 // ------------------------------------------------------------------------
83 template < typename TSurface, typename TInternalProbingAlgorithm >
84 inline
85 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
86 ~PlaneProbingDigitalSurfaceLocalEstimator ()
87 {
88  myProbingAlgorithm = nullptr;
89 }
90 
91 // ----------------- model of CSurfelLocalEstimator -----------------------
92 
93 // ------------------------------------------------------------------------
94 template < typename TSurface, typename TInternalProbingAlgorithm >
95 template < typename SurfelConstIterator >
96 inline
97 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
98 init (Scalar const& h, SurfelConstIterator /* itb */, SurfelConstIterator /* ite */)
99 {
100  myH = h;
101 }
102 
103 // ------------------------------------------------------------------------
104 template < typename TSurface, typename TInternalProbingAlgorithm >
105 template < typename SurfelConstIterator >
106 inline
107 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::Quantity
108 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
109 eval (SurfelConstIterator it)
110 {
111  ASSERT(mySurface != nullptr);
112  ASSERT(myProbingFactory);
113 
114  // If no pre-estimation is given, we make one using maximal segments
115  RealPoint preEstimation = getPreEstimation(it);
116 
117  // Compute an initial frame from the surfel
118  Surfel s = *it;
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);
123 
124  if (res.first) {
125  //If we have found a frame, we initialize the plane-probing algorithm
126  myProbingAlgorithm = myProbingFactory(res.second, myPredicate);
127 
128  // We use slightly different versions depending on the number of zeros
129  // in the pre-estimation vector.
130  const auto zeros = findZeros(preEstimation);
131 
132  Point normal;
133  if (zeros.size() == 0)
134  {
135  normal = myProbingAlgorithm->compute();
136  }
137  else if (zeros.size() == 1)
138  {
139  int index = zeros[0];
140  normal = myProbingAlgorithm->compute(getProbingRaysOneFlatDirection(index));
141  }
142  else if (zeros.size() == 2)
143  {
144  normal = res.second.normal;
145  }
146 
147  delete myProbingAlgorithm;
148  myProbingAlgorithm = nullptr;
149 
150  return normal;
151 
152  } else {
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;
156  }
157 }
158 
159 // ------------------------------------------------------------------------
160 template < typename TSurface, typename TInternalProbingAlgorithm >
161 template < typename SurfelConstIterator, typename OutputIterator >
162 inline
163 OutputIterator
164 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
165 eval (SurfelConstIterator itb, SurfelConstIterator ite, OutputIterator out)
166 {
167  for (auto it = itb; it != ite; ++it)
168  {
169  *out++ = eval(it);
170  }
171 
172  return out;
173 }
174 
175 // ------------------------------------------------------------------------
176 template < typename TSurface, typename TInternalProbingAlgorithm >
177 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::Scalar
178 inline
179 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
180 h () const
181 {
182  return myH;
183 }
184 
185 // --------------- model of CDigitalSurfaceLocalEstimator ------------------
186 
187 // ------------------------------------------------------------------------
188 template < typename TSurface, typename TInternalProbingAlgorithm >
189 inline
190 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
191 attach (ConstAlias<Surface> aSurface)
192 {
193  mySurface = aSurface;
194  myPredicate = Predicate(mySurface);
195  myPreEstimationEstimator = PreEstimation(mySurface);
196 }
197 
198 // ------------------------------------------------------------------------
199 template < typename TSurface, typename TInternalProbingAlgorithm >
200 inline
201 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
202 setParams (ProbingFactory const& aProbingFactory,
203  std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
204  bool aVerbose)
205 {
206  myProbingFactory = aProbingFactory;
207  myPreEstimations = aPreEstimations;
208  myVerbose = aVerbose;
209 }
210 
211 // ------------------------- Internals ------------------------------------
212 
213 // ------------------------------------------------------------------------
214 template < typename TSurface, typename TInternalProbingAlgorithm >
215 inline
216 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame
217 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
218 probingFrameFromSurfel (Surfel const& aSurfel) const
219 {
220  ASSERT(mySurface != nullptr);
221 
222  KSpace const& K = mySurface->container().space();
223 
224  typename KSpace::DirIterator q1 = K.sDirs(aSurfel), q2 = q1;
225  ++q2;
226 
227  // Incident linels
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);
233 
234  ASSERT(p1_1 == p2_1);
235 
236  Point myP = K.uCoords(p1_1),
237  myB1 = K.uCoords(p2_2) - myP,
238  myB2 = K.uCoords(p1_2) - myP;
239 
240  // Make the normal consistent with the exterior normal
241  Point n = myB1.crossProduct(myB2);
242 
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
247 
248  if (n.dot(n_interior) > 0) {
249  myP += myB2;
250  myB2 = -myB2;
251  }
252 
253  Point myNormal = myB1.crossProduct(myB2);
254  ASSERT(myNormal.dot(n_interior) < 0);
255 
256  return { myP, myB1, myB2, myNormal };
257 }
258 
259 // ------------------------------------------------------------------------
260 template < typename TSurface, typename TInternalProbingAlgorithm >
261 inline
262 std::pair<bool, typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame >
263 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
264 probingFrameWithPreEstimation (ProbingFrame const& aInitialFrame, RealPoint const& aPreEstimation) const
265 {
266  ProbingFrame frame = aInitialFrame;
267 
268  for (int i = 0; i < 4; ++i)
269  {
270  Point shift = frame.shift();
271  // We search for a frame whose shift point is outside...
272  if (! myPredicate(frame.p + shift))
273  {
274  //... and that matches the pre-estimation
275  int signs = 0;
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]));
279 
280  if (signs == 3)
281  {
282  return std::make_pair(true, frame);
283  }
284  }
285 
286  frame = frame.rotatedCopy();
287  }
288 
289  // No frame that matches the pre-estimation found,
290  return std::make_pair(false, aInitialFrame);
291 }
292 
293 ///////////////////////////////////////////////////////////////////////////////
294 // Interface - public :
295 
296 /**
297  * Writes/Displays the object on an output stream.
298  * @param out the output stream where the object is written.
299  */
300 template < typename TSurface, typename TInternalProbingAlgorithm >
301 inline
302 void
303 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::selfDisplay ( std::ostream & out ) const
304 {
305  out << "[PlaneProbingDigitalSurfaceLocalEstimator]";
306 }
307 
308 /**
309  * Checks the validity/consistency of the object.
310  * @return 'true' if the object is valid, 'false' otherwise.
311  */
312 template < typename TSurface, typename TInternalProbingAlgorithm >
313 inline
314 bool
315 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::isValid() const
316 {
317  return true;
318 }
319 
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
326 {
327  Surfel const& s = *it;
328 
329  if (myPreEstimations.count(s))
330  {
331  return myPreEstimations.at(s);
332  }
333  else
334  {
335  RealPoint preEstimation = myPreEstimationEstimator.eval(it);
336  myPreEstimations[s] = preEstimation; // cache the value for future calls
337  return preEstimation;
338  }
339 }
340 
341 
342 ///////////////////////////////////////////////////////////////////////////////
343 // Implementation of inline functions //
344 
345 template < typename TSurface, typename TInternalProbingAlgorithm >
346 inline
347 std::ostream&
348 DGtal::operator<< ( std::ostream & out,
349  const PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm> & object )
350 {
351  object.selfDisplay( out );
352  return out;
353 }
354 
355 // //
356 ///////////////////////////////////////////////////////////////////////////////
357 
358