27 #include "boost/format.hpp"
29 #pragma warning (push)
30 #pragma warning (disable: 444)
32 #include "boost/archive/text_oarchive.hpp"
40 namespace pexExcept = lsst::pex::exceptions;
41 namespace afwGeom = lsst::afw::geom;
42 namespace afwMath = lsst::afw::math;
53 daf::base::Citizen(typeid(this)),
54 _spatialFunctionList(),
65 unsigned int nKernelParams,
68 daf::base::Citizen(typeid(this)),
69 _spatialFunctionList(),
74 _nKernelParams(nKernelParams)
76 if ((width < 1) || (height < 1)) {
77 std::ostringstream os;
78 os <<
"kernel height = " << height <<
" and/or width = " << width <<
" < 1";
79 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
81 if (dynamic_cast<const NullSpatialFunction*>(&spatialFunction)) {
84 if (nKernelParams == 0) {
85 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"Kernel function has no parameters");
87 for (
unsigned int ii = 0; ii < nKernelParams; ++ii) {
101 std::ostringstream os;
103 <<
") != (" << this->getWidth() <<
", " << this->getHeight() <<
") = kernel dimensions";
104 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
106 image.
setXY0(-_ctrX, -_ctrY);
107 if (this->isSpatiallyVarying()) {
108 this->setKernelParametersFromSpatialModel(x, y);
110 return doComputeImage(image, doNormalize);
116 std::vector<SpatialFunctionPtr> spatialFunctionList)
118 daf::base::Citizen(typeid(this)),
123 _nKernelParams(spatialFunctionList.size())
125 if ((width < 1) || (height < 1)) {
126 std::ostringstream os;
127 os <<
"kernel height = " << height <<
" and/or width = " << width <<
" < 1";
128 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
130 for (
unsigned int ii = 0; ii < spatialFunctionList.size(); ++ii) {
141 unsigned int nKernelParams = this->getNKernelParameters();
142 if (params.size() != nKernelParams) {
143 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
144 (
boost::format(
"params has %d entries instead of %d") % params.size() % nKernelParams).str());
146 unsigned int nSpatialParams = this->getNSpatialParameters();
147 for (
unsigned int ii = 0; ii < nKernelParams; ++ii) {
148 if (params[ii].size() != nSpatialParams) {
149 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
151 ii % params[ii].size() % nSpatialParams).str());
155 if (nSpatialParams > 0) {
156 for (
unsigned int ii = 0; ii < nKernelParams; ++ii) {
157 this->_spatialFunctionList[ii]->setParameters(params[ii]);
163 std::vector<double> &kernelParams,
double x,
double y)
const {
164 std::vector<double>::iterator paramIter = kernelParams.begin();
165 std::vector<SpatialFunctionPtr>::const_iterator funcIter = _spatialFunctionList.begin();
166 for ( ; funcIter != _spatialFunctionList.end(); ++funcIter, ++paramIter) {
167 *paramIter = (*(*funcIter))(x,y);
174 if (index >= _spatialFunctionList.size()) {
175 if (!this->isSpatiallyVarying()) {
176 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"kernel is not spatially varying");
178 std::ostringstream errStream;
179 errStream <<
"index = " << index <<
"; must be < , " << _spatialFunctionList.size();
180 throw LSST_EXCEPT(pexExcept::InvalidParameterError, errStream.str());
183 return _spatialFunctionList[index]->clone();
188 std::vector<SpatialFunctionPtr> spFuncCopyList;
189 for (std::vector<SpatialFunctionPtr>::const_iterator spFuncIter = _spatialFunctionList.begin();
190 spFuncIter != _spatialFunctionList.end(); ++spFuncIter) {
191 spFuncCopyList.push_back((**spFuncIter).clone());
193 return spFuncCopyList;
197 return std::vector<double>();
209 std::ostringstream os;
211 << getWidth() <<
", " << getHeight() <<
") in one or both dimensions";
212 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
225 std::ostringstream os;
226 os << prefix <<
"Kernel:" << std::endl;
227 os << prefix <<
"..height, width: " << _height <<
", " << _width << std::endl;
228 os << prefix <<
"..ctr (X, Y): " << _ctrX <<
", " << _ctrY << std::endl;
229 os << prefix <<
"..nKernelParams: " << _nKernelParams << std::endl;
230 os << prefix <<
"..isSpatiallyVarying: " << (this->isSpatiallyVarying() ?
"True" :
"False") << std::endl;
231 if (this->isSpatiallyVarying()) {
232 os << prefix <<
"..spatialFunctions:" << std::endl;
233 for (std::vector<SpatialFunctionPtr>::const_iterator spFuncPtr = _spatialFunctionList.begin();
234 spFuncPtr != _spatialFunctionList.end(); ++spFuncPtr) {
235 os << prefix <<
"...." << (*spFuncPtr)->toString() << std::endl;
241 #if 0 // This fails to compile with icc
242 void afwMath::Kernel::toFile(std::string fileName)
const {
243 std::ofstream os(fileName.c_str());
244 boost::archive::text_oarchive oa(os);
254 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"Kernel has no kernel parameters");
258 std::vector<SpatialFunctionPtr>::const_iterator funcIter = _spatialFunctionList.begin();
259 for (
int ii = 0; funcIter != _spatialFunctionList.end(); ++funcIter, ++ii) {
260 this->setKernelParameter(ii, (*(*funcIter))(x,y));
void setXY0(geom::Point2I const origin)
boost::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
Declare the Kernel class and subclasses.
Extent2I const getDimensions() const
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Include files required for standard LSST Exception handling.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
lsst::afw::geom::Box2I growBBox(lsst::afw::geom::Box2I const &bbox) const
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Extent< int, 2 > Extent2I
An integer coordinate rectangle.
table::Key< table::Array< Kernel::Pixel > > image
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
void ImageT ImageT int float saturatedPixelValue int const width
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
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.
int getHeight() const
Return the number of rows in the image.
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
void ImageT ImageT int float saturatedPixelValue int const height
void setSpatialParameters(const std::vector< std::vector< double > > params)
Set the parameters of all spatial functions.
#define LSST_EXCEPT(type,...)
Kernel has only one non-zero pixel.
virtual Ptr clone() const =0
Return a pointer to a deep copy of this function.
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
std::vector< SpatialFunctionPtr > _spatialFunctionList
Kernel()
Construct a null Kernel of size 0,0.
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
Point2I const getMin() const
int getWidth() const
Return the number of columns in the image.
lsst::afw::geom::Box2I shrinkBBox(lsst::afw::geom::Box2I const &bbox) const
Tags carrying information about Kernels Kernel with no special properties.