lsst.afw  16.0-12-gf08c31002
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  // Description of a cell to interpolate in one dimension.
95  struct Bounds {
96  int const step; // step between min and max, except in the special last row/column
97  int min; // lower-bound of cell (coordinate of known value and one before first point to fill in)
98  int max; // upper-bound of cell (coordinate of known value)
99  int end; // upper-bound of cell (one after last point to fill in)
100 
101  // Construct from step only.
102  //
103  // Other variables are initialized (and re-initialized) by calls to reset().
104  explicit Bounds(int step_) : step(step_), min(0), max(0), end(0) {}
105 
106  // Reset all points (aside from the step) to the first cell in this dimension.
107  void reset(int min_) {
108  min = min_;
109  max = min_ + step;
110  end = min_ + step;
111  }
112  };
113 
114  // Construct an object to interpolate the given BoundedField on an evenly-spaced grid within a region.
115  Interpolator(BoundedField const *field, lsst::geom::Box2I const *region, int xStep, int yStep)
116  : _field(field),
117  _region(region),
118  _x(xStep),
119  _y(yStep),
124 
125  // Actually do the interpolation.
126  //
127  // This method iterates over rows of cells in y, calling _runRow()
128  // to iterate over cells in x.
129  template <typename T, typename F>
130  void run(image::Image<T> &img, F functor) {
131  _y.reset(_region->getBeginY());
132  while (_y.end < _region->getEndY()) {
133  _runRow(img, functor);
134  _y.min = _y.max;
135  _y.max += _y.step;
136  _y.end = _y.max;
137  }
138  { // special-case last iteration in y
139  _y.max = _region->getMaxY();
140  _y.end = _region->getEndY();
141  _runRow(img, functor);
142  }
143  }
144 
145 private:
146  // Process a row of cells, calling _runCell() on each one.
147  template <typename T, typename F>
148  void _runRow(image::Image<T> &img, F functor) {
149  _x.reset(_region->getBeginX());
150  _z00 = _field->evaluate(_x.min, _y.min);
151  _z01 = _field->evaluate(_x.min, _y.max);
152  while (_x.max < _region->getEndX()) {
153  _z10 = _field->evaluate(_x.max, _y.min);
154  _z11 = _field->evaluate(_x.max, _y.max);
155  _runCell(img, functor);
156  _x.min = _x.max;
157  _x.max += _x.step;
158  _x.end = _x.max;
159  _z00 = _z10;
160  _z01 = _z11;
161  }
162  { // special-case last iteration in x
163  _x.max = _region->getMaxX();
164  _x.end = _region->getEndX();
165  _z10 = _field->evaluate(_x.max, _y.min);
166  _z11 = _field->evaluate(_x.max, _y.max);
167  _runCell(img, functor);
168  }
169  }
170 
171  // Interpolate all points in a cell, which is defined as a rectangle for which
172  // the BoundedField has been evaluated at all four corners. The main complication
173  // comes from the need to special-case the unusually-sized final cells and final
174  // rows/columns within cells in each dimension.
175  template <typename T, typename F>
176  void _runCell(image::Image<T> const &img, F functor) const {
177  int dy = _y.max - _y.min;
178  int dx = _x.max - _x.min;
179  // First iteration of each of the for loops below has been
180  // split into an explicit block. This avoids a few instructions
181  // when no interpolation is necessary, but more importantly it
182  // allows us to handle dx==0 and dy==0 with no divide-by-zero
183  // problems.
184  { // y=_y.min
185  auto rowIter = img.x_at(_x.min - img.getX0(), _y.min - img.getY0());
186  { // x=_x.min
187  functor(*rowIter, _z00);
188  ++rowIter;
189  }
190  for (int x = _x.min + 1; x < _x.end; ++x, ++rowIter) {
191  functor(*rowIter, ((_x.max - x) * _z00 + (x - _x.min) * _z10) / dx);
192  }
193  }
194  for (int y = _y.min + 1; y < _y.end; ++y) {
195  auto rowIter = img.x_at(_x.min - img.getX0(), y - img.getY0());
196  double z0 = ((_y.max - y) * _z00 + (y - _y.min) * _z01) / dy;
197  double z1 = ((_y.max - y) * _z10 + (y - _y.min) * _z11) / dy;
198  { // x=_x.min
199  functor(*rowIter, z0);
200  ++rowIter;
201  }
202  for (int x = _x.min + 1; x < _x.end; ++x, ++rowIter) {
203  functor(*rowIter, ((_x.max - x) * z0 + (x - _x.min) * z1) / dx);
204  }
205  }
206  }
207 
208  BoundedField const *_field;
209  lsst::geom::Box2I const *_region;
210  Bounds _x;
211  Bounds _y;
212  double _z00, _z01, _z10, _z11;
213 };
214 
215 } // namespace
216 
217 template <typename T, typename F>
218 void applyToImage(BoundedField const &field, image::Image<T> &img, F functor, bool overlapOnly, int xStep,
219  int yStep) {
220  lsst::geom::Box2I region(field.getBBox());
221  if (overlapOnly) {
222  region.clip(img.getBBox(image::PARENT));
223  } else if (region != img.getBBox(image::PARENT)) {
224  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
225  "Image bounding box does not match field bounding box");
226  }
227 
228  if (yStep > 1 || xStep > 1) {
229  Interpolator interpolator(&field, &region, xStep, yStep);
230  interpolator.run(img, functor);
231  } else {
232  for (int y = region.getBeginY(), yEnd = region.getEndY(); y < yEnd; ++y) {
233  auto rowIter = img.x_at(region.getBeginX() - img.getX0(), y - img.getY0());
234  for (int x = region.getBeginX(), xEnd = region.getEndX(); x < xEnd; ++x, ++rowIter) {
235  functor(*rowIter, field.evaluate(x, y));
236  }
237  }
238  }
239 }
240 
241 } // namespace
242 
244  return *bf * scale;
245 }
246 
247 template <typename T>
248 void BoundedField::fillImage(image::Image<T> &img, bool overlapOnly, int xStep, int yStep) const {
249  applyToImage(*this, img, Assign(), overlapOnly, xStep, yStep);
250 }
251 
252 template <typename T>
253 void BoundedField::addToImage(image::Image<T> &img, double scaleBy, bool overlapOnly, int xStep,
254  int yStep) const {
255  applyToImage(*this, img, ScaledAdd(scaleBy), overlapOnly, xStep, yStep);
256 }
257 
258 template <typename T>
259 void BoundedField::multiplyImage(image::Image<T> &img, bool overlapOnly, int xStep, int yStep) const {
260  applyToImage(*this, img, Multiply(), overlapOnly, xStep, yStep);
261 }
262 
263 template <typename T>
264 void BoundedField::divideImage(image::Image<T> &img, bool overlapOnly, int xStep, int yStep) const {
265  applyToImage(*this, img, Divide(), overlapOnly, xStep, yStep);
266 }
267 
268 #define INSTANTIATE(T) \
269  template void BoundedField::fillImage(image::Image<T> &, bool, int, int) const; \
270  template void BoundedField::addToImage(image::Image<T> &, double, bool, int, int) const; \
271  template void BoundedField::multiplyImage(image::Image<T> &, bool, int, int) const; \
272  template void BoundedField::divideImage(image::Image<T> &, bool, int, int) const
273 
274 INSTANTIATE(float);
275 INSTANTIATE(double);
276 
277 } // namespace math
278 } // namespace afw
279 } // namespace lsst
virtual double mean() const
Compute the mean of this function over its bounding-box.
Definition: BoundedField.cc:43
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:113
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:329
int getEndY() const noexcept
int const step
Definition: BoundedField.cc:96
void multiplyImage(image::Image< T > &image, bool overlapOnly=false, int xStep=1, int yStep=1) const
Multiply an image by the field in-place.
int getBeginY() const noexcept
double scaleBy
Definition: BoundedField.cc:68
BoundedField(BoundedField const &)=default
A base class for image defects.
int end
Definition: BoundedField.cc:99
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:468
int getMaxY() const noexcept
int max
Definition: BoundedField.cc:98
int getBeginX() const noexcept
#define INSTANTIATE(T)
double x
def run(suite, exit=True)
int getMaxX() const noexcept
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.
#define LSST_EXCEPT(type,...)
int min
Definition: BoundedField.cc:97
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:337
int getEndX() const noexcept
void clip(Box2I const &other) noexcept
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:430
table::Key< int > a
void divideImage(image::Image< T > &image, bool overlapOnly=false, int xStep=1, int yStep=1) const
Divide an image by the field in-place.
virtual double evaluate(lsst::geom::Point2D const &position) const =0
Evaluate the field at the given point.
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:67
int y
Definition: SpanSet.cc:44
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