LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiEuclideanSimilarity.h

00001 /*
00002  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiEuclideanSimilarity.h
00027  * authors ....: Stefan Syberichs, Jochen Wickel
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 30.1.2001
00030  * revisions ..: $Id: ltiEuclideanSimilarity.h,v 1.7 2006/02/08 12:20:12 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_EUCLID_SIMILARITY_H_
00034 #define _LTI_EUCLID_SIMILARITY_H_
00035 
00036 #include "ltiSimilarityFunctor.h"
00037 
00038 namespace lti {
00039 
00040   /**
00041    * Euclidean Similarity Measure
00042    *
00043    * This class computes the similarity of two vectors or matrices
00044    * by means of a measure based on the euclidean distance of the two vectors.
00045    *
00046    */
00047   template <class T>
00048   class euclideanSimilarity : public similarityFunctor<T> {
00049   public:
00050     /**
00051      * the parameters for the class similarityFunctor
00052      */
00053     class parameters : public similarityFunctor<T>::parameters {
00054     public:
00055       /**
00056        * Types of similarity measures based on the euclidean distance
00057        */
00058       enum eEuclideanSimilarityType {
00059         Inverse,     /**< Based on the inverse of the distance
00060                       *   (1/(1+|a-b|^2))
00061                       */
00062         Exponential  /** Based on the exponential exp(-|a-b|^2) */
00063       };
00064 
00065       /**
00066        * default constructor
00067        */
00068       parameters() : similarityFunctor<T>::parameters() {
00069         rowWise=true;
00070       };
00071 
00072       /**
00073        * copy constructor
00074        * @param other the parameters object to be copied
00075        */
00076       parameters(const parameters& other) {
00077         copy(other);
00078       };
00079 
00080       /**
00081        * destructor
00082        */
00083       virtual ~parameters() {
00084       };
00085 
00086       /**
00087        * returns name of this type
00088        */
00089       const char* getTypeName() const {
00090         return "distanceFunctor::parameters";
00091       };
00092 
00093       /**
00094        * copy the contents of a parameters object
00095        * @param other the parameters object to be copied
00096        * @return a reference to this parameters object
00097        */
00098       parameters& copy(const parameters& other) {
00099 # ifndef _LTI_MSC_6
00100     // MS Visual C++ 6 is not able to compile this...
00101         similarityFunctor<T>::parameters::copy(other);
00102 # else
00103     // ...so we have to use this workaround.
00104     // Conditional on that, copy may not be virtual.
00105         similarityFunctor<T>::parameters&
00106           (similarityFunctor<T>::parameters::* p_copy)
00107           (const similarityFunctor<T>::parameters&) =
00108           similarityFunctor<T>::parameters::copy;
00109           (this->*p_copy)(other);
00110 # endif
00111 
00112         rowWise=other.rowWise;
00113 
00114         return *this;
00115       };
00116 
00117       /**
00118        * returns a pointer to a clone of the parameters
00119        */
00120       virtual functor::parameters* clone() const {
00121         return new parameters(*this);
00122       };
00123 
00124 # ifndef _LTI_MSC_6
00125       /**
00126        * write the parameters in the given ioHandler
00127        * @param handler the ioHandler to be used
00128        * @param complete if true (the default) the enclosing begin/end will
00129        *        be also written, otherwise only the data block will be written.
00130        * @return true if write was successful
00131        */
00132       bool write(ioHandler& handler,
00133                  const bool complete) const
00134 # else
00135       /**
00136        * this function is required by MSVC only, as a workaround for a
00137        * very awful bug, which exists since MSVC V.4.0, and still by
00138        * V.6.0 with all bugfixes (so called "service packs") remains
00139        * there...  This method is also public due to another bug, so please
00140        * NEVER EVER call this method directly: use write() instead
00141        */
00142       bool writeMS(ioHandler& handler,
00143                    const bool complete) const
00144 # endif
00145       {
00146         bool b(true);
00147 
00148         if (complete) {
00149           b = b && handler.writeBegin();
00150         }
00151 
00152         switch(type) {
00153           case Inverse:
00154             b = b && lti::write(handler, "type", "Inverse");
00155             break;
00156           case Exponential:
00157             b = b && lti::write(handler, "type", "Exponential");
00158             break;
00159           default:
00160             b = b && lti::write(handler, "type", "Inverse");
00161         }
00162 
00163         if (complete) {
00164           b = b && handler.writeEnd();
00165         }
00166 
00167 
00168 #     ifndef _LTI_MSC_6
00169         // This is the standard C++ code, which MS Visual C++ 6 is not able to
00170         // compile...
00171         b = b && similarityFunctor<T>::parameters::write(handler,false);
00172 #     else
00173         bool (similarityFunctor<T>::parameters::* p_writeMS)(ioHandler&,
00174                                                          const bool) const =
00175           similarityFunctor<T>::parameters::writeMS;
00176         b = b && (this->*p_writeMS)(handler,false);
00177 #     endif
00178 
00179         return b;
00180       }
00181 
00182 # ifdef _LTI_MSC_6
00183       /**
00184        * write the parameters in the given ioHandler
00185        * @param handler the ioHandler to be used
00186        * @param complete if true (the default) the enclosing begin/end will
00187        *        be also written, otherwise only the data block will be written.
00188        * @return true if write was successful
00189        */
00190       bool write(ioHandler& handler,
00191                  const bool complete) const {
00192        // ...we need this workaround to cope with another really awful MSVC
00193        // bug.
00194         return writeMS(handler,complete);
00195       }
00196 # endif
00197 
00198 
00199 # ifndef _LTI_MSC_6
00200       /**
00201        * read the parametersfrom the given ioHandler
00202        * @param handler the ioHandler to be used
00203        * @param complete if true (the default) the enclosing begin/end will
00204        *        be also written, otherwise only the data block will be written.
00205        * @return true if write was successful
00206        */
00207       bool read(ioHandler& handler,
00208                 const bool complete)
00209 # else
00210       /**
00211        * this function is required by MSVC only, as a workaround for a
00212        * very awful bug, which exists since MSVC V.4.0, and still by
00213        * V.6.0 with all bugfixes (so called "service packs") remains
00214        * there...  This method is also public due to another bug, so please
00215        * NEVER EVER call this method directly: use read() instead
00216        */
00217       bool readMS(ioHandler& handler,
00218                   const bool complete)
00219 # endif
00220       {
00221         bool b(true);
00222 
00223         if (complete) {
00224           b = b && handler.readBegin();
00225         }
00226 
00227         std::string str;
00228 
00229         b = b && lti::read(handler, "type", str);
00230         if (str == "Exponential") {
00231           type = Exponential;
00232         } else {
00233           type = Inverse;
00234         }
00235 
00236 #     ifndef _LTI_MSC_6
00237         // This is the standard C++ code, which MS Visual C++ 6 is not able to
00238         // compile...
00239         b = b && similarityFunctor<T>::parameters::read(handler,false);
00240 #     else
00241         bool (similarityFunctor<T>::parameters::* p_readMS)(ioHandler&,
00242                                                         const bool) =
00243           similarityFunctor<T>::parameters::readMS;
00244         b = b && (this->*p_readMS)(handler,false);
00245 #     endif
00246 
00247         if (complete) {
00248           b = b && handler.readEnd();
00249         }
00250 
00251         return b;
00252       }
00253 
00254 # ifdef _LTI_MSC_6
00255       /**
00256        * read the parametersfrom the given ioHandler
00257        * @param handler the ioHandler to be used
00258        * @param complete if true (the default) the enclosing begin/end will
00259        *        be also written, otherwise only the data block will be written.
00260        * @return true if write was successful
00261        */
00262       bool read(ioHandler& handler,
00263                 const bool complete) {
00264         // ...we need this workaround to cope with another really awful MSVC
00265         // bug.
00266         return readMS(handler,complete);
00267       }
00268 # endif
00269 
00270       /**
00271        * determine how the euclidean distance is used.
00272        *
00273        * Default value: Inverse
00274        */
00275       eEuclideanSimilarityType type;
00276     };
00277 
00278     /**
00279      * default constructor
00280      */
00281     euclideanSimilarity();
00282 
00283     /**
00284      * copy constructor
00285      * @param other the object to be copied
00286      */
00287     euclideanSimilarity(const euclideanSimilarity<T>& other);
00288 
00289     /**
00290      * destructor
00291      */
00292     virtual ~euclideanSimilarity();
00293 
00294     /**
00295      * returns the name of this type ("l1Similarity")
00296      */
00297     virtual const char* getTypeName() const;
00298 
00299     /**
00300      * calculates the a similarity of the vectors a and b
00301      * based on their euclidean distance.
00302      * If both vectors have different sizes, the returned value will be
00303      * negative!
00304      * @param a the first vector<T>
00305      * @param b the second vector<T>
00306      * @return the cos2-type of similarity of a and b
00307      */
00308     T apply(const vector<T>& a, const vector<T>& b) const;
00309 
00310     /**
00311      * is not (yet) defined for matrices.
00312      @return always null
00313      */
00314     T apply(const matrix<T>& a, const matrix<T>& b) const;
00315 
00316     /**
00317      * Is not (yet) defined for matrices.
00318      * @return always null
00319      */
00320     vector<T>& apply(const matrix<T>& a,
00321                      const vector<T>& b,
00322                            vector<T>& dest) const;
00323 
00324     /**
00325      * copy data of "other" functor.
00326      * @param other the functor to be copied
00327      * @return a reference to this functor object
00328      */
00329     euclideanSimilarity& copy(const euclideanSimilarity& other);
00330 
00331     /**
00332      * returns a pointer to a clone of this functor.
00333      */
00334     virtual functor* clone() const;
00335 
00336     /**
00337      * returns used parameters
00338      */
00339     const parameters& getParameters() const;
00340 
00341 
00342   };
00343 
00344 
00345 }
00346 
00347 #include "ltiEuclideanSimilarity_template.h"
00348 
00349 #endif

Generated on Sat Apr 10 15:25:26 2010 for LTI-Lib by Doxygen 1.6.1