28 #include "lsst/pex/exceptions.h"
31 namespace pexExcept = lsst::pex::exceptions;
39 _kernelColFunctionPtr(),
40 _kernelRowFunctionPtr(),
53 :
Kernel(width, height, kernelColFunction.getNParameters() + kernelRowFunction.getNParameters(),
55 _kernelColFunctionPtr(kernelColFunction.
clone()),
56 _kernelRowFunctionPtr(kernelRowFunction.
clone()),
58 _localRowList(height),
68 std::vector<Kernel::SpatialFunctionPtr>
const& spatialFunctionList)
69 :
Kernel(width, height, spatialFunctionList),
70 _kernelColFunctionPtr(kernelColFunction.
clone()),
71 _kernelRowFunctionPtr(kernelRowFunction.
clone()),
73 _localRowList(height),
79 spatialFunctionList.size()) {
80 std::ostringstream os;
81 os <<
"kernelColFunction.getNParameters() + kernelRowFunction.getNParameters() = "
83 <<
" != " << spatialFunctionList.size() <<
" = "
84 <<
"spatialFunctionList.size()";
85 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
92 std::shared_ptr<Kernel> retPtr;
98 *(this->_kernelRowFunctionPtr)));
100 retPtr->setCtr(this->
getCtr());
106 bool doNormalize,
double x,
double y)
const {
107 if (static_cast<int>(colList.size()) != this->
getWidth() ||
108 static_cast<int>(rowList.size()) != this->
getHeight()) {
109 std::ostringstream os;
110 os <<
"colList.size(), rowList.size() = (" << colList.size() <<
", " << rowList.size() <<
") != ("
112 <<
"kernel dimensions";
113 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
119 return basicComputeVectors(colList, rowList, doNormalize);
123 return _kernelColFunctionPtr->clone();
127 return _kernelRowFunctionPtr->clone();
131 std::ostringstream os;
132 os << prefix <<
"SeparableKernel:" << std::endl;
134 <<
"..x (width) function: " << (_kernelColFunctionPtr ? _kernelColFunctionPtr->toString() :
"None")
137 <<
"..y (rows) function: " << (_kernelRowFunctionPtr ? _kernelRowFunctionPtr->toString() :
"None")
144 std::vector<double> allParams = _kernelColFunctionPtr->getParameters();
145 std::vector<double> yParams = _kernelRowFunctionPtr->getParameters();
146 std::copy(yParams.begin(), yParams.end(), std::back_inserter(allParams));
155 double imSum = basicComputeVectors(_localColList, _localRowList, doNormalize);
157 for (
int y = 0; y != image.
getHeight(); ++y) {
159 for (std::vector<Pixel>::iterator colIter = _localColList.begin(); colIter != _localColList.end();
160 ++colIter, ++imPtr) {
161 *imPtr = (*colIter) * _localRowList[y];
169 unsigned int const nColParams = _kernelColFunctionPtr->getNParameters();
170 if (ind < nColParams) {
171 _kernelColFunctionPtr->setParameter(ind, value);
173 _kernelRowFunctionPtr->setParameter(ind - nColParams, value);
181 double SeparableKernel::basicComputeVectors(std::vector<Pixel>& colList, std::vector<Pixel>& rowList,
182 bool doNormalize)
const {
184 if (_kernelColCache.empty()) {
185 for (
unsigned int i = 0; i != colList.size(); ++i) {
186 double colFuncValue = (*_kernelColFunctionPtr)(_kernelX[i]);
187 colList[i] = colFuncValue;
188 colSum += colFuncValue;
191 int const cacheSize = _kernelColCache.size();
195 std::vector<double>& cachedValues = _kernelColCache.at(indx);
196 for (
unsigned int i = 0; i != colList.size(); ++i) {
197 double colFuncValue = cachedValues[i];
198 colList[i] = colFuncValue;
199 colSum += colFuncValue;
204 if (_kernelRowCache.empty()) {
205 for (
unsigned int i = 0; i != rowList.size(); ++i) {
206 double rowFuncValue = (*_kernelRowFunctionPtr)(_kernelY[i]);
207 rowList[i] = rowFuncValue;
208 rowSum += rowFuncValue;
211 int const cacheSize = _kernelRowCache.size();
215 std::vector<double>& cachedValues = _kernelRowCache.at(indx);
216 for (
unsigned int i = 0; i != rowList.size(); ++i) {
217 double rowFuncValue = cachedValues[i];
218 rowList[i] = rowFuncValue;
219 rowSum += rowFuncValue;
222 if (indx == cacheSize/2) {
223 if (::fabs(rowFuncValue - (*_kernelRowFunctionPtr)(_kernelX[i])) > 1e-2) {
224 std::cout << indx <<
" " << i <<
" "
225 << rowFuncValue <<
" "
226 << (*_kernelRowFunctionPtr)(_kernelX[i])
234 double imSum = colSum * rowSum;
236 if ((colSum == 0) || (rowSum == 0)) {
237 throw LSST_EXCEPT(pexExcept::OverflowError,
"Cannot normalize; kernel sum is 0");
239 for (std::vector<Pixel>::iterator colIter = colList.begin(); colIter != colList.end(); ++colIter) {
243 for (std::vector<Pixel>::iterator rowIter = rowList.begin(); rowIter != rowList.end(); ++rowIter) {
255 void _computeCache(
int const cacheSize, std::vector<double>
const&
x,
257 if (cacheSize <= 0) {
258 kernelCache->erase(kernelCache->begin(), kernelCache->end());
262 if (kernelCache[0].size() != x.size()) {
263 kernelCache->erase(kernelCache->begin(), kernelCache->end());
266 int const old_cacheSize = kernelCache->size();
268 if (cacheSize == old_cacheSize) {
272 if (cacheSize < old_cacheSize) {
273 kernelCache->erase(kernelCache->begin() + cacheSize, kernelCache->end());
275 kernelCache->resize(cacheSize);
276 for (
int i = old_cacheSize; i != cacheSize; ++i) {
277 (*kernelCache)[i].resize(x.size());
283 for (
int i = 0; i != cacheSize; ++i) {
284 func->setParameter(0, (i + 0.5) / static_cast<double>(cacheSize));
285 for (
unsigned int j = 0; j != x.size(); ++j) {
286 (*kernelCache)[i][j] = (*func)(x[j]);
296 _computeCache(cacheSize, _kernelY, func, &_kernelColCache);
299 _computeCache(cacheSize, _kernelX, func, &_kernelRowCache);
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
std::shared_ptr< KernelFunction > KernelFunctionPtr
virtual int getCacheSize() const
Get the current cache size (0 if none)
double computeVectors(std::vector< Pixel > &colList, std::vector< Pixel > &rowList, bool doNormalize, double x=0.0, double y=0.0) const
Compute the column and row arrays in place, where kernel(col, row) = colList(col) * rowList(row) ...
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
A Function taking two arguments.
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
int getHeight() const
Return the Kernel's height.
int getWidth() const
Return the Kernel's width.
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
A Function taking one argument.
SeparableKernel()
Construct an empty spatially invariant SeparableKernel of size 0x0.
int getHeight() const
Return the number of rows in the image.
lsst::afw::geom::Point2I getCtr() const
Return index of kernel's center.
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual void computeCache(int const cacheSize)
Compute a cache of Kernel values, if desired.
unsigned int getNParameters() const
Return the number of function parameters.
std::vector< SpatialFunctionPtr > _spatialFunctionList
KernelFunctionPtr getKernelRowFunction() const
Get a deep copy of the row kernel function.
KernelFunctionPtr getKernelColFunction() const
Get a deep copy of the col kernel function.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y'th row.
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Kernels are used for convolution with MaskedImages and (eventually) Images.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)