CVR-Lib last update 20 Sep 2009

cvrQuickMedian.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  * \file   cvrQuickMedian.h
00044  *         This file contains the functor quickMedian, which
00045  *         calculates the median quickly
00046  * \author Guy Wafo
00047  * \date   27.03.2001
00048  *
00049  * $Id: cvrQuickMedian.h,v 1.5 2007/10/13 00:18:22 alvarado Exp $
00050  */
00051 
00052 #ifndef _CVR_QUICKMEDIAN_H_
00053 #define _CVR_QUICKMEDIAN_H_
00054 
00055 #include "cvrFunctor.h"
00056 #include "cvrVector.h"
00057 #include "cvrMatrix.h"
00058 #include "cvrMedianEvenCase.h"
00059 
00060 namespace cvr {
00061 
00062   /**
00063    * Quick median search.
00064    *
00065    * This class is used to extract the median of the elements of a
00066    * given vector or matrix.  The median is defined as the element at
00067    * the middle position of the sorted vector.  The algorithm used is
00068    * based on the quick sort.
00069    * For vectors (or matrices) with an even number n of elements, the
00070    * median will be the element at (n/2) or (n/2)-1 depending on the
00071    * parameter settings.
00072    *
00073    * On-place applies are in this implementation faster than on copy ones.
00074    *
00075    * The type of the vector elements (T) must accept the operators <
00076    * and >.
00077    *
00078    * @ingroup gStatistics
00079    */
00080   class quickMedian : public functor {
00081   public:
00082 
00083     /**
00084      * The parameters for the class quickMedian
00085      */
00086     class parameters : public functor::parameters {
00087     public:
00088       /**
00089        * Default constructor
00090        */
00091       parameters();
00092 
00093       /**
00094        * copy constructor
00095        * @param other the parameters object to be copied
00096        */
00097       parameters(const parameters& other);
00098 
00099       /**
00100        * destructor
00101        */
00102       ~parameters();
00103 
00104       /**
00105        * Returns the name of this class.
00106        */
00107       const std::string& name() const;
00108 
00109       /**
00110        * copy the contents of a parameters object
00111        * @param other the parameters object to be copied
00112        * @return a reference to this parameters object
00113        */
00114       parameters& copy(const parameters& other);
00115 
00116       /**
00117        * Alias for copy member
00118        * @param other the functor to be copied
00119        * @return a reference to this functor object
00120        */
00121       parameters& operator=(const parameters& other);
00122 
00123       /**
00124        * returns a pointer to a clone of the parameters
00125        */
00126       virtual parameters* clone() const;
00127 
00128       /**
00129        * returns a pointer to a new instance of the parameters
00130        */
00131       virtual parameters* newInstance() const;
00132 
00133       /**
00134        * Write the parameters in the given ioHandler
00135        * @param handler the ioHandler to be used
00136        * @param complete if true (the default) the enclosing begin/end will
00137        *        be also written, otherwise only the data block will be written.
00138        * @return true if write was successful
00139        */
00140       virtual bool write(ioHandler& handler,const bool complete=true) const;
00141 
00142       /**
00143        * Read the parameters from the given ioHandler
00144        * @param handler the ioHandler to be used
00145        * @param complete if true (the default) the enclosing begin/end will
00146        *        be also written, otherwise only the data block will be written.
00147        * @return true if write was successful
00148        */
00149       virtual bool read(ioHandler& handler,const bool complete=true);
00150 
00151       // ------------------------------------------------
00152       // the parameters
00153       // ------------------------------------------------
00154 
00155       /**
00156        * Specifies how to consider the case when the number of elements of
00157        * the vector is even (see cvr::eMedianEvenCase)
00158        *
00159        * This parameter has no effect for odd-sized vectors.
00160        *
00161        * Default value: TakeLower
00162        */
00163       eMedianEvenCase medianEvenCase;
00164     };
00165 
00166     /**
00167      * Default constructor
00168      */
00169     quickMedian();
00170 
00171     /**
00172      * Constructor to set parameters
00173      */
00174     quickMedian(const parameters& param);
00175 
00176     /**
00177      * Constructor with indicator what to do for even-sized vectors
00178      */
00179     quickMedian(const eMedianEvenCase medianEvenCase);
00180 
00181     /**
00182      * copy constructor
00183      * @param other the object to be copied
00184      */
00185     quickMedian(const quickMedian& other);
00186 
00187     /**
00188      * destructor
00189      */
00190     virtual ~quickMedian();
00191 
00192     /**
00193      * Returns the name of this class.
00194      */
00195     const std::string& name() const;
00196 
00197     /**
00198      * Calculates the median of the given matrix, which WILL BE modified.
00199      * The elements of the matrix will be considered as part of a vector
00200      * with "columns()" times "rows()" elements.
00201      *
00202      * @param src matrix<T> with the source data.
00203      * @param median the median value of the given matrix.
00204      * @return \c true on success \c false otherwise
00205      */
00206     template <typename T>
00207     bool apply(matrix<T>& src, T& median) const;
00208 
00209     /**
00210      * Calculates the median of the given matrix, which is NOT modified.
00211      * The elements of the matrix will be considered as part of a vector
00212      * with "columns()" times "rows()" elements.
00213      *
00214      * @param src matrix<T> with the source data.
00215      * @param median the median value of the given matrix.
00216      * @return \c true on success \c false otherwise
00217      */
00218     template <typename T>
00219     bool apply(const matrix<T>& src, T& median) const;
00220 
00221     /**
00222      * Calculates the median of \a src, the result is left in \a dest.
00223      * The elements of the matrix will be considered as part of a vector
00224      * with "columns()" times "rows()" elements.
00225      *
00226      * @param src matrix<T> with the source data.
00227      * @param dest partially sorted matrix.
00228      * @param median the median value of the given matrix.
00229      * @return true on success false otherwise
00230      */
00231     template <typename T>
00232     bool apply(const matrix<T>& src, matrix<T>& dest, T& median) const;
00233 
00234     /**
00235      * Find the median of a vector type V, which can be an cvr::genericVector
00236      * or its derived classes, or a std::vector.  In principle the container
00237      * type V just needs to support:
00238      * - the type definitions V::size_type and V::value_type,
00239      * - the operator[] returning elements of type V::value_type
00240      * - the V::value_type has to be comparable with the operator<.
00241      * - the method empty()
00242      * - the method size() returning a V::size_type
00243      *
00244      * The resulting vector contains the elements less or equal than the median
00245      * for the indexes <code>x</code> such that <code>x < size()/2</code>,
00246      * and higher or equal otherwise.
00247      *
00248      * @param srcdest vector<T> with the source data.  The result
00249      *                 will be left here too.
00250      * @param median  the median value
00251      *
00252      * @return true on success false otherwise
00253      */
00254     template <class V>
00255     bool apply(V& srcdest, typename V::value_type& median) const;
00256 
00257     /**
00258      * Operates on the given parameter.
00259      * @param src vector with the source data.
00260      * @param median the median value
00261      * @return true on success false otherwise
00262      */
00263     template <class V>
00264     bool apply(const V& src, typename V::value_type& median) const;
00265 
00266     /**
00267      * Operates on the given parameter.
00268      * @param src vector with the source data.
00269      * @param dest the partially sorted vector.  The elements at the
00270      *             first half of the vector are less or equal than the median
00271      *             and on the other half greater or equal.
00272      * @param median  the median value
00273      * @return true on success false otherwise
00274      */
00275     template <class V>
00276     bool apply(const V& src, V& dest,
00277                typename V::value_type& median) const;
00278 
00279     /**
00280      * A shortcut function for apply(const vector<T>&, T&) const. Note
00281      * that internally another vector and T are used.
00282      *
00283      * @param src vector whose median is sought
00284      * @returns the median of \a src
00285      */
00286     template <class V>
00287     typename V::value_type median(const V& src) const;
00288 
00289     /**
00290      * Copy data of "other" functor.
00291      * @param other the functor to be copied
00292      * @return a reference to this functor object
00293      */
00294     quickMedian& copy(const quickMedian& other);
00295 
00296     /**
00297      * Returns a pointer to a clone of this functor.
00298      */
00299     virtual quickMedian* clone() const;
00300 
00301     /**
00302      * Returns a pointer to a new instance of this functor.
00303      */
00304     virtual quickMedian* newInstance() const;
00305 
00306     /**
00307      * Returns used parameters
00308      */
00309     const parameters& getParameters() const;
00310 
00311   protected:
00312     /**
00313      * This method calculates the median in a recursively form.
00314      * The template type V has to be a vector following an interface like
00315      * the cvr::vector or the std::vector, implementing the operator[]
00316      */
00317     template <class V>
00318     typename V::value_type findMedian(V& vct,
00319                                       const int begin,
00320                                       const int end,
00321                                       const int medianPos) const;
00322 
00323     /**
00324      * This method chooses a pivot-value and ensures that lower values lie
00325      * at the left and higher values at the right of its final position, which
00326      * will be returned.
00327      * The template type V has to be a vector following an interface like
00328      * the cvr::vector or the std::vector, implementing the operator[]
00329      */
00330     template <class V>
00331     int partition(V& vct,
00332                   const int begin,
00333                   const int end) const;
00334 
00335   };
00336 
00337 }
00338 
00339 
00340 #include "cvrQuickMedian_template.h"
00341 
00342 #endif
00343 
00344 

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8