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

ltiSimilarityFunctor.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 .......: ltiSimilarityFunctor.h
00027  * authors ....: Stefan Syberichs, Jochen Wickel
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 30.1.2001
00030  * revisions ..: $Id: ltiSimilarityFunctor.h,v 1.8 2006/02/08 12:44:27 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_SIMILARITY_FUNCTOR_H_
00034 #define _LTI_SIMILARITY_FUNCTOR_H_
00035 
00036 #include "ltiLinearAlgebraFunctor.h"
00037 
00038 namespace lti {
00039 
00040   /**
00041    * This class is the base class for all functors which compute
00042    * similarity measures between two vectors or matrices.
00043    *
00044    * It contains two apply functions, one for each type.
00045    *
00046    * @see distanceFunctor
00047    *
00048    * Be careful with the use of the parameters::rowWise.  It indicates
00049    * if the matrix should be considered as having row vectors (true) of
00050    * columns vectors (false).  Depending on that the computations will be
00051    * very different.
00052    */
00053   template <class T>
00054   class similarityFunctor: public linearAlgebraFunctor {
00055   public:
00056     /**
00057      * the parameters for the class similarityFunctor
00058      */
00059     class parameters : public linearAlgebraFunctor::parameters {
00060     public:
00061       /**
00062        * default constructor
00063        */
00064       parameters() : linearAlgebraFunctor::parameters() {
00065         rowWise=true;
00066       };
00067 
00068       /**
00069        * copy constructor
00070        * @param other the parameters object to be copied
00071        */
00072       parameters(const parameters& other) 
00073         : linearAlgebraFunctor::parameters() {
00074         copy(other);
00075       };
00076 
00077       /**
00078        * destructor
00079        */
00080       virtual ~parameters() {
00081       };
00082 
00083       /**
00084        * returns name of this type
00085        */
00086       const char* getTypeName() const {
00087         return "distanceFunctor::parameters";
00088       };
00089 
00090       /**
00091        * copy the contents of a parameters object
00092        * @param other the parameters object to be copied
00093        * @return a reference to this parameters object
00094        */
00095       parameters& copy(const parameters& other) {
00096 # ifndef _LTI_MSC_6
00097     // MS Visual C++ 6 is not able to compile this...
00098         linearAlgebraFunctor::parameters::copy(other);
00099 # else
00100     // ...so we have to use this workaround.
00101     // Conditional on that, copy may not be virtual.
00102         functor::parameters&
00103           (functor::parameters::* p_copy)
00104           (const functor::parameters&) =
00105           functor::parameters::copy;
00106           (this->*p_copy)(other);
00107 # endif
00108 
00109         rowWise=other.rowWise;
00110 
00111         return *this;
00112       };
00113 
00114       /**
00115        * returns a pointer to a clone of the parameters
00116        */
00117       virtual functor::parameters* clone() const {
00118         return new parameters(*this);
00119       };
00120 
00121 # ifndef _LTI_MSC_6
00122       /**
00123        * write the parameters in the given ioHandler
00124        * @param handler the ioHandler to be used
00125        * @param complete if true (the default) the enclosing begin/end will
00126        *        be also written, otherwise only the data block will be written.
00127        * @return true if write was successful
00128        */
00129       bool write(ioHandler& handler,
00130                  const bool complete) const
00131 # else
00132       /**
00133        * this function is required by MSVC only, as a workaround for a
00134        * very awful bug, which exists since MSVC V.4.0, and still by
00135        * V.6.0 with all bugfixes (so called "service packs") remains
00136        * there...  This method is also public due to another bug, so please
00137        * NEVER EVER call this method directly: use write() instead
00138        */
00139       bool writeMS(ioHandler& handler,
00140                    const bool complete) const
00141 # endif
00142       {
00143         bool b(true);
00144 
00145         if (complete) {
00146           b = b && handler.writeBegin();
00147         }
00148 
00149         b = b && lti::write(handler, "rowWise", rowWise);
00150 
00151         if (complete) {
00152           b = b && handler.writeEnd();
00153         }
00154 
00155         return b;
00156       }
00157 
00158 # ifdef _LTI_MSC_6
00159       /**
00160        * write the parameters in the given ioHandler
00161        * @param handler the ioHandler to be used
00162        * @param complete if true (the default) the enclosing begin/end will
00163        *        be also written, otherwise only the data block will be written.
00164        * @return true if write was successful
00165        */
00166       bool write(ioHandler& handler,
00167                  const bool complete) const {
00168        // ...we need this workaround to cope with another really awful MSVC
00169        // bug.
00170         return writeMS(handler,complete);
00171       }
00172 # endif
00173 
00174 
00175 # ifndef _LTI_MSC_6
00176       /**
00177        * read the parametersfrom the given ioHandler
00178        * @param handler the ioHandler to be used
00179        * @param complete if true (the default) the enclosing begin/end will
00180        *        be also written, otherwise only the data block will be written.
00181        * @return true if write was successful
00182        */
00183       bool read(ioHandler& handler,
00184                 const bool complete)
00185 # else
00186       /**
00187        * this function is required by MSVC only, as a workaround for a
00188        * very awful bug, which exists since MSVC V.4.0, and still by
00189        * V.6.0 with all bugfixes (so called "service packs") remains
00190        * there...  This method is also public due to another bug, so please
00191        * NEVER EVER call this method directly: use read() instead
00192        */
00193       bool readMS(ioHandler& handler,
00194                   const bool complete)
00195 # endif
00196       {
00197         bool b(true);
00198 
00199         if (complete) {
00200           b = b && handler.readBegin();
00201         }
00202 
00203         b = b && lti::read(handler, "rowWise", rowWise);
00204 
00205         if (complete) {
00206           b = b && handler.readEnd();
00207         }
00208 
00209         return b;
00210       }
00211 
00212 # ifdef _LTI_MSC_6
00213       /**
00214        * read the parametersfrom the given ioHandler
00215        * @param handler the ioHandler to be used
00216        * @param complete if true (the default) the enclosing begin/end will
00217        *        be also written, otherwise only the data block will be written.
00218        * @return true if write was successful
00219        */
00220       bool read(ioHandler& handler,
00221                 const bool complete) {
00222         // ...we need this workaround to cope with another really awful MSVC
00223         // bug.
00224         return readMS(handler,complete);
00225       }
00226 # endif
00227 
00228       /**
00229        * determine whether norms and distances between the row vectors in
00230        * a matrix (true) or the column vectors in it (false) should
00231        * be calculated.
00232        *
00233        * Default value: true
00234        */
00235       bool rowWise;
00236     };
00237 
00238 
00239     /**
00240      * default constructor
00241      */
00242     similarityFunctor();
00243 
00244     /**
00245      * copy constructor
00246      * @param other the object to be copied
00247      */
00248     similarityFunctor(const similarityFunctor<T>& other);
00249 
00250     /**
00251      * destructor
00252      */
00253     virtual ~similarityFunctor();
00254 
00255     /**
00256      * returns the name of this type ("distanceFunctor")
00257      */
00258     virtual const char* getTypeName() const;
00259 
00260     /**
00261      * calculate the similarity of the vectors a and b
00262      * @param a the first vector<T>
00263      * @param b the second vector<T>
00264      * @return the similarity of the vectors
00265      */
00266     virtual T apply(const vector<T>& a, const vector<T>& b) const = 0;
00267 
00268     /**
00269      * calculate the similarity of the matrices a and b
00270      * @param a the first matrix<T>
00271      * @param b the second matrix<T>
00272      * @return the similarity of the matrices
00273      */
00274     virtual T apply(const matrix<T>& a, const matrix<T>& b) const = 0;
00275 
00276     /**
00277      * copy data of "other" functor.
00278      * @param other the functor to be copied
00279      * @return a reference to this functor object
00280      */
00281     similarityFunctor<T>& copy(const similarityFunctor<T>& other);
00282 
00283     /**
00284      * returns used parameters
00285      */
00286     const parameters& getParameters() const;
00287   };
00288 
00289 }
00290 
00291 #include "ltiSimilarityFunctor_template.h"
00292 
00293 #endif

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