last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 2007 00003 * Pablo Alvarado 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 * \file cvrUniformDiscreteDistribution.h 00043 * Contains the class cvr::uniformDiscreteDistribution to 00044 * produce random integers uniformly distributed in an specified 00045 * interval 00046 * \author Pablo Alvarado 00047 * \date 25.09.2007 00048 * 00049 * revisions ..: $Id: cvrUniformDiscreteDistribution.h,v 1.2 2007/09/28 00:06:17 alvarado Exp $ 00050 */ 00051 00052 #ifndef _CVR_UNIFORM_DISCRETE_DISTRIBUTION_H_ 00053 #define _CVR_UNIFORM_DISCRETE_DISTRIBUTION_H_ 00054 00055 #include "cvrUnivariateDiscreteDistribution.h" 00056 00057 namespace cvr { 00058 00059 /** 00060 * Class uniformDiscreteDistribution 00061 * 00062 * This class generates (pseudo) random numbers uniformly distributed in an 00063 * interval specified in the parameters. 00064 * 00065 * In the sake of speed, the classical modulo operation is used to 00066 * avoid multiplications and divisions. The error introduced is 00067 * however in most applications totally negligible. 00068 * 00069 * @see uniformDiscreteDistribution::parameters. 00070 * 00071 * @ingroup gRandomDiscrete 00072 */ 00073 class uniformDiscreteDistribution : public univariateDiscreteDistribution { 00074 public: 00075 /** 00076 * The parameters for the class uniformDiscreteDistribution 00077 */ 00078 class parameters : public univariateDiscreteDistribution::parameters { 00079 public: 00080 /** 00081 * Default constructor 00082 */ 00083 parameters(); 00084 00085 /** 00086 * Copy constructor 00087 * @param other the parameters object to be copied 00088 */ 00089 parameters(const parameters& other); 00090 00091 /** 00092 * Destructor 00093 */ 00094 ~parameters(); 00095 00096 /** 00097 * Copy the contents of a parameters object 00098 * @param other the parameters object to be copied 00099 * @return a reference to this parameters object 00100 */ 00101 parameters& copy(const parameters& other); 00102 00103 /** 00104 * Copy the contents of a parameters object 00105 * @param other the parameters object to be copied 00106 * @return a reference to this parameters object 00107 */ 00108 parameters& operator=(const parameters& other); 00109 00110 /** 00111 * Returns the complete name of the parameters class. 00112 */ 00113 virtual const std::string& name() const; 00114 00115 /** 00116 * Returns a pointer to a clone of the parameters. 00117 */ 00118 virtual parameters* clone() const; 00119 00120 /** 00121 * Returns a pointer to a new instance of the parameters. 00122 */ 00123 virtual parameters* newInstance() const; 00124 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 virtual bool write(ioHandler& handler,const bool complete=true) const; 00133 00134 /** 00135 * Read the parameters from the given ioHandler 00136 * @param handler the ioHandler to be used 00137 * @param complete if true (the default) the enclosing begin/end will 00138 * be also written, otherwise only the data block will be written. 00139 * @return true if write was successful 00140 */ 00141 virtual bool read(ioHandler& handler,const bool complete=true); 00142 00143 // ------------------------------------------------ 00144 // the parameters 00145 // ------------------------------------------------ 00146 00147 /** 00148 * Lower bound of the value interval. 00149 * 00150 * This is the smallest value that can be generated. 00151 * 00152 * Default value: 0 00153 */ 00154 int min; 00155 00156 /** 00157 * Higher bound of the value interval. 00158 * 00159 * This is the highest value that can be generated. 00160 * 00161 * Default value: 100 00162 */ 00163 int max; 00164 00165 }; 00166 00167 /** 00168 * Default constructor 00169 */ 00170 uniformDiscreteDistribution(); 00171 00172 /** 00173 * Constructor with a given interval. 00174 * 00175 * @param min lower interval bound. 00176 * @param max higher interval bound. 00177 */ 00178 uniformDiscreteDistribution(const int min,const int max); 00179 00180 /** 00181 * Construct a functor using the given parameters 00182 */ 00183 uniformDiscreteDistribution(const parameters& par); 00184 00185 /** 00186 * Copy constructor 00187 * @param other the object to be copied 00188 */ 00189 uniformDiscreteDistribution(const uniformDiscreteDistribution& other); 00190 00191 /** 00192 * Destructor 00193 */ 00194 virtual ~uniformDiscreteDistribution(); 00195 00196 /** 00197 * Get a random number. 00198 * 00199 * Returns a random number distributed accordingly to the type of the 00200 * current instance. 00201 * 00202 * The uniformDiscreteDistribution can be used to obtain numbers 00203 * in the interval [min(),max()]. 00204 * 00205 * @param rnd double reference where the random number has to be left. 00206 * @return true if apply successful or false otherwise. 00207 */ 00208 virtual bool apply(int& rnd); 00209 00210 /** 00211 * Virtual method to get a integer random number. 00212 * 00213 * Returns a random number distributed accordingly to the type of the 00214 * current instance. 00215 * 00216 * The uniformDiscreteDistribution can be used to obtain numbers 00217 * in the interval [min(),max()]. 00218 * 00219 * @return a random float number. 00220 */ 00221 virtual int draw(); 00222 00223 /** 00224 * Non-virtual method to get a integer random number. 00225 * 00226 * Returns a random number distributed accordingly to the type of the 00227 * current instance. 00228 * 00229 * The uniformDiscreteDistribution can be used to obtain numbers 00230 * in the interval [min(),max()]. 00231 * 00232 * @return a random float number. 00233 */ 00234 int rand(); 00235 00236 /** 00237 * Virtual method to obtain the maximum possible number 00238 * (inclusive) to be returned by this distribution 00239 */ 00240 virtual int max() const; 00241 00242 /** 00243 * Virtual method to obtain the minimum possible number 00244 * (inclusive) to be returned by this distribution. 00245 */ 00246 virtual int min() const; 00247 00248 /** 00249 * Change the interval, modifying the parameters as well 00250 */ 00251 bool setInterval(const int lower,const int upper); 00252 00253 /** 00254 * Copy data of "other" functor. 00255 * @param other the functor to be copied 00256 * @return a reference to this functor object 00257 */ 00258 uniformDiscreteDistribution& 00259 copy(const uniformDiscreteDistribution& other); 00260 00261 /** 00262 * Alias for copy member 00263 * @param other the functor to be copied 00264 * @return a reference to this functor object 00265 */ 00266 uniformDiscreteDistribution& 00267 operator=(const uniformDiscreteDistribution& other); 00268 00269 /** 00270 * Returns the complete name of the functor class 00271 */ 00272 virtual const std::string& name() const; 00273 00274 /** 00275 * Returns a pointer to a clone of this functor. 00276 */ 00277 virtual uniformDiscreteDistribution* clone() const; 00278 00279 /** 00280 * Returns a pointer to a new instance of this functor. 00281 */ 00282 virtual uniformDiscreteDistribution* newInstance() const; 00283 00284 /** 00285 * Returns used parameters 00286 */ 00287 const parameters& getParameters() const; 00288 00289 /** 00290 * Update parameters 00291 */ 00292 bool updateParameters(); 00293 00294 protected: 00295 /** 00296 * Returns used parameters 00297 */ 00298 parameters& getParameters(); 00299 00300 /** 00301 * Shadow of the parameters.min; 00302 */ 00303 int minimum_; 00304 00305 /** 00306 * Shadow of the parameters.max; 00307 */ 00308 int maximum_; 00309 00310 /** 00311 * delta_ = (maximum_ - minimum_ + 1) 00312 */ 00313 int delta_; 00314 }; 00315 } 00316 00317 #endif 00318