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 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
24 * Implementation of inline methods defined in ExactLpPowerSeparableMetric.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 template <typename T, DGtal::uint32_t p, typename P>
42 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::ExactPredicateLpPowerSeparableMetric()
45 //------------------------------------------------------------------------------
46 template <typename T, DGtal::uint32_t p, typename P>
48 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::~ExactPredicateLpPowerSeparableMetric()
51 //------------------------------------------------------------------------------
52 template <typename T, DGtal::uint32_t p, typename P>
54 typename DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::Promoted
55 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::exactDistanceRepresentation (const Point &aP,
56 const Point &aQ) const
58 Promoted res= NumberTraits<Promoted>::ZERO;
59 for(DGtal::Dimension d=0; d< Point::dimension ; ++d)
61 res += functions::power(static_cast<Promoted>(abs(aP[d]-aQ[d])), p);
65 //------------------------------------------------------------------------------
66 template <typename T,DGtal::uint32_t p, typename P>
68 typename DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::Weight
69 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::powerDistance (const Point &aP,
71 const Weight &aW) const
73 return exactDistanceRepresentation(aP,aQ) - aW;
75 //------------------------------------------------------------------------------
76 template <typename T, DGtal::uint32_t p, typename P>
79 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::closestPower (const Point &origin,
83 const Weight &ws) const
87 a = exactDistanceRepresentation(origin, first) - wf;
88 b = exactDistanceRepresentation(origin, second) - ws;
98 //------------------------------------------------------------------------------
99 template <typename T, DGtal::uint32_t p, typename P>
101 typename DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::Abscissa
102 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::binarySearchHidden(const Abscissa &udim,
103 const Abscissa &vdim,
106 const Abscissa &lower,
107 const Abscissa &upper) const
109 ASSERT( (nu + functions::power( static_cast<Promoted>(abs( udim - lower)), p)) <
110 (nv + functions::power( static_cast<Promoted>(abs( vdim - lower)), p)));
113 if ( (upper - lower) <= NumberTraits<Abscissa>::ONE)
116 Promoted nuUpdated = nu + functions::power( static_cast<Promoted>(abs( udim - upper )), p);
117 Promoted nvUpdated = nv + functions::power( static_cast<Promoted>(abs( vdim - upper )), p);
118 if (nuUpdated < nvUpdated)
124 Abscissa mid = (lower + upper)/2;
125 Promoted nuUpdated = nu + functions::power( static_cast<Promoted>(abs( udim - mid )), p);
126 Promoted nvUpdated = nv + functions::power( static_cast<Promoted>(abs( vdim - mid )), p);
129 if ( nuUpdated < nvUpdated)
130 return binarySearchHidden(udim,vdim,nu,nv,mid,upper);
132 return binarySearchHidden(udim,vdim,nu,nv,lower,mid);
134 //------------------------------------------------------------------------------
135 template <typename T, DGtal::uint32_t p , typename P>
138 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::hiddenByPower(const Point &u,
144 const Point &startingPoint,
145 const Point &endPoint,
146 const typename Point::UnsignedComponent dim) const
148 //Interval bound for the binary search
149 Abscissa lower = startingPoint[dim];
150 Abscissa upper = endPoint[dim];
152 //Partial norm computation
153 // (sum_{i!=dim} |u_i-v_i|^p
157 for(DGtal::Dimension i = 0 ; i < Point::dimension ; i++)
160 nu += functions::power ( static_cast<Promoted>(abs(u[i] - startingPoint[i])) , p);
161 nv += functions::power ( static_cast<Promoted>(abs(v[i] - startingPoint[i] )) , p);
162 nw += functions::power ( static_cast<Promoted>(abs(w[i] - startingPoint[i] )) , p);
165 //Abscissa of voronoi edges
167 Promoted dv,dw,du,ddv,ddw;
169 //checking distances to lower bound
170 du = nu + functions::power( static_cast<Promoted>(abs( u[dim] - lower)), p);
171 dv = nv + functions::power( static_cast<Promoted>(abs( v[dim] - lower)), p);
172 dw = nw + functions::power( static_cast<Promoted>(abs( w[dim] - lower)), p);
174 //Precondition of binarySearchHidden is true
177 uv = binarySearchHidden(u[dim],v[dim],nu,nv,lower,upper);
180 vw = binarySearchHidden(v[dim],w[dim],nv,nw,lower,upper); //precondition
188 //check if uv + 1 is stricly in W
190 //first, optimisation
191 if (uv == upper) return true;
194 ddv = nv + functions::power( static_cast<Promoted>(abs( v[dim] - uv -1)), p);
195 ddw = nw + functions::power( static_cast<Promoted>(abs( w[dim] - uv -1)), p);
210 //------------------------------------------------------------------------------
211 template <typename T, DGtal::uint32_t p, typename P>
214 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::selfDisplay ( std::ostream & out ) const
216 out << "[ExactPredicateLpPowerSeparableMetric] p="<<p;
218 //------------------------------------------------------------------------------
219 template <typename T, DGtal::uint32_t p, typename P>
222 DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::isValid() const
226 //------------------------------------------------------------------------------
227 template <typename T, DGtal::uint32_t p, typename P>
230 DGtal::operator<< ( std::ostream & out,
231 const ExactPredicateLpPowerSeparableMetric<T,p,P> & object )
233 object.selfDisplay( out );
237 ///////////////////////////////////////////////////////////////////////////////
238 // L_2 specialization //
239 ///////////////////////////////////////////////////////////////////////////////
242 // ----------------------- Standard services ------------------------------
243 template <typename T, typename P>
245 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::ExactPredicateLpPowerSeparableMetric()
248 //------------------------------------------------------------------------------
249 template <typename T, typename P>
251 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::~ExactPredicateLpPowerSeparableMetric()
254 //------------------------------------------------------------------------------
255 template <typename T, typename P>
257 typename DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::Promoted
258 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::exactDistanceRepresentation (const Point &aP,
259 const Point &aQ) const
261 Promoted res= NumberTraits<Promoted>::ZERO;
262 for(DGtal::Dimension d=0; d< Point::dimension ; ++d)
264 res += static_cast<Promoted>(aP[d]-aQ[d])*static_cast<Promoted>(aP[d]-aQ[d]);
268 //------------------------------------------------------------------------------
269 template <typename T, typename P>
271 typename DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::Weight
272 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::powerDistance (const Point &aP,
274 const Weight &aW) const
276 return exactDistanceRepresentation(aP,aQ) - aW;
278 //------------------------------------------------------------------------------
279 template <typename T, typename P>
282 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::closestPower (const Point &origin,
286 const Weight &wS) const
288 Promoted a=NumberTraits<Promoted>::ZERO,
289 b=NumberTraits<Promoted>::ZERO;
291 a = exactDistanceRepresentation(origin,first) - wF;
292 b = exactDistanceRepresentation(origin,second) - wS;
298 return ClosestSECOND;
302 //------------------------------------------------------------------------------
303 template <typename T, typename P>
306 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::hiddenByPower(const Point &u,
312 const Point &startingPoint,
313 const Point &/*endPoint*/,
314 const typename Point::UnsignedComponent dim) const
326 for(DGtal::Dimension i = 0 ; i < Point::dimension ; i++)
329 d2_u += static_cast<Promoted>(u[i] - startingPoint[i] ) *static_cast<Promoted>(u[i] - startingPoint[i] );
330 d2_v += static_cast<Promoted>(v[i] - startingPoint[i] ) *static_cast<Promoted>(v[i] - startingPoint[i] );
331 d2_w += static_cast<Promoted>(w[i] - startingPoint[i] ) *static_cast<Promoted>(w[i] - startingPoint[i] );
334 return (c * d2_v - b*d2_u - a*d2_w - a*b*c) > 0 ;
336 //------------------------------------------------------------------------------
337 template <typename T, typename P>
340 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::selfDisplay ( std::ostream & out ) const
342 out << "[ExactPredicateLpPowerSeparableMetric] p=2";
344 //------------------------------------------------------------------------------
345 template <typename T, typename P>
348 DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::isValid() const