Added the images for test.
[pom.git] / integral_array.h
1
2 //////////////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify         //
4 // it under the terms of the version 3 of the GNU General Public License        //
5 // as published by the Free Software Foundation.                                //
6 //                                                                              //
7 // This program is distributed in the hope that it will be useful, but          //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of                   //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU             //
10 // General Public License for more details.                                     //
11 //                                                                              //
12 // You should have received a copy of the GNU General Public License            //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.         //
14 //                                                                              //
15 // Written by Francois Fleuret                                                  //
16 // (C) Ecole Polytechnique Federale de Lausanne                                 //
17 // Contact <pom@epfl.ch> for comments & bug reports                             //
18 //////////////////////////////////////////////////////////////////////////////////
19
20 #ifndef INTEGRAL_ARRAY_H
21 #define INTEGRAL_ARRAY_H
22
23 #include <iostream>
24
25 using namespace std;
26
27 #include "array.h"
28
29 template <class T>
30 class IntegralArray: public Array<T> {
31 public:
32
33   void compute(const Array<T> *m) {
34     T *v = Array<T>::content, *w = m->Array<T>::content;
35     for(int x = 0; x < Array<T>::height; x++) *(v++) = 0;
36
37     register T sl;
38     for(int y = 1; y < Array<T>::width; y++) {
39       sl = 0; *(v++) = 0;
40       for(int x = 0; x < Array<T>::height - 1; x++) {
41         sl += *(w++);
42         *(v++) = sl + *(v - Array<T>::height);
43       }
44     }
45   }
46
47   IntegralArray(int w, int h) : Array<T>(w+1, h+1) { }
48
49   IntegralArray(const Array<T> &m) : Array<T>(m->get_width() + 1, m->get_height() + 1) {
50     compute(m);
51   }
52
53   // Integral on xmin <= x < xmax, ymin <= y < ymax
54   // Thus, xmax and ymax can go up to m->width+1 and m->height+1 respectively
55
56   inline T integral(int xmin, int ymin, int xmax, int ymax) const {
57     ASSERT(xmin <= xmax && ymin <= ymax, "Inconsistent bounds for integral");
58     ASSERT(xmin >= 0 && xmax < Array<T>::width &&
59            ymin >= 0 && ymax < Array<T>::height, "Index out of bounds in Array::operator () const");
60     return Array<T>::heads[xmax][ymax] + Array<T>::heads[xmin][ymin]
61       - Array<T>::heads[xmax][ymin] - Array<T>::heads[xmin][ymax];
62   }
63 };
64
65 #endif