Add image to simplex conversion code
[libstick.git] / tests / image.h
diff --git a/tests/image.h b/tests/image.h
new file mode 100644 (file)
index 0000000..ff7817a
--- /dev/null
@@ -0,0 +1,80 @@
+#ifndef image_h_PooBeibuceushabo
+#define image_h_PooBeibuceushabo
+
+#include <cpptest.h>
+#include <cpptest-suite.h>
+
+#include <libstick-0.1/image.h>
+
+#include <cassert>
+#include <math.h>
+
+
+using namespace libstick;
+
+
+class image_TestSuite: public Test::Suite {
+
+    private:
+
+    public:
+        image_TestSuite()
+            {
+            TEST_ADD(image_TestSuite::test_incidences);
+        }
+
+    protected:
+        virtual void setup() {
+        }
+
+        virtual void tear_down() {
+        }
+
+        void test_incidences() {
+            const size_t height=128, width=64;
+
+            //Create "some" image
+            float image[height*width];
+            for (unsigned i=0; i < height; ++i)
+                for (unsigned j=0; j < width; ++j)
+                    image[i*width + j] = (sin(j/10.0) + 0.5*sin(j/4.0))*cos(i/7.0) + i*j*0.5*1e-3;
+
+            // Create complex and add image
+            typedef simplicial_complex<2, uint32_t, float> scomplex;
+            scomplex s;
+            add_image_to_complex(s, image, width, height);
+            assert(s.is_monotone());
+
+            typedef scomplex::simplex_order sorder;
+            sorder so(s);
+            assert(so.is_filtration());
+
+            typedef sorder::boundary_matrix bmatrix;
+            bmatrix bm = so.get_boundary_matrix();
+
+            // Check for the right vertex incidences
+            for (unsigned i=0; i < height; ++i) {
+                for (unsigned j=0; j < width; ++j) {
+                    // Get matrix row of this vertex. Its size is the number of edges
+                    // incident to this vertex
+                    const bmatrix::row_type &row = bm.get_row(1 + i*width + j);
+
+                    // Somewhere at the boundary of the image
+                    if (i == 0 || i == height-1 || j == 0 || j == width-1) {
+                        // top-left and bottom-right corner
+                        if ((i == 0 && j == 0) || (i == height - 1 && j == width - 1)) {
+                            TEST_ASSERT(row.size() == 3);
+                        } else if ((i == 0 && j == width - 1) || (i == height - 1 && j == 0)) {
+                            TEST_ASSERT(row.size() == 2);
+                        } else {
+                            TEST_ASSERT(row.size() == 4);
+                        }
+                    } else {
+                        TEST_ASSERT(row.size() == 6);
+                    }
+                }
+            }
+        }
+};
+
+#endif