X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=pom.git;a=blobdiff_plain;f=integral_array.h;fp=integral_array.h;h=e84141bbc23a3641a271d0e3e8e63626077e87ae;hp=0000000000000000000000000000000000000000;hb=97a7e68f234cc09807d2d55f550e2516be0e9093;hpb=48c9926a2ed03737a3b024a85cda348caebf4cfe
diff --git a/integral_array.h b/integral_array.h
new file mode 100644
index 0000000..e84141b
--- /dev/null
+++ b/integral_array.h
@@ -0,0 +1,65 @@
+
+//////////////////////////////////////////////////////////////////////////////////
+// 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