//////////////////////////////////////////////////////////////////////////////////
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the version 3 of the GNU General Public License //
// as published by the Free Software Foundation. //
// //
// 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, see . //
// //
// Written by Francois Fleuret //
// (C) Ecole Polytechnique Federale de Lausanne //
// Contact for comments & bug reports //
//////////////////////////////////////////////////////////////////////////////////
#ifndef INTEGRAL_ARRAY_H
#define INTEGRAL_ARRAY_H
#include
using namespace std;
#include "array.h"
template
class IntegralArray: public Array {
public:
void compute(const Array *m) {
T *v = Array::content, *w = m->Array::content;
for(int x = 0; x < Array::height; x++) *(v++) = 0;
register T sl;
for(int y = 1; y < Array::width; y++) {
sl = 0; *(v++) = 0;
for(int x = 0; x < Array::height - 1; x++) {
sl += *(w++);
*(v++) = sl + *(v - Array::height);
}
}
}
IntegralArray(int w, int h) : Array(w+1, h+1) { }
IntegralArray(const Array &m) : Array(m->get_width() + 1, m->get_height() + 1) {
compute(m);
}
// Integral on xmin <= x < xmax, ymin <= y < ymax
// Thus, xmax and ymax can go up to m->width+1 and m->height+1 respectively
inline T integral(int xmin, int ymin, int xmax, int ymax) const {
ASSERT(xmin <= xmax && ymin <= ymax, "Inconsistent bounds for integral");
ASSERT(xmin >= 0 && xmax < Array::width &&
ymin >= 0 && ymax < Array::height, "Index out of bounds in Array::operator () const");
return Array::heads[xmax][ymax] + Array::heads[xmin][ymin]
- Array::heads[xmax][ymin] - Array::heads[xmin][ymax];
}
};
#endif