lsst.afw  13.0-100-ge0459d59f+6
DeltaFunctionKernel.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 #include <sstream>
25 #include <vector>
26 
27 #include "lsst/pex/exceptions.h"
28 #include "lsst/afw/math/Kernel.h"
30 
31 namespace pexExcept = lsst::pex::exceptions;
32 
33 namespace lsst {
34 namespace afw {
35 namespace math {
36 
37 DeltaFunctionKernel::DeltaFunctionKernel(int width, int height, geom::Point2I const& point)
38  : Kernel(width, height, 0), _pixel(point) {
39  if (point.getX() < 0 || point.getX() >= width || point.getY() < 0 || point.getY() >= height) {
40  std::ostringstream os;
41  os << "point (" << point.getX() << ", " << point.getY() << ") lies outside " << width << "x" << height
42  << " sized kernel";
43  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
44  }
45 }
46 
47 std::shared_ptr<Kernel> DeltaFunctionKernel::clone() const {
48  std::shared_ptr<Kernel> retPtr(
49  new DeltaFunctionKernel(this->getWidth(), this->getHeight(), this->_pixel));
50  retPtr->setCtr(this->getCtr());
51  return retPtr;
52 }
53 
54 std::shared_ptr<Kernel> DeltaFunctionKernel::resized(int width, int height) const {
55  int padX = width - getWidth();
56  int padY = height - getHeight();
57  if ((padX % 2) || (padY % 2)) {
58  std::ostringstream os;
59  os << "Cannot resize DeltaFunctionKernel from (" << getWidth() << ", " << getHeight() << ") to ("
60  << width << ", " << height << "), because at least one dimension would change by an odd value.";
61  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
62  }
63  int newPixelX = getPixel().getX() + padX/2;
64  int newPixelY = getPixel().getY() + padY/2;
65  std::shared_ptr<Kernel> retPtr = std::make_shared<DeltaFunctionKernel>(
66  width, height, lsst::afw::geom::Point2I(newPixelX, newPixelY));
67  return retPtr;
68 }
69 
70 std::string DeltaFunctionKernel::toString(std::string const& prefix) const {
71  const int pixelX = getPixel().getX(); // active pixel in Kernel
72  const int pixelY = getPixel().getY();
73 
74  std::ostringstream os;
75  os << prefix << "DeltaFunctionKernel:" << std::endl;
76  os << prefix << "Pixel (c,r) " << pixelX << "," << pixelY << ")" << std::endl;
77  os << Kernel::toString(prefix + "\t");
78  return os.str();
79 }
80 
82  const int pixelX = getPixel().getX(); // active pixel in Kernel
83  const int pixelY = getPixel().getY();
84 
85  image = 0;
86  *image.xy_at(pixelX, pixelY) = 1;
87 
88  return 1;
89 }
90 
91 // ------ Persistence ---------------------------------------------------------------------------------------
92 
93 namespace {
94 
95 struct DeltaFunctionKernelPersistenceHelper : public Kernel::PersistenceHelper {
96  table::PointKey<int> pixel;
97 
98  static DeltaFunctionKernelPersistenceHelper const& get() {
99  static DeltaFunctionKernelPersistenceHelper const instance;
100  return instance;
101  }
102 
103  // No copying
104  DeltaFunctionKernelPersistenceHelper(const DeltaFunctionKernelPersistenceHelper&) = delete;
105  DeltaFunctionKernelPersistenceHelper& operator=(const DeltaFunctionKernelPersistenceHelper&) = delete;
106 
107  // No moving
108  DeltaFunctionKernelPersistenceHelper(DeltaFunctionKernelPersistenceHelper&&) = delete;
109  DeltaFunctionKernelPersistenceHelper& operator=(DeltaFunctionKernelPersistenceHelper&&) = delete;
110 
111 private:
112  explicit DeltaFunctionKernelPersistenceHelper()
113  : Kernel::PersistenceHelper(0),
114  pixel(table::PointKey<int>::addFields(schema, "pixel", "position of nonzero pixel", "pixel")) {
115  schema.getCitizen().markPersistent();
116  }
117 };
118 
119 } // anonymous
120 
122 public:
123  virtual std::shared_ptr<afw::table::io::Persistable> read(InputArchive const& archive,
124  CatalogVector const& catalogs) const {
125  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
126  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
127  DeltaFunctionKernelPersistenceHelper const& keys = DeltaFunctionKernelPersistenceHelper::get();
128  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
129  afw::table::BaseRecord const& record = catalogs.front().front();
130  std::shared_ptr<DeltaFunctionKernel> result(
131  new DeltaFunctionKernel(record.get(keys.dimensions.getX()),
132  record.get(keys.dimensions.getY()), record.get(keys.pixel)));
133  result->setCtr(record.get(keys.center));
134  return result;
135  }
136 
137  explicit Factory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
138 };
139 
140 namespace {
141 
142 std::string getDeltaFunctionKernelPersistenceName() { return "DeltaFunctionKernel"; }
143 
144 DeltaFunctionKernel::Factory registration(getDeltaFunctionKernelPersistenceName());
145 
146 } // anonymous
147 
149  return getDeltaFunctionKernelPersistenceName();
150 }
151 
153  DeltaFunctionKernelPersistenceHelper const& keys = DeltaFunctionKernelPersistenceHelper::get();
154  std::shared_ptr<afw::table::BaseRecord> record = keys.write(handle, *this);
155  record->set(keys.pixel, _pixel);
156 }
157 }
158 }
159 } // namespace lsst::afw::math
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:239
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
Add a pair of _x, _y fields to a Schema, and return a PointKey that points to them.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
An object passed to Persistable::write to allow it to persist itself.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual std::shared_ptr< afw::table::io::Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const
Construct a new object from the given InputArchive and vector of catalogs.
DeltaFunctionKernel(int width, int height, lsst::afw::geom::Point2I const &point)
Construct a spatially invariant DeltaFunctionKernel.
virtual std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
afw::table::Schema schema
Definition: GaussianPsf.cc:43
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:214
lsst::afw::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:244
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
A base class for image defects.
Definition: cameraGeom.dox:3
lsst::afw::geom::Point2I getPixel() const
Definition: Kernel.h:720
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:234
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
Base class for all records.
Definition: BaseRecord.h:29
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
table::PointKey< int > pixel
Point< int, 2 > Point2I
Definition: Point.h:270
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123
xy_locator xy_at(int x, int y) const
Return an xy_locator at the point (x, y) in the image.
Definition: ImageBase.h:426
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197