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 ------------------------------
40template <typename T, DGtal::uint32_t p, typename P>
42DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::ExactPredicateLpPowerSeparableMetric()
45//------------------------------------------------------------------------------
46template <typename T, DGtal::uint32_t p, typename P>
48DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::~ExactPredicateLpPowerSeparableMetric()
51//------------------------------------------------------------------------------
52template <typename T, DGtal::uint32_t p, typename P>
54typename DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::Promoted
55DGtal::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//------------------------------------------------------------------------------
66template <typename T,DGtal::uint32_t p, typename P>
68typename DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::Weight
69DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::powerDistance (const Point &aP,
71 const Weight &aW) const
73 return exactDistanceRepresentation(aP,aQ) - aW;
75//------------------------------------------------------------------------------
76template <typename T, DGtal::uint32_t p, typename P>
79DGtal::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//------------------------------------------------------------------------------
99template <typename T, DGtal::uint32_t p, typename P>
101typename DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::Abscissa
102DGtal::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//------------------------------------------------------------------------------
135template <typename T, DGtal::uint32_t p , typename P>
138DGtal::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//------------------------------------------------------------------------------
211template <typename T, DGtal::uint32_t p, typename P>
214DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::selfDisplay ( std::ostream & out ) const
216 out << "[ExactPredicateLpPowerSeparableMetric] p="<<p;
218//------------------------------------------------------------------------------
219template <typename T, DGtal::uint32_t p, typename P>
222DGtal::ExactPredicateLpPowerSeparableMetric<T,p,P>::isValid() const
226//------------------------------------------------------------------------------
227template <typename T, DGtal::uint32_t p, typename P>
230DGtal::operator<< ( std::ostream & out,
231 const ExactPredicateLpPowerSeparableMetric<T,p,P> & object )
233 object.selfDisplay( out );
237///////////////////////////////////////////////////////////////////////////////
238// L_2 specialization //
239///////////////////////////////////////////////////////////////////////////////
242// ----------------------- Standard services ------------------------------
243template <typename T, typename P>
245DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::ExactPredicateLpPowerSeparableMetric()
248//------------------------------------------------------------------------------
249template <typename T, typename P>
251DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::~ExactPredicateLpPowerSeparableMetric()
254//------------------------------------------------------------------------------
255template <typename T, typename P>
257typename DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::Promoted
258DGtal::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//------------------------------------------------------------------------------
269template <typename T, typename P>
271typename DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::Weight
272DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::powerDistance (const Point &aP,
274 const Weight &aW) const
276 return exactDistanceRepresentation(aP,aQ) - aW;
278//------------------------------------------------------------------------------
279template <typename T, typename P>
282DGtal::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//------------------------------------------------------------------------------
303template <typename T, typename P>
306DGtal::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//------------------------------------------------------------------------------
337template <typename T, typename P>
340DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::selfDisplay ( std::ostream & out ) const
342 out << "[ExactPredicateLpPowerSeparableMetric] p=2";
344//------------------------------------------------------------------------------
345template <typename T, typename P>
348DGtal::ExactPredicateLpPowerSeparableMetric<T,2,P>::isValid() const