LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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 { namespace afw { namespace math {
29 
30 ndarray::Array<double,1,1> BoundedField::evaluate(
31  ndarray::Array<double const,1> const & x,
32  ndarray::Array<double const,1> const & y
33 ) 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 namespace {
42 
43 // We use these operator-based functors to implement the various image-modifying routines
44 // in BoundedField. I don't think this is necessarily the best way to add interoperability
45 // with images, but it seems like a reasonable point on the simplicity vs. featurefulness
46 // scale, and I don't have time to explore more complex solutions (expression templates!).
47 // Of course, in C++11, we could replace all these with lambdas.
48 
49 struct Assign {
50 
51  template <typename T>
52  void operator()(T & out, double a) const { out = a; }
53 
54 };
55 
56 struct ScaledAdd {
57 
58  explicit ScaledAdd(double s) : scaleBy(s) {}
59 
60  template <typename T>
61  void operator()(T & out, double a) const { out += scaleBy * a; }
62 
63  double scaleBy;
64 };
65 
66 struct Multiply {
67 
68  template <typename T>
69  void operator()(T & out, double a) const { out *= a; }
70 
71 };
72 
73 struct Divide {
74 
75  template <typename T>
76  void operator()(T & out, double a) const { out /= a; }
77 
78 };
79 
80 template <typename T, typename F>
81 void applyToImage(BoundedField const & field, image::Image<T> & img, F functor, bool overlapOnly) {
82  geom::Box2I region(field.getBBox());
83  if (overlapOnly) {
84  region.clip(img.getBBox(image::PARENT));
85  } else if (region != img.getBBox(image::PARENT)) {
86  throw LSST_EXCEPT(
87  pex::exceptions::RuntimeError,
88  "Image bounding box does not match field bounding box"
89  );
90  }
91  for (int y = region.getBeginY(), yEnd = region.getEndY(); y < yEnd; ++y) {
92  typename image::Image<T>::x_iterator rowIter = img.x_at(
93  region.getBeginX() - img.getX0(), y - img.getY0()
94  );
95  for (int x = region.getBeginX(), xEnd = region.getEndX(); x < xEnd; ++x, ++rowIter) {
96  functor(*rowIter, field.evaluate(x, y));
97  }
98  }
99 }
100 
101 } // anonymous
102 
103 PTR(BoundedField) operator*(double const scale, CONST_PTR(BoundedField) bf) {
104  return *bf*scale;
105 }
106 
107 template <typename T>
108 void BoundedField::fillImage(image::Image<T> & img, bool overlapOnly) const {
109  applyToImage(*this, img, Assign(), overlapOnly);
110 }
111 
112 template <typename T>
113 void BoundedField::addToImage(image::Image<T> & img, double scaleBy, bool overlapOnly) const {
114  applyToImage(*this, img, ScaledAdd(scaleBy), overlapOnly);
115 }
116 
117 template <typename T>
118 void BoundedField::multiplyImage(image::Image<T> & img, bool overlapOnly) const {
119  applyToImage(*this, img, Multiply(), overlapOnly);
120 }
121 
122 template <typename T>
123 void BoundedField::divideImage(image::Image<T> & img, bool overlapOnly) const {
124  applyToImage(*this, img, Divide(), overlapOnly);
125 }
126 
127 #define INSTANTIATE(T) \
128  template void BoundedField::fillImage(image::Image<T> &, bool) const; \
129  template void BoundedField::addToImage(image::Image<T> &, double, bool) const; \
130  template void BoundedField::multiplyImage(image::Image<T> &, bool) const; \
131  template void BoundedField::divideImage(image::Image<T> &, bool) const
132 
133 INSTANTIATE(float);
134 INSTANTIATE(double);
135 
136 }}} // namespace lsst::afw::math
int y
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: Image.h:330
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.
Include files required for standard LSST Exception handling.
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:378
int getX0() const
Return the image&#39;s column-origin.
Definition: Image.h:248
An integer coordinate rectangle.
Definition: Box.h:53
double scaleBy
Definition: BoundedField.cc:63
metadata import lsst afw display as afwDisplay n
#define INSTANTIATE(T)
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
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
#define PTR(...)
Definition: base.h:41
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:49
#define CONST_PTR(...)
A shared pointer to a const object.
Definition: base.h:47
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:416
table::Key< int > field
Definition: ApCorrMap.cc:72
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:256