|
| 1 | +#include <CGAL/Exact_predicates_exact_constructions_kernel.h> |
| 2 | +#include <CGAL/number_utils.h> |
| 3 | +#include <kigumi/Mesh_indices.h> |
| 4 | +#include <kigumi/Triangle_soup.h> |
| 5 | +#include <kigumi/Triangle_soup_io.h> |
| 6 | +#include <mcut/mcut.h> |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <chrono> |
| 10 | +#include <exception> |
| 11 | +#include <iostream> |
| 12 | +#include <stdexcept> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +using K = CGAL::Exact_predicates_exact_constructions_kernel; |
| 17 | +using Triangle_soup = kigumi::Triangle_soup<K>; |
| 18 | +using kigumi::read_triangle_soup; |
| 19 | +using kigumi::Vertex_index; |
| 20 | +using kigumi::write_triangle_soup; |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +struct Mesh { |
| 25 | + std::vector<double> vertices; |
| 26 | + std::vector<McUint32> vertex_indices; |
| 27 | + std::vector<McUint32> face_sizes; |
| 28 | + McUint32 num_vertices; |
| 29 | + McUint32 num_faces; |
| 30 | +}; |
| 31 | + |
| 32 | +bool read_mesh(const std::string& filename, Mesh& mesh) { |
| 33 | + Triangle_soup soup; |
| 34 | + if (!read_triangle_soup(filename, soup)) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + for (auto vi : soup.vertices()) { |
| 39 | + const auto& p = soup.point(vi); |
| 40 | + mesh.vertices.push_back(CGAL::to_double(p.x())); |
| 41 | + mesh.vertices.push_back(CGAL::to_double(p.y())); |
| 42 | + mesh.vertices.push_back(CGAL::to_double(p.z())); |
| 43 | + } |
| 44 | + for (auto fi : soup.faces()) { |
| 45 | + const auto& f = soup.face(fi); |
| 46 | + mesh.vertex_indices.push_back(f[0].idx()); |
| 47 | + mesh.vertex_indices.push_back(f[1].idx()); |
| 48 | + mesh.vertex_indices.push_back(f[2].idx()); |
| 49 | + mesh.face_sizes.push_back(3); |
| 50 | + } |
| 51 | + mesh.num_vertices = soup.num_vertices(); |
| 52 | + mesh.num_faces = soup.num_faces(); |
| 53 | + |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +bool write_mesh(const std::string& filename, const Mesh& mesh) { |
| 58 | + Triangle_soup soup; |
| 59 | + for (McUint32 i = 0; i < mesh.num_vertices; ++i) { |
| 60 | + soup.add_vertex( |
| 61 | + {mesh.vertices.at(3 * i), mesh.vertices.at(3 * i + 1), mesh.vertices.at(3 * i + 2)}); |
| 62 | + } |
| 63 | + for (McUint32 i = 0; i < mesh.num_faces; ++i) { |
| 64 | + soup.add_face({Vertex_index(mesh.vertex_indices.at(3 * i)), |
| 65 | + Vertex_index(mesh.vertex_indices.at(3 * i + 1)), |
| 66 | + Vertex_index(mesh.vertex_indices.at(3 * i + 2))}); |
| 67 | + } |
| 68 | + |
| 69 | + return write_triangle_soup(filename, soup); |
| 70 | +} |
| 71 | + |
| 72 | +void check(McResult result) { |
| 73 | + if (result != MC_NO_ERROR) { |
| 74 | + throw std::runtime_error("mcut failed"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace |
| 79 | + |
| 80 | +int main(int argc, char* argv[]) { |
| 81 | + try { |
| 82 | + std::vector<std::string> args(argv + 1, argv + argc); |
| 83 | + |
| 84 | + Mesh first; |
| 85 | + Mesh second; |
| 86 | + |
| 87 | + read_mesh(args.at(0), first); |
| 88 | + read_mesh(args.at(1), second); |
| 89 | + |
| 90 | + McContext context = MC_NULL_HANDLE; |
| 91 | + check(mcCreateContext(&context, MC_NULL_HANDLE)); |
| 92 | + |
| 93 | + McFlags dispatch_flags = |
| 94 | + MC_DISPATCH_VERTEX_ARRAY_DOUBLE | MC_DISPATCH_ENFORCE_GENERAL_POSITION | |
| 95 | + MC_DISPATCH_FILTER_FRAGMENT_SEALING_INSIDE | MC_DISPATCH_FILTER_FRAGMENT_LOCATION_BELOW; |
| 96 | + |
| 97 | + auto start = std::chrono::high_resolution_clock::now(); |
| 98 | + check(mcDispatch(context, dispatch_flags, first.vertices.data(), first.vertex_indices.data(), |
| 99 | + first.face_sizes.data(), first.num_vertices, first.num_faces, |
| 100 | + second.vertices.data(), second.vertex_indices.data(), second.face_sizes.data(), |
| 101 | + second.num_vertices, second.num_faces)); |
| 102 | + auto end = std::chrono::high_resolution_clock::now(); |
| 103 | + std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start) << std::endl; |
| 104 | + |
| 105 | + McUint32 num_ccs = 0; |
| 106 | + McConnectedComponentType cc_types = static_cast<McConnectedComponentType>( |
| 107 | + MC_CONNECTED_COMPONENT_TYPE_FRAGMENT | MC_CONNECTED_COMPONENT_TYPE_PATCH); |
| 108 | + check(mcGetConnectedComponents(context, cc_types, 0, nullptr, &num_ccs)); |
| 109 | + std::vector<McConnectedComponent> ccs(num_ccs, MC_NULL_HANDLE); |
| 110 | + check(mcGetConnectedComponents(context, cc_types, num_ccs, ccs.data(), nullptr)); |
| 111 | + |
| 112 | + Mesh result; |
| 113 | + |
| 114 | + for (McUint32 i = 0; i < num_ccs; ++i) { |
| 115 | + McConnectedComponent cc = ccs.at(i); |
| 116 | + McSize num_bytes = 0; |
| 117 | + |
| 118 | + check(mcGetConnectedComponentData(context, cc, MC_CONNECTED_COMPONENT_DATA_VERTEX_DOUBLE, 0, |
| 119 | + nullptr, &num_bytes)); |
| 120 | + McUint32 num_vertices = num_bytes / (3 * sizeof(double)); |
| 121 | + result.vertices.resize(3 * (result.num_vertices + num_vertices)); |
| 122 | + check(mcGetConnectedComponentData(context, cc, MC_CONNECTED_COMPONENT_DATA_VERTEX_DOUBLE, |
| 123 | + num_bytes, result.vertices.data() + 3 * result.num_vertices, |
| 124 | + nullptr)); |
| 125 | + |
| 126 | + check(mcGetConnectedComponentData(context, cc, MC_CONNECTED_COMPONENT_DATA_FACE_TRIANGULATION, |
| 127 | + 0, nullptr, &num_bytes)); |
| 128 | + McUint32 num_faces = num_bytes / (3 * sizeof(McUint32)); |
| 129 | + result.vertex_indices.resize(3 * (result.num_faces + num_faces)); |
| 130 | + check(mcGetConnectedComponentData( |
| 131 | + context, cc, MC_CONNECTED_COMPONENT_DATA_FACE_TRIANGULATION, num_bytes, |
| 132 | + result.vertex_indices.data() + 3 * result.num_faces, nullptr)); |
| 133 | + std::transform(result.vertex_indices.begin() + 3 * result.num_faces, |
| 134 | + result.vertex_indices.end(), |
| 135 | + result.vertex_indices.begin() + 3 * result.num_faces, |
| 136 | + [offset = result.num_vertices](McUint32 idx) { return offset + idx; }); |
| 137 | + |
| 138 | + result.num_vertices += num_vertices; |
| 139 | + result.num_faces += num_faces; |
| 140 | + } |
| 141 | + |
| 142 | + result.face_sizes.resize(result.num_faces, 3); |
| 143 | + |
| 144 | + mcReleaseConnectedComponents(context, 0, nullptr); |
| 145 | + mcReleaseContext(context); |
| 146 | + |
| 147 | + write_mesh(args.at(2), result); |
| 148 | + |
| 149 | + return 0; |
| 150 | + } catch (const std::exception& e) { |
| 151 | + std::cerr << "error: " << e.what() << std::endl; |
| 152 | + return 1; |
| 153 | + } catch (...) { |
| 154 | + std::cerr << "unknown error" << std::endl; |
| 155 | + return 1; |
| 156 | + } |
| 157 | +} |
0 commit comments