LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
WarpAtOnePoint.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 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 #include <vector>
25 
26 #include "lsst/afw/math/Kernel.h"
27 #include "lsst/afw/image/Image.h"
29 #include "lsst/afw/geom/Point.h"
30 
31 namespace lsst {
32 namespace afw {
33 namespace math {
34 namespace detail {
35 
39  template<typename DestImageT, typename SrcImageT>
41  public:
43  SrcImageT const &srcImage,
44  WarpingControl const &control,
45  typename DestImageT::SinglePixel padValue
46  ) :
47  _srcImage(srcImage),
48  _kernelPtr(control.getWarpingKernel()),
49  _maskKernelPtr(control.getMaskWarpingKernel()),
50  _hasMaskKernel(control.getMaskWarpingKernel()),
51  _kernelCtr(_kernelPtr->getCtr()),
52  _maskKernelCtr(_maskKernelPtr ? _maskKernelPtr->getCtr() : lsst::afw::geom::Point2I(0, 0)),
53  _growFullMask(control.getGrowFullMask()),
54  _xList(_kernelPtr->getWidth()),
55  _yList(_kernelPtr->getHeight()),
56  _maskXList(_maskKernelPtr ? _maskKernelPtr->getWidth() : 0),
57  _maskYList(_maskKernelPtr ? _maskKernelPtr->getHeight() : 0),
58  _padValue(padValue),
59  _srcGoodBBox(_kernelPtr->shrinkBBox(srcImage.getBBox(lsst::afw::image::LOCAL)))
60  { };
61 
67  bool operator()(
68  typename DestImageT::x_iterator &destXIter,
69  lsst::afw::geom::Point2D const &srcPos,
70  double relativeArea,
72  ) {
73  // Compute associated source pixel index as integer and nonnegative fractional parts;
74  // the latter is used to compute the remapping kernel.
75  std::pair<int, double> srcIndFracX = _srcImage.positionToIndex(srcPos[0], lsst::afw::image::X);
76  std::pair<int, double> srcIndFracY = _srcImage.positionToIndex(srcPos[1], lsst::afw::image::Y);
77  if (srcIndFracX.second < 0) {
78  ++srcIndFracX.second;
79  --srcIndFracX.first;
80  }
81  if (srcIndFracY.second < 0) {
82  ++srcIndFracY.second;
83  --srcIndFracY.first;
84  }
85 
86  if (_srcGoodBBox.contains(lsst::afw::geom::Point2I(srcIndFracX.first, srcIndFracY.first))) {
87  // Offset source pixel index from kernel center to kernel corner (0, 0)
88  // so we can convolveAtAPoint the pixels that overlap between source and kernel
89  int srcStartX = srcIndFracX.first - _kernelCtr[0];
90  int srcStartY = srcIndFracY.first - _kernelCtr[1];
91 
92  // Compute warped pixel
93  double kSum = _setFracIndex(srcIndFracX.second, srcIndFracY.second);
94 
95  typename SrcImageT::const_xy_locator srcLoc = _srcImage.xy_at(srcStartX, srcStartY);
96 
97  *destXIter = lsst::afw::math::convolveAtAPoint<DestImageT, SrcImageT>(srcLoc, _xList, _yList);
98  *destXIter *= relativeArea/kSum;
99  return true;
100  } else {
101  // Edge pixel
102  *destXIter = _padValue;
103  return false;
104  }
105  }
106 
114  typename DestImageT::x_iterator &destXIter,
115  lsst::afw::geom::Point2D const &srcPos,
116  double relativeArea,
118  ) {
119  // Compute associated source pixel index as integer and nonnegative fractional parts;
120  // the latter is used to compute the remapping kernel.
121  std::pair<int, double> srcIndFracX = _srcImage.positionToIndex(srcPos[0], lsst::afw::image::X);
122  std::pair<int, double> srcIndFracY = _srcImage.positionToIndex(srcPos[1], lsst::afw::image::Y);
123  if (srcIndFracX.second < 0) {
124  ++srcIndFracX.second;
125  --srcIndFracX.first;
126  }
127  if (srcIndFracY.second < 0) {
128  ++srcIndFracY.second;
129  --srcIndFracY.first;
130  }
131 
132  if (_srcGoodBBox.contains(lsst::afw::geom::Point2I(srcIndFracX.first, srcIndFracY.first))) {
133  // Offset source pixel index from kernel center to kernel corner (0, 0)
134  // so we can convolveAtAPoint the pixels that overlap between source and kernel
135  int srcStartX = srcIndFracX.first - _kernelCtr[0];
136  int srcStartY = srcIndFracY.first - _kernelCtr[1];
137 
138  // Compute warped pixel
139  double kSum = _setFracIndex(srcIndFracX.second, srcIndFracY.second);
140 
141  typename SrcImageT::const_xy_locator srcLoc = _srcImage.xy_at(srcStartX, srcStartY);
142 
143  *destXIter = lsst::afw::math::convolveAtAPoint<DestImageT, SrcImageT>(srcLoc, _xList, _yList);
144  *destXIter *= relativeArea/kSum;
145 
146  if (_hasMaskKernel) {
147  // compute mask value based on the mask kernel (replacing the value computed above)
148  int maskStartX = srcIndFracX.first - _maskKernelCtr[0];
149  int maskStartY = srcIndFracY.first - _maskKernelCtr[1];
150 
151  typename SrcImageT::Mask::const_xy_locator srcMaskLoc = \
152  _srcImage.getMask()->xy_at(maskStartX, maskStartY);
153 
154  typedef typename std::vector<lsst::afw::math::Kernel::Pixel>::const_iterator k_iter;
155 
156  typename DestImageT::Mask::SinglePixel destMaskValue = 0;
157  for (k_iter kernelYIter = _maskYList.begin(), yEnd = _maskYList.end();
158  kernelYIter != yEnd; ++kernelYIter) {
159 
160  typename DestImageT::Mask::SinglePixel destMaskValueY = 0;
161  for (k_iter kernelXIter = _maskXList.begin(), xEnd = _maskXList.end();
162  kernelXIter != xEnd; ++kernelXIter, ++srcMaskLoc.x()) {
163  typename lsst::afw::math::Kernel::Pixel const kValX = *kernelXIter;
164  if (kValX != 0) {
165  destMaskValueY |= *srcMaskLoc;
166  }
167  }
168 
169  double const kValY = *kernelYIter;
170  if (kValY != 0) {
171  destMaskValue |= destMaskValueY;
172  }
173 
174  srcMaskLoc += lsst::afw::image::detail::difference_type(-_maskXList.size(), 1);
175  }
176 
177  destXIter.mask() = (destXIter.mask() & _growFullMask) | destMaskValue;
178  }
179  return true;
180  } else {
181  // Edge pixel
182  *destXIter = _padValue;
183  return false;
184  }
185  }
186 
187  private:
193  double _setFracIndex(double xFrac, double yFrac) {
194  std::pair<double, double> srcFracInd(xFrac, yFrac);
195  _kernelPtr->setKernelParameters(srcFracInd);
196  double kSum = _kernelPtr->computeVectors(_xList, _yList, false);
197  if (_maskKernelPtr) {
198  _maskKernelPtr->setKernelParameters(srcFracInd);
199  _maskKernelPtr->computeVectors(_maskXList, _maskYList, false);
200  }
201  return kSum;
202  }
203 
204  SrcImageT _srcImage;
206  PTR(lsst::afw::math::SeparableKernel) _maskKernelPtr;
208  lsst::afw::geom::Point2I _kernelCtr;
209  lsst::afw::geom::Point2I _maskKernelCtr;
211  std::vector<double> _xList;
212  std::vector<double> _yList;
213  std::vector<double> _maskXList;
214  std::vector<double> _maskYList;
215  typename DestImageT::SinglePixel _padValue;
216  lsst::afw::geom::Box2I const _srcGoodBBox;
217  };
218 
219 }}}} // lsst::afw::math::detail
DestImageT::SinglePixel _padValue
boost::uint16_t MaskPixel
Declare the Kernel class and subclasses.
A coordinate class intended to represent absolute positions.
#define PTR(...)
Definition: base.h:41
A kernel described by a pair of functions: func(x, y) = colFunc(x) * rowFunc(y)
Definition: Kernel.h:986
lsst::afw::image::MaskPixel _growFullMask
A functor that computes one warped pixel.
Parameters to control convolution.
Definition: warpExposure.h:239
Point< int, 2 > Point2I
Definition: PSF.h:39
double _setFracIndex(double xFrac, double yFrac)
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
bool operator()(typename DestImageT::x_iterator &destXIter, lsst::afw::geom::Point2D const &srcPos, double relativeArea, lsst::afw::image::detail::MaskedImage_tag)
boost::shared_ptr< lsst::afw::math::SeparableKernel > _maskKernelPtr
A traits class for MaskedImage.
Definition: MaskedImage.h:53
tag for an Image
Definition: Image.h:75
bool operator()(typename DestImageT::x_iterator &destXIter, lsst::afw::geom::Point2D const &srcPos, double relativeArea, lsst::afw::image::detail::Image_tag)
Support for 2-D images.
lsst::afw::geom::Point2I _maskKernelCtr
bool contains(Point2I const &point) const
Return true if the box contains the point.
lsst::afw::geom::Box2I const _srcGoodBBox
boost::shared_ptr< lsst::afw::math::SeparableKernel > _kernelPtr
Implementation of the Class MaskedImage.
lsst::afw::geom::Point2I _kernelCtr
WarpAtOnePoint(SrcImageT const &srcImage, WarpingControl const &control, typename DestImageT::SinglePixel padValue)