39 _kernelColFunctionPtr(),
40 _kernelRowFunctionPtr(),
53 :
Kernel(width, height, kernelColFunction.getNParameters() + kernelRowFunction.getNParameters(),
55 _kernelColFunctionPtr(kernelColFunction.
clone()),
56 _kernelRowFunctionPtr(kernelRowFunction.
clone()),
58 _localRowList(height),
69 :
Kernel(width, height, spatialFunctionList),
70 _kernelColFunctionPtr(kernelColFunction.
clone()),
71 _kernelRowFunctionPtr(kernelRowFunction.
clone()),
73 _localRowList(height),
79 spatialFunctionList.
size()) {
81 os <<
"kernelColFunction.getNParameters() + kernelRowFunction.getNParameters() = " 83 <<
" != " << spatialFunctionList.
size() <<
" = " 84 <<
"spatialFunctionList.size()";
98 *(this->_kernelRowFunctionPtr)));
100 retPtr->setCtr(this->
getCtr());
108 retPtr = std::make_shared<SeparableKernel>(width, height, *_kernelColFunctionPtr,
111 retPtr = std::make_shared<SeparableKernel>(width, height, *_kernelColFunctionPtr,
112 *_kernelRowFunctionPtr);
118 bool doNormalize,
double x,
double y)
const {
119 if (static_cast<int>(colList.
size()) != this->
getWidth() ||
122 os <<
"colList.size(), rowList.size() = (" << colList.
size() <<
", " << rowList.
size() <<
") != (" 124 <<
"kernel dimensions";
131 return basicComputeVectors(colList, rowList, doNormalize);
135 return _kernelColFunctionPtr->clone();
139 return _kernelRowFunctionPtr->clone();
144 os << prefix <<
"SeparableKernel:" <<
std::endl;
146 <<
"..x (width) function: " << (_kernelColFunctionPtr ? _kernelColFunctionPtr->toString() :
"None")
149 <<
"..y (rows) function: " << (_kernelRowFunctionPtr ? _kernelRowFunctionPtr->toString() :
"None")
167 double imSum = basicComputeVectors(_localColList, _localRowList, doNormalize);
172 ++colIter, ++imPtr) {
173 *imPtr = (*colIter) * _localRowList[
y];
181 unsigned int const nColParams = _kernelColFunctionPtr->getNParameters();
182 if (ind < nColParams) {
183 _kernelColFunctionPtr->setParameter(ind, value);
185 _kernelRowFunctionPtr->setParameter(ind - nColParams, value);
194 bool doNormalize)
const {
196 if (_kernelColCache.
empty()) {
197 for (
unsigned int i = 0; i != colList.
size(); ++i) {
198 double colFuncValue = (*_kernelColFunctionPtr)(_kernelX[i]);
199 colList[i] = colFuncValue;
200 colSum += colFuncValue;
203 int const cacheSize = _kernelColCache.
size();
208 for (
unsigned int i = 0; i != colList.
size(); ++i) {
209 double colFuncValue = cachedValues[i];
210 colList[i] = colFuncValue;
211 colSum += colFuncValue;
216 if (_kernelRowCache.
empty()) {
217 for (
unsigned int i = 0; i != rowList.
size(); ++i) {
218 double rowFuncValue = (*_kernelRowFunctionPtr)(_kernelY[i]);
219 rowList[i] = rowFuncValue;
220 rowSum += rowFuncValue;
223 int const cacheSize = _kernelRowCache.
size();
228 for (
unsigned int i = 0; i != rowList.
size(); ++i) {
229 double rowFuncValue = cachedValues[i];
230 rowList[i] = rowFuncValue;
231 rowSum += rowFuncValue;
234 if (indx == cacheSize/2) {
235 if (::fabs(rowFuncValue - (*_kernelRowFunctionPtr)(_kernelX[i])) > 1e-2) {
237 << rowFuncValue <<
" " 238 << (*_kernelRowFunctionPtr)(_kernelX[i])
246 double imSum = colSum * rowSum;
248 if ((colSum == 0) || (rowSum == 0)) {
269 if (cacheSize <= 0) {
270 kernelCache->erase(kernelCache->begin(), kernelCache->end());
274 if (kernelCache[0].size() != x.
size()) {
275 kernelCache->erase(kernelCache->begin(), kernelCache->end());
278 int const old_cacheSize = kernelCache->size();
280 if (cacheSize == old_cacheSize) {
284 if (cacheSize < old_cacheSize) {
285 kernelCache->erase(kernelCache->begin() + cacheSize, kernelCache->end());
287 kernelCache->resize(cacheSize);
288 for (
int i = old_cacheSize; i != cacheSize; ++i) {
289 (*kernelCache)[i].resize(x.
size());
295 for (
int i = 0; i != cacheSize; ++i) {
296 func->setParameter(0, (i + 0.5) / static_cast<double>(cacheSize));
297 for (
unsigned int j = 0; j != x.
size(); ++j) {
298 (*kernelCache)[i][j] = (*func)(x[j]);
308 _computeCache(cacheSize, _kernelY, func, &_kernelColCache);
311 _computeCache(cacheSize, _kernelX, func, &_kernelRowCache);
int getHeight() const
Return the Kernel's height.
unsigned int getNParameters() const noexcept
Return the number of function parameters.
std::vector< double > getKernelParameters() const override
Return the current kernel parameters.
int getHeight() const
Return the number of rows in the image.
std::shared_ptr< Kernel > resized(int width, int height) const override
Return a pointer to a clone with specified kernel dimensions.
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
void setKernelParameter(unsigned int ind, double value) const override
Set one kernel parameter.
double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const override
Low-level version of computeImage.
double getKernelParameter(unsigned int i) const override
Return a particular Kernel Parameter (no bounds checking).
A Function taking two arguments.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y'th row.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
std::shared_ptr< Kernel > clone() const override
Return a pointer to a deep copy of this kernel.
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) ...
A base class for image defects.
KernelFunctionPtr getKernelRowFunction() const
Get a deep copy of the row kernel function.
KernelFunctionPtr getKernelColFunction() const
Get a deep copy of the col kernel function.
A Function taking one argument.
SeparableKernel()
Construct an empty spatially invariant SeparableKernel of size 0x0.
lsst::geom::Point2I getCtr() const
Return index of kernel's center.
int getCacheSize() const override
Get the current cache size (0 if none)
void computeCache(int const cacheSize) override
Compute a cache of Kernel values, if desired.
std::string toString(std::string const &prefix="") const override
Return a string representation of the kernel.
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
int getWidth() const
Return the Kernel's width.
#define LSST_EXCEPT(type,...)
T back_inserter(T... args)
std::vector< SpatialFunctionPtr > _spatialFunctionList
Kernels are used for convolution with MaskedImages and (eventually) Images.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.