lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FixedKernel.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 <stdexcept>
25 #include <numeric>
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 FixedKernel::FixedKernel() : Kernel(), _image(), _sum(0) {}
38 
40  : Kernel(image.getWidth(), image.getHeight(), 0), _image(image, true), _sum(0) {
41  typedef image::Image<Pixel>::x_iterator XIter;
42  double imSum = 0.0;
43  for (int y = 0; y != image.getHeight(); ++y) {
44  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y); imPtr != imEnd; ++imPtr) {
45  imSum += *imPtr;
46  }
47  }
48  this->_sum = imSum;
49 }
50 
51 FixedKernel::FixedKernel(Kernel const& kernel, geom::Point2D const& pos)
52  : Kernel(kernel.getWidth(), kernel.getHeight(), 0), _image(kernel.getDimensions()), _sum(0) {
53  _sum = kernel.computeImage(_image, false, pos[0], pos[1]);
54 }
55 
56 std::shared_ptr<Kernel> FixedKernel::clone() const {
57  std::shared_ptr<Kernel> retPtr(new FixedKernel(_image));
58  retPtr->setCtr(this->getCtr());
59  return retPtr;
60 }
61 
62 double FixedKernel::doComputeImage(image::Image<Pixel>& image, bool doNormalize) const {
63  double multFactor = 1.0;
64  double imSum = this->_sum;
65  if (doNormalize) {
66  if (imSum == 0) {
67  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
68  }
69  multFactor = 1.0 / static_cast<double>(this->_sum);
70  imSum = 1.0;
71  }
72 
73  typedef image::Image<Pixel>::x_iterator XIter;
74 
75  for (int y = 0; y != this->getHeight(); ++y) {
76  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y), kPtr = this->_image.row_begin(y);
77  imPtr != imEnd; ++imPtr, ++kPtr) {
78  imPtr[0] = multFactor * kPtr[0];
79  }
80  }
81 
82  return imSum;
83 }
84 
85 std::string FixedKernel::toString(std::string const& prefix) const {
86  std::ostringstream os;
87  os << prefix << "FixedKernel:" << std::endl;
88  os << prefix << "..sum: " << _sum << std::endl;
89  os << Kernel::toString(prefix + "\t");
90  return os.str();
91 }
92 
93 // ------ Persistence ---------------------------------------------------------------------------------------
94 
95 namespace {
96 
97 struct FixedKernelPersistenceHelper : public Kernel::PersistenceHelper {
98  table::Key<table::Array<Kernel::Pixel> > image;
99 
100  explicit FixedKernelPersistenceHelper(geom::Extent2I const& dimensions)
101  : Kernel::PersistenceHelper(0),
102  image(schema.addField<table::Array<Kernel::Pixel> >("image", "pixel values (row-major)",
103  dimensions.getX() * dimensions.getY())) {}
104 
105  explicit FixedKernelPersistenceHelper(table::Schema const& schema_)
106  : Kernel::PersistenceHelper(schema_), image(schema["image"]) {}
107 };
108 
109 } // anonymous
110 
112 public:
113  virtual std::shared_ptr<afw::table::io::Persistable> read(InputArchive const& archive,
114  CatalogVector const& catalogs) const {
115  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
116  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
117  FixedKernelPersistenceHelper const keys(catalogs.front().getSchema());
118  afw::table::BaseRecord const& record = catalogs.front().front();
119  image::Image<Pixel> image(geom::Extent2I(record.get(keys.dimensions)));
120  ndarray::flatten<1>(ndarray::static_dimension_cast<2>(image.getArray())) = record[keys.image];
121  std::shared_ptr<FixedKernel> result = std::make_shared<FixedKernel>(image);
122  result->setCtr(record.get(keys.center));
123  return result;
124  }
125 
126  explicit Factory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
127 };
128 
129 namespace {
130 
131 std::string getFixedKernelPersistenceName() { return "FixedKernel"; }
132 
133 FixedKernel::Factory registration(getFixedKernelPersistenceName());
134 
135 } // anonymous
136 
137 std::string FixedKernel::getPersistenceName() const { return getFixedKernelPersistenceName(); }
138 
140  FixedKernelPersistenceHelper const keys(getDimensions());
141  std::shared_ptr<afw::table::BaseRecord> record = keys.write(handle, *this);
142  (*record)[keys.image] = ndarray::flatten<1>(ndarray::copy(_image.getArray()));
143 }
144 }
145 }
146 } // namespace lsst::afw::math
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:210
PersistableFactory(std::string const &name)
Constructor for the factory.
An object passed to Persistable::write to allow it to persist itself.
#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
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:147
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: FixedKernel.cc:137
Extent< int, 2 > Extent2I
Definition: Extent.h:341
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: FixedKernel.cc:85
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:227
FixedKernel()
Construct an empty FixedKernel of size 0x0.
Definition: FixedKernel.cc:37
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: Image.h:415
double computeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize, double x=0.0, double y=0.0) const
Compute an image (pixellized representation of the kernel) in place.
Definition: Kernel.cc:88
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
lsst::afw::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:232
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.
Definition: FixedKernel.cc:113
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
Base class for all records.
Definition: BaseRecord.h:29
afw::table::Key< afw::table::Array< ImagePixelT > > image
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: FixedKernel.cc:139
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
Definition: FixedKernel.cc:56
double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
Definition: FixedKernel.cc:62
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: Image.h:412
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
py::object result
Definition: schema.cc:290
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123
afw::table::Schema schema
Definition: GaussianPsf.cc:43
Factory(std::string const &name)
Definition: FixedKernel.cc:126