last update 20 Sep 2009 |
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 cvrQuickMedian2.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: cvrQuickMedian2.h,v 1.3 2007/10/13 00:17:19 alvarado Exp $ 00050 */ 00051 00052 #ifndef _CVR_QUICKMEDIAN2_H_ 00053 #define _CVR_QUICKMEDIAN2_H_ 00054 00055 #include "cvrFunctor.h" 00056 #include "cvrVector.h" 00057 #include "cvrMatrix.h" 00058 #include "cvrQuickMedian.h" 00059 00060 namespace cvr { 00061 00062 00063 /** 00064 * Quick median for two vectors. 00065 * 00066 * This class is used to extract the median of the elements of a 00067 * given vector or matrix, partitioning at the same time a second 00068 * vector. The median is defined as the element at the middle 00069 * position of the sorted vector. The algorithm used is based on 00070 * the quick sort. 00071 * 00072 * The difference with the cvr::quickMedian functor is that you can 00073 * "sort" a second vector, which might contain for example the indices 00074 * of the elements. This way, you can easily find out, which elements of the 00075 * original vector are under the median, and which ones above. 00076 * 00077 * @see cvr::quickMedian, cvr::sort, cvr::sort2 00078 * 00079 * For vectors (or matrices) with an even number n of elements, the 00080 * median will be the element at (n/2) or (n/2)-1 depending on the 00081 * parameter settings. 00082 * 00083 * The type of the vector elements (T) must accept the operators < 00084 * and >. 00085 * 00086 * @ingroup gStatistics 00087 */ 00088 class quickMedian2 : public functor { 00089 public: 00090 00091 typedef quickMedian::parameters parameters; 00092 00093 /** 00094 * Default constructor 00095 */ 00096 quickMedian2(); 00097 00098 /** 00099 * Constructor to set parameters 00100 */ 00101 quickMedian2(const parameters& param); 00102 00103 /** 00104 * Constructor with indicator what to do for even-sized vectors 00105 */ 00106 quickMedian2(const eMedianEvenCase medianEvenCase); 00107 00108 /** 00109 * copy constructor 00110 * @param other the object to be copied 00111 */ 00112 quickMedian2(const quickMedian2& other); 00113 00114 /** 00115 * Destructor 00116 */ 00117 virtual ~quickMedian2(); 00118 00119 /** 00120 * Returns the name of this class. 00121 */ 00122 const std::string& name() const; 00123 00124 /** 00125 * Returns a pointer to a clone of this functor. 00126 */ 00127 virtual quickMedian2* clone() const; 00128 00129 /** 00130 * Returns a pointer to a new instance of this functor. 00131 */ 00132 virtual quickMedian2* newInstance() const; 00133 00134 /** 00135 * Returns used parameters 00136 */ 00137 const parameters& getParameters() const; 00138 00139 /** 00140 * Operates on the given arguments. 00141 * 00142 * Please note that the arguments will be both modified. 00143 * 00144 * The resulting keys vector contains the elements less or equal than the 00145 * median for the indexes <code>x</code> such that 00146 * <code>x < size()/2</code>, and higher or equal otherwise. 00147 * 00148 * Both vectors must have the same size. 00149 * 00150 * The types V and W, can be an cvr::genericVector 00151 * or its derived classes, or a std::vector. In principle the container 00152 * type V,W just needs to support: 00153 * - the type definitions V::size_type and V::value_type, 00154 * - the operator[] returning elements of type V::value_type 00155 * - the V::value_type has to be comparable with the operator<. 00156 * - the method empty() 00157 * - the method size() returning a V::size_type 00158 * 00159 * @param keys vector with the key data. The median of this data 00160 * is partially sorted while looking for the median. 00161 * @param data vector with data to be sorted the same way as the keys. 00162 * You can for example use a ivector initialized with the 00163 * index values (i.e. data(i)=i), so that after the apply 00164 * method you can check which elements are below the median 00165 * and which above. 00166 * @param median the median value of keys. 00167 * @return bool upon success, false otherwise 00168 */ 00169 template<class V,class W> 00170 bool apply(V& keys,W& data, typename V::value_type& median) const; 00171 00172 /** 00173 * Operates on the given arguments. 00174 * 00175 * Please note that the arguments will be both modified. 00176 * 00177 * The resulting keys vector contains the elements less or equal than the 00178 * median for the indexes <code>x</code> such that 00179 * <code>x < size()/2</code>, and higher or equal otherwise. 00180 * 00181 * Both vectors must have the same size. 00182 * 00183 * @param keys V with the key data. The median of this data 00184 * is partially sorted while looking for the median. 00185 * @param data W with data to be sorted the same way as the keys. 00186 * You can for example use a ivector initialized with the 00187 * index values (i.e. data(i)=i), so that after the apply 00188 * method you can check which elements are below the median 00189 * and which above. 00190 * @return bool upon success, false otherwise 00191 */ 00192 template<class V,class W> 00193 bool apply(V& keys,W& data) const; 00194 00195 00196 protected: 00197 /** 00198 * this method calculates the median in a recursively form 00199 */ 00200 template<class V,class W> 00201 typename V::value_type findMedian(V& vct, 00202 W& data, 00203 const int begin, 00204 const int end, 00205 const int medianPos) const; 00206 00207 /** 00208 * this method chooses a pivot-value and ensures that lower values lie 00209 * at the left and higher values at the right of its final position, which 00210 * will be returned. 00211 */ 00212 template<class V,class W> 00213 int partition(V& vct, 00214 W& data, 00215 const int begin, 00216 const int end) const; 00217 }; 00218 00219 } 00220 00221 #include "cvrQuickMedian2_template.h" 00222 00223 #endif