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 cvrNormalDistribution.h 00043 * Contains the class cvr::normalDistribution to 00044 * produce random integers normally distributed in an specified 00045 * interval 00046 * \author Pablo Alvarado 00047 * \date 25.09.2007 00048 * 00049 * revisions ..: $Id: cvrNormalDistribution.h,v 1.2 2007/09/29 00:37:11 alvarado Exp $ 00050 */ 00051 00052 #ifndef _CVR_NORMAL_DISTRIBUTION_H_ 00053 #define _CVR_NORMAL_DISTRIBUTION_H_ 00054 00055 #include "cvrUnivariateContinuousDistribution.h" 00056 00057 namespace cvr { 00058 00059 /** 00060 * Class normalDistribution 00061 * 00062 * This class generates (pseudo) random numbers normally distributed 00063 * with mean and standard deviation specified in the parameters. 00064 * 00065 * The equation for the normal probability density distribution is: 00066 * \f[ 00067 * p(x) = \frac{1}{\sigma\sqrt{2\pi}} 00068 * e^{\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2} 00069 * \f] 00070 * 00071 * where \f$\mu\f$ stands for the mean value and \f$\sigma\f$ for 00072 * the standard deviation. 00073 * 00074 * @see normalDistribution::parameters. 00075 * 00076 * @ingroup gRandomContinuous 00077 */ 00078 class normalDistribution : public univariateContinuousDistribution { 00079 00080 public: 00081 /** 00082 * The parameters for the class normalDistribution 00083 */ 00084 class parameters : public univariateContinuousDistribution::parameters { 00085 public: 00086 /** 00087 * Default constructor 00088 */ 00089 parameters(); 00090 00091 /** 00092 * Copy constructor 00093 * @param other the parameters object to be copied 00094 */ 00095 parameters(const parameters& other); 00096 00097 /** 00098 * Destructor 00099 */ 00100 ~parameters(); 00101 00102 /** 00103 * Copy the contents of a parameters object 00104 * @param other the parameters object to be copied 00105 * @return a reference to this parameters object 00106 */ 00107 parameters& copy(const parameters& other); 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& operator=(const parameters& other); 00115 00116 /** 00117 * Returns the complete name of the parameters class. 00118 */ 00119 virtual const std::string& name() const; 00120 00121 /** 00122 * Returns a pointer to a clone of the parameters. 00123 */ 00124 virtual parameters* clone() const; 00125 00126 /** 00127 * Returns a pointer to a new instance of the parameters. 00128 */ 00129 virtual parameters* newInstance() const; 00130 00131 /** 00132 * Write the parameters in the given ioHandler 00133 * @param handler the ioHandler to be used 00134 * @param complete if true (the default) the enclosing begin/end will 00135 * be also written, otherwise only the data block will be written. 00136 * @return true if write was successful 00137 */ 00138 virtual bool write(ioHandler& handler,const bool complete=true) const; 00139 00140 /** 00141 * Read the parameters from the given ioHandler 00142 * @param handler the ioHandler to be used 00143 * @param complete if true (the default) the enclosing begin/end will 00144 * be also written, otherwise only the data block will be written. 00145 * @return true if write was successful 00146 */ 00147 virtual bool read(ioHandler& handler,const bool complete=true); 00148 00149 // ------------------------------------------------ 00150 // the parameters 00151 // ------------------------------------------------ 00152 00153 /** 00154 * Mean value of the distribution. 00155 * 00156 * Default value: 0.0 00157 */ 00158 double mean; 00159 00160 /** 00161 * Standard deviation of the distribution. 00162 * 00163 * The variance is the square of the standard deviation, and 00164 * hence, the value you indicate here is the square root of the 00165 * variance. 00166 * 00167 * Default value: 1.0 00168 */ 00169 double sigma; 00170 }; 00171 00172 /** 00173 * Default constructor 00174 */ 00175 normalDistribution(); 00176 00177 /** 00178 * Constructor with a given interval. 00179 * 00180 * @param mean mean value (\f$\mu\f$) of the normal distribution 00181 * @param sigma standard deviation of the distribution 00182 */ 00183 normalDistribution(const double mean,const double sigma); 00184 00185 /** 00186 * Construct a functor using the given parameters 00187 */ 00188 normalDistribution(const parameters& par); 00189 00190 /** 00191 * Copy constructor 00192 * @param other the object to be copied 00193 */ 00194 normalDistribution(const normalDistribution& other); 00195 00196 /** 00197 * Destructor 00198 */ 00199 virtual ~normalDistribution(); 00200 00201 /** 00202 * Get a random number. 00203 * 00204 * Returns a random number distributed accordingly to the type of the 00205 * current instance. 00206 * 00207 * @param rnd double reference where the random number has to be left. 00208 * @return true if apply successful or false otherwise. 00209 */ 00210 virtual bool apply(float& rnd); 00211 00212 /** 00213 * Get a random number. 00214 * 00215 * Returns a random number distributed accordingly to the type of the 00216 * current instance. 00217 * 00218 * @param rnd double reference where the random number has to be left. 00219 * @return true if apply successful or false otherwise. 00220 */ 00221 virtual bool apply(double& rnd); 00222 00223 /** 00224 * Virtual method to get a single precision random number. 00225 * 00226 * Returns a random number distributed accordingly to the type of the 00227 * current instance. 00228 * 00229 * The univariateContinuousDistribution can be used to obtain numbers 00230 * in the interval [0,max()], where max() is the method of this class. 00231 * 00232 * @return a random float number. 00233 */ 00234 virtual float fdraw(); 00235 00236 /** 00237 * Virtual method to get a double precision random number. 00238 * 00239 * Returns a random number distributed accordingly to the type of the 00240 * current instance. 00241 * 00242 * The univariateContinuousDistribution can be used to obtain numbers 00243 * in the interval [0,max()], where max() is the method of this class. 00244 * 00245 * @return a random float number. 00246 */ 00247 virtual double draw(); 00248 00249 /** 00250 * Non-virtual method to get a single precision random number. 00251 * 00252 * Returns a random number distributed accordingly to the type of the 00253 * current instance. 00254 * 00255 * This method can be used to obtain numbers in the interval [min(),max()]. 00256 * 00257 * @return a random float number. 00258 */ 00259 float frand(); 00260 00261 /** 00262 * Non-virtual method to get a double precision random number. 00263 * 00264 * Returns a random number distributed accordingly to the type of the 00265 * current instance. 00266 * 00267 * This method can be used to obtain numbers in the interval [min(),max()]. 00268 * 00269 * @return a random double number. 00270 */ 00271 double rand(); 00272 00273 /** 00274 * Copy data of "other" functor. 00275 * @param other the functor to be copied 00276 * @return a reference to this functor object 00277 */ 00278 normalDistribution& copy(const normalDistribution& other); 00279 00280 /** 00281 * Alias for copy member 00282 * @param other the functor to be copied 00283 * @return a reference to this functor object 00284 */ 00285 normalDistribution& operator=(const normalDistribution& other); 00286 00287 /** 00288 * Returns the complete name of the functor class 00289 */ 00290 virtual const std::string& name() const; 00291 00292 /** 00293 * Returns a pointer to a clone of this functor. 00294 */ 00295 virtual normalDistribution* clone() const; 00296 00297 /** 00298 * Returns a pointer to a new instance of this functor. 00299 */ 00300 virtual normalDistribution* newInstance() const; 00301 00302 /** 00303 * Returns used parameters 00304 */ 00305 const parameters& getParameters() const; 00306 00307 /** 00308 * Update parameters 00309 */ 00310 bool updateParameters(); 00311 00312 protected: 00313 00314 /** 00315 * Simple structure to shadow the parameters and precomputations. 00316 * 00317 * The template type has to be float or double 00318 */ 00319 template<typename T> 00320 struct shadows { 00321 /** 00322 * Default constructor 00323 */ 00324 shadows(); 00325 00326 /** 00327 * Shadow of the parameters.sigma; 00328 */ 00329 T sigma; 00330 00331 /** 00332 * Shadow of the parameters.mean; 00333 */ 00334 T mu; 00335 00336 /** 00337 * Precomputed value 00338 */ 00339 T precomputed; 00340 00341 /** 00342 * Flag to indicate if a precomputation is available 00343 */ 00344 bool precomputationAvailable; 00345 00346 /** 00347 * Normalization constant 00348 */ 00349 T norm; 00350 }; 00351 00352 /** 00353 * Parameters and status for float precision values 00354 */ 00355 shadows<float> fshadow_; 00356 00357 /** 00358 * Parameters and status for double precision values 00359 */ 00360 shadows<double> dshadow_; 00361 00362 }; 00363 } 00364 00365 #endif 00366