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