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 PlaneProbingR1Neighborhood.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 TPredicate >
44 DGtal::PlaneProbingR1Neighborhood<TPredicate>::
45 PlaneProbingR1Neighborhood(Predicate const& aPredicate, Point const& aQ, Triangle const& aM)
46 : DGtal::PlaneProbingRNeighborhood<TPredicate>(aPredicate, aQ, aM)
49 // ------------------------------------------------------------------------
50 template < typename TPredicate >
52 DGtal::PlaneProbingR1Neighborhood<TPredicate>::
53 ~PlaneProbingR1Neighborhood()
56 ///////////////////////////////////////////////////////////////////////////////
57 // ----------------------- Plane Probing services ------------------------------
59 // ------------------------------------------------------------------------
60 template < typename TPredicate >
62 typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::HexagonState
63 DGtal::PlaneProbingR1Neighborhood<TPredicate>::hexagonState ()
65 this->myCandidates.clear();
67 int code = getNeighborhoodCode();
70 // One vertex of the H-neighborhood is in P
72 this->myCandidates.push_back(PointOnProbingRay({ 0, 1, 2 }, 0));
76 this->myCandidates.push_back(PointOnProbingRay({ 0, 2, 1 }, 0));
80 this->myCandidates.push_back(PointOnProbingRay({ 1, 2, 0 }, 0));
84 this->myCandidates.push_back(PointOnProbingRay({ 1, 0, 2 }, 0));
88 this->myCandidates.push_back(PointOnProbingRay({ 2, 0, 1 }, 0));
92 this->myCandidates.push_back(PointOnProbingRay({ 2, 1, 0 }, 0));
95 // Two vertices of the H-neighborhood are in P
97 this->myCandidates.push_back(PointOnProbingRay({ 0, 2, 1 }, 0));
98 this->myCandidates.push_back(PointOnProbingRay({ 1, 2, 0 }, 0));
102 this->myCandidates.push_back(PointOnProbingRay({ 1, 0, 2 }, 0));
103 this->myCandidates.push_back(PointOnProbingRay({ 2, 0, 1 }, 0));
107 this->myCandidates.push_back(PointOnProbingRay({ 2, 1, 0 }, 0));
108 this->myCandidates.push_back(PointOnProbingRay({ 0, 1, 2 }, 0));
112 this->myCandidates.push_back(closestRayPoint(candidateRay(0)));
116 this->myCandidates.push_back(closestRayPoint(candidateRay(1)));
120 this->myCandidates.push_back(closestRayPoint(candidateRay(2)));
123 // Three vertices of the H-neighborhood are in P
126 std::pair<PointOnProbingRay, PointOnProbingRay> candidate = candidateRay(0);
127 std::vector<PointOnProbingRay> shortList = { candidate.second, PointOnProbingRay({ 2, 1, 0 }, 0) };
128 PointOnProbingRay closest = this->closestPointInList(shortList);
129 //PointOnProbingRay closest = this->closestPointInList({ candidate.second, PointOnProbingRay({ 2, 1, 0 }, 0) });
130 this->myCandidates.push_back(closestRayPoint(std::make_pair(candidate.first, closest)));
136 std::pair<PointOnProbingRay, PointOnProbingRay> candidate = candidateRay(0);
137 std::vector<PointOnProbingRay> shortList = { candidate.second, PointOnProbingRay({ 1, 2, 0 }, 0) };
138 PointOnProbingRay closest = this->closestPointInList(shortList);
139 this->myCandidates.push_back(closestRayPoint(std::make_pair(candidate.first, closest)));
145 std::pair<PointOnProbingRay, PointOnProbingRay> candidate = candidateRay(1);
146 std::vector<PointOnProbingRay> shortList = { candidate.second, PointOnProbingRay({ 0, 2, 1 }, 0) };
147 PointOnProbingRay closest = this->closestPointInList(shortList);
148 this->myCandidates.push_back(closestRayPoint(std::make_pair(candidate.first, closest)));
154 std::pair<PointOnProbingRay, PointOnProbingRay> candidate = candidateRay(1);
155 std::vector<PointOnProbingRay> shortList = { candidate.second, PointOnProbingRay({ 2, 0, 1 }, 0) };
156 PointOnProbingRay closest = this->closestPointInList(shortList);
157 this->myCandidates.push_back(closestRayPoint(std::make_pair(candidate.first, closest)));
163 std::pair<PointOnProbingRay, PointOnProbingRay> candidate = candidateRay(2);
164 std::vector<PointOnProbingRay> shortList = { candidate.second, PointOnProbingRay({ 1, 0, 2 }, 0) };
165 PointOnProbingRay closest = this->closestPointInList(shortList);
166 this->myCandidates.push_back(closestRayPoint(std::make_pair(candidate.first, closest)));
172 std::pair<PointOnProbingRay, PointOnProbingRay> candidate = candidateRay(2);
173 std::vector<PointOnProbingRay> shortList = { candidate.second, PointOnProbingRay({ 0, 1, 2 }, 0) };
174 PointOnProbingRay closest = this->closestPointInList(shortList);
175 this->myCandidates.push_back(closestRayPoint(std::make_pair(candidate.first, closest)));
183 return this->classify(myState);
186 // ------------------------------------------------------------------------
187 template < typename TPredicate >
189 int DGtal::PlaneProbingR1Neighborhood<TPredicate>::getNeighborhoodCode () const
193 myState = { false, false, false, false, false, false };
194 for (int i = 0; i < 6; ++i) {
195 PointOnProbingRay const& r = this->myNeighborhood[i];
197 // We skip the ray if it is not part of the rays that should be considered at this step
198 if (! this->isNeighbor(r))
203 myState[i] = this->myPredicate(this->absolutePoint(r));
213 // ------------------------------------------------------------------------
214 template < typename TPredicate >
216 std::pair<typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::PointOnProbingRay,
217 typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::PointOnProbingRay>
218 DGtal::PlaneProbingR1Neighborhood<TPredicate>::candidateRay (Index const& index) const
220 Index indexM1 = (index + 2) % 3, indexM2 = (index + 1) % 3;
221 PointOnProbingRay r1({ index, indexM1, indexM2 }, 0),
222 r2({ index, indexM2, indexM1 }, 0);
224 if (detail::squaredNorm(this->myM[indexM1]) >= detail::squaredNorm(this->myM[indexM2])) {
225 return std::make_pair(r1, r2.getBase());
227 return std::make_pair(r2, r1.getBase());
231 // ------------------------------------------------------------------------
232 template < typename TPredicate >
234 std::vector<typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::PointOnProbingRay>
235 DGtal::PlaneProbingR1Neighborhood<TPredicate>::intersectSphereRay (PointOnProbingRay const& aPoint, PointOnProbingRay const& aRay) const
237 using NumberTraits = DGtal::NumberTraits<Integer>;
239 PointOnProbingRay startingPoint = aRay.getBase();
240 assert(this->myPredicate(this->absolutePoint(startingPoint)));
242 Point origin = -this->myM[aRay.sigma(0)],
243 y = this->myM[aRay.sigma(1)],
244 x = this->myM[aRay.sigma(2)];
246 Integer a = detail::squaredNorm(x),
247 b = detail::distToSphere<Point>({ -this->myM[0], -this->myM[1], -this->myM[2],
248 this->relativePoint(aPoint), origin + x }) - a + 2*x.dot(y),
249 c = detail::distToSphere<Point>({ -this->myM[0], -this->myM[1], -this->myM[2],
250 this->relativePoint(aPoint), origin + y });
251 Integer delta = b*b - 4*a*c;
253 std::vector<PointOnProbingRay> res;
259 Integer i1 = std::ceil(double(-b - sqrt(delta)) / (2*a) ),
260 i2 = std::floor(double(-b + sqrt(delta)) / (2*a) );
261 Integer zero = NumberTraits::ZERO;
262 if (i1 <= i2 && i2 >= zero) {
263 i1 = std::max(zero, i1);
265 PointOnProbingRay p1(aRay.sigma(), i1), p2(aRay.sigma(), i2);
267 // Case of cospherical points
268 if (detail::distToSphere<Point>({ -this->myM[0], -this->myM[1], -this->myM[2],
269 this->relativePoint(aPoint) , this->relativePoint(p1) }) == 0) {
270 if (this->absolutePoint(p1) >= this->absolutePoint(aPoint)) {
275 if (detail::distToSphere<Point>({ -this->myM[0], -this->myM[1], -this->myM[2],
276 this->relativePoint(aPoint) , this->relativePoint(p2) }) == 0) {
277 if (this->absolutePoint(p2) >= this->absolutePoint(aPoint)) {
286 p1 = PointOnProbingRay(aRay.sigma(), i1);
287 p2 = PointOnProbingRay(aRay.sigma(), i2);
289 // Add extremal points to the list
297 // ------------------------------------------------------------------------
298 template < typename TPredicate >
301 DGtal::PlaneProbingR1Neighborhood<TPredicate>::isValidIntersectSphereRay (PointOnProbingRay const& aPoint, PointOnProbingRay const& aRay,
302 std::vector<PointOnProbingRay> const& aLst) const
304 PointOnProbingRay startingPoint = aRay.getBase();
305 assert(this->myPredicate(this->absolutePoint(startingPoint)));
307 Point refPoint = this->relativePoint(aPoint);
310 if (aLst.size() > 0) {
311 PointOnProbingRay currentX = startingPoint;
312 while (! this->isSmallest(refPoint, this->relativePoint(currentX))) {
313 currentX = currentX.next(1);
316 PointOnProbingRay first = currentX;
317 while (this->isSmallest(refPoint, this->relativePoint(currentX))) {
318 currentX = currentX.next(1);
321 PointOnProbingRay last = currentX.previous(1);
322 return (aLst.size() == 2) && (first == aLst[0]) && (last == aLst[1]);
325 PointOnProbingRay currentX = startingPoint;
326 while (res && currentX.position() < n) {
327 res = !this->isSmallest(refPoint, this->relativePoint(currentX));
328 currentX = currentX.next(1);
335 // ------------------------------------------------------------------------
336 template < typename TPredicate >
338 typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::PointOnProbingRay
339 DGtal::PlaneProbingR1Neighborhood<TPredicate>::closestPointOnRayConstant (PointOnProbingRay const& aRay) const
341 using NumberTraits = DGtal::NumberTraits<Integer>;
343 PointOnProbingRay startingPoint = aRay.getBase();
344 assert(this->myPredicate(this->absolutePoint(startingPoint)));
346 Point origin = -this->myM[aRay.sigma(0)],
347 u = this->myM[aRay.sigma(1)],
348 v = this->myM[aRay.sigma(2)];
350 Integer a = detail::squaredNorm(v), b = 3*a,
351 c = 2 * u.dot(v) + detail::distToSphere<Point>({ -this->myM[0], -this->myM[1], -this->myM[2],
352 origin + u , origin + v });
353 Integer delta = b*b - 4*a*c;
356 Integer index = std::ceil(double(-b + sqrt(delta)) / (2*a));
357 index = std::max(NumberTraits::ZERO, index);
359 // Case of cospherical points
360 PointOnProbingRay p1(aRay.sigma(), index), p2(aRay.sigma(), index+1);
362 if (detail::distToSphere<Point>({ -this->myM[0], -this->myM[1], -this->myM[2],
363 this->relativePoint(p1), this->relativePoint(p2) }) == 0) {
364 if (this->absolutePoint(p1) >= this->absolutePoint(p2)) {
369 return PointOnProbingRay(aRay.sigma(), index);
371 return aRay.getBase();
375 // ------------------------------------------------------------------------
376 template < typename TPredicate >
378 typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::PointOnProbingRay
379 DGtal::PlaneProbingR1Neighborhood<TPredicate>::closestPointOnRayLinear (PointOnProbingRay const& aRay) const
381 PointOnProbingRay startingPoint = aRay.getBase();
382 assert(this->myPredicate(this->absolutePoint(startingPoint)));
384 PointOnProbingRay previousX = startingPoint;
385 PointOnProbingRay currentX = previousX.next(1);
386 while (this->isSmallest(this->relativePoint(previousX), this->relativePoint(currentX))) {
387 previousX = currentX;
388 currentX = previousX.next(1);
394 // ------------------------------------------------------------------------
395 template < typename TPredicate >
397 typename DGtal::PlaneProbingR1Neighborhood<TPredicate>::PointOnProbingRay
398 DGtal::PlaneProbingR1Neighborhood<TPredicate>::closestRayPoint (std::pair<PointOnProbingRay, PointOnProbingRay> const& aRayPoint) const
400 PointOnProbingRay const& ray = aRayPoint.first;
401 PointOnProbingRay const& point = aRayPoint.second;
403 PointOnProbingRay res = point;
404 std::vector<PointOnProbingRay> intersections = intersectSphereRay(point, ray);
405 assert(isValidIntersectSphereRay(point, ray, intersections));
407 if (intersections.size() > 0) {
408 PointOnProbingRay y1 = intersections[0], y2 = intersections[1];
409 if (this->myPredicate(this->absolutePoint(y1))) {
410 PointOnProbingRay y = closestPointOnRayConstant(ray);
411 assert(y == closestPointOnRayLinear(ray));
412 if (y1 <= y && y <= y2) {
413 if (this->myPredicate(this->absolutePoint(y))) {
416 res = this->closestPointOnRayLogWithPredicate(y1);
417 assert(res == this->closestPointOnRayLinearWithPredicate(ray.getBase()));
426 ///////////////////////////////////////////////////////////////////////////////
427 // Interface - public :
430 * Writes/Displays the object on an output stream.
431 * @param out the output stream where the object is written.
433 template <typename TPredicate>
436 DGtal::PlaneProbingR1Neighborhood<TPredicate>::selfDisplay ( std::ostream & out ) const
438 out << "[PlaneProbingR1Neighborhood]";
442 * Checks the validity/consistency of the object.
443 * @return 'true' if the object is valid, 'false' otherwise.
445 template <typename TPredicate>
448 DGtal::PlaneProbingR1Neighborhood<TPredicate>::isValid() const
455 ///////////////////////////////////////////////////////////////////////////////
456 // Implementation of inline functions //
458 template <typename TPredicate>
461 DGtal::operator<< ( std::ostream & out,
462 const PlaneProbingR1Neighborhood<TPredicate> & object )
464 object.selfDisplay( out );
469 ///////////////////////////////////////////////////////////////////////////////