DGtal  1.2.0
PlaneProbingRNeighborhood.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/04
23  *
24  * Implementation of inline methods defined in PlaneProbingRNeighborhood.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 TPredicate >
43 inline
44 DGtal::PlaneProbingRNeighborhood<TPredicate>::
45 PlaneProbingRNeighborhood(Predicate const& aPredicate, Point const& aQ, Triangle const& aM)
46  : DGtal::PlaneProbingNeighborhood<TPredicate>(aPredicate, aQ, aM)
47 {}
48 
49 // ------------------------------------------------------------------------
50 template < typename TPredicate >
51 inline
52 DGtal::PlaneProbingRNeighborhood<TPredicate>::
53 ~PlaneProbingRNeighborhood()
54 {}
55 
56 ///////////////////////////////////////////////////////////////////////////////
57 // ----------------------- Plane Probing services ------------------------------
58 
59 // ------------------------------------------------------------------------
60 template < typename TPredicate >
61 inline
62 typename DGtal::PlaneProbingRNeighborhood<TPredicate>::HexagonState
63 DGtal::PlaneProbingRNeighborhood<TPredicate>::hexagonState ()
64 {
65  this->myCandidates.clear();
66 
67  std::array< bool, 6 > state = { false, false, false, false, false, false };
68  for (int i = 0; i < 6; ++i)
69  {
70  PointOnProbingRay r = this->myNeighborhood[i].getBase();
71 
72  if (this->isNeighbor(r))
73  {
74  state[i] = this->myPredicate(this->absolutePoint(r));
75 
76  if (state[i])
77  {
78  PointOnProbingRay pt = closestPointOnRayLogWithPredicate(r.getBase());
79 
80  assert(pt == closestPointOnRayLinearWithPredicate(r.getBase()));
81  assert(this->myPredicate(this->absolutePoint(pt)));
82 
83  this->myCandidates.push_back(pt);
84  }
85  }
86  }
87 
88  return this->classify(state);
89 }
90 
91 // ------------------------------------------------------------------------
92 template < typename TPredicate >
93 inline
94 typename DGtal::PlaneProbingRNeighborhood<TPredicate>::PointOnProbingRay
95 DGtal::PlaneProbingRNeighborhood<TPredicate>::closestPointOnRayLogWithPredicate (PointOnProbingRay const& aRay) const
96 {
97  assert(this->myPredicate(this->absolutePoint(aRay)));
98 
99  // Exponential march
100  PointOnProbingRay Xk = aRay, Xl = aRay.next(1);
101  while (this->myPredicate(this->absolutePoint(Xk)) &&
102  this->isSmallest(this->relativePoint(Xk), this->relativePoint(Xl))) {
103  Integer d = Xl.index() - Xk.index();
104  Xk = Xl;
105  Xl = Xl.next(2 * d);
106  }
107  Xk = Xk.previous(Integer((Xl.index() - Xk.index()) / 2));
108 
109  // Binary search
110  Integer d = Xl.index() - Xk.index();
111  while (d > 4) {
112  assert(this->myPredicate(this->absolutePoint(Xk)));
113 
114  PointOnProbingRay Xalpha = Xk.next(Integer(d / 4)),
115  Xbeta = Xk.next(Integer(d / 2)),
116  Xgamma = Xk.next(Integer(3*d/4));
117 
118  assert(Xk.index() < Xalpha.index() && Xalpha.index() < Xbeta.index() &&
119  Xbeta.index() < Xgamma.index() && Xgamma.index() < Xl.index());
120 
121  if (this->myPredicate(this->absolutePoint(Xbeta)) &&
122  this->isSmallest(this->relativePoint(Xbeta), this->relativePoint(Xgamma))) {
123  Xk = Xbeta;
124  } else if (! this->myPredicate(this->absolutePoint(Xalpha)) ||
125  this->isSmallest(this->relativePoint(Xbeta), this->relativePoint(Xalpha))) {
126  Xl = Xbeta;
127  } else {
128  Xk = Xalpha;
129  Xl = Xgamma;
130  }
131 
132  d = Xl.index() - Xk.index();
133  }
134 
135  return closestPointOnRayLinearWithPredicate(Xk);
136 }
137 
138 // ------------------------------------------------------------------------
139 template < typename TPredicate >
140 inline
141 typename DGtal::PlaneProbingRNeighborhood<TPredicate>::PointOnProbingRay
142 DGtal::PlaneProbingRNeighborhood<TPredicate>::closestPointOnRayLinearWithPredicate (PointOnProbingRay const& aRay) const
143 {
144  assert(this->myPredicate(this->absolutePoint(aRay)));
145 
146  PointOnProbingRay previousX = aRay, currentX = previousX.next(1);
147  while (this->myPredicate(this->absolutePoint(currentX)) &&
148  this->isSmallest(this->relativePoint(previousX), this->relativePoint(currentX))) {
149  previousX = currentX;
150  currentX = previousX.next(1);
151  }
152 
153  assert(this->myPredicate(this->absolutePoint(previousX)));
154 
155  return previousX;
156 }
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 // Interface - public :
160 
161 /**
162  * Writes/Displays the object on an output stream.
163  * @param out the output stream where the object is written.
164  */
165 template <typename TPredicate>
166 inline
167 void
168 DGtal::PlaneProbingRNeighborhood<TPredicate>::selfDisplay ( std::ostream & out ) const
169 {
170  out << "[PlaneProbingRNeighborhood]";
171 }
172 
173 /**
174  * Checks the validity/consistency of the object.
175  * @return 'true' if the object is valid, 'false' otherwise.
176  */
177 template <typename TPredicate>
178 inline
179 bool
180 DGtal::PlaneProbingRNeighborhood<TPredicate>::isValid() const
181 {
182  return true;
183 }
184 
185 
186 
187 ///////////////////////////////////////////////////////////////////////////////
188 // Implementation of inline functions //
189 
190 template <typename TPredicate>
191 inline
192 std::ostream&
193 DGtal::operator<< ( std::ostream & out,
194  const PlaneProbingRNeighborhood<TPredicate> & object )
195 {
196  object.selfDisplay( out );
197  return out;
198 }
199 
200 // //
201 ///////////////////////////////////////////////////////////////////////////////
202 
203