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
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 namespace afwGeom = lsst::afw::geom;
33 namespace afwMath = lsst::afw::math;
34 namespace afwImage = lsst::afw::image;
35 
37 :
38  Kernel(),
39  _image(),
40  _sum(0) {
41 }
42 
45 ) :
46  Kernel(image.getWidth(), image.getHeight(), 0),
47  _image(image, true),
48  _sum(0) {
49 
51  double imSum = 0.0;
52  for (int y = 0; y != image.getHeight(); ++y) {
53  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y); imPtr != imEnd; ++imPtr) {
54  imSum += *imPtr;
55  }
56  }
57  this->_sum = imSum;
58 }
59 
61  afwMath::Kernel const& kernel,
62  afwGeom::Point2D const& pos
63 ) :
64  Kernel(kernel.getWidth(), kernel.getHeight(), 0),
65  _image(kernel.getDimensions()),
66  _sum(0) {
67  _sum = kernel.computeImage(_image, false, pos[0], pos[1]);
68 }
69 
70 PTR(afwMath::Kernel) afwMath::FixedKernel::clone() const {
71  PTR(afwMath::Kernel) retPtr(new afwMath::FixedKernel(_image));
72  retPtr->setCtr(this->getCtr());
73  return retPtr;
74 }
75 
78  bool doNormalize
79 ) const {
80  double multFactor = 1.0;
81  double imSum = this->_sum;
82  if (doNormalize) {
83  if (imSum == 0) {
84  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
85  }
86  multFactor = 1.0/static_cast<double>(this->_sum);
87  imSum = 1.0;
88  }
89 
91 
92  for (int y = 0; y != this->getHeight(); ++y) {
93  for (XIter imPtr = image.row_begin(y), imEnd = image.row_end(y), kPtr = this->_image.row_begin(y);
94  imPtr != imEnd; ++imPtr, ++kPtr) {
95  imPtr[0] = multFactor*kPtr[0];
96  }
97  }
98 
99  return imSum;
100 }
101 
102 std::string afwMath::FixedKernel::toString(std::string const& prefix) const {
103  std::ostringstream os;
104  os << prefix << "FixedKernel:" << std::endl;
105  os << prefix << "..sum: " << _sum << std::endl;
106  os << Kernel::toString(prefix + "\t");
107  return os.str();
108 }
109 
110 // ------ Persistence ---------------------------------------------------------------------------------------
111 
112 namespace lsst { namespace afw { namespace math {
113 
114 namespace {
115 
116 struct FixedKernelPersistenceHelper : public Kernel::PersistenceHelper {
117  table::Key< table::Array<Kernel::Pixel> > image;
118 
119  explicit FixedKernelPersistenceHelper(geom::Extent2I const & dimensions) :
120  Kernel::PersistenceHelper(0),
121  image(
122  schema.addField< table::Array<Kernel::Pixel> >(
123  "image", "pixel values (row-major)", dimensions.getX() * dimensions.getY()
124  )
125  )
126  {}
127 
128  explicit FixedKernelPersistenceHelper(table::Schema const & schema_) :
129  Kernel::PersistenceHelper(schema_),
130  image(schema["image"])
131  {}
132 };
133 
134 } // anonymous
135 
137 public:
138 
140  read(InputArchive const & archive, CatalogVector const & catalogs) const {
141  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
142  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
143  FixedKernelPersistenceHelper const keys(catalogs.front().getSchema());
144  afw::table::BaseRecord const & record = catalogs.front().front();
145  image::Image<Pixel> image(geom::Extent2I(record.get(keys.dimensions)));
146  ndarray::flatten<1>(
148  ) = record[keys.image];
149  PTR(FixedKernel) result = boost::make_shared<FixedKernel>(image);
150  result->setCtr(record.get(keys.center));
151  return result;
152  }
153 
154  explicit Factory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
155 };
156 
157 namespace {
158 
159 std::string getFixedKernelPersistenceName() { return "FixedKernel"; }
160 
161 FixedKernel::Factory registration(getFixedKernelPersistenceName());
162 
163 } // anonymous
164 
165 std::string FixedKernel::getPersistenceName() const { return getFixedKernelPersistenceName(); }
166 
168  FixedKernelPersistenceHelper const keys(getDimensions());
169  PTR(afw::table::BaseRecord) record = keys.write(handle, *this);
170  (*record)[keys.image] = ndarray::flatten<1>(ndarray::copy(_image.getArray()));
171 }
172 
173 }}} // namespace lsst::afw::math
int y
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: FixedKernel.cc:102
lsst::afw::image::Image< Pixel > _image
Definition: Kernel.h:601
Declare the Kernel class and subclasses.
#define PTR(...)
Definition: base.h:41
table::Key< std::string > name
Definition: ApCorrMap.cc:71
x_iterator row_begin(int y) const
Definition: Image.h:319
An object passed to Persistable::write to allow it to persist itself.
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: Image.h:324
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:224
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:231
FixedKernel()
Construct an empty FixedKernel of size 0x0.
Definition: FixedKernel.cc:36
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
Definition: FixedKernel.cc:76
Factory(std::string const &name)
Definition: FixedKernel.cc:154
Array< T, N, C_ > static_dimension_cast(Array< T, N, C > const &array)
Definition: casts.h:89
std::map< Citizen const *, CitizenInfo > table
Definition: Citizen.h:93
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: FixedKernel.cc:165
afw::table::PointKey< int > dimensions
ImageT::Pixel _sum
Definition: CR.cc:313
tbl::Schema schema
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:26
Base class for all records.
Definition: BaseRecord.h:27
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:239
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:28
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: FixedKernel.cc:167
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:134
A kernel created from an Image.
Definition: Kernel.h:551
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:94
Include files required for standard LSST Exception handling.