latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 1999, 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 .......: ltiRandDist.h 00027 * authors ....: Thomas Rusert 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 21.04.99 00030 * revisions ..: $Id: ltiRandDist.h,v 1.11 2008/08/17 22:20:13 alvarado Exp $ 00031 */ 00032 00033 #ifndef _LTI_RANDDIST_H_ 00034 #define _LTI_RANDDIST_H_ 00035 00036 #include "ltiFunctor.h" 00037 #include <cstdlib> // for RAND_MAX 00038 00039 namespace lti { 00040 /** 00041 * Base random number class. 00042 * 00043 * You can use an instance of this class to initialize the random 00044 * number generator. 00045 */ 00046 class randomDistribution : public functor { 00047 public: 00048 00049 /** 00050 * parameters of the random distribution 00051 */ 00052 class parameters : public functor::parameters { 00053 public: 00054 /** 00055 * copy data of "other" parameters 00056 */ 00057 parameters& copy(const parameters& other) { 00058 #ifndef _LTI_MSC_6 00059 // MS Visual C++ 6 is not able to compile this... 00060 functor::parameters::copy(other); 00061 #else 00062 // ...so we have to use this workaround. 00063 // Conditional on that, copy may not be virtual. 00064 functor::parameters& 00065 (functor::parameters::*p_copy)(const functor::parameters&) = 00066 functor::parameters::copy; 00067 (this->*p_copy)(other); 00068 #endif 00069 00070 return *this; 00071 }; 00072 00073 }; 00074 00075 /** 00076 * default constructor. 00077 * 00078 * Initializes the pseudo-random number generator using system time. 00079 * If reInit is false, the generator is initialized only when 00080 * instantiating the first randomDistribution object. 00081 */ 00082 randomDistribution(bool reInit=false); 00083 00084 /** 00085 * this constructor initializes the pseudo-random number generator 00086 * using the given value 00087 */ 00088 randomDistribution(const unsigned int theValue); 00089 00090 /** 00091 * destructor 00092 */ 00093 virtual ~randomDistribution(); 00094 00095 /** 00096 * re-initializes the random number generator using system time 00097 */ 00098 void init() const; 00099 00100 /** 00101 * re-initializes the random number generator using the given value 00102 */ 00103 void init(const unsigned int theValue) const; 00104 00105 /** 00106 * returns a pointer to a clone of the functor. 00107 */ 00108 virtual functor* clone() const { 00109 return (new randomDistribution(*this)); 00110 }; 00111 00112 /** 00113 * returns the name of this type 00114 */ 00115 virtual const char* getTypeName() const {return "randomDistribution";}; 00116 00117 /** 00118 * copy member 00119 */ 00120 randomDistribution& copy(const randomDistribution& other); 00121 00122 protected: 00123 /** 00124 * This is a wrapper for the rand() function that returns a value 00125 * between 0.0 (inclusive) and 1.0 (exclusive). 00126 */ 00127 inline double random() const { 00128 static const double randmax = (RAND_MAX+1.0); 00129 return rand()/randmax; 00130 } 00131 00132 private: 00133 static bool initialized; 00134 }; 00135 } 00136 00137 #endif