9b07eed430a70a3cf275e9f733e57389454e18b6
[libstick.git] / tests / image.h
1 #ifndef image_h_PooBeibuceushabo
2 #define image_h_PooBeibuceushabo
3
4 #include <cpptest.h>
5 #include <cpptest-suite.h>
6
7 #include <libstick/image.h>
8
9 #include <cassert>
10 #include <math.h>
11
12
13 using namespace libstick;
14
15
16 class image_TestSuite: public Test::Suite {
17
18 private:
19
20 public:
21 image_TestSuite()
22 {
23 TEST_ADD(image_TestSuite::test_incidences);
24 }
25
26 protected:
27 virtual void setup() {
28 }
29
30 virtual void tear_down() {
31 }
32
33 void test_incidences() {
34 const size_t height=128, width=64;
35
36 //Create "some" image
37 float image[height*width];
38 for (unsigned i=0; i < height; ++i)
39 for (unsigned j=0; j < width; ++j)
40 image[i*width + j] = (sin(j/10.0) + 0.5*sin(j/4.0))*cos(i/7.0) + i*j*0.5*1e-3;
41
42 // Create complex and add image
43 typedef simplicial_function<2, uint32_t, float> sfunction;
44 typedef sfunction::simplcompltype scomplex;
45 scomplex s;
46 sfunction f(s);
47 add_image_to_simplicialfunction(f, image, width, height);
48 assert(f.is_monotone());
49
50 typedef scomplex::simplex_order sorder;
51 sorder so(s);
52 assert(so.is_filtration());
53
54 typedef boolean_colrowmatrix<uint32_t> bmatrix;
55 bmatrix bm = so.get_boundary_matrix();
56
57 // Check for the right vertex incidences
58 for (unsigned i=0; i < height; ++i) {
59 for (unsigned j=0; j < width; ++j) {
60 // Get matrix row of this vertex. Its size is the number of edges
61 // incident to this vertex
62 const bmatrix::row_type &row = bm.get_row(1 + i*width + j);
63
64 // Somewhere at the boundary of the image
65 if (i == 0 || i == height-1 || j == 0 || j == width-1) {
66 // top-left and bottom-right corner
67 if ((i == 0 && j == 0) || (i == height - 1 && j == width - 1)) {
68 TEST_ASSERT(row.size() == 3);
69 } else if ((i == 0 && j == width - 1) || (i == height - 1 && j == 0)) {
70 TEST_ASSERT(row.size() == 2);
71 } else {
72 TEST_ASSERT(row.size() == 4);
73 }
74 } else {
75 TEST_ASSERT(row.size() == 6);
76 }
77 }
78 }
79 }
80 };
81
82 #endif