LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
RandomImage.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
31 #include "lsst/afw/image/Image.h"
33 #include "lsst/afw/math/Random.h"
34 
35 namespace lsst {
36 namespace afw {
37 namespace math {
38 
39 namespace {
40 
41  template<typename T>
42  struct do_random : public lsst::afw::image::pixelOp0<T>{
43  do_random(Random &rand) : _rand(rand) {}
44  protected:
45  Random &_rand;
46  };
47 
48  template<typename T>
49  struct do_uniform : public do_random<T> {
50  do_uniform(Random &rand) : do_random<T>(rand) {}
51 
52  virtual T operator()() const { return do_random<T>::_rand.uniform(); }
53  };
54 
55  template<typename T>
56  struct do_uniformPos : public do_random<T> {
57  do_uniformPos(Random &rand) : do_random<T>(rand) {}
58 
59  virtual T operator()() const { return do_random<T>::_rand.uniformPos(); }
60  };
61 
62  template<typename T>
63  struct do_uniformInt : public do_random<T> {
64  do_uniformInt(Random &rand, unsigned long n) : do_random<T>(rand), _n(n) {}
65 
66  virtual T operator()() const { return do_random<T>::_rand.uniformInt(_n); }
67  private:
68  unsigned long _n;
69  };
70 
71  template<typename T>
72  struct do_flat : public do_random<T> {
73  do_flat(Random &rand, double const a, double const b) : do_random<T>(rand), _a(a), _b(b) {}
74 
75  virtual T operator()() const { return do_random<T>::_rand.flat(_a, _b); }
76  private:
77  double const _a;
78  double const _b;
79  };
80 
81  template<typename T>
82  struct do_gaussian : public do_random<T> {
83  do_gaussian(Random &rand) : do_random<T>(rand) {}
84 
85  virtual T operator()() const { return do_random<T>::_rand.gaussian(); }
86  };
87 
88  template<typename T>
89  struct do_chisq : public do_random<T> {
90  do_chisq(Random &rand, double nu) : do_random<T>(rand), _nu(nu) {}
91 
92  virtual T operator()() const { return do_random<T>::_rand.chisq(_nu); }
93  private:
94  double const _nu;
95  };
96 
97  template<typename T>
98  struct do_poisson : public do_random<T> {
99  do_poisson(Random &rand, double mu) : do_random<T>(rand), _mu(mu) {}
100 
101  virtual T operator()() const { return do_random<T>::_rand.poisson(_mu); }
102  private:
103  double const _mu;
104  };
105 }
106 
107 /************************************************************************************************************/
111 template<typename ImageT>
112 void randomUniformImage(ImageT *image,
113  Random &rand
114  ) {
115  lsst::afw::image::for_each_pixel(*image, do_uniform<typename ImageT::Pixel>(rand));
116 }
117 
121 template<typename ImageT>
123  Random &rand
124  ) {
125  lsst::afw::image::for_each_pixel(*image, do_uniformPos<typename ImageT::Pixel>(rand));
126 }
127 
131 template<typename ImageT>
133  Random &rand,
134  unsigned long n
135  ) {
136  lsst::afw::image::for_each_pixel(*image, do_uniformInt<typename ImageT::Pixel>(rand, n));
137 }
138 
142 template<typename ImageT>
143 void randomFlatImage(ImageT *image,
144  Random &rand,
145  double const a,
146  double const b
147  ) {
148  lsst::afw::image::for_each_pixel(*image, do_flat<typename ImageT::Pixel>(rand, a, b));
149 }
150 
154 template<typename ImageT>
156  Random &rand
157  ) {
158  lsst::afw::image::for_each_pixel(*image, do_gaussian<typename ImageT::Pixel>(rand));
159 }
160 
164 template<typename ImageT>
165 void randomChisqImage(ImageT *image,
166  Random &rand,
167  double const nu
168  ) {
169  lsst::afw::image::for_each_pixel(*image, do_chisq<typename ImageT::Pixel>(rand, nu));
170 }
171 
172 
176 template<typename ImageT>
177 void randomPoissonImage(ImageT *image,
178  Random &rand,
179  double const mu
180  ) {
181  lsst::afw::image::for_each_pixel(*image, do_poisson<typename ImageT::Pixel>(rand, mu));
182 }
183 
184 /************************************************************************************************************/
185 //
186 // Explicit instantiations
187 //
189 #define INSTANTIATE(T) \
190  template void randomUniformImage(lsst::afw::image::Image<T> *image, Random &rand); \
191  template void randomUniformPosImage(lsst::afw::image::Image<T> *image, Random &rand); \
192  template void randomUniformIntImage(lsst::afw::image::Image<T> *image, Random &rand, unsigned long n); \
193  template void randomFlatImage(lsst::afw::image::Image<T> *image, \
194  Random &rand, double const a, double const b); \
195  template void randomGaussianImage(lsst::afw::image::Image<T> *image, Random &rand); \
196  template void randomChisqImage(lsst::afw::image::Image<T> *image, Random &rand, double const nu); \
197  template void randomPoissonImage(lsst::afw::image::Image<T> *image, Random &rand, double const mu);
198 
199 INSTANTIATE(double)
200 INSTANTIATE(float)
202 
203 }}}
void randomUniformImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:112
Random & _rand
Definition: RandomImage.cc:45
#define INSTANTIATE(MATCH)
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
unsigned long _n
Definition: RandomImage.cc:68
void randomFlatImage(ImageT *image, Random &rand, double const a, double const b)
Definition: RandomImage.cc:143
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
void randomUniformPosImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:122
void randomChisqImage(ImageT *image, Random &rand, double const nu)
Definition: RandomImage.cc:165
void randomGaussianImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:155
Support for 2-D images.
double const _a
Definition: RandomImage.cc:77
Support for functors over Image&#39;s pixels.
Random number generator class.
void randomUniformIntImage(ImageT *image, Random &rand, unsigned long n)
Definition: RandomImage.cc:132
void randomPoissonImage(ImageT *image, Random &rand, double const mu)
Definition: RandomImage.cc:177
double const _nu
Definition: RandomImage.cc:94
double const _b
Definition: RandomImage.cc:78
afw::table::Key< double > b
double const _mu
Definition: RandomImage.cc:103