 
                     if (img[img_index + img.stepz / img.elem_size + img.stepy / img.elem_size]) {
                         P |= (P0 << 20);
-                    }
-
-                }
-            }
+                        foreground |= (1 << 6);
+                    }
+
+                }
+            }
+        }*/
 
 #undef P0
+
+            // Store foreground voxels bitmask into memory
+            if (x + 1 < labels.x) {
+                labels[labels_index + 1] = foreground;
+            }
+            else if (y + 1 < labels.y) {
+                labels[labels_index + labels.stepy / labels.elem_size] = foreground;
+            }
+            else if (z + 1 < labels.z) {
+                labels[labels_index + labels.stepz / labels.elem_size] = foreground;
+            }
+            else {
+                *last_cube_fg = foreground;
+            }
+
 
             // checks on borders
 

            if (z + 1 < img.z) {
                if (img[img_index + img.stepz / img.elem_size]) {
                    P |= (P0 << 16);
                }

                if (x + 1 < img.x) {

                    if (img[img_index + img.stepz / img.elem_size + 1]) {
                        P |= (P0 << 17);
                    }

                    //if (y + 1 < img.y && img[img_index + img.stepz / img.elem_size + img.stepy / img.elem_size + 1]) {
                    //    P |= (P0 << 21);
                    //}

                }


    }

    void AllScans() {
        grid_size_ = dim3(((d_img_.x + 1) / 2 + BLOCK_X - 1) / BLOCK_X, ((d_img_.y + 1) / 2 + BLOCK_Y - 1) / BLOCK_Y, ((d_img_.z + 1) / 2 + BLOCK_Z - 1) / BLOCK_Z);
        block_size_ = dim3(BLOCK_X, BLOCK_Y, BLOCK_Z);

        InitLabeling << <grid_size_, block_size_ >> > (d_img_labels_);

