Add image to simplex conversion code
[libstick.git] / include / libstick-0.1 / simplicialcomplex.h
index 707fd4f3240ae7fe71c4c90e7a4d1ab25926689e..988c090f56fb2378ad43044418b1fc7e0b6f0b98 100644 (file)
@@ -23,7 +23,7 @@ namespace libstick {
  * Each 0-dimensional simplex automatically has this simplex as its face.
  * Consequently, the innner class simplex_order gives the extended boundary
  * matrix. */
-template<int MAXDIM, class IT=uint32_t, class VT=double>
+template<int MAXDIM, class IT, class VT>
 class simplicial_complex {
 
     public:
@@ -116,7 +116,7 @@ class simplicial_complex {
                 }
 
                 /** Get i-th simplex in the simplex order. */
-                const Simplex& get_simplex(size_t i) const {
+                const Simplex& get_simplex(index_type i) const {
                     assert(i < size());
                     return c.simplices[order.at(i)];
                 }
@@ -204,14 +204,31 @@ class simplicial_complex {
         }
 
         /** Add a simplex to the complex. The dimension of the faces must be
-         * dim-1, and they must already be part of the complex. */
-        void add_simplex(int dim, index_type* faces, value_type value) {
-            add_simplex(Simplex::create(dim, faces, value));
+         * dim-1, and they must already be part of the complex. Returns the
+         * index of the added simplex. */
+        index_type add_simplex(int dim, index_type* faces, value_type value) {
+            return add_simplex(Simplex::create(dim, faces, value));
+        }
+
+        /** Add a simplex to the complex of at least dimension 1. The dimension
+         * of the faces must be dim-1, and they must already be part of the
+         * complex. The value of the simplex is set to the maximum value of its
+         * faces. Returns the index of the added simplex. */
+        index_type add_simplex(int dim, index_type* faces) {
+            assert(dim >= 1);
+
+            // Get max value of its faces
+            VT value = simplices[faces[0]].value;
+            for (size_t i=0; i < Simplex::face_count_bydim(dim); ++i)
+                value = std::max(value, simplices[faces[i]].value);
+
+            return add_simplex(dim, faces, value);
         }
 
         /** Add a simplex to the complex. The dimension of the faces must be
-         * dim-1, and they must already be part of the complex. */
-        void add_simplex(Simplex s) {
+         * dim-1, and they must already be part of the complex. Returns the
+         * index of the added simplex. */
+        index_type add_simplex(Simplex s) {
             // Check requirements for faces
             for (unsigned i=0; i < s.face_count(); ++i) {
                 // Faces are already in complex.
@@ -220,7 +237,12 @@ class simplicial_complex {
                 assert(simplices[s.faces[i]].dim == s.dim-1);
             }
 
+            // index_type must be large enough
+            assert(simplices.size() < std::numeric_limits<IT>::max());
+
+            index_type idx = simplices.size();
             simplices.push_back(s);
+            return idx;
         }
 
         /** Add an array of simplices */