SelfIntersection::SelfIntersection( const MatrixFr& vertices, const MatrixIr& faces) : m_faces(faces) { const size_t num_vertices = vertices.rows(); const size_t dim = vertices.cols(); const size_t num_faces = faces.rows(); const size_t vertex_per_face = faces.cols(); if (dim != 3) { throw NotImplementedError( "Self intersection check only support 3D"); } if (vertex_per_face != 3) { throw NotImplementedError( "Self intersection check only works with triangles"); } m_points.resize(num_vertices); for (size_t i=0; i<num_vertices; i++) { m_points[i] = Point_3( vertices(i,0), vertices(i,1), vertices(i,2)); } }
void SimpleInflator::generate_joint( const MatrixFr& pts, const VectorI& source_ids, size_t vertex_index) { const size_t dim = m_wire_network->get_dim(); ConvexHullEngine::Ptr convex_hull = ConvexHullEngine::create(dim, "qhull"); convex_hull->run(pts); MatrixFr vertices = convex_hull->get_vertices(); MatrixIr faces = convex_hull->get_faces(); VectorI index_map = convex_hull->get_index_map(); if (dim == 2) { // Need to triangulate the loop. const size_t num_vertices = vertices.rows(); TriangleWrapper tri(vertices, faces); tri.run(std::numeric_limits<Float>::max(), false, false, false); vertices = tri.get_vertices(); faces = tri.get_faces(); assert(vertices.rows() == num_vertices); } m_vertex_list.push_back(vertices); const size_t num_faces = faces.rows(); for (size_t i=0; i<num_faces; i++) { const auto& face = faces.row(i); if (dim == 3) { auto ori_indices = map_indices(face, index_map); if (belong_to_the_same_loop(ori_indices, source_ids)) continue; } m_face_list.push_back(face.array() + m_num_vertex_accumulated); m_face_source_list.push_back(vertex_index+1); } m_num_vertex_accumulated += vertices.rows(); }
CarveMeshPtr create_mesh(const MatrixFr& vertices, const MatrixIr& faces) { const size_t num_vertices = vertices.rows(); const size_t num_faces = faces.rows(); if (vertices.cols() != 3) { throw NotImplementedError("Only 3D mesh is supported."); } if (faces.cols() != 3) { throw NotImplementedError("Only triangle mesh is supported."); } std::vector<CarveVector> points; for (size_t i=0; i<num_vertices; i++) { const auto& v = vertices.row(i); CarveVector p; p.v[0] = v[0]; p.v[1] = v[1]; p.v[2] = v[2]; points.push_back(p); } std::vector<int> raw_faces; raw_faces.reserve(num_faces * 4); for (size_t i=0; i<num_faces; i++) { raw_faces.push_back(3); raw_faces.push_back(faces(i,0)); raw_faces.push_back(faces(i,1)); raw_faces.push_back(faces(i,2)); } return CarveMeshPtr(new CarveMesh(points, num_faces, raw_faces)); }
GeoMeshPtr GeogramMeshUtils::raw_to_geomesh( const MatrixFr& vertices, const MatrixIr& faces) { const size_t dim = vertices.cols(); const size_t vertex_per_face = faces.cols(); const size_t num_vertices = vertices.rows(); const size_t num_faces = faces.rows(); if (vertex_per_face != 3) { throw NotImplementedError("Converting non-triangle mesh to " "Geogram mesh is not yet implemented"); } auto geo_mesh = std::make_shared<GeoMesh>(dim, false); geo_mesh->vertices.clear(); geo_mesh->vertices.create_vertices(num_vertices); geo_mesh->facets.clear(); geo_mesh->facets.create_triangles(num_faces); for (size_t i=0; i<num_vertices; i++) { auto& p = geo_mesh->vertices.point(i); for (size_t j=0; j<dim; j++) { p[j] = vertices(i,j); } } for (size_t i=0; i<num_faces; i++) { geo_mesh->facets.set_vertex(i, 0, faces(i,0)); geo_mesh->facets.set_vertex(i, 1, faces(i,1)); geo_mesh->facets.set_vertex(i, 2, faces(i,2)); } return geo_mesh; }
Polyhedron generate_polyhedron( const MatrixFr& vertices, const MatrixIr& faces) { Polyhedron P; PolyhedronBuilder<HalfedgeDS> triangle(vertices, faces); P.delegate(triangle); assert(vertices.rows() == P.size_of_vertices()); assert(faces.rows() == P.size_of_facets()); return P; }
void InflatorEngine::save_mesh(const std::string& filename, const MatrixFr& vertices, const MatrixIr& faces, VectorF debug) { VectorF flattened_vertices(vertices.rows() * vertices.cols()); std::copy(vertices.data(), vertices.data() + vertices.rows() * vertices.cols(), flattened_vertices.data()); VectorI flattened_faces(faces.rows() * faces.cols()); std::copy(faces.data(), faces.data() + faces.rows() * faces.cols(), flattened_faces.data()); VectorI voxels = VectorI::Zero(0); Mesh::Ptr mesh = MeshFactory().load_data( flattened_vertices, flattened_faces, voxels, vertices.cols(), faces.cols(), 0).create_shared(); mesh->add_attribute("debug"); mesh->set_attribute("debug", debug); MeshWriter::Ptr writer = MeshWriter::create(filename); writer->with_attribute("debug"); writer->write_mesh(*mesh); }
void triangulate(MatrixFr vertices, MatrixIr edges, MatrixFr& output_vertices, MatrixIr& output_faces, Float max_area) { assert(edges.rows() >= 3); MeshCleaner cleaner; cleaner.remove_isolated_vertices(vertices, edges); cleaner.remove_duplicated_vertices(vertices, edges, 1e-12); assert(vertices.rows() >= 3); TriangleWrapper triangle(vertices, edges); triangle.run(max_area, false, true, true); output_vertices = triangle.get_vertices(); output_faces = triangle.get_faces(); }
Boundary::Ptr Boundary::extract_surface_boundary_raw( MatrixFr& vertices, MatrixIr& faces) { VectorF flattened_vertices = Eigen::Map<VectorF>(vertices.data(), vertices.rows() * vertices.cols()); VectorI flattened_faces = Eigen::Map<VectorI>(faces.data(), faces.rows() * faces.cols()); VectorI voxels = VectorI::Zero(0); MeshFactory factory; Mesh::Ptr mesh = factory.load_data(flattened_vertices, flattened_faces, voxels, vertices.cols(), faces.cols(), 0).create(); return extract_surface_boundary(*mesh); }
EdgeMap compute_edge_map(const MatrixIr& faces) { assert(faces.cols() == 3); EdgeMap result; const size_t num_faces = faces.rows(); for (size_t i=0; i<num_faces; i++) { const Vector3I& f = faces.row(i); Triplet e0(f[1], f[2]); Triplet e1(f[2], f[0]); Triplet e2(f[0], f[1]); result.insert(e0, i); result.insert(e1, i); result.insert(e2, i); } return result; }
std::vector<Box> get_triangle_bboxes( const SelfIntersection::Points& pts, const MatrixIr& faces) { const size_t num_faces = faces.rows(); std::vector<Box> boxes; boxes.reserve(num_faces); for (size_t i=0; i<num_faces; i++) { const Vector3I f = faces.row(i); const std::vector<SelfIntersection::Point_3> corners{ pts[f[0]], pts[f[1]], pts[f[2]] }; if (CGAL::collinear(pts[f[0]], pts[f[1]], pts[f[2]])) { // Triangle is degenerated. continue; } boxes.emplace_back(CGAL::bbox_3(corners.begin(), corners.end())); boxes.back().set_id(i); } return boxes; }
std::vector<bool> create_duplication_mask(const MatrixIr& edges) { const size_t num_edges = edges.rows(); std::unordered_set<Triplet, hash> unique_set; std::vector<bool> mask(num_edges, false); for (size_t i=0; i<num_edges; i++) { const auto& edge = edges.row(i); Triplet key(edge[0], edge[1]); auto itr = unique_set.find(key); if (itr == unique_set.end()) { unique_set.insert(key); } else { mask[i] = true; } } return mask; }
Boundary::Ptr Boundary::extract_volume_boundary_raw( MatrixFr& vertices, MatrixIr& voxels) { VectorF flattened_vertices = Eigen::Map<VectorF>(vertices.data(), vertices.rows() * vertices.cols()); VectorI faces = VectorI::Zero(0); VectorI flattened_voxels = Eigen::Map<VectorI>(voxels.data(), voxels.rows() * voxels.cols()); size_t vertex_per_voxel = voxels.cols(); size_t vertex_per_face=0; if (vertex_per_voxel == 4) vertex_per_face = 3; else if (vertex_per_voxel == 8) vertex_per_face = 4; else { throw RuntimeError("Unknown voxel type."); } MeshFactory factory; Mesh::Ptr mesh = factory.load_data(flattened_vertices, faces, flattened_voxels, vertices.cols(), vertex_per_face, vertex_per_voxel).create(); return extract_volume_boundary(*mesh); }
void SimpleInflator::connect_end_loops() { const size_t dim = m_wire_network->get_dim(); const Float ave_thickness = m_thickness.sum() / m_thickness.size(); const auto& edge_lengths = m_wire_network->get_attribute("edge_length"); const size_t num_edges = m_wire_network->get_num_edges(); const size_t loop_size = m_profile->size(); const MatrixIr connecting_faces = generate_faces_connecting_loops( loop_size, dim != 2); const size_t num_connecting_faces = connecting_faces.rows(); for (size_t i=0; i<num_edges; i++) { Float edge_length = edge_lengths(i, 0); const auto& end_loops = m_end_loops[i]; const size_t num_segments = std::max(1.0, std::round(edge_length / ave_thickness)); MatrixFr pts((num_segments+1)*loop_size, dim); for (size_t j=0; j<num_segments+1; j++) { Float alpha = Float(j) / Float(num_segments); pts.block(j*loop_size, 0, loop_size, dim) = end_loops.first * (1.0 - alpha) + end_loops.second * alpha; } MatrixIr faces(num_connecting_faces * num_segments, 3); for (size_t j=0; j<num_segments; j++) { faces.block(j*num_connecting_faces, 0, num_connecting_faces, 3) = connecting_faces.array() + j*loop_size; } m_vertex_list.push_back(pts); m_face_list.push_back(faces.array() + m_num_vertex_accumulated); m_face_source_list.push_back(int(i)*(-1)-1); m_num_vertex_accumulated += pts.rows(); } }
bool MeshValidation::face_source_is_valid( const MatrixFr& vertices, const MatrixIr& faces, const VectorI& face_sources) { const Float EPS = 1e-6; const size_t num_vertices = vertices.rows(); const size_t num_faces = faces.rows(); Vector3F bbox_min = vertices.colwise().minCoeff(); Vector3F bbox_max = vertices.colwise().maxCoeff(); bool result = true; for (size_t i=0; i<num_faces; i++) { const Vector3I& f = faces.row(i); const Vector3F& v0 = vertices.row(f[0]); const Vector3F& v1 = vertices.row(f[1]); const Vector3F& v2 = vertices.row(f[2]); if (fabs(v0[0] - bbox_min[0]) < EPS && fabs(v1[0] - bbox_min[0]) < EPS && fabs(v2[0] - bbox_min[0]) < EPS) { result = result && (face_sources[i] == 0); continue; } if (fabs(v0[1] - bbox_min[1]) < EPS && fabs(v1[1] - bbox_min[1]) < EPS && fabs(v2[1] - bbox_min[1]) < EPS) { result = result && (face_sources[i] == 0); continue; } if (fabs(v0[2] - bbox_min[2]) < EPS && fabs(v1[2] - bbox_min[2]) < EPS && fabs(v2[2] - bbox_min[2]) < EPS) { result = result && (face_sources[i] == 0); continue; } if (fabs(v0[0] - bbox_max[0]) < EPS && fabs(v1[0] - bbox_max[0]) < EPS && fabs(v2[0] - bbox_max[0]) < EPS) { result = result && (face_sources[i] == 0); continue; } if (fabs(v0[1] - bbox_max[1]) < EPS && fabs(v1[1] - bbox_max[1]) < EPS && fabs(v2[1] - bbox_max[1]) < EPS) { result = result && (face_sources[i] == 0); continue; } if (fabs(v0[2] - bbox_max[2]) < EPS && fabs(v1[2] - bbox_max[2]) < EPS && fabs(v2[2] - bbox_max[2]) < EPS) { result = result && (face_sources[i] == 0); continue; } result = result && (face_sources[i] != 0); if (!result) { std::cout << i << ": "; std::cout << face_sources[i] << std::endl; std::cout << v0.transpose() << std::endl; std::cout << v1.transpose() << std::endl; std::cout << v2.transpose() << std::endl; return result; } } return result; }