diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fabeffb66d091beddfb9044c021a6221b9dc103..817e7c78b91c7cad13358117f000fbc4a79d5151 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(morphist "src/morphist_viewer.hpp" "src/sdf.cpp" "src/sdf.hpp" + "src/marching_cubes.cpp" ) target_include_directories(morphist PUBLIC "pmp-library/src/") diff --git a/src/marching_cubes.cpp b/src/marching_cubes.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc707a466623a9868b84d1e2c453170fae32d9ee --- /dev/null +++ b/src/marching_cubes.cpp @@ -0,0 +1,91 @@ +#include "marching_cubes.h" + +pmp::Point interpolate(pmp::Point p1, float v1, pmp::Point p2, float v2, float isovalue) +{ + float e = 0.001; + if (abs(v1 - isovalue) < e) + return p1; + if (abs(v2 - isovalue) < e) + return p2; + if (abs(v2 - v1) < e) + return p1; + float diff = (isovalue - v1) / (v2 - v1); + + return p1 + diff * (p2 - p1); +} + +pmp::SurfaceMesh MarchingCubes::computeSurface(SDF sdf) +{ + const std::vector<float> data = sdf.getData(); + const size_t height = sdf.getHeight(); + const size_t width = sdf.getWidth(); + const size_t depth = sdf.getDepth(); + + pmp::SurfaceMesh mc_mesh; + + for (int i = 0; i < width - 1; i++) + { + for (int j = 0; j < height - 1; j++) + { + for (int k = 0; k < depth - 1; k++) + { + // We get the 8 values for the current cube and their positions + std::vector<float> edge_table_index; + edge_table_index.push_back(sdf.get(i, j, k)); + edge_table_index.push_back(sdf.get(i + 1, j, k)); + edge_table_index.push_back(sdf.get(i, j + 1, k)); + edge_table_index.push_back(sdf.get(i + 1, j + 1, k)); + edge_table_index.push_back(sdf.get(i, j, k + 1)); + edge_table_index.push_back(sdf.get(i + 1, j, k + 1)); + edge_table_index.push_back(sdf.get(i, j + 1, k + 1)); + edge_table_index.push_back(sdf.get(i + 1, j + 1, k + 1)); + + std::vector<pmp::Point> vertices_position; // getWidth = grid_size + vertices_position.push_back((pmp::Point(i, j, k) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i + 1, j, k) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i, j + 1, k) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i + 1, j + 1, k) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i, j, k + 1) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i + 1, j, k + 1) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i, j + 1, k + 1) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + vertices_position.push_back((pmp::Point(i + 1, j + 1, k + 1) / sdf.getWidth()) - pmp::Point(0.5, 0.5, 0.5)); + + size_t size_cube = edge_table_index.size(); // 8 + size_t combination = 0; + + for (int ind = 0; ind < size_cube; ind++) + if (edge_table_index[ind] > 0) + combination = combination | (1 << ind); // set the vertex index bit at 1 + + std::vector<int> triangleCombination = TriangleTable[combination]; // Lookup which triangles we want to create + // triangleCombination contains 0, 1 or multiple set of 3 vertices index, we want to create triangles for each of them in mc_mesh + + size_t combination_size = triangleCombination.size(); + for (int current_indices_set = 0; current_indices_set < combination_size; current_indices_set += 3) // move 3 per 3 + { + pmp::Point p1 = vertices_position[EdgeVertexIndices[triangleCombination[current_indices_set]][0]]; + pmp::Point p2 = vertices_position[EdgeVertexIndices[triangleCombination[current_indices_set]][1]]; + pmp::Point p3 = vertices_position[EdgeVertexIndices[triangleCombination[current_indices_set + 1]][0]]; + pmp::Point p4 = vertices_position[EdgeVertexIndices[triangleCombination[current_indices_set + 1]][1]]; + pmp::Point p5 = vertices_position[EdgeVertexIndices[triangleCombination[current_indices_set + 2]][0]]; + pmp::Point p6 = vertices_position[EdgeVertexIndices[triangleCombination[current_indices_set + 2]][1]]; + + float value1 = edge_table_index[EdgeVertexIndices[triangleCombination[current_indices_set]][0]]; + float value2 = edge_table_index[EdgeVertexIndices[triangleCombination[current_indices_set]][1]]; + float value3 = edge_table_index[EdgeVertexIndices[triangleCombination[current_indices_set + 1]][0]]; + float value4 = edge_table_index[EdgeVertexIndices[triangleCombination[current_indices_set + 1]][1]]; + float value5 = edge_table_index[EdgeVertexIndices[triangleCombination[current_indices_set + 2]][0]]; + float value6 = edge_table_index[EdgeVertexIndices[triangleCombination[current_indices_set + 2]][1]]; + + auto v1 = mc_mesh.add_vertex(interpolate(p1, value1, p2, value2, 0)); + auto v2 = mc_mesh.add_vertex(interpolate(p3, value3, p4, value4, 0)); + auto v3 = mc_mesh.add_vertex(interpolate(p5, value5, p6, value6, 0)); + + mc_mesh.add_triangle(v1, v2, v3); + } + } + } + } + return mc_mesh; +} + diff --git a/src/marching_cubes.h b/src/marching_cubes.h new file mode 100644 index 0000000000000000000000000000000000000000..8fca6dc333d2c700430f8902670283bf0c1cfa7e --- /dev/null +++ b/src/marching_cubes.h @@ -0,0 +1,10 @@ +#pragma once +#include "marching_cubes_table.h" +#include "sdf.hpp" +#include <pmp/surface_mesh.h> + +class MarchingCubes +{ +public: + static pmp::SurfaceMesh computeSurface(SDF sdf); +}; diff --git a/src/marching_cubes_table.h b/src/marching_cubes_table.h new file mode 100644 index 0000000000000000000000000000000000000000..2057c4ac15d75dd7df9ce33eb0ac3527f387faea --- /dev/null +++ b/src/marching_cubes_table.h @@ -0,0 +1,278 @@ +#pragma once + +#include <iostream> +#include <vector> + +const std::vector<std::vector<int>> EdgeVertexIndices = { + {0, 1}, + {1, 3}, + {3, 2}, + {2, 0}, + {4, 5}, + {5, 7}, + {7, 6}, + {6, 4}, + {0, 4}, + {1, 5}, + {3, 7}, + {2, 6}, +}; + +const std::vector<std::vector<int>> TriangleTable = { + {}, + {0, 3, 8}, + {0, 9, 1}, + {3, 8, 1, 1, 8, 9}, + {2, 11, 3}, + {8, 0, 11, 11, 0, 2}, + {3, 2, 11, 1, 0, 9}, + {11, 1, 2, 11, 9, 1, 11, 8, 9}, + {1, 10, 2}, + {0, 3, 8, 2, 1, 10}, + {10, 2, 9, 9, 2, 0}, + {8, 2, 3, 8, 10, 2, 8, 9, 10}, + {11, 3, 10, 10, 3, 1}, + {10, 0, 1, 10, 8, 0, 10, 11, 8}, + {9, 3, 0, 9, 11, 3, 9, 10, 11}, + {8, 9, 11, 11, 9, 10}, + {4, 8, 7}, + {7, 4, 3, 3, 4, 0}, + {4, 8, 7, 0, 9, 1}, + {1, 4, 9, 1, 7, 4, 1, 3, 7}, + {8, 7, 4, 11, 3, 2}, + {4, 11, 7, 4, 2, 11, 4, 0, 2}, + {0, 9, 1, 8, 7, 4, 11, 3, 2}, + {7, 4, 11, 11, 4, 2, 2, 4, 9, 2, 9, 1}, + {4, 8, 7, 2, 1, 10}, + {7, 4, 3, 3, 4, 0, 10, 2, 1}, + {10, 2, 9, 9, 2, 0, 7, 4, 8}, + {10, 2, 3, 10, 3, 4, 3, 7, 4, 9, 10, 4}, + {1, 10, 3, 3, 10, 11, 4, 8, 7}, + {10, 11, 1, 11, 7, 4, 1, 11, 4, 1, 4, 0}, + {7, 4, 8, 9, 3, 0, 9, 11, 3, 9, 10, 11}, + {7, 4, 11, 4, 9, 11, 9, 10, 11}, + {9, 4, 5}, + {9, 4, 5, 8, 0, 3}, + {4, 5, 0, 0, 5, 1}, + {5, 8, 4, 5, 3, 8, 5, 1, 3}, + {9, 4, 5, 11, 3, 2}, + {2, 11, 0, 0, 11, 8, 5, 9, 4}, + {4, 5, 0, 0, 5, 1, 11, 3, 2}, + {5, 1, 4, 1, 2, 11, 4, 1, 11, 4, 11, 8}, + {1, 10, 2, 5, 9, 4}, + {9, 4, 5, 0, 3, 8, 2, 1, 10}, + {2, 5, 10, 2, 4, 5, 2, 0, 4}, + {10, 2, 5, 5, 2, 4, 4, 2, 3, 4, 3, 8}, + {11, 3, 10, 10, 3, 1, 4, 5, 9}, + {4, 5, 9, 10, 0, 1, 10, 8, 0, 10, 11, 8}, + {11, 3, 0, 11, 0, 5, 0, 4, 5, 10, 11, 5}, + {4, 5, 8, 5, 10, 8, 10, 11, 8}, + {8, 7, 9, 9, 7, 5}, + {3, 9, 0, 3, 5, 9, 3, 7, 5}, + {7, 0, 8, 7, 1, 0, 7, 5, 1}, + {7, 5, 3, 3, 5, 1}, + {5, 9, 7, 7, 9, 8, 2, 11, 3}, + {2, 11, 7, 2, 7, 9, 7, 5, 9, 0, 2, 9}, + {2, 11, 3, 7, 0, 8, 7, 1, 0, 7, 5, 1}, + {2, 11, 1, 11, 7, 1, 7, 5, 1}, + {8, 7, 9, 9, 7, 5, 2, 1, 10}, + {10, 2, 1, 3, 9, 0, 3, 5, 9, 3, 7, 5}, + {7, 5, 8, 5, 10, 2, 8, 5, 2, 8, 2, 0}, + {10, 2, 5, 2, 3, 5, 3, 7, 5}, + {8, 7, 5, 8, 5, 9, 11, 3, 10, 3, 1, 10}, + {5, 11, 7, 10, 11, 5, 1, 9, 0}, + {11, 5, 10, 7, 5, 11, 8, 3, 0}, + {5, 11, 7, 10, 11, 5}, + {6, 7, 11}, + {7, 11, 6, 3, 8, 0}, + {6, 7, 11, 0, 9, 1}, + {9, 1, 8, 8, 1, 3, 6, 7, 11}, + {3, 2, 7, 7, 2, 6}, + {0, 7, 8, 0, 6, 7, 0, 2, 6}, + {6, 7, 2, 2, 7, 3, 9, 1, 0}, + {6, 7, 8, 6, 8, 1, 8, 9, 1, 2, 6, 1}, + {11, 6, 7, 10, 2, 1}, + {3, 8, 0, 11, 6, 7, 10, 2, 1}, + {0, 9, 2, 2, 9, 10, 7, 11, 6}, + {6, 7, 11, 8, 2, 3, 8, 10, 2, 8, 9, 10}, + {7, 10, 6, 7, 1, 10, 7, 3, 1}, + {8, 0, 7, 7, 0, 6, 6, 0, 1, 6, 1, 10}, + {7, 3, 6, 3, 0, 9, 6, 3, 9, 6, 9, 10}, + {6, 7, 10, 7, 8, 10, 8, 9, 10}, + {11, 6, 8, 8, 6, 4}, + {6, 3, 11, 6, 0, 3, 6, 4, 0}, + {11, 6, 8, 8, 6, 4, 1, 0, 9}, + {1, 3, 9, 3, 11, 6, 9, 3, 6, 9, 6, 4}, + {2, 8, 3, 2, 4, 8, 2, 6, 4}, + {4, 0, 6, 6, 0, 2}, + {9, 1, 0, 2, 8, 3, 2, 4, 8, 2, 6, 4}, + {9, 1, 4, 1, 2, 4, 2, 6, 4}, + {4, 8, 6, 6, 8, 11, 1, 10, 2}, + {1, 10, 2, 6, 3, 11, 6, 0, 3, 6, 4, 0}, + {11, 6, 4, 11, 4, 8, 10, 2, 9, 2, 0, 9}, + {10, 4, 9, 6, 4, 10, 11, 2, 3}, + {4, 8, 3, 4, 3, 10, 3, 1, 10, 6, 4, 10}, + {1, 10, 0, 10, 6, 0, 6, 4, 0}, + {4, 10, 6, 9, 10, 4, 0, 8, 3}, + {4, 10, 6, 9, 10, 4}, + {6, 7, 11, 4, 5, 9}, + {4, 5, 9, 7, 11, 6, 3, 8, 0}, + {1, 0, 5, 5, 0, 4, 11, 6, 7}, + {11, 6, 7, 5, 8, 4, 5, 3, 8, 5, 1, 3}, + {3, 2, 7, 7, 2, 6, 9, 4, 5}, + {5, 9, 4, 0, 7, 8, 0, 6, 7, 0, 2, 6}, + {3, 2, 6, 3, 6, 7, 1, 0, 5, 0, 4, 5}, + {6, 1, 2, 5, 1, 6, 4, 7, 8}, + {10, 2, 1, 6, 7, 11, 4, 5, 9}, + {0, 3, 8, 4, 5, 9, 11, 6, 7, 10, 2, 1}, + {7, 11, 6, 2, 5, 10, 2, 4, 5, 2, 0, 4}, + {8, 4, 7, 5, 10, 6, 3, 11, 2}, + {9, 4, 5, 7, 10, 6, 7, 1, 10, 7, 3, 1}, + {10, 6, 5, 7, 8, 4, 1, 9, 0}, + {4, 3, 0, 7, 3, 4, 6, 5, 10}, + {10, 6, 5, 8, 4, 7}, + {9, 6, 5, 9, 11, 6, 9, 8, 11}, + {11, 6, 3, 3, 6, 0, 0, 6, 5, 0, 5, 9}, + {11, 6, 5, 11, 5, 0, 5, 1, 0, 8, 11, 0}, + {11, 6, 3, 6, 5, 3, 5, 1, 3}, + {9, 8, 5, 8, 3, 2, 5, 8, 2, 5, 2, 6}, + {5, 9, 6, 9, 0, 6, 0, 2, 6}, + {1, 6, 5, 2, 6, 1, 3, 0, 8}, + {1, 6, 5, 2, 6, 1}, + {2, 1, 10, 9, 6, 5, 9, 11, 6, 9, 8, 11}, + {9, 0, 1, 3, 11, 2, 5, 10, 6}, + {11, 0, 8, 2, 0, 11, 10, 6, 5}, + {3, 11, 2, 5, 10, 6}, + {1, 8, 3, 9, 8, 1, 5, 10, 6}, + {6, 5, 10, 0, 1, 9}, + {8, 3, 0, 5, 10, 6}, + {6, 5, 10}, + {10, 5, 6}, + {0, 3, 8, 6, 10, 5}, + {10, 5, 6, 9, 1, 0}, + {3, 8, 1, 1, 8, 9, 6, 10, 5}, + {2, 11, 3, 6, 10, 5}, + {8, 0, 11, 11, 0, 2, 5, 6, 10}, + {1, 0, 9, 2, 11, 3, 6, 10, 5}, + {5, 6, 10, 11, 1, 2, 11, 9, 1, 11, 8, 9}, + {5, 6, 1, 1, 6, 2}, + {5, 6, 1, 1, 6, 2, 8, 0, 3}, + {6, 9, 5, 6, 0, 9, 6, 2, 0}, + {6, 2, 5, 2, 3, 8, 5, 2, 8, 5, 8, 9}, + {3, 6, 11, 3, 5, 6, 3, 1, 5}, + {8, 0, 1, 8, 1, 6, 1, 5, 6, 11, 8, 6}, + {11, 3, 6, 6, 3, 5, 5, 3, 0, 5, 0, 9}, + {5, 6, 9, 6, 11, 9, 11, 8, 9}, + {5, 6, 10, 7, 4, 8}, + {0, 3, 4, 4, 3, 7, 10, 5, 6}, + {5, 6, 10, 4, 8, 7, 0, 9, 1}, + {6, 10, 5, 1, 4, 9, 1, 7, 4, 1, 3, 7}, + {7, 4, 8, 6, 10, 5, 2, 11, 3}, + {10, 5, 6, 4, 11, 7, 4, 2, 11, 4, 0, 2}, + {4, 8, 7, 6, 10, 5, 3, 2, 11, 1, 0, 9}, + {1, 2, 10, 11, 7, 6, 9, 5, 4}, + {2, 1, 6, 6, 1, 5, 8, 7, 4}, + {0, 3, 7, 0, 7, 4, 2, 1, 6, 1, 5, 6}, + {8, 7, 4, 6, 9, 5, 6, 0, 9, 6, 2, 0}, + {7, 2, 3, 6, 2, 7, 5, 4, 9}, + {4, 8, 7, 3, 6, 11, 3, 5, 6, 3, 1, 5}, + {5, 0, 1, 4, 0, 5, 7, 6, 11}, + {9, 5, 4, 6, 11, 7, 0, 8, 3}, + {11, 7, 6, 9, 5, 4}, + {6, 10, 4, 4, 10, 9}, + {6, 10, 4, 4, 10, 9, 3, 8, 0}, + {0, 10, 1, 0, 6, 10, 0, 4, 6}, + {6, 10, 1, 6, 1, 8, 1, 3, 8, 4, 6, 8}, + {9, 4, 10, 10, 4, 6, 3, 2, 11}, + {2, 11, 8, 2, 8, 0, 6, 10, 4, 10, 9, 4}, + {11, 3, 2, 0, 10, 1, 0, 6, 10, 0, 4, 6}, + {6, 8, 4, 11, 8, 6, 2, 10, 1}, + {4, 1, 9, 4, 2, 1, 4, 6, 2}, + {3, 8, 0, 4, 1, 9, 4, 2, 1, 4, 6, 2}, + {6, 2, 4, 4, 2, 0}, + {3, 8, 2, 8, 4, 2, 4, 6, 2}, + {4, 6, 9, 6, 11, 3, 9, 6, 3, 9, 3, 1}, + {8, 6, 11, 4, 6, 8, 9, 0, 1}, + {11, 3, 6, 3, 0, 6, 0, 4, 6}, + {8, 6, 11, 4, 6, 8}, + {10, 7, 6, 10, 8, 7, 10, 9, 8}, + {3, 7, 0, 7, 6, 10, 0, 7, 10, 0, 10, 9}, + {6, 10, 7, 7, 10, 8, 8, 10, 1, 8, 1, 0}, + {6, 10, 7, 10, 1, 7, 1, 3, 7}, + {3, 2, 11, 10, 7, 6, 10, 8, 7, 10, 9, 8}, + {2, 9, 0, 10, 9, 2, 6, 11, 7}, + {0, 8, 3, 7, 6, 11, 1, 2, 10}, + {7, 6, 11, 1, 2, 10}, + {2, 1, 9, 2, 9, 7, 9, 8, 7, 6, 2, 7}, + {2, 7, 6, 3, 7, 2, 0, 1, 9}, + {8, 7, 0, 7, 6, 0, 6, 2, 0}, + {7, 2, 3, 6, 2, 7}, + {8, 1, 9, 3, 1, 8, 11, 7, 6}, + {11, 7, 6, 1, 9, 0}, + {6, 11, 7, 0, 8, 3}, + {11, 7, 6}, + {7, 11, 5, 5, 11, 10}, + {10, 5, 11, 11, 5, 7, 0, 3, 8}, + {7, 11, 5, 5, 11, 10, 0, 9, 1}, + {7, 11, 10, 7, 10, 5, 3, 8, 1, 8, 9, 1}, + {5, 2, 10, 5, 3, 2, 5, 7, 3}, + {5, 7, 10, 7, 8, 0, 10, 7, 0, 10, 0, 2}, + {0, 9, 1, 5, 2, 10, 5, 3, 2, 5, 7, 3}, + {9, 7, 8, 5, 7, 9, 10, 1, 2}, + {1, 11, 2, 1, 7, 11, 1, 5, 7}, + {8, 0, 3, 1, 11, 2, 1, 7, 11, 1, 5, 7}, + {7, 11, 2, 7, 2, 9, 2, 0, 9, 5, 7, 9}, + {7, 9, 5, 8, 9, 7, 3, 11, 2}, + {3, 1, 7, 7, 1, 5}, + {8, 0, 7, 0, 1, 7, 1, 5, 7}, + {0, 9, 3, 9, 5, 3, 5, 7, 3}, + {9, 7, 8, 5, 7, 9}, + {8, 5, 4, 8, 10, 5, 8, 11, 10}, + {0, 3, 11, 0, 11, 5, 11, 10, 5, 4, 0, 5}, + {1, 0, 9, 8, 5, 4, 8, 10, 5, 8, 11, 10}, + {10, 3, 11, 1, 3, 10, 9, 5, 4}, + {3, 2, 8, 8, 2, 4, 4, 2, 10, 4, 10, 5}, + {10, 5, 2, 5, 4, 2, 4, 0, 2}, + {5, 4, 9, 8, 3, 0, 10, 1, 2}, + {2, 10, 1, 4, 9, 5}, + {8, 11, 4, 11, 2, 1, 4, 11, 1, 4, 1, 5}, + {0, 5, 4, 1, 5, 0, 2, 3, 11}, + {0, 11, 2, 8, 11, 0, 4, 9, 5}, + {5, 4, 9, 2, 3, 11}, + {4, 8, 5, 8, 3, 5, 3, 1, 5}, + {0, 5, 4, 1, 5, 0}, + {5, 4, 9, 3, 0, 8}, + {5, 4, 9}, + {11, 4, 7, 11, 9, 4, 11, 10, 9}, + {0, 3, 8, 11, 4, 7, 11, 9, 4, 11, 10, 9}, + {11, 10, 7, 10, 1, 0, 7, 10, 0, 7, 0, 4}, + {3, 10, 1, 11, 10, 3, 7, 8, 4}, + {3, 2, 10, 3, 10, 4, 10, 9, 4, 7, 3, 4}, + {9, 2, 10, 0, 2, 9, 8, 4, 7}, + {3, 4, 7, 0, 4, 3, 1, 2, 10}, + {7, 8, 4, 10, 1, 2}, + {7, 11, 4, 4, 11, 9, 9, 11, 2, 9, 2, 1}, + {1, 9, 0, 4, 7, 8, 2, 3, 11}, + {7, 11, 4, 11, 2, 4, 2, 0, 4}, + {4, 7, 8, 2, 3, 11}, + {9, 4, 1, 4, 7, 1, 7, 3, 1}, + {7, 8, 4, 1, 9, 0}, + {3, 4, 7, 0, 4, 3}, + {7, 8, 4}, + {11, 10, 8, 8, 10, 9}, + {0, 3, 9, 3, 11, 9, 11, 10, 9}, + {1, 0, 10, 0, 8, 10, 8, 11, 10}, + {10, 3, 11, 1, 3, 10}, + {3, 2, 8, 2, 10, 8, 10, 9, 8}, + {9, 2, 10, 0, 2, 9}, + {8, 3, 0, 10, 1, 2}, + {2, 10, 1}, + {2, 1, 11, 1, 9, 11, 9, 8, 11}, + {11, 2, 3, 9, 0, 1}, + {11, 0, 8, 2, 0, 11}, + {3, 11, 2}, + {1, 8, 3, 9, 8, 1}, + {1, 9, 0}, + {8, 3, 0}, + {}, +}; \ No newline at end of file diff --git a/src/morphist_viewer.cpp b/src/morphist_viewer.cpp index b6218a8353184aa91ac48dd31e3a0b6b2a1c7b3c..2cdbcb5615e709f88af2867f10e5976bb9c02105 100644 --- a/src/morphist_viewer.cpp +++ b/src/morphist_viewer.cpp @@ -1,5 +1,6 @@ #include "morphist_viewer.hpp" #include "sdf.hpp" +#include "marching_cubes.h" #include <imgui.h> #include <pmp/surface_mesh.h> @@ -21,13 +22,12 @@ void MorphistViewer::process_imgui() pmp::SurfaceMesh mesh1; pmp::SurfaceMesh mesh2; - if (ImGui::CollapsingHeader("Meshes selection")) { ImGui::Text("Mesh 1"); if (ImGui::Button("Open")) { - pmp::read(mesh1,"suzanne.obj"); + pmp::read(mesh1, "suzanne.obj"); } ImGui::Spacing(); @@ -35,7 +35,7 @@ void MorphistViewer::process_imgui() ImGui::Text("Mesh 2"); if (ImGui::Button("Open")) { - pmp::read(mesh2,"suzanne.obj"); + pmp::read(mesh2, "suzanne.obj"); } } @@ -45,9 +45,9 @@ void MorphistViewer::process_imgui() if (ImGui::CollapsingHeader("Bakery")) { ImGui::Text("Method"); - const char* items[] = { "Knn", "Spheric" }; + const char *items[] = {"Knn", "Spheric"}; static int item_current_idx = 0; // Here we store our selection data as an index. - if (ImGui::BeginListBox("", ImVec2(-FLT_MIN, 2.25 * ImGui::GetTextLineHeightWithSpacing()))) + if (ImGui::BeginListBox("##", ImVec2(-FLT_MIN, 2.25 * ImGui::GetTextLineHeightWithSpacing()))) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) { @@ -64,13 +64,20 @@ void MorphistViewer::process_imgui() if (ImGui::Button("Bake")) { + if (!mesh_.is_empty()) { - sdf = SDF::from_mesh(mesh_, 64); + std::cout << "Start SDF" << std::endl; + sdf = SDF::from_mesh(mesh_, 32); std::cout << "DONE!" << std::endl; + std::cout << "Started marching cubes algorithm" << std::endl; + mesh_ = MarchingCubes::computeSurface(sdf); + + std::cout << "Marching cubes ended " << std::endl; + std::cout << "Vertices: " << mesh_.n_vertices() << " Triangles: " << mesh_.n_faces() << std::endl; + + sdf.save_to_disk("../test.bin"); } - - sdf.save_to_disk("test.bin"); } } } diff --git a/src/sdf.hpp b/src/sdf.hpp index 9dcb977a46d01596765cc9ac9647d0657ce40130..e8cd5123d304c8ba9e6ed961fe0368eb99fb86eb 100644 --- a/src/sdf.hpp +++ b/src/sdf.hpp @@ -12,7 +12,6 @@ private: size_t height, width, depth; public: - SDF(); SDF(size_t dimension); SDF(size_t height, size_t width, size_t depth); @@ -20,4 +19,12 @@ public: void save_to_disk(std::string filepath); static SDF from_mesh(pmp::SurfaceMesh &mesh, size_t grid_size); + + inline std::vector<float> getData() { return data; } + + inline float get(size_t x, size_t y, size_t z) { return data[z + width * y + width * height * x];} + + inline size_t getHeight() { return height; } + inline size_t getWidth() { return width; } + inline size_t getDepth() { return depth; } };