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 InexactLpSeparableMetric.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, typename V>
42DGtal::InexactPredicateLpSeparableMetric<T,V>::InexactPredicateLpSeparableMetric(const double anExponent):
43 myExponent(anExponent)
46//------------------------------------------------------------------------------
47template <typename T, typename V>
49DGtal::InexactPredicateLpSeparableMetric<T,V>::~InexactPredicateLpSeparableMetric()
52//------------------------------------------------------------------------------
53template <typename T, typename V>
55typename DGtal::InexactPredicateLpSeparableMetric<T,V>::Value
56DGtal::InexactPredicateLpSeparableMetric<T,V>::rawDistance (const Point &aP,
57 const Point &aQ) const
60 for(DGtal::Dimension d=0; d< Point::dimension ; ++d)
62 res += std::pow(static_cast<Value>(abs(aP[d]-aQ[d])), myExponent);
66//------------------------------------------------------------------------------
67template <typename T, typename V>
69typename DGtal::InexactPredicateLpSeparableMetric<T,V>::Value
70DGtal::InexactPredicateLpSeparableMetric<T,V>::operator() (const Point &aP,
71 const Point &aQ) const
73 return std::pow( rawDistance(aP,aQ) , 1.0/myExponent);
75//------------------------------------------------------------------------------
76template <typename T, typename V>
79DGtal::InexactPredicateLpSeparableMetric<T,V>::closest (const Point &origin,
81 const Point &second) const
85 a = rawDistance(origin,first);
86 b = rawDistance(origin,second);
96//------------------------------------------------------------------------------
97template <typename T, typename V>
99typename DGtal::InexactPredicateLpSeparableMetric<T,V>::Abscissa
100DGtal::InexactPredicateLpSeparableMetric<T,V>::binarySearchHidden(const Abscissa &udim,
101 const Abscissa &vdim,
104 const Abscissa &lower,
105 const Abscissa &upper) const
107 ASSERT( (nu + std::pow( static_cast<Value>(abs( udim - lower)), myExponent)) <
108 (nv + std::pow( static_cast<Value>(abs( vdim - lower)), myExponent)));
111 if ( (upper - lower) <= NumberTraits<Abscissa>::ONE)
114 Value nuUpdated = nu + std::pow( static_cast<Value>(abs( udim - upper )), myExponent);
115 Value nvUpdated = nv + std::pow( static_cast<Value>(abs( vdim - upper )), myExponent);
116 if (nuUpdated < nvUpdated)
122 Abscissa mid = (lower + upper)/2;
123 Value nuUpdated = nu + std::pow( static_cast<Value>(abs( udim - mid )), myExponent);
124 Value nvUpdated = nv + std::pow( static_cast<Value>(abs( vdim - mid )), myExponent);
127 if ( nuUpdated < nvUpdated)
128 return binarySearchHidden(udim,vdim,nu,nv,mid,upper);
130 return binarySearchHidden(udim,vdim,nu,nv,lower,mid);
133//------------------------------------------------------------------------------
134template <typename T, typename V>
137DGtal::InexactPredicateLpSeparableMetric<T,V>::hiddenBy(const Point &u,
140 const Point &startingPoint,
141 const Point &endPoint,
142 const typename Point::UnsignedComponent dim) const
144 //Interval bound for the binary search
145 Abscissa lower = startingPoint[dim];
146 Abscissa upper = endPoint[dim];
148 //Partial norm computation (sum_{i!=dim} |u_i-v_i|^p
152 for(DGtal::Dimension i = 0 ; i < Point::dimension ; i++)
155 nu += std::pow( static_cast<Value>(abs(u[i] - startingPoint[i])), myExponent);
156 nv += std::pow( static_cast<Value>(abs(v[i] - startingPoint[i])), myExponent);
157 nw += std::pow( static_cast<Value>(abs(w[i] - startingPoint[i])), myExponent);
160 //Abscissa of voronoi edges
162 Value dv,dw,du,ddv,ddw;
164 //checking distances to lower bound
165 du = nu + std::pow( static_cast<Value>(abs( u[dim] - lower)), myExponent);
166 dv = nv + std::pow( static_cast<Value>(abs( v[dim] - lower)), myExponent);
167 dw = nw + std::pow( static_cast<Value>(abs( w[dim] - lower)), myExponent);
169 //Precondition of binarySearchHidden is true
172 uv = binarySearchHidden(u[dim],v[dim],nu,nv,lower,upper);
175 vw = binarySearchHidden(v[dim],w[dim],nv,nw,lower,upper); //precondition
183 //check if uv + 1 is stricly in W
185 //first, optimisation
186 if (uv == upper) return true;
189 ddv = nv + std::pow( static_cast<Value>(abs( v[dim] - uv -1)), myExponent);
190 ddw = nw + std::pow( static_cast<Value>(abs( w[dim] - uv -1)), myExponent);
205//------------------------------------------------------------------------------
206template <typename T, typename V>
209DGtal::InexactPredicateLpSeparableMetric<T,V>::selfDisplay ( std::ostream & out ) const
211 out << "[InexactPredicateLpSeparableMetric] p="<<myExponent;
213//------------------------------------------------------------------------------
214template <typename T, typename V>
217DGtal::InexactPredicateLpSeparableMetric<T,V>::isValid() const
221//------------------------------------------------------------------------------
222template <typename T, typename V>
225DGtal::operator<< ( std::ostream & out,
226 const InexactPredicateLpSeparableMetric<T,V> & object )
228 object.selfDisplay( out );