Factor out simplicial_function
[libstick.git] / include / libstick / image.h
1 #ifndef image_h_uiHiKahYooroomai
2 #define image_h_uiHiKahYooroomai
3
4 #include "simplicialfunction.h"
5
6
7 namespace libstick {
8
9 /** We consider an image, i.e. a rectangular arrangement of pixels, of given
10 * height and width. Every pixel has a specific value (e.g., grey value) given
11 * by the 'values' array as concatenation of all rows. We add every pixel as
12 * vertex with its value as simplex value. We further triangulate the resulting
13 * rectangular latice as a grid graph together with south-west to north-east
14 * edges for each of the small squares of the grid graph. Finally the resulting
15 * triangles are added. For all non-vertex simplices the maximum of its
16 * vertex-values is used as its value. The first pixel is considered to be at
17 * bottom-left.
18 *
19 * Requires that f.is_complete() gives true.
20 * */
21 template<class IT, class VT>
22 void add_image_to_simplicialfunction(simplicial_function<2, IT, VT> &f, const VT *values, size_t width, size_t height) {
23 assert(f.is_complete());
24
25 // Add the vertices
26 const IT v1st = f.size();
27 for (unsigned i=0; i < height; ++i)
28 for (unsigned j=0; j < width; ++j)
29 f.add_simplex(0, NULL, values[i*width + j]);
30
31 // row i+1 o-------o
32 // | /|
33 // | / |
34 // | 2/ |
35 // 0 | / |
36 // | / |
37 // | / |
38 // |/ 1 |
39 // row i o-------o
40 // ^
41 // column j
42
43 IT faces[3];
44
45 // Adding horizontal edges
46 const IT he1st = f.size();
47 for (unsigned i=0; i < height; ++i)
48 for (unsigned j=0; j < width-1; ++j) {
49 faces[0] = v1st + i*width + j;
50 faces[1] = faces[0] + 1;
51 f.add_simplex(1, faces);
52 }
53
54 // Adding vertical edges
55 const IT ve1st = f.size();
56 for (unsigned i=0; i < height-1; ++i)
57 for (unsigned j=0; j < width; ++j) {
58 faces[0] = v1st + i*width + j;
59 faces[1] = faces[0] + width;
60 f.add_simplex(1, faces);
61 }
62
63 // Adding diagonal edges
64 const IT de1st = f.size();
65 for (unsigned i=0; i < height-1; ++i)
66 for (unsigned j=0; j < width-1; ++j) {
67 faces[0] = v1st + i*width + j;
68 faces[1] = faces[0] + width + 1;
69 f.add_simplex(1, faces);
70 }
71
72 // Add triangles
73 for (unsigned i=0; i < height-1; ++i)
74 for (unsigned j=0; j < width-1; ++j) {
75 faces[0] = he1st + i*(width-1) + j;
76 faces[1] = ve1st + i*width + j + 1;
77 faces[2] = de1st + i*(width-1) + j;
78 f.add_simplex(2, faces);
79
80 faces[0] = he1st + (i+1)*(width-1) + j;
81 faces[1] = ve1st + i*width + j;
82 faces[2] = de1st + i*(width-1) + j;
83 f.add_simplex(2, faces);
84 }
85 }
86
87
88 }
89
90 #endif
91