CVR-Lib last update 20 Sep 2009

cvrEuclideanDistance.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998 - 2005
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 
00043 /**
00044  * \file   cvrEuclideanDistance.h
00045  *         This file contains the functions T euclideanDistance(const
00046  *         T&, const T&) and T euclideanDistanceSqr(const T&, const
00047  *         T&) which calculate the Euclidean distance and its square,
00048  *         respectively.
00049  * \author Jochen Wickel
00050  * \date   28.06.2000
00051  *
00052  * $Id: cvrEuclideanDistance.h,v 1.2 2006/06/06 09:42:39 doerfler Exp $
00053  */
00054 
00055 #ifndef _CVR_EUCLIDEAN_DISTANCE_H_
00056 #define _CVR_EUCLIDEAN_DISTANCE_H_
00057 
00058 #include <vector>
00059 #include "cvrPoint.h"
00060 #include "cvrPoint3D.h"
00061 #include "cvrRGBPixel.h"
00062 #include "cvrRGBAPixel.h"
00063 #include "cvrVector.h"
00064 #include "cvrMatrix.h"
00065 #include "cvrDistanceType.h"
00066 
00067 namespace cvr {
00068 
00069   /**
00070    * @name Euclidean Distances Functions
00071    * global functions to compute the L2 distance between two n-dimensional
00072    * point representations
00073    *
00074    * @ingroup gLinearAlgebra
00075    */
00076   //@{
00077 
00078   /**
00079    * Default implementation of the L2 distance between two values of
00080    * type T. It is assumed that T is scalar and an efficient
00081    * implementation is used. T must define the operator- and T must be
00082    * castable to distanceType<T>::square_distance_type, which in turn must be
00083    * a possible argument of cvr::abs().
00084    *
00085    * Overloads for most CVR-Lib types are implemented.
00086    */
00087   template<class T>
00088   typename distanceType<T>::square_distance_type
00089   euclideanDistance(const T& a, const T& b) {
00090     return abs(static_cast<typename distanceType<T>::square_distance_type>(a)
00091                -static_cast<typename distanceType<T>::square_distance_type>(b));
00092   }
00093 
00094   /**
00095    * euclideanDistance computes the L2 distance between
00096    * the vectors a and b.
00097    */
00098   template<class T>
00099   typename distanceType<T>::square_distance_type
00100   euclideanDistance(const vector<T>& a,const vector<T>& b) {
00101     typename vector<T>::const_iterator ita,itb,ite;
00102     assert(a.size()==b.size());
00103     typename distanceType<T>::square_distance_type sum(0);
00104     for (ita=a.begin(),itb=b.begin(),ite=a.end();
00105          ita!=ite;
00106          ++ita,++itb) {
00107       const typename distanceType<T>::square_distance_type diff
00108         = static_cast<typename distanceType<T>::square_distance_type>(*ita)
00109         -static_cast<typename distanceType<T>::square_distance_type>(*itb);
00110       sum+=diff*diff;
00111     }
00112     return sqrt(sum);
00113   }
00114 
00115   /**
00116    * euclideanDistance computes the L2 distance between
00117    * the cvr::matrix a and b.
00118    */
00119   template<class T>
00120   typename distanceType<T>::square_distance_type
00121   euclideanDistance(const matrix<T>& a,const matrix<T>& b) {
00122     assert(a.size() == b.size());
00123 
00124     typename matrix<T>::const_iterator ait,bit,eit;
00125     typename distanceType<T>::square_distance_type dist(0);
00126     ait = a.begin();
00127     bit = b.begin();
00128     eit = a.end();
00129 
00130     while (ait != eit) {
00131       const typename distanceType<T>::square_distance_type diff
00132         = static_cast<typename distanceType<T>::square_distance_type>(*ait)
00133         -static_cast<typename distanceType<T>::square_distance_type>(*bit);
00134       dist+=diff*diff;
00135       ++ait;
00136       ++bit;
00137     }
00138     return sqrt(dist);
00139   }
00140 
00141 
00142   /**
00143    * euclideanDistance computes the L2 distance between
00144    * the std::vectors a and b.
00145    */
00146   template<class T>
00147   typename distanceType<T>::square_distance_type
00148   euclideanDistance(const std::vector<T>& a,const std::vector<T>& b) {
00149     typename std::vector<T>::const_iterator ita,itb,ite;
00150     assert(a.size()==b.size());
00151     typename distanceType<T>::square_distance_type sum(0);
00152     for (ita=a.begin(),itb=b.begin(),ite=a.end();
00153          ita!=ite;
00154          ++ita,++itb) {
00155       const typename distanceType<T>::square_distance_type diff
00156         = static_cast<typename distanceType<T>::square_distance_type>(*ita)
00157         -static_cast<typename distanceType<T>::square_distance_type>(*itb);
00158       sum+=diff*diff;
00159     }
00160     return sqrt(sum);
00161   }
00162 
00163   /**
00164    * euclideanDistance computes the L2 distance between
00165    * the points a and b.
00166    */
00167   template<class T >
00168   typename distanceType<T>::square_distance_type
00169   euclideanDistance(const point<T>& a,const point<T>& b) {
00170     return sqrt(static_cast<typename distanceType<T>::square_distance_type>
00171       (a.distanceSqr(b)));
00172   }
00173 
00174   /**
00175    * euclideanDistance computes the L2 distance between
00176    * the points a and b.
00177    */
00178   template<class T>
00179   typename distanceType<T>::square_distance_type
00180   euclideanDistance(const point3D<T>& a, const point3D<T>& b) {
00181     return sqrt(static_cast<typename distanceType<T>::square_distance_type>
00182       (a.distanceSqr(b)));
00183   }
00184 
00185   /**
00186    * euclideanDistance computes the L2 distance between
00187    * the RGB values a and b in the RGB color space.
00188    */
00189   template<class T>
00190   inline typename distanceType<T>::square_distance_type
00191   euclideanDistance(const rgbPixel<T>& a, const rgbPixel<T>& b) {
00192     return sqrt(static_cast<typename distanceType<T>::square_distance_type>
00193        (a.distanceSqr(b)));
00194   }
00195 
00196   /**
00197    * euclideanDistance computes the L2 distance between
00198    * the RGB values a and b in the RGB color space.
00199    */
00200   inline distanceType<rgbaPixel>::square_distance_type
00201   euclideanDistance(const rgbaPixel& a, const rgbaPixel& b) {
00202     return cvr::sqrt(static_cast<distanceType<rgbaPixel>::square_distance_type>
00203       (a.distanceSqr(b)));
00204   }
00205 
00206   //@}
00207 
00208   /**
00209    * @name square of Euclidean Distances Functions
00210    *
00211    * global functions to compute the square of the L2 distance between
00212    * two n-dimensional point representations
00213    *
00214    * @ingroup gLinearAlgebra
00215    */
00216   //@{
00217 
00218   /**
00219    * Default implementation of the square of the L2 distance between
00220    * two values of type T. It is assumed that T is scalar and an
00221    * efficient implementation is used. T must define the operator- and
00222    * T must be castable to distanceType<T>::square_distance_type,
00223    * which in turn must be a possible argument of cvr::abs().
00224    *
00225    * Overloads for most CVR-Lib types are implemented.
00226    */
00227   template<class T>
00228   typename distanceType<T>::square_distance_type
00229   euclideanDistanceSqr(const T& a, const T& b) {
00230     const typename distanceType<T>::square_distance_type diff
00231       = static_cast<typename distanceType<T>::square_distance_type>(a)
00232       -static_cast<typename distanceType<T>::square_distance_type>(b);
00233     return diff*diff;
00234   }
00235 
00236   /**
00237    * euclideanDistanceSqr computes the L2 distance between
00238    * the vectors a and b.
00239    */
00240   template<class T>
00241   typename distanceType<T>::square_distance_type
00242   euclideanDistanceSqr(const vector<T>& a,const vector<T>& b) {
00243     typename vector<T>::const_iterator ita,itb,ite;
00244     assert(a.size()==b.size());
00245     typename distanceType<T>::square_distance_type sum(0);
00246     for (ita=a.begin(),itb=b.begin(),ite=a.end();
00247          ita!=ite;
00248          ++ita,++itb) {
00249       const typename distanceType<T>::square_distance_type diff
00250         = static_cast<typename distanceType<T>::square_distance_type>(*ita)
00251         -static_cast<typename distanceType<T>::square_distance_type>(*itb);
00252       sum+=diff*diff;
00253     }
00254     return sum;
00255   }
00256 
00257   /**
00258    * euclideanDistanceSqr computes the L2 distance between
00259    * the cvr::matrix a and b.
00260    */
00261   template<class T>
00262   typename distanceType<T>::square_distance_type
00263   euclideanDistanceSqr(const matrix<T>& a,const matrix<T>& b) {
00264     assert(a.size() == b.size());
00265 
00266     typename matrix<T>::const_iterator ait,bit,eit;
00267     typename distanceType<T>::square_distance_type dist(0);
00268     ait = a.begin();
00269     bit = b.begin();
00270     eit = a.end();
00271 
00272     while (ait != eit) {
00273       const typename distanceType<T>::square_distance_type diff
00274         = static_cast<typename distanceType<T>::square_distance_type>(*ait)
00275         -static_cast<typename distanceType<T>::square_distance_type>(*bit);
00276       dist+=diff*diff;
00277       ++ait;
00278       ++bit;
00279     }
00280     return dist;
00281   }
00282 
00283 
00284   /**
00285    * euclideanDistanceSqr computes the L2 distance between
00286    * the std::vectors a and b.
00287    */
00288   template<class T>
00289   typename distanceType<T>::square_distance_type
00290   euclideanDistanceSqr(const std::vector<T>& a,const std::vector<T>& b) {
00291     typename std::vector<T>::const_iterator ita,itb,ite;
00292     assert(a.size()==b.size());
00293     typename distanceType<T>::square_distance_type sum(0);
00294     for (ita=a.begin(),itb=b.begin(),ite=a.end();
00295          ita!=ite;
00296          ++ita,++itb) {
00297       const typename distanceType<T>::square_distance_type diff
00298         = static_cast<typename distanceType<T>::square_distance_type>(*ita)
00299         -static_cast<typename distanceType<T>::square_distance_type>(*itb);
00300       sum+=diff*diff;
00301     }
00302     return sum;
00303   }
00304 
00305   /**
00306    * euclideanDistanceSqr computes the L2 distance between
00307    * the points a and b.
00308    */
00309   template<class T >
00310   typename distanceType<T>::square_distance_type
00311   euclideanDistanceSqr(const point<T>& a,const point<T>& b) {
00312     return static_cast<typename distanceType<T>::square_distance_type>
00313       (a.distanceSqr(b));
00314   }
00315 
00316   /**
00317    * euclideanDistanceSqr computes the L2 distance between
00318    * the points a and b.
00319    */
00320   template<class T>
00321   typename distanceType<T>::square_distance_type
00322   euclideanDistanceSqr(const point3D<T>& a, const point3D<T>& b) {
00323     return static_cast<typename distanceType<T>::square_distance_type>
00324       (a.distanceSqr(b));
00325   }
00326 
00327   /**
00328    * euclideanDistanceSqr computes the L2 distance between
00329    * the RGB values a and b in the RGB color space.
00330    */
00331   template<class T>
00332   inline typename distanceType<T>::square_distance_type
00333   euclideanDistanceSqr(const rgbPixel<T>& a, const rgbPixel<T>& b) {
00334     return static_cast<typename distanceType<T>::square_distance_type>
00335       (a.distanceSqr(b));
00336   }
00337 
00338   /**
00339    * euclideanDistanceSqr computes the L2 distance between
00340    * the RGB values a and b in the RGB color space.
00341    */
00342   inline distanceType<rgbaPixel>::square_distance_type
00343   euclideanDistanceSqr(const rgbaPixel& a, const rgbaPixel& b) {
00344     return static_cast<distanceType<rgbaPixel>::square_distance_type>
00345       (a.distanceSqr(b));
00346   }
00347 
00348   //@}
00349 
00350 
00351 }
00352 
00353 #endif
00354 

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8