// Copyright (C) 2006  Paul A. Price (price@ifa.hawaii.edu)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include "pslib.h"
#include "pois.h"

bool poisCheckKernel(const psVector *solution,// Kernel solution
                     const psArray *kernels, // Kernel basis functions
                     const poisConfig *config // Configuration
    )
{
    psTrace("pois.checkKernel", 1, "Checking kernel....\n");

    // Get kernel value as a function of radius
    psVector *radKernel = psVectorAlloc((2*config->xKernel + 1) * (2*config->yKernel + 1), PS_TYPE_F32);
    psVector *radii = psVectorAlloc((2*config->xKernel + 1) * (2*config->yKernel + 1), PS_TYPE_F32); // Radius
    for (int i = 0; i < solution->n - 1; i++) {
        poisKernelBasis *kernel = kernels->data[i]; // The kernel basis function
        if (kernel->xOrder == 0 && kernel->yOrder == 0) {
            int u = kernel->u;          // x offset
            int v = kernel->v;          // y offset
            float distance = sqrtf((float)(u*u) + (float)(v*v)); // Distance from the centre
            radKernel->data.F32[i] = solution->data.F64[i];
            radii->data.F32[i] = distance;
        }
    }

    psVector *sortIndex = psVectorSortIndex(NULL, radii); // Indices from sort
    float distance = 0.0;               // Distance from the centre that's being examined
    float sum = 0.0;                    // Sum of kernel for that distance
    int num = 0;                        // Number of pixels at that distance
    int centreIndex = sortIndex->data.U32[0]; // Index of the centre pixel
    int numSuspect = 0;                 // Number of radii considered suspect
    for (int i = 0; i < (2*config->xKernel + 1) * (2*config->yKernel + 1); i++) {
        unsigned int index = sortIndex->data.U32[i];
        if (radii->data.F32[index] != distance) {
            psTrace("pois.checkKernel", 7, "Radius: %f\tMean: %f\n", distance, sum/(float)num);
            if (fabs(distance*sum/(float)num) > radKernel->data.F32[centreIndex]) {
                numSuspect++;
            }
            sum = 0.0;
            num = 0;
            distance = radii->data.F32[index];
        }
        sum += radKernel->data.F32[index];
        num++;
    }
    psTrace("pois.checkKernel", 7, "Radius: %f\tMean: %f\n", distance, sum/(float)num);
    if (fabs(distance*sum/(float)num) > radKernel->data.F32[centreIndex]) {
        numSuspect++;
    }

    psFree(radKernel);
    psFree(radii);
    psFree(sortIndex);

    psTrace("pois.checkKernel", 5, "%d radii considered suspect.\n", numSuspect);

    if (numSuspect > 0) {
        psWarning("There is significant power at large radii.  The kernel may be bad.\n");
        return false;
    }

    return true;
}
