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
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 
34 #include <algorithm>
35 #include <cmath>
36 #include <iostream>
37 #include <sstream>
38 #include <vector>
39 
40 #include "boost/assign/list_of.hpp"
41 
42 #include "lsst/pex/exceptions.h"
43 #include "lsst/pex/logging/Trace.h"
44 #include "lsst/afw/geom.h"
47 
48 namespace pexExcept = lsst::pex::exceptions;
49 namespace pexLog = lsst::pex::logging;
50 namespace afwGeom = lsst::afw::geom;
51 namespace afwImage = lsst::afw::image;
52 namespace mathDetail = lsst::afw::math::detail;
53 
60  KernelConstPtr kernelPtr,
61  lsst::afw::geom::Box2I const &bbox,
62  lsst::afw::geom::Point2I const &xy0,
65  bool doNormalize)
66 :
67  lsst::daf::base::Citizen(typeid(this)),
68  _kernelPtr(kernelPtr),
69  _bbox(bbox),
70  _xy0(xy0),
71  _doNormalize(doNormalize),
72  _imagePtrList(4)
73 {
74  if (!_kernelPtr) {
75  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernelPtr is null");
76  }
77  pexLog::TTrace<6>("lsst.afw.math.convolve",
78  "KernelImagesForRegion(bbox(minimum=(%d, %d), extent=(%d, %d)), xy0=(%d, %d), doNormalize=%d, images...)",
80 }
81 
93  KernelConstPtr const kernelPtr,
94  lsst::afw::geom::Box2I const &bbox,
95  lsst::afw::geom::Point2I const &xy0,
98  bool doNormalize,
99  ImagePtr bottomLeftImagePtr,
100  ImagePtr bottomRightImagePtr,
101  ImagePtr topLeftImagePtr,
102  ImagePtr topRightImagePtr)
103 :
104  lsst::daf::base::Citizen(typeid(this)),
105  _kernelPtr(kernelPtr),
106  _bbox(bbox),
107  _xy0(xy0),
108  _doNormalize(doNormalize),
109  _imagePtrList(4)
110 {
111  if (!_kernelPtr) {
112  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernelPtr is null");
113  }
114  _insertImage(BOTTOM_LEFT, bottomLeftImagePtr);
115  _insertImage(BOTTOM_RIGHT, bottomRightImagePtr);
116  _insertImage(TOP_LEFT, topLeftImagePtr);
117  _insertImage(TOP_RIGHT, topRightImagePtr);
118  pexLog::TTrace<6>("lsst.afw.math.convolve",
119  "KernelImagesForRegion(bbox(minimum=(%d, %d), extent=(%d, %d)), xy0=(%d, %d), doNormalize=%d, images...)",
121 }
122 
129  Location location)
130 const {
131  if (_imagePtrList[location]) {
132  return _imagePtrList[location];
133  }
134 
135  ImagePtr imagePtr(new Image(_kernelPtr->getDimensions()));
136  _imagePtrList[location] = imagePtr;
137  _computeImage(location);
138  return imagePtr;
139 }
140 
146  Location location)
147 const {
148  switch (location) {
149  case BOTTOM_LEFT:
150  return _bbox.getMin();
151  break; // paranoia
152  case BOTTOM_RIGHT:
153  return afwGeom::Point2I(_bbox.getMaxX() + 1, _bbox.getMinY());
154  break; // paranoia
155  case TOP_LEFT:
156  return afwGeom::Point2I(_bbox.getMinX(), _bbox.getMaxY() + 1);
157  break; // paranoia
158  case TOP_RIGHT:
159  return afwGeom::Point2I(_bbox.getMaxX() + 1, _bbox.getMaxY() + 1);
160  break; // paranoia
161  default: {
162  std::ostringstream os;
163  os << "Bug: unhandled location = " << location;
164  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
165  }
166  }
167 }
168 
178  RowOfKernelImagesForRegion &regionRow)
179 const {
180  if (regionRow.isLastRow()) {
181  return false;
182  }
183 
184  bool hasData = regionRow.hasData();
185  int startY;
186  if (hasData) {
187  startY = regionRow.front()->getBBox().getMaxY() + 1;
188  } else {
189  startY = this->_bbox.getMinY();
190  }
191 
192  int yInd = regionRow.incrYInd();
193  int remHeight = 1 + this->_bbox.getMaxY() - startY;
194  int remYDiv = regionRow.getNY() - yInd;
195  int height = _computeNextSubregionLength(remHeight, remYDiv);
196 
197  if (hasData) {
198  // Move each region up one segment
199  bool isFirst = true;
200  for (RowOfKernelImagesForRegion::Iterator rgnIter = regionRow.begin(), rgnEnd = regionRow.end();
201  rgnIter != rgnEnd; ++rgnIter) {
202  (*rgnIter)->_moveUp(isFirst, height);
203  isFirst = false;
204  }
205 
206  } else {
207  ImagePtr blImagePtr = getImage(BOTTOM_LEFT);
208  ImagePtr brImagePtr;
209  ImagePtr tlImagePtr;
210  ImagePtr const trImageNullPtr;
211 
212  afwGeom::Point2I blCorner = afwGeom::Point2I(this->_bbox.getMinX(), startY);
213 
214  int remWidth = this->_bbox.getWidth();
215  int remXDiv = regionRow.getNX();
216  for (RowOfKernelImagesForRegion::Iterator rgnIter = regionRow.begin(), rgnEnd = regionRow.end();
217  rgnIter != rgnEnd; ++rgnIter) {
218  int width = _computeNextSubregionLength(remWidth, remXDiv);
219  --remXDiv;
220  remWidth -= width;
221 
223  _kernelPtr,
224  afwGeom::Box2I(blCorner, afwGeom::Extent2I(width, height)),
225  _xy0,
226  _doNormalize,
227  blImagePtr,
228  brImagePtr,
229  tlImagePtr,
230  trImageNullPtr));
231  *rgnIter = regionPtr;
232 
233  if (!tlImagePtr) {
234  regionPtr->getImage(TOP_LEFT);
235  }
236 
237  blCorner += afwGeom::Extent2I(width, 0);
238  blImagePtr = regionPtr->getImage(BOTTOM_RIGHT);
239  tlImagePtr = regionPtr->getImage(TOP_RIGHT);
240  }
241  }
242  return true;
243 }
244 
251  ImagePtr imagePtr = _imagePtrList[location];
252  if (!imagePtr) {
253  std::ostringstream os;
254  os << "Null imagePtr at location " << location;
255  throw LSST_EXCEPT(pexExcept::NotFoundError, os.str());
256  }
257 
258  afwGeom::Point2I pixelIndex = getPixelIndex(location);
259  _kernelPtr->computeImage(
260  *imagePtr,
261  _doNormalize,
262  afwImage::indexToPosition(pixelIndex.getX() + _xy0[0]),
263  afwImage::indexToPosition(pixelIndex.getY() + _xy0[1]));
264 }
265 
275  int length,
276  int nDivisions)
277 {
278  if ((nDivisions > length) || (nDivisions < 1)) {
279  std::ostringstream os;
280  os << "nDivisions = " << nDivisions << " not in range [1, " << length << " = length]";
281  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
282  }
283  std::vector<int> regionLengths;
284  int remLength = length;
285  for (int remNDiv = nDivisions; remNDiv > 0; --remNDiv) {
286  int subLength = _computeNextSubregionLength(remLength, remNDiv);
287  if (subLength < 1) {
288  std::ostringstream os;
289  os << "Bug! _computeSubregionLengths(length=" << length << ", nDivisions=" << nDivisions <<
290  ") computed sublength = " << subLength << " < 0; remLength = " << remLength;
291  throw LSST_EXCEPT(pexExcept::RuntimeError, os.str());
292  }
293  regionLengths.push_back(subLength);
294  remLength -= subLength;
295  }
296  return regionLengths;
297 }
298 
310  bool isFirst,
311  int newHeight)
312 {
313  // move bbox up (this must be done before recomputing the top kernel images)
316  afwGeom::Extent2I(_bbox.getWidth(), newHeight));
317 
318  // swap top and bottom image pointers
319  _imagePtrList[BOTTOM_RIGHT].swap(_imagePtrList[TOP_RIGHT]);
320  _imagePtrList[BOTTOM_LEFT].swap(_imagePtrList[TOP_LEFT]);
321 
322  // recompute top right, and if the first image also recompute top left
323  _computeImage(TOP_RIGHT);
324  if (isFirst) {
325  _computeImage(TOP_LEFT);
326  }
327 }
328 
329 
331 
336  int nx,
337  int ny)
338 :
339  _nx(nx),
340  _ny(ny),
341  _yInd(-1),
342  _regionList(nx)
343 {
344  if ((nx < 1) || (ny < 1)) {
345  std::ostringstream os;
346  os << "nx = " << nx << " and/or ny = " << ny << " < 1";
347  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
348  };
349 }
Convolution support.
An include file to include the header files for lsst::afw::geom.
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:54
boost::shared_ptr< Image > ImagePtr
Definition: Convolve.h:118
boost::shared_ptr< lsst::afw::math::Kernel const > KernelConstPtr
Definition: Convolve.h:116
definition of the Trace messaging facilities
int getMinY() const
Definition: Box.h:125
ImagePtr getImage(Location location) const
Point< int, 2 > Point2I
Definition: PSF.h:39
int getMaxX() const
Definition: Box.h:128
boost::shared_ptr< KernelImagesForRegion > front()
Return the first region in the list.
Definition: Convolve.h:231
Image utility functions.
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
RegionList::const_iterator end() const
Return the end iterator for the list.
Definition: Convolve.h:219
int getHeight() const
Definition: Box.h:155
geom::Box2I const & _bbox
Definition: fits_io_mpl.h:80
void _moveUp(bool isFirst, int newHeight)
Move the region up one segment.
static std::vector< int > _computeSubregionLengths(int length, int nDivisions)
A row of KernelImagesForRegion.
Definition: Convolve.h:205
int getMaxY() const
Definition: Box.h:129
int getMinX() const
Definition: Box.h:124
bool computeNextRow(RowOfKernelImagesForRegion &regionRow) const
Compute next row of subregions.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
RegionList::const_iterator begin() const
Return the begin iterator for the list.
Definition: Convolve.h:215
boost::shared_ptr< KernelImagesForRegion > Ptr
Definition: Convolve.h:121
lsst::afw::geom::Point2I getPixelIndex(Location location) const
Point2I const getMin() const
Definition: Box.h:123
void _insertImage(Location location, ImagePtr imagePtr) const
Definition: Convolve.h:318
geom::Point2I & _xy0
Definition: fits_io_mpl.h:78
KernelImagesForRegion(KernelConstPtr kernelPtr, lsst::afw::geom::Box2I const &bbox, lsst::afw::geom::Point2I const &xy0, bool doNormalize)
RowOfKernelImagesForRegion(int nx, int ny)
Construct a RowOfKernelImagesForRegion.
Extent< int, 2 > Extent2I
Definition: Extent.h:355
int getWidth() const
Definition: Box.h:154
Include files required for standard LSST Exception handling.