lsst.afw  14.0-59-g0c7cc230b
KernelImagesForRegion.cc
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 
25 /*
26  * Definition of KernelImagesForRegion class declared in detail/ConvolveImage.h
27  */
28 #include <algorithm>
29 #include <cmath>
30 #include <iostream>
31 #include <sstream>
32 #include <vector>
33 
34 #include "boost/assign/list_of.hpp"
35 
36 #include "lsst/pex/exceptions.h"
37 #include "lsst/log/Log.h"
38 #include "lsst/afw/geom.h"
41 
43 
44 namespace lsst {
45 namespace afw {
46 namespace math {
47 namespace detail {
48 
50  geom::Point2I const &xy0, bool doNormalize)
51  : daf::base::Citizen(typeid(this)),
52  _kernelPtr(kernelPtr),
53  _bbox(bbox),
54  _xy0(xy0),
55  _doNormalize(doNormalize),
56  _imagePtrList(4) {
57  if (!_kernelPtr) {
58  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernelPtr is null");
59  }
60  LOGL_DEBUG("TRACE5.afw.math.convolve.KernelImagesForRegion",
61  "KernelImagesForRegion(bbox(minimum=(%d, %d), extent=(%d, %d)), xy0=(%d, %d), doNormalize=%d, "
62  "images...)",
63  _bbox.getMinX(), _bbox.getMinY(), _bbox.getWidth(), _bbox.getHeight(), _xy0[0], _xy0[1],
64  _doNormalize);
65 }
66 
68  geom::Point2I const &xy0, bool doNormalize,
69  ImagePtr bottomLeftImagePtr, ImagePtr bottomRightImagePtr,
70  ImagePtr topLeftImagePtr, ImagePtr topRightImagePtr)
71  : daf::base::Citizen(typeid(this)),
72  _kernelPtr(kernelPtr),
73  _bbox(bbox),
74  _xy0(xy0),
75  _doNormalize(doNormalize),
76  _imagePtrList(4) {
77  if (!_kernelPtr) {
78  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernelPtr is null");
79  }
80  _insertImage(BOTTOM_LEFT, bottomLeftImagePtr);
81  _insertImage(BOTTOM_RIGHT, bottomRightImagePtr);
82  _insertImage(TOP_LEFT, topLeftImagePtr);
83  _insertImage(TOP_RIGHT, topRightImagePtr);
84  LOGL_DEBUG("TRACE5.afw.math.convolve.KernelImagesForRegion",
85  "KernelImagesForRegion(bbox(minimum=(%d, %d), extent=(%d, %d)), xy0=(%d, %d), doNormalize=%d, "
86  "images...)",
87  _bbox.getMinX(), _bbox.getMinY(), _bbox.getWidth(), _bbox.getHeight(), _xy0[0], _xy0[1],
88  _doNormalize);
89 }
90 
92  if (_imagePtrList[location]) {
93  return _imagePtrList[location];
94  }
95 
96  ImagePtr imagePtr(new Image(_kernelPtr->getDimensions()));
97  _imagePtrList[location] = imagePtr;
98  _computeImage(location);
99  return imagePtr;
100 }
101 
103  switch (location) {
104  case BOTTOM_LEFT:
105  return _bbox.getMin();
106  break; // paranoia
107  case BOTTOM_RIGHT:
108  return geom::Point2I(_bbox.getMaxX() + 1, _bbox.getMinY());
109  break; // paranoia
110  case TOP_LEFT:
111  return geom::Point2I(_bbox.getMinX(), _bbox.getMaxY() + 1);
112  break; // paranoia
113  case TOP_RIGHT:
114  return geom::Point2I(_bbox.getMaxX() + 1, _bbox.getMaxY() + 1);
115  break; // paranoia
116  default: {
118  os << "Bug: unhandled location = " << location;
120  }
121  }
122 }
123 
125  if (regionRow.isLastRow()) {
126  return false;
127  }
128 
129  bool hasData = regionRow.hasData();
130  int startY;
131  if (hasData) {
132  startY = regionRow.front()->getBBox().getMaxY() + 1;
133  } else {
134  startY = this->_bbox.getMinY();
135  }
136 
137  int yInd = regionRow.incrYInd();
138  int remHeight = 1 + this->_bbox.getMaxY() - startY;
139  int remYDiv = regionRow.getNY() - yInd;
140  int height = _computeNextSubregionLength(remHeight, remYDiv);
141 
142  if (hasData) {
143  // Move each region up one segment
144  bool isFirst = true;
145  for (RowOfKernelImagesForRegion::Iterator rgnIter = regionRow.begin(), rgnEnd = regionRow.end();
146  rgnIter != rgnEnd; ++rgnIter) {
147  (*rgnIter)->_moveUp(isFirst, height);
148  isFirst = false;
149  }
150 
151  } else {
152  ImagePtr blImagePtr = getImage(BOTTOM_LEFT);
153  ImagePtr brImagePtr;
154  ImagePtr tlImagePtr;
155  ImagePtr const trImageNullPtr;
156 
157  geom::Point2I blCorner = geom::Point2I(this->_bbox.getMinX(), startY);
158 
159  int remWidth = this->_bbox.getWidth();
160  int remXDiv = regionRow.getNX();
161  for (RowOfKernelImagesForRegion::Iterator rgnIter = regionRow.begin(), rgnEnd = regionRow.end();
162  rgnIter != rgnEnd; ++rgnIter) {
163  int width = _computeNextSubregionLength(remWidth, remXDiv);
164  --remXDiv;
165  remWidth -= width;
166 
168  _kernelPtr, geom::Box2I(blCorner, geom::Extent2I(width, height)), _xy0, _doNormalize,
169  blImagePtr, brImagePtr, tlImagePtr, trImageNullPtr));
170  *rgnIter = regionPtr;
171 
172  if (!tlImagePtr) {
173  regionPtr->getImage(TOP_LEFT);
174  }
175 
176  blCorner += geom::Extent2I(width, 0);
177  blImagePtr = regionPtr->getImage(BOTTOM_RIGHT);
178  tlImagePtr = regionPtr->getImage(TOP_RIGHT);
179  }
180  }
181  return true;
182 }
183 
184 void KernelImagesForRegion::_computeImage(Location location) const {
185  ImagePtr imagePtr = _imagePtrList[location];
186  if (!imagePtr) {
188  os << "Null imagePtr at location " << location;
190  }
191 
192  geom::Point2I pixelIndex = getPixelIndex(location);
193  _kernelPtr->computeImage(*imagePtr, _doNormalize, image::indexToPosition(pixelIndex.getX() + _xy0[0]),
194  image::indexToPosition(pixelIndex.getY() + _xy0[1]));
195 }
196 
197 std::vector<int> KernelImagesForRegion::_computeSubregionLengths(int length, int nDivisions) {
198  if ((nDivisions > length) || (nDivisions < 1)) {
200  os << "nDivisions = " << nDivisions << " not in range [1, " << length << " = length]";
202  }
203  std::vector<int> regionLengths;
204  int remLength = length;
205  for (int remNDiv = nDivisions; remNDiv > 0; --remNDiv) {
206  int subLength = _computeNextSubregionLength(remLength, remNDiv);
207  if (subLength < 1) {
209  os << "Bug! _computeSubregionLengths(length=" << length << ", nDivisions=" << nDivisions
210  << ") computed sublength = " << subLength << " < 0; remLength = " << remLength;
212  }
213  regionLengths.push_back(subLength);
214  remLength -= subLength;
215  }
216  return regionLengths;
217 }
218 
219 void KernelImagesForRegion::_moveUp(bool isFirst, int newHeight) {
220  // move bbox up (this must be done before recomputing the top kernel images)
221  _bbox = geom::Box2I(geom::Point2I(_bbox.getMinX(), _bbox.getMaxY() + 1),
222  geom::Extent2I(_bbox.getWidth(), newHeight));
223 
224  // swap top and bottom image pointers
225  _imagePtrList[BOTTOM_RIGHT].swap(_imagePtrList[TOP_RIGHT]);
226  _imagePtrList[BOTTOM_LEFT].swap(_imagePtrList[TOP_LEFT]);
227 
228  // recompute top right, and if the first image also recompute top left
229  _computeImage(TOP_RIGHT);
230  if (isFirst) {
231  _computeImage(TOP_LEFT);
232  }
233 }
234 
235 int const KernelImagesForRegion::_MinInterpolationSize = 10;
236 
238  : _nx(nx), _ny(ny), _yInd(-1), _regionList(nx) {
239  if ((nx < 1) || (ny < 1)) {
241  os << "nx = " << nx << " and/or ny = " << ny << " < 1";
243  };
244 }
245 }
246 }
247 }
248 } // end lsst::afw::math::detail
Point2I const getMin() const
Definition: Box.h:123
int getMinX() const
Definition: Box.h:124
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:55
ImagePtr getImage(Location location) const
Return the image and sum at the specified location.
Location
locations of various points in the region
Definition: Convolve.h:192
std::shared_ptr< KernelImagesForRegion > front()
Return the first region in the list.
Definition: Convolve.h:366
Extent< int, 2 > Extent2I
Definition: Extent.h:380
int getHeight() const
Definition: Box.h:155
#define LOGL_DEBUG(logger, message...)
An integer coordinate rectangle.
Definition: Box.h:55
A base class for image defects.
bool computeNextRow(RowOfKernelImagesForRegion &regionRow) const
Compute next row of subregions.
T str(T... args)
A row of KernelImagesForRegion.
Definition: Convolve.h:334
int getWidth() const
Definition: Box.h:154
#define LSST_EXCEPT(type,...)
lsst::afw::geom::Point2I getPixelIndex(Location location) const
Compute pixel index of a given location, relative to the parent image (thus offset by bottom left cor...
Citizen(const std::type_info &)
RegionList::const_iterator end() const
Return the end iterator for the list.
Definition: Convolve.h:354
int getMaxX() const
Definition: Box.h:128
RegionList::const_iterator begin() const
Return the begin iterator for the list.
Definition: Convolve.h:350
RowOfKernelImagesForRegion(int nx, int ny)
Construct a RowOfKernelImagesForRegion.
table::Box2IKey bbox
KernelImagesForRegion(KernelConstPtr kernelPtr, lsst::afw::geom::Box2I const &bbox, lsst::afw::geom::Point2I const &xy0, bool doNormalize)
Construct a KernelImagesForRegion.
int getMaxY() const
Definition: Box.h:129
Point< int, 2 > Point2I
Definition: Point.h:297
int getMinY() const
Definition: Box.h:125
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > Image
Definition: Convolve.h:175