DGtal  1.2.0
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 using the pre-estimation
118  Surfel s = *it;
119  ProbingFrame initialFrame = probingFrameFromSurfel(s);
120  ProbingFrame frame = probingFrameWithPreEstimation(initialFrame, preEstimation);
121 
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)
126  try
127  {
128  myProbingAlgorithm = myProbingFactory(frame, myPredicate);
129  }
130  catch (std::runtime_error const& e)
131  {
132  return frame.normal;
133  }
134 
135  // We use slightly different versions depending on the number of zeros
136  // in the pre-estimation vector.
137  const auto zeros = findZeros(preEstimation);
138 
139  Point normal;
140  if (zeros.size() == 0)
141  {
142  normal = myProbingAlgorithm->compute();
143  }
144  else if (zeros.size() == 1)
145  {
146  int index = zeros[0];
147  normal = myProbingAlgorithm->compute(getProbingRaysOneFlatDirection(index));
148  // normal = getNormalOneFlatDirection(index);
149  }
150  else if (zeros.size() == 2)
151  {
152  normal = frame.normal;
153  }
154 
155  delete myProbingAlgorithm;
156  myProbingAlgorithm = nullptr;
157 
158  return normal;
159 }
160 
161 // ------------------------------------------------------------------------
162 template < typename TSurface, typename TInternalProbingAlgorithm >
163 template < typename SurfelConstIterator, typename OutputIterator >
164 inline
165 OutputIterator
166 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
167 eval (SurfelConstIterator itb, SurfelConstIterator ite, OutputIterator out)
168 {
169  for (auto it = itb; it != ite; ++it)
170  {
171  *out++ = eval(it);
172  }
173 
174  return out;
175 }
176 
177 // ------------------------------------------------------------------------
178 template < typename TSurface, typename TInternalProbingAlgorithm >
179 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::Scalar
180 inline
181 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
182 h () const
183 {
184  return myH;
185 }
186 
187 // --------------- model of CDigitalSurfaceLocalEstimator ------------------
188 
189 // ------------------------------------------------------------------------
190 template < typename TSurface, typename TInternalProbingAlgorithm >
191 inline
192 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
193 attach (ConstAlias<Surface> aSurface)
194 {
195  mySurface = aSurface;
196  myPredicate = Predicate(mySurface);
197  myPreEstimationEstimator = PreEstimation(mySurface);
198 }
199 
200 // ------------------------------------------------------------------------
201 template < typename TSurface, typename TInternalProbingAlgorithm >
202 inline
203 void DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
204 setParams (ProbingFactory const& aProbingFactory,
205  std::unordered_map<Surfel, RealPoint> const& aPreEstimations,
206  bool aVerbose)
207 {
208  myProbingFactory = aProbingFactory;
209  myPreEstimations = aPreEstimations;
210  myVerbose = aVerbose;
211 }
212 
213 // ------------------------- Internals ------------------------------------
214 
215 // ------------------------------------------------------------------------
216 template < typename TSurface, typename TInternalProbingAlgorithm >
217 inline
218 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame
219 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
220 probingFrameFromSurfel (Surfel const& aSurfel) const
221 {
222  ASSERT(mySurface != nullptr);
223 
224  KSpace const& K = mySurface->container().space();
225 
226  typename KSpace::DirIterator q1 = K.sDirs(aSurfel), q2 = q1;
227  ++q2;
228 
229  // Incident linels
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);
235 
236  ASSERT(p1_1 == p2_1);
237 
238  Point myP = K.uCoords(p1_1),
239  myB1 = K.uCoords(p2_2) - myP,
240  myB2 = K.uCoords(p1_2) - myP;
241 
242  // Make the normal consistent with the exterior normal
243  Point n = myB1.crossProduct(myB2);
244 
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
249 
250  if (n.dot(n_interior) > 0) {
251  myP += myB2;
252  myB2 = -myB2;
253  }
254 
255  Point myNormal = myB1.crossProduct(myB2);
256  ASSERT(myNormal.dot(n_interior) < 0);
257 
258  return { myP, myB1, myB2, myNormal };
259 }
260 
261 // ------------------------------------------------------------------------
262 template < typename TSurface, typename TInternalProbingAlgorithm >
263 inline
264 typename DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::ProbingFrame
265 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::
266 probingFrameWithPreEstimation (ProbingFrame const& aInitialFrame, RealPoint const& aPreEstimation) const
267 {
268  ProbingFrame frame = aInitialFrame;
269  ProbingFrame frameQExt;
270 
271  for (int i = 0; i < 4; ++i)
272  {
273  Point shift = frame.shift();
274  if (! myPredicate(frame.p + shift))
275  {
276  frameQExt = frame;
277 
278  int signs = 0;
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]));
282 
283  if (signs == 3)
284  {
285  return frame;
286  }
287  }
288 
289  frame = frame.rotatedCopy();
290  }
291 
292  return frameQExt;
293 }
294 
295 ///////////////////////////////////////////////////////////////////////////////
296 // Interface - public :
297 
298 /**
299  * Writes/Displays the object on an output stream.
300  * @param out the output stream where the object is written.
301  */
302 template < typename TSurface, typename TInternalProbingAlgorithm >
303 inline
304 void
305 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::selfDisplay ( std::ostream & out ) const
306 {
307  out << "[PlaneProbingDigitalSurfaceLocalEstimator]";
308 }
309 
310 /**
311  * Checks the validity/consistency of the object.
312  * @return 'true' if the object is valid, 'false' otherwise.
313  */
314 template < typename TSurface, typename TInternalProbingAlgorithm >
315 inline
316 bool
317 DGtal::PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm>::isValid() const
318 {
319  return true;
320 }
321 
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
328 {
329  Surfel const& s = *it;
330 
331  if (myPreEstimations.count(s))
332  {
333  return myPreEstimations.at(s);
334  }
335  else
336  {
337  RealPoint preEstimation = myPreEstimationEstimator.eval(it);
338  myPreEstimations[s] = preEstimation; // cache the value for future calls
339  return preEstimation;
340  }
341 }
342 
343 
344 ///////////////////////////////////////////////////////////////////////////////
345 // Implementation of inline functions //
346 
347 template < typename TSurface, typename TInternalProbingAlgorithm >
348 inline
349 std::ostream&
350 DGtal::operator<< ( std::ostream & out,
351  const PlaneProbingDigitalSurfaceLocalEstimator<TSurface, TInternalProbingAlgorithm> & object )
352 {
353  object.selfDisplay( out );
354  return out;
355 }
356 
357 // //
358 ///////////////////////////////////////////////////////////////////////////////
359 
360