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

ltiDistanceFunctor.h

00001 /*
00002  * Copyright (C) 2000, 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 .......: ltiDistanceFunctor.h
00027  * authors ....: Jochen Wickel
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 28.6.2000
00030  * revisions ..: $Id: ltiDistanceFunctor.h,v 1.8 2006/02/08 12:17:41 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_DISTANCE_FUNCTOR_H_
00034 #define _LTI_DISTANCE_FUNCTOR_H_
00035 
00036 #include "ltiLinearAlgebraFunctor.h"
00037 #include "ltiMath.h"
00038 
00039 namespace lti {
00040 
00041   /**
00042    * This class is the base class for all functors which compute
00043    * distances between two vectors or matrices as well as norms of
00044    * vectors and matrices.
00045    *
00046    * @see similarityFunctor
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 distanceFunctor: public linearAlgebraFunctor {
00055   public:
00056     /**
00057      * the parameters for the class distanceFunctor
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     distanceFunctor();
00243 
00244     /**
00245      * copy constructor
00246      * @param other the object to be copied
00247      */
00248     distanceFunctor(const distanceFunctor<T>& other);
00249 
00250     /**
00251      * destructor
00252      */
00253     virtual ~distanceFunctor();
00254 
00255     /**
00256      * returns the name of this type ("distanceFunctor")
00257      */
00258     virtual const char* getTypeName() const;
00259 
00260     /**
00261      * calculate the norm of vector v.
00262      * @param v the vector<T>
00263      * @param norm the norm of the vector
00264      * @return false on error
00265      */
00266     virtual bool apply(const vector<T>& v, T& norm) const = 0;
00267 
00268     /**
00269      * calculate the norm of vector v
00270      * @param v the vector<T>
00271      * @return the norm of the vector
00272      */
00273     virtual T apply(const vector<T>& v) const =0;
00274 
00275     /**
00276      * calculate the norms of rows or columns of the matrix
00277      * @param m the matrix<T>
00278      * @param norms the norms of the rows/columns
00279      * @return false on error
00280      */
00281     virtual bool apply(const matrix<T>& m, vector<T>& norms) const=0;
00282 
00283     /**
00284      * calculate something like the norm of the matrix: the matrix
00285      * is seen as a vector.
00286      * @param m the matrix<T>
00287      * @param norm the 'norm' of the matrix
00288      * @return false on error
00289      */
00290     virtual bool apply(const matrix<T>& m, T& norm) const=0;
00291 
00292     /**
00293      * calculate something like the norm of the matrix: the matrix
00294      * is seen as a vector.
00295      * @param m the matrix<T>
00296      * @return the 'norm' of the matrix
00297      */
00298     virtual T apply(const matrix<T>& m) const=0;
00299 
00300     /**
00301      * calculate the distance between the vectors a and b
00302      * @param a the first vector<T>
00303      * @param b the second vector<T>
00304      * @param dist the distance between the vectors
00305      * @return false on error -> see status string
00306      */
00307     virtual bool apply(const vector<T>& a, const vector<T>& b,
00308                        T& dist) const=0;
00309 
00310     /**
00311      * calculate the distance between the vectors a and b
00312      * @param a the first vector<T>
00313      * @param b the second vector<T>
00314      * @return the distance between the vectors
00315      */
00316     virtual T apply(const vector<T>& a, const vector<T>& b) const=0;
00317 
00318     /**
00319      * calculate the distances between the rows or columns of the
00320      * matrices a and b, determined by the parameters rowWise.
00321      * @param a the first vector<T>
00322      * @param b the second vector<T>
00323      * @param dists the distances between the matrices
00324      * @return false on error -> see status string
00325      */
00326     virtual bool apply(const matrix<T>& a, const matrix<T>& b,
00327                        vector<T>& dists) const=0;
00328 
00329     /**
00330      * Calculate the distance between each row or column of m
00331      * depending on the value of rowWise and the vector v.
00332      * @param m the matrix<T>
00333      * @param v the vector to be compared with
00334      * @param dest the vector with the distances to the vector v
00335      * @return false on error
00336      */
00337     virtual bool apply(const matrix<T>& m, const vector<T>& v,
00338                        vector<T>& dest) const=0;
00339 
00340     /**
00341      * calculate something like the distance between the matrices a and b:
00342      * both matrices are seen as vectors.
00343      * @param a the first matrix<T>
00344      * @param b the second matrix<T>
00345      * @param dist the 'distance' between the matrices
00346      * @return false on error -> see status string
00347      */
00348     virtual bool apply(const matrix<T>& a, const matrix<T>& b,
00349                        T& dist) const=0;
00350 
00351     /**
00352      * calculate something like the distance between the matrices a and b:
00353      * both matrices are seen as vectors.
00354      * @param a the first matrix<T>
00355      * @param b the second matrix<T>
00356      * @return the 'distance' between the matrices
00357      */
00358     virtual T apply(const matrix<T>& a, const matrix<T>& b) const=0;
00359 
00360     /**
00361      * copy data of "other" functor.
00362      * @param other the functor to be copied
00363      * @return a reference to this functor object
00364      */
00365     distanceFunctor<T>& copy(const distanceFunctor<T>& other);
00366 
00367     /**
00368      * returns a pointer to a clone of this functor.
00369      */
00370     virtual functor* clone() const=0;
00371 
00372     /**
00373      * returns used parameters
00374      */
00375     const parameters& getParameters() const;
00376   };
00377 
00378 }
00379 
00380 #include "ltiDistanceFunctor_template.h"
00381 
00382 #endif

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