lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 template <typename T, typename F>
86 void applyToImage(BoundedField const &field, image::Image<T> &img, F functor, bool overlapOnly) {
87  geom::Box2I region(field.getBBox());
88  if (overlapOnly) {
89  region.clip(img.getBBox(image::PARENT));
90  } else if (region != img.getBBox(image::PARENT)) {
91  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
92  "Image bounding box does not match field bounding box");
93  }
94  for (int y = region.getBeginY(), yEnd = region.getEndY(); y < yEnd; ++y) {
95  typename image::Image<T>::x_iterator rowIter =
96  img.x_at(region.getBeginX() - img.getX0(), y - img.getY0());
97  for (int x = region.getBeginX(), xEnd = region.getEndX(); x < xEnd; ++x, ++rowIter) {
98  functor(*rowIter, field.evaluate(x, y));
99  }
100  }
101 }
102 
103 } // anonymous
104 
105 std::shared_ptr<BoundedField> operator*(double const scale, std::shared_ptr<BoundedField const> bf) {
106  return *bf * scale;
107 }
108 
109 template <typename T>
110 void BoundedField::fillImage(image::Image<T> &img, bool overlapOnly) const {
111  applyToImage(*this, img, Assign(), overlapOnly);
112 }
113 
114 template <typename T>
115 void BoundedField::addToImage(image::Image<T> &img, double scaleBy, bool overlapOnly) const {
116  applyToImage(*this, img, ScaledAdd(scaleBy), overlapOnly);
117 }
118 
119 template <typename T>
120 void BoundedField::multiplyImage(image::Image<T> &img, bool overlapOnly) const {
121  applyToImage(*this, img, Multiply(), overlapOnly);
122 }
123 
124 template <typename T>
125 void BoundedField::divideImage(image::Image<T> &img, bool overlapOnly) const {
126  applyToImage(*this, img, Divide(), overlapOnly);
127 }
128 
129 #define INSTANTIATE(T) \
130  template void BoundedField::fillImage(image::Image<T> &, bool) const; \
131  template void BoundedField::addToImage(image::Image<T> &, double, bool) const; \
132  template void BoundedField::multiplyImage(image::Image<T> &, bool) const; \
133  template void BoundedField::divideImage(image::Image<T> &, bool) const
134 
135 INSTANTIATE(float);
136 INSTANTIATE(double);
137 }
138 }
139 } // namespace lsst::afw::math
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: Image.h:418
table::Key< int > field
Definition: ApCorrMap.cc:72
void multiplyImage(image::Image< T > &image, bool overlapOnly=false) const
Multiply an image by the field in-place.
virtual double evaluate(geom::Point2D const &position) const =0
Evaluate the field at the given point.
virtual double integrate() const
Compute the integral of this function over its bounding-box.
Definition: BoundedField.cc:41
#define INSTANTIATE(T)
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:456
virtual double mean() const
Compute the mean of this function over its bounding-box.
Definition: BoundedField.cc:43
int getX0() const
Return the image&#39;s column-origin.
Definition: Image.h:317
void divideImage(image::Image< T > &image, bool overlapOnly=false) const
Divide an image by the field in-place.
void fillImage(image::Image< T > &image, bool overlapOnly=false) const
Assign the field to an image, overwriting values already present.
double x
double scaleBy
Definition: BoundedField.cc:68
std::shared_ptr< BoundedField > operator*(double const scale, std::shared_ptr< BoundedField const > bf)
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:486
void addToImage(image::Image< T > &image, double scaleBy=1.0, bool overlapOnly=false) const
Add the field or a constant multiple of it to an image in-place.
int getY0() const
Return the image&#39;s row-origin.
Definition: Image.h:325