DGtal  0.9.2
SeparableMetricAdapter.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 David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2013/21/04
23  *
24  * Implementation of inline methods defined in ExactLpSeparableMetric.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 //------------------------------------------------------------------------------
40 template <typename TM>
41 inline
42 typename DGtal::SeparableMetricAdapter<TM>::Abscissa
43 DGtal::SeparableMetricAdapter<TM>::binarySearchHidden(const Point &u,
44  const Point &v,
45  const typename Point::UnsignedComponent dim,
46  const Point &lower,
47  const Point &upper) const
48 {
49  //Recurrence stop
50  if ( (upper[dim] - lower[dim]) <= NumberTraits<Abscissa>::ONE)
51  {
52  //testing upper
53  Value du = myMetric->operator()(u, upper);
54  Value dv = myMetric->operator()(v, upper);
55  if (du < dv)
56  return upper[dim];
57  else
58  return lower[dim];
59  }
60 
61  Point mid = lower;
62  mid[dim] = (lower[dim] + upper[dim])/2;
63 
64  Value duUpdated = myMetric->operator()(u, mid);
65  Value dvUpdated = myMetric->operator()(v, mid);
66 
67  //Recursive call
68  if ( duUpdated < dvUpdated)
69  return binarySearchHidden(u,v,dim,mid,upper);
70  else
71  return binarySearchHidden(u,v,dim,lower,mid);
72 
73 }
74 //------------------------------------------------------------------------------
75 template <typename TM>
76 inline
77 bool
78 DGtal::SeparableMetricAdapter<TM>::hiddenBy(const Point &u,
79  const Point &v,
80  const Point &w,
81  const Point &startingPoint,
82  const Point &endPoint,
83  const typename Point::UnsignedComponent dim) const
84 {
85  //Abscissa of voronoi edges
86  Abscissa uv,vw;
87  Value dv,dw,du,ddv,ddw;
88 
89  //checking distances to lower bound
90  du = myMetric->operator()(u, startingPoint);
91  dv = myMetric->operator()(v, startingPoint);
92  dw = myMetric->operator()(w, startingPoint);
93 
94  //Precondition of binarySearchHidden is true
95  if (du < dv )
96  {
97  uv = binarySearchHidden(u,v,dim,startingPoint,endPoint);
98  if (dv < dw)
99  {
100  vw = binarySearchHidden(v,w,dim,startingPoint,endPoint); //precondition
101  return (uv > vw);
102  }
103 
104  if (dw > dv)
105  return true;
106  else
107  {
108  //check if uv + 1 is stricly in W
109  //first, optimisation
110  if (uv == endPoint[dim]) return true;
111 
112  //distances at uv+1
113  Point p = startingPoint;
114  p[dim] = uv + 1;
115  ddv = myMetric->operator()(v, p);
116  ddw = myMetric->operator()(w, p);
117 
118  if (ddw < ddv)
119  return true;
120  else
121  return false;
122  }
123  }
124  else // du >= dv
125  {
126  if (dv <= dw)
127  return false;
128  else
129  return true;
130  }
131 }
132 //------------------------------------------------------------------------------
133 template <typename TM>
134 inline
135 void
136 DGtal::SeparableMetricAdapter<TM>::selfDisplay ( std::ostream & out ) const
137 {
138  out << "[SeparableMetricAdapter] metric="<<myMetric;
139 }
140 //------------------------------------------------------------------------------
141 template <typename TM>
142 inline
143 std::ostream&
144 DGtal::operator<< ( std::ostream & out,
145  const SeparableMetricAdapter<TM> & object )
146 {
147  object.selfDisplay( out );
148  return out;
149 }