lsst.afw  13.0-100-ge0459d59f+6
BoundedField.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2014 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 #include "lsst/pex/exceptions.h"
27 
28 namespace lsst {
29 namespace afw {
30 namespace math {
31 
32 ndarray::Array<double, 1, 1> BoundedField::evaluate(ndarray::Array<double const, 1> const &x,
33  ndarray::Array<double const, 1> const &y) const {
34  ndarray::Array<double, 1, 1> out = ndarray::allocate(x.getSize<0>());
35  for (int i = 0, n = x.getSize<0>(); i < n; ++i) {
36  out[i] = evaluate(x[i], y[i]);
37  }
38  return out;
39 }
40 
41 double BoundedField::integrate() const { throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented"); }
42 
43 double BoundedField::mean() const { throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented"); }
44 
45 namespace {
46 
47 // We use these operator-based functors to implement the various image-modifying routines
48 // in BoundedField. I don't think this is necessarily the best way to add interoperability
49 // with images, but it seems like a reasonable point on the simplicity vs. featurefulness
50 // scale, and I don't have time to explore more complex solutions (expression templates!).
51 // Of course, in C++11, we could replace all these with lambdas.
52 
53 struct Assign {
54  template <typename T>
55  void operator()(T &out, double a) const {
56  out = a;
57  }
58 };
59 
60 struct ScaledAdd {
61  explicit ScaledAdd(double s) : scaleBy(s) {}
62 
63  template <typename T>
64  void operator()(T &out, double a) const {
65  out += scaleBy * a;
66  }
67 
68  double scaleBy;
69 };
70 
71 struct Multiply {
72  template <typename T>
73  void operator()(T &out, double a) const {
74  out *= a;
75  }
76 };
77 
78 struct Divide {
79  template <typename T>
80  void operator()(T &out, double a) const {
81  out /= a;
82  }
83 };
84 
85 namespace {
86 
87 // Helper class to do bilinear interpolation with no dynamic memory
88 // allocation. This means we evaluate the function multiple times at some
89 // points (when we move to a new interval in y, to be specific). Could
90 // probably be optimized for the cases where either step is 1, but that seems
91 // premature.
92 class Interpolator {
93 public:
94 
95  // Description of a cell to interpolate in one dimension.
96  struct Bounds {
97  int const step; // step between min and max, except in the special last row/column
98  int min; // lower-bound of cell (coordinate of known value and one before first point to fill in)
99  int max; // upper-bound of cell (coordinate of known value)
100  int end; // upper-bound of cell (one after last point to fill in)
101 
102  // Construct from step only.
103  //
104  // Other variables are initialized (and re-initialized) by calls to reset().
105  explicit Bounds(int step_) : step(step_), min(0), max(0), end(0) {}
106 
107  // Reset all points (aside from the step) to the first cell in this dimension.
108  void reset(int min_) {
109  min = min_;
110  max = min_ + step;
111  end = min_ + step;
112  }
113  };
114 
115  // Construct an object to interpolate the given BoundedField on an evenly-spaced grid within a region.
116  Interpolator(
117  BoundedField const * field,
118  geom::Box2I const * region,
119  int xStep,
120  int yStep
121  ) :
122  _field(field),
123  _region(region),
124  _x(xStep),
125  _y(yStep),
126  _z00(std::numeric_limits<double>::quiet_NaN()),
127  _z01(std::numeric_limits<double>::quiet_NaN()),
128  _z10(std::numeric_limits<double>::quiet_NaN()),
129  _z11(std::numeric_limits<double>::quiet_NaN())
130  {}
131 
132  // Actually do the interpolation.
133  //
134  // This method iterates over rows of cells in y, calling _runRow()
135  // to iterate over cells in x.
136  template <typename T, typename F>
137  void run(image::Image<T> & img, F functor) {
138  _y.reset(_region->getBeginY());
139  while (_y.end < _region->getEndY()) {
140  _runRow(img, functor);
141  _y.min = _y.max;
142  _y.max += _y.step;
143  _y.end = _y.max;
144  }
145  { // special-case last iteration in y
146  _y.max = _region->getMaxY();
147  _y.end = _region->getEndY();
148  _runRow(img, functor);
149  }
150  }
151 
152 private:
153 
154  // Process a row of cells, calling _runCell() on each one.
155  template <typename T, typename F>
156  void _runRow(image::Image<T> & img, F functor) {
157  _x.reset(_region->getBeginX());
158  _z00 = _field->evaluate(_x.min, _y.min);
159  _z01 = _field->evaluate(_x.min, _y.max);
160  while (_x.max < _region->getEndX()) {
161  _z10 = _field->evaluate(_x.max, _y.min);
162  _z11 = _field->evaluate(_x.max, _y.max);
163  _runCell(img, functor);
164  _x.min = _x.max;
165  _x.max += _x.step;
166  _x.end = _x.max;
167  _z00 = _z10;
168  _z01 = _z11;
169  }
170  { // special-case last iteration in x
171  _x.max = _region->getMaxX();
172  _x.end = _region->getEndX();
173  _z10 = _field->evaluate(_x.max, _y.min);
174  _z11 = _field->evaluate(_x.max, _y.max);
175  _runCell(img, functor);
176  }
177  }
178 
179  // Interpolate all points in a cell, which is defined as a rectangle for which
180  // the BoundedField has been evaluated at all four corners. The main complication
181  // comes from the need to special-case the unusually-sized final cells and final
182  // rows/columns within cells in each dimension.
183  template <typename T, typename F>
184  void _runCell(image::Image<T> const & img, F functor) const {
185  int dy = _y.max - _y.min;
186  int dx = _x.max - _x.min;
187  // First iteration of each of the for loops below has been
188  // split into an explicit block. This avoids a few instructions
189  // when no interpolation is necessary, but more importantly it
190  // allows us to handle dx==0 and dy==0 with no divide-by-zero
191  // problems.
192  { // y=_y.min
193  auto rowIter =
194  img.x_at(_x.min - img.getX0(), _y.min - img.getY0());
195  { // x=_x.min
196  functor(*rowIter, _z00);
197  ++rowIter;
198  }
199  for (int x = _x.min + 1; x < _x.end; ++x, ++rowIter) {
200  functor(*rowIter, ((_x.max - x)*_z00 + (x - _x.min)*_z10)/dx);
201  }
202  }
203  for (int y = _y.min + 1; y < _y.end; ++y) {
204  auto rowIter =
205  img.x_at(_x.min - img.getX0(), y - img.getY0());
206  double z0 = ((_y.max - y)*_z00 + (y - _y.min)*_z01)/dy;
207  double z1 = ((_y.max - y)*_z10 + (y - _y.min)*_z11)/dy;
208  { // x=_x.min
209  functor(*rowIter, z0);
210  ++rowIter;
211  }
212  for (int x = _x.min + 1; x < _x.end; ++x, ++rowIter) {
213  functor(*rowIter, ((_x.max - x)*z0 + (x - _x.min)*z1)/dx);
214  }
215  }
216  }
217 
218  BoundedField const * _field;
219  geom::Box2I const * _region;
220  Bounds _x;
221  Bounds _y;
222  double _z00, _z01, _z10, _z11;
223 };
224 
225 } // anonymous
226 
227 
228 template <typename T, typename F>
229 void applyToImage(BoundedField const &field, image::Image<T> &img, F functor, bool overlapOnly, int xStep,
230  int yStep) {
231  geom::Box2I region(field.getBBox());
232  if (overlapOnly) {
233  region.clip(img.getBBox(image::PARENT));
234  } else if (region != img.getBBox(image::PARENT)) {
235  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
236  "Image bounding box does not match field bounding box");
237  }
238 
239  if (yStep > 1 || xStep > 1) {
240  Interpolator interpolator(&field, &region, xStep, yStep);
241  interpolator.run(img, functor);
242  } else {
243  for (int y = region.getBeginY(), yEnd=region.getEndY(); y < yEnd; ++y) {
244  auto rowIter = img.x_at(region.getBeginX() - img.getX0(), y - img.getY0());
245  for (int x = region.getBeginX(), xEnd=region.getEndX(); x < xEnd; ++x, ++rowIter) {
246  functor(*rowIter, field.evaluate(x, y));
247  }
248  }
249  }
250 }
251 
252 } // anonymous
253 
254 std::shared_ptr<BoundedField> operator*(double const scale, std::shared_ptr<BoundedField const> bf) {
255  return *bf * scale;
256 }
257 
258 template <typename T>
259 void BoundedField::fillImage(image::Image<T> &img, bool overlapOnly, int xStep, int yStep) const {
260  applyToImage(*this, img, Assign(), overlapOnly, xStep, yStep);
261 }
262 
263 template <typename T>
264 void BoundedField::addToImage(image::Image<T> &img, double scaleBy, bool overlapOnly,
265  int xStep, int yStep) const {
266  applyToImage(*this, img, ScaledAdd(scaleBy), overlapOnly, xStep, yStep);
267 }
268 
269 template <typename T>
270 void BoundedField::multiplyImage(image::Image<T> &img, bool overlapOnly, int xStep, int yStep) const {
271  applyToImage(*this, img, Multiply(), overlapOnly, xStep, yStep);
272 }
273 
274 template <typename T>
275 void BoundedField::divideImage(image::Image<T> &img, bool overlapOnly, int xStep, int yStep) const {
276  applyToImage(*this, img, Divide(), overlapOnly, xStep, yStep);
277 }
278 
279 #define INSTANTIATE(T) \
280  template void BoundedField::fillImage(image::Image<T> &, bool, int, int) const; \
281  template void BoundedField::addToImage(image::Image<T> &, double, bool, int, int) const; \
282  template void BoundedField::multiplyImage(image::Image<T> &, bool, int, int) const; \
283  template void BoundedField::divideImage(image::Image<T> &, bool, int, int) const
284 
285 INSTANTIATE(float);
286 INSTANTIATE(double);
287 
288 }
289 }
290 } // namespace lsst::afw::math
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:446
virtual double mean() const
Compute the mean of this function over its bounding-box.
Definition: BoundedField.cc:43
virtual double evaluate(geom::Point2D const &position) const =0
Evaluate the field at the given point.
virtual std::shared_ptr< BoundedField > operator*(double const scale) const =0
Return a scaled BoundedField.
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:117
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:307
int const step
Definition: BoundedField.cc:97
void multiplyImage(image::Image< T > &image, bool overlapOnly=false, int xStep=1, int yStep=1) const
Multiply an image by the field in-place.
double scaleBy
Definition: BoundedField.cc:68
A base class for image defects.
Definition: cameraGeom.dox:3
int end
int max
Definition: BoundedField.cc:99
#define INSTANTIATE(T)
BoundedField(geom::Box2I const &bbox)
Definition: BoundedField.h:206
double x
void fillImage(image::Image< T > &image, bool overlapOnly=false, int xStep=1, int yStep=1) const
Assign the field to an image, overwriting values already present.
int min
Definition: BoundedField.cc:98
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:315
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:408
void divideImage(image::Image< T > &image, bool overlapOnly=false, int xStep=1, int yStep=1) const
Divide an image by the field in-place.
void addToImage(image::Image< T > &image, double scaleBy=1.0, bool overlapOnly=false, int xStep=1, int yStep=1) const
Add the field or a constant multiple of it to an image in-place.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:68
table::Key< int > field
Definition: ApCorrMap.cc:72
virtual double integrate() const
Compute the integral of this function over its bounding-box.
Definition: BoundedField.cc:41