/** * Function to auto-cropping image * * Parameters: * src The source image * dst The destination image */ void autocrop(cv::Mat& src, cv::Mat& dst) { cv::Rect win(0, 0, src.cols, src.rows); std::vector<cv::Rect> edges; edges.push_back(cv::Rect(0, 0, src.cols, 1)); edges.push_back(cv::Rect(src.cols-2, 0, 1, src.rows)); edges.push_back(cv::Rect(0, src.rows-2, src.cols, 1)); edges.push_back(cv::Rect(0, 0, 1, src.rows)); cv::Mat edge; int nborder = 0; cv::Vec3b color = src.at<cv::Vec3b>(0,0); for (int i = 0; i < edges.size(); ++i) { edge = src(edges[i]); nborder += is_border(edge, color); } if (nborder < 4) { src.copyTo(dst); return; } bool next; do { edge = src(cv::Rect(win.x, win.height-2, win.width, 1)); if (next = is_border(edge, color)) win.height--; } while (next && win.height > 0); do { edge = src(cv::Rect(win.width-2, win.y, 1, win.height)); if (next = is_border(edge, color)) win.width--; } while (next && win.width > 0); do { edge = src(cv::Rect(win.x, win.y, win.width, 1)); if (next = is_border(edge, color)) win.y++, win.height--; } while (next && win.y <= src.rows); do { edge = src(cv::Rect(win.x, win.y, 1, win.height)); if (next = is_border(edge, color)) win.x++, win.width--; } while (next && win.x <= src.cols); dst = src(win); }
void YSlim::add_error(int v1, int v2) { if(v1 >= vertices.size() || v2 >= vertices.size()) return; if(is_border(v1, v2)) return; errors.push( Error(v1, v2, calculate_error(v1, v2)) ); adj[v1].push_back(v2); adj[v2].push_back(v1); }
bool SkinMeme::compute_delt() { static bool debug = ::debug || Config::get_var_bool("DEBUG_SKIN_MEMES",false); _delt = Wvec::null(); if (is_frozen()) return false; if (!is_tracking()) { err_msg("SkinMeme::compute_delt: not tracking, can't proceed"); return false; } if (!is_boss()) { err_adv(debug, "SkinMeme::compute_delt: non-boss; bailing..."); track_to_target(loc()); return false; } Wpt cur = loc(); Wpt centroid = vert()->qr_centroid(); static const double bw = Config::get_var_dbl("SKIN_SMOOTH_CENTROID_WEIGHT",0.5); Wpt target = interp(cur, centroid, bw); // Target is where we want to be, but we'll settle for // somewhere on the skeleton surface that is as close as // possible. Wpt tp; if (track_to_target(target, tp)) { // double d = target.dist(tp); // Wpt new_loc = target + norm() * (fabs(_h) - d); // Wpt new_loc = tp + ((target - tp).normalized() * fabs(_h)); // XXX - hack // can we figure this out, one day, soon?? Wpt new_loc; if (is_border(track_simplex())) { new_loc = tp + track_norm()*_h; } else { new_loc = tp + ((target - tp).normalized() * fabs(_h)); } _delt = new_loc - cur; return true; } err_adv(debug, "SkinMeme::compute_delt: track to target failed"); return false; }
void YSlim::pack_vertex() { p_.clear(); idx_.clear(); int t1 = clock(); for(int i=0; i<vertices.size(); ++i) { p_.push_back(i); idx_.push_back(vertices[i]); } sort(idx_.begin(), idx_.end(), cmp_idx); //printf("%d\n", vertices.size()); for(int i=0; i<idx_.size(); ++i) { for(int j=i+1; j<idx_.size(); ++j) { if(distance(vertices[i], vertices[j]) < eps) { //printf("%d %d %.4lf\n", i, j, distance(vertices[i], vertices[j])); if(is_border(i, j)) continue; int u = find(idx_[i].id), v = find(idx_[j].id); p_[u] = v; } if(fabs(idx_[i].x - idx_[j].x) > eps || fabs(idx_[i].y - idx_[j].y) > eps || fabs(idx_[i].z - idx_[j].z) > eps) break; } } for(int i=0; i<faces.size(); ++i) { faces[i].id_vertex[0] = find(faces[i].id_vertex[0]); faces[i].id_vertex[1] = find(faces[i].id_vertex[1]); faces[i].id_vertex[2] = find(faces[i].id_vertex[2]); } int t2 = clock(); pack_vertex_time_ = t2 - t1; //Log::log("vc: %d, fc: %d\n", get_vert_num(), get_face_num()); //Log::log("pack vertex: %d ms\n", t2 - t1); }
static void overfill_lake(int x, int y, Shoreline * shore, int lake_id) { // Starting point is a local minimum // Lake growth is done iteratively by flooding the lowest shore point and rising water level // shore point = neighbour without water // (at this point we have no water in the map, except other lakes and rivers) // // We have a list of shore points sorted by altitude int i, level; if (is_border(x, y)) return; set_river_tile(x, y); level = ALT(x, y); // find neighbours for (i = 0; i < 8; i++) { if (in_map(x + di[i], y + dj[i]) && !IS_WATER(x + di[i], y + dj[i])) try_shore_point(x + di[i], y + dj[i], shore); } if (shore->next != NULL) { shore = shore->next; x = shore->x; y = shore->y; if ((ALT(x, y) < level)) { set_river_tile(x, y); // create river and continue to build shoreline // we will continue to overfill (from a lower point) until we reach border of the map //fprintf(stdout, "We found a pass x %i, y %i, alt %i \n", x, y, ALT(x,y)); setup_one_river(x, y, lake_id, shore); } overfill_lake(x, y, shore, lake_id); } else { // Q: ? Should this happen ? // A: yes if we are in a lake that was previously filled by a higher one which overfilled here // else ? it should not happen ? //fprintf(stderr,"the shoreline list is empty, x = %i, y = %i\n", x, y); } }
int main(int argc, char* argv[]) { const char* filename = (argc > 1) ? argv[1] : "data/mech-holes-shark.off"; LCC mesh; CGAL::read_off(filename, mesh); // Incrementally fill the holes unsigned int nb_holes = 0; for ( halfedge_iterator it=halfedges(mesh).begin(); it!=halfedges(mesh).end(); ++it) { halfedge_descriptor h=*it; if(is_border(h,mesh)) { std::vector<face_descriptor> patch_facets; std::vector<vertex_descriptor> patch_vertices; bool success = CGAL::cpp11::get<0>( CGAL::Polygon_mesh_processing::triangulate_refine_and_fair_hole( mesh, h, std::back_inserter(patch_facets), std::back_inserter(patch_vertices), CGAL::Polygon_mesh_processing::parameters::vertex_point_map(get(CGAL::vertex_point, mesh)). geom_traits(Kernel())) ); std::cout << "* Number of facets in constructed patch: " << patch_facets.size() << std::endl; std::cout << " Number of vertices in constructed patch: " << patch_vertices.size() << std::endl; std::cout << " Is fairing successful: " << success << std::endl; nb_holes++; } } std::cout << std::endl; std::cout << nb_holes << " holes have been filled" << std::endl; std::ofstream out("filled_LCC.off"); out.precision(17); CGAL::write_off(out, mesh); return 0; }
/// Obtain the address of an dataitem associated with its bounded element. const void *Halfedge::addr( const COM::DataItem *a) const { COM_assertion( !is_border()); COM_assertion_msg( a, "Unexpected NULL pointer"); COM_assertion_msg( a->is_elemental(), "Expect a element-centered dataitem"); const COM::DataItem *attr; const COM::Pane *pn = _pm->pane(); int pid = pn->id(); if ( a->pane()->id() == pid) attr = a; else { if ( a->window() != pn->window()) { pn = &a->window()->pane( pid); COM_assertion_msg( pn, "Pane does not exist"); } attr=pn->dataitem( a->id()); COM_assertion_msg( attr, "DataItem does not exist"); } return ((const char*)attr->pointer()) + COM_get_sizeof( attr->data_type(), attr->stride()*(_eID.eid()-1)); }
// defined here just for fatify code void MasstreeIntermediatePage::split_foster_migrate_records_new_first_root(const void* arg) { ASSERT_ND(!header().snapshot_); ASSERT_ND(!is_moved()); ASSERT_ND(!is_retired()); ASSERT_ND(get_low_fence() == kInfimumSlice); ASSERT_ND(is_high_fence_supremum()); ASSERT_ND(get_layer() == 0); ASSERT_ND(!is_border()); const std::vector<Child>* new_children = reinterpret_cast< const std::vector<Child>* >(arg); // we have to mold the pointer list into IntermediateSplitStrategy. IntermediateSplitStrategy strategy; std::memset(&strategy, 0, sizeof(strategy)); for (uint32_t i = 0; i < new_children->size(); ++i) { const Child& child = new_children->at(i); strategy.separators_[i] = child.high_; strategy.pointers_[i].volatile_pointer_ = child.pointer_; } strategy.total_separator_count_ = new_children->size(); strategy.mid_separator_ = kSupremumSlice; strategy.mid_index_ = new_children->size(); split_foster_migrate_records(strategy, 0, new_children->size(), kSupremumSlice); }
bool check_for_discontinuity(Edge *e) { return is_border(e); }
bool operator()(const Edge& e) const { return is_border(e,g); }
bool fill_hole(std::vector<std::size_t> const & hole, UniGraph const & graph, mve::TriangleMesh::ConstPtr mesh, mve::MeshInfo const & mesh_info, std::vector<std::vector<VertexProjectionInfo> > * vertex_projection_infos, std::vector<TexturePatch::Ptr> * texture_patches) { mve::TriangleMesh::FaceList const & mesh_faces = mesh->get_faces(); mve::TriangleMesh::VertexList const & vertices = mesh->get_vertices(); std::map<std::size_t, std::set<std::size_t> > tmp; for (std::size_t const face_id : hole) { std::size_t const v0 = mesh_faces[face_id * 3]; std::size_t const v1 = mesh_faces[face_id * 3 + 1]; std::size_t const v2 = mesh_faces[face_id * 3 + 2]; tmp[v0].insert(face_id); tmp[v1].insert(face_id); tmp[v2].insert(face_id); } std::size_t const num_vertices = tmp.size(); /* Only fill small holes. */ if (num_vertices > MAX_HOLE_NUM_FACES) return false; /* Calculate 2D parameterization using the technique from libremesh/patch2d, * which was published as sourcecode accompanying the following paper: * * Isotropic Surface Remeshing * Simon Fuhrmann, Jens Ackermann, Thomas Kalbe, Michael Goesele */ std::size_t seed = -1; std::vector<bool> is_border(num_vertices, false); std::vector<std::vector<std::size_t> > adj_verts_via_border(num_vertices); /* Index structures to map from local <-> global vertex id. */ std::map<std::size_t, std::size_t> g2l; std::vector<std::size_t> l2g(num_vertices); /* Index structure to determine column in matrix/vector. */ std::vector<std::size_t> idx(num_vertices); std::size_t num_border_vertices = 0; bool disk_topology = true; std::map<std::size_t, std::set<std::size_t> >::iterator it = tmp.begin(); for (std::size_t j = 0; j < num_vertices; ++j, ++it) { std::size_t vertex_id = it->first; g2l[vertex_id] = j; l2g[j] = vertex_id; /* Check topology in original mesh. */ if (mesh_info[vertex_id].vclass != mve::MeshInfo::VERTEX_CLASS_SIMPLE) { /* Complex/Border vertex in original mesh */ disk_topology = false; break; } /* Check new topology and determine if vertex is now at the border. */ std::vector<std::size_t> const & adj_faces = mesh_info[vertex_id].faces; std::set<std::size_t> const & adj_hole_faces = it->second; std::vector<std::pair<std::size_t, std::size_t> > fan; for (std::size_t k = 0; k < adj_faces.size(); ++k) { std::size_t adj_face = adj_faces[k]; if (graph.get_label(adj_faces[k]) == 0 && adj_hole_faces.find(adj_face) != adj_hole_faces.end()) { std::size_t curr = adj_faces[k]; std::size_t next = adj_faces[(k + 1) % adj_faces.size()]; std::pair<std::size_t, std::size_t> pair(curr, next); fan.push_back(pair); } } std::size_t gaps = 0; for (std::size_t k = 0; k < fan.size(); k++) { std::size_t curr = fan[k].first; std::size_t next = fan[(k + 1) % fan.size()].first; if (fan[k].second != next) { ++gaps; for (std::size_t l = 0; l < 3; ++l) { if(mesh_faces[curr * 3 + l] == vertex_id) { std::size_t second = mesh_faces[curr * 3 + (l + 2) % 3]; adj_verts_via_border[j].push_back(second); } if(mesh_faces[next * 3 + l] == vertex_id) { std::size_t first = mesh_faces[next * 3 + (l + 1) % 3]; adj_verts_via_border[j].push_back(first); } } } } is_border[j] = gaps == 1; /* Check if vertex is now complex. */ if (gaps > 1) { /* Complex vertex in hole */ disk_topology = false; break; } if (is_border[j]) { idx[j] = num_border_vertices++; seed = vertex_id; } else { idx[j] = j - num_border_vertices; } } tmp.clear(); /* No disk or genus zero topology */ if (!disk_topology || num_border_vertices == 0) return false; std::vector<std::size_t> border; border.reserve(num_border_vertices); std::size_t prev = seed; std::size_t curr = seed; while (prev == seed || curr != seed) { std::size_t next = std::numeric_limits<std::size_t>::max(); std::vector<std::size_t> const & adj_verts = adj_verts_via_border[g2l[curr]]; for (std::size_t adj_vert : adj_verts) { assert(is_border[g2l[adj_vert]]); if (adj_vert != prev && adj_vert != curr) { next = adj_vert; break; } } if (next != std::numeric_limits<std::size_t>::max()) { prev = curr; curr = next; border.push_back(next); } else { /* No new border vertex */ border.clear(); break; } /* Loop within border */ if (border.size() > num_border_vertices) break; } if (border.size() != num_border_vertices) return false; float total_length = 0.0f; float total_projection_length = 0.0f; for (std::size_t j = 0; j < border.size(); ++j) { std::size_t vi0 = border[j]; std::size_t vi1 = border[(j + 1) % border.size()]; std::vector<VertexProjectionInfo> const & vpi0 = vertex_projection_infos->at(vi0); std::vector<VertexProjectionInfo> const & vpi1 = vertex_projection_infos->at(vi0); /* According to the previous checks (vertex class within the origial * mesh and boundary) there already has to be at least one projection * of each border vertex. */ assert(!vpi0.empty() && !vpi1.empty()); math::Vec2f vp0(0.0f), vp1(0.0f); for (VertexProjectionInfo const & info0 : vpi0) { for (VertexProjectionInfo const & info1 : vpi1) { if (info0.texture_patch_id == info1.texture_patch_id) { vp0 = info0.projection; vp1 = info1.projection; break; } } } total_projection_length += (vp0 - vp1).norm(); math::Vec3f const & v0 = vertices[vi0]; math::Vec3f const & v1 = vertices[vi1]; total_length += (v0 - v1).norm(); } float radius = total_projection_length / (2.0f * MATH_PI); if (total_length < std::numeric_limits<float>::epsilon()) return false; float length = 0.0f; std::vector<math::Vec2f> projections(num_vertices); for (std::size_t j = 0; j < border.size(); ++j) { float angle = 2.0f * MATH_PI * (length / total_length); projections[g2l[border[j]]] = math::Vec2f(std::cos(angle), std::sin(angle)); math::Vec3f const & v0 = vertices[border[j]]; math::Vec3f const & v1 = vertices[border[(j + 1) % border.size()]]; length += (v0 - v1).norm(); } typedef Eigen::Triplet<float, int> Triplet; std::vector<Triplet> coeff; std::size_t matrix_size = num_vertices - border.size(); Eigen::VectorXf xx(matrix_size), xy(matrix_size); if (matrix_size != 0) { Eigen::VectorXf bx(matrix_size); Eigen::VectorXf by(matrix_size); for (std::size_t j = 0; j < num_vertices; ++j) { if (is_border[j]) continue; std::size_t const vertex_id = l2g[j]; /* Calculate "Mean Value Coordinates" as proposed by Michael S. Floater */ std::map<std::size_t, float> weights; std::vector<std::size_t> const & adj_faces = mesh_info[vertex_id].faces; for (std::size_t adj_face : adj_faces) { std::size_t v0 = mesh_faces[adj_face * 3]; std::size_t v1 = mesh_faces[adj_face * 3 + 1]; std::size_t v2 = mesh_faces[adj_face * 3 + 2]; if (v1 == vertex_id) std::swap(v1, v0); if (v2 == vertex_id) std::swap(v2, v0); math::Vec3f v01 = vertices[v1] - vertices[v0]; float v01n = v01.norm(); math::Vec3f v02 = vertices[v2] - vertices[v0]; float v02n = v02.norm(); /* Ensure numerical stability */ if (v01n * v02n < std::numeric_limits<float>::epsilon()) return false; float alpha = std::acos(v01.dot(v02) / (v01n * v02n)); weights[g2l[v1]] += std::tan(alpha / 2.0f) / v01n; weights[g2l[v2]] += std::tan(alpha / 2.0f) / v02n; } std::map<std::size_t, float>::iterator it; float sum = 0.0f; for (it = weights.begin(); it != weights.end(); ++it) sum += it->second; assert(sum > 0.0f); for (it = weights.begin(); it != weights.end(); ++it) it->second /= sum; bx[idx[j]] = 0.0f; by[idx[j]] = 0.0f; for (it = weights.begin(); it != weights.end(); ++it) { if (is_border[it->first]) { std::size_t border_vertex_id = border[idx[it->first]]; bx[idx[j]] += projections[g2l[border_vertex_id]][0] * it->second; by[idx[j]] += projections[g2l[border_vertex_id]][1] * it->second; } else { coeff.push_back(Triplet(idx[j], idx[it->first], -it->second)); } } } for (std::size_t j = 0; j < matrix_size; ++j) { coeff.push_back(Triplet(j, j, 1.0f)); } typedef Eigen::SparseMatrix<float> SpMat; SpMat A(matrix_size, matrix_size); A.setFromTriplets(coeff.begin(), coeff.end()); Eigen::SparseLU<SpMat> solver; solver.analyzePattern(A); solver.factorize(A); xx = solver.solve(bx); xy = solver.solve(by); } float const max_hole_patch_size = MAX_HOLE_PATCH_SIZE; int image_size = std::min(std::floor(radius * 1.1f) * 2.0f, max_hole_patch_size); /* Ensure a minimum scale of one */ image_size += 2 * (1 + texture_patch_border); int scale = image_size / 2 - texture_patch_border; for (std::size_t j = 0, k = 0; j < num_vertices; ++j) { if (is_border[j]) { projections[j] = projections[j] * scale + image_size / 2; } else { projections[j] = math::Vec2f(xx[k], xy[k]) * scale + image_size / 2; ++k; } } mve::ByteImage::Ptr image = mve::ByteImage::create(image_size, image_size, 3); //DEBUG image->fill_color(*math::Vec4uc(0, 255, 0, 255)); std::vector<math::Vec2f> texcoords; texcoords.reserve(hole.size()); for (std::size_t const face_id : hole) { for (std::size_t j = 0; j < 3; ++j) { std::size_t const vertex_id = mesh_faces[face_id * 3 + j]; math::Vec2f const & projection = projections[g2l[vertex_id]]; texcoords.push_back(projection); } } TexturePatch::Ptr texture_patch = TexturePatch::create(0, hole, texcoords, image); std::size_t texture_patch_id; #pragma omp critical { texture_patches->push_back(texture_patch); texture_patch_id = texture_patches->size() - 1; } for (std::size_t j = 0; j < num_vertices; ++j) { std::size_t const vertex_id = l2g[j]; std::vector<std::size_t> const & adj_faces = mesh_info[vertex_id].faces; std::vector<std::size_t> faces; faces.reserve(adj_faces.size()); for (std::size_t adj_face : adj_faces) { if (graph.get_label(adj_face) == 0) { faces.push_back(adj_face); } } VertexProjectionInfo info = {texture_patch_id, projections[j], faces}; #pragma omp critical vertex_projection_infos->at(vertex_id).push_back(info); } return true; }
void generate_texture_patches(UniGraph const & graph, std::vector<TextureView> const & texture_views, mve::TriangleMesh::ConstPtr mesh, mve::VertexInfoList::ConstPtr vertex_infos, std::vector<std::vector<VertexProjectionInfo> > * vertex_projection_infos, std::vector<TexturePatch> * texture_patches) { util::WallTimer timer; mve::TriangleMesh::FaceList const & mesh_faces = mesh->get_faces(); mve::TriangleMesh::VertexList const & vertices = mesh->get_vertices(); vertex_projection_infos->resize(vertices.size()); std::size_t num_patches = 0; std::cout << "\tRunning... " << std::flush; #pragma omp parallel for schedule(dynamic) for (std::size_t i = 0; i < texture_views.size(); ++i) { std::vector<std::vector<std::size_t> > subgraphs; int const label = i + 1; graph.get_subgraphs(label, &subgraphs); std::list<TexturePatchCandidate> candidates; for (std::size_t j = 0; j < subgraphs.size(); ++j) { candidates.push_back(generate_candidate(label, texture_views[i], subgraphs[j], mesh)); } /* Merge candidates which contain the same image content. */ std::list<TexturePatchCandidate>::iterator it, sit; for (it = candidates.begin(); it != candidates.end(); ++it) { for (sit = candidates.begin(); sit != candidates.end();) { Rect<int> bounding_box = sit->bounding_box; if (it != sit && bounding_box.is_inside(&it->bounding_box)) { TexturePatch::Faces & faces = it->texture_patch.get_faces(); TexturePatch::Faces & ofaces = sit->texture_patch.get_faces(); faces.insert(faces.end(), ofaces.begin(), ofaces.end()); TexturePatch::Texcoords & texcoords = it->texture_patch.get_texcoords(); TexturePatch::Texcoords & otexcoords = sit->texture_patch.get_texcoords(); math::Vec2f offset; offset[0] = sit->bounding_box.min_x - it->bounding_box.min_x; offset[1] = sit->bounding_box.min_y - it->bounding_box.min_y; for (std::size_t i = 0; i < otexcoords.size(); ++i) { texcoords.push_back(otexcoords[i] + offset); } sit = candidates.erase(sit); } else { ++sit; } } } it = candidates.begin(); for (; it != candidates.end(); ++it) { std::size_t texture_patch_id; #pragma omp critical { texture_patches->push_back(it->texture_patch); texture_patch_id = num_patches++; } std::vector<std::size_t> const & faces = it->texture_patch.get_faces(); std::vector<math::Vec2f> const & texcoords = it->texture_patch.get_texcoords(); for (std::size_t i = 0; i < faces.size(); ++i) { std::size_t const face_id = faces[i]; std::size_t const face_pos = face_id * 3; for (std::size_t j = 0; j < 3; ++j) { std::size_t const vertex_id = mesh_faces[face_pos + j]; math::Vec2f const projection = texcoords[i * 3 + j]; VertexProjectionInfo info = {texture_patch_id, projection, {face_id}}; #pragma omp critical vertex_projection_infos->at(vertex_id).push_back(info); } } } } merge_vertex_projection_infos(vertex_projection_infos); std::size_t num_holes = 0; std::size_t num_hole_faces = 0; //if (!settings.skip_hole_filling) { { std::vector<std::vector<std::size_t> > subgraphs; graph.get_subgraphs(0, &subgraphs); #pragma omp parallel for schedule(dynamic) for (std::size_t i = 0; i < subgraphs.size(); ++i) { std::vector<std::size_t> const & subgraph = subgraphs[i]; std::map<std::size_t, std::set<std::size_t> > tmp; for (std::size_t const face_id : subgraph) { std::size_t const v0 = mesh_faces[face_id * 3]; std::size_t const v1 = mesh_faces[face_id * 3 + 1]; std::size_t const v2 = mesh_faces[face_id * 3 + 2]; tmp[v0].insert(face_id); tmp[v1].insert(face_id); tmp[v2].insert(face_id); } std::size_t const num_vertices = tmp.size(); /* Only fill small holes. */ if (num_vertices > 100) { //std::cerr << "Hole to large" << std::endl; continue; } /* Calculate 2D parameterization using the technique from libremesh/patch2d, * which was published as sourcecode accompanying the following paper: * * Isotropic Surface Remeshing * Simon Fuhrmann, Jens Ackermann, Thomas Kalbe, Michael Goesele */ std::size_t seed = -1; std::vector<bool> is_border(num_vertices, false); std::vector<std::vector<std::size_t> > adj_verts_via_border(num_vertices); /* Index structures to map from local <-> global vertex id. */ std::map<std::size_t, std::size_t> g2l; std::vector<std::size_t> l2g(num_vertices); /* Index structure to determine column in matrix/vector. */ std::vector<std::size_t> idx(num_vertices); std::size_t num_border_vertices = 0; bool disk_topology = true; std::map<std::size_t, std::set<std::size_t> >::iterator it = tmp.begin(); for (std::size_t j = 0; j < num_vertices; ++j, ++it) { std::size_t vertex_id = it->first; g2l[vertex_id] = j; l2g[j] = vertex_id; /* Check topology in original mesh. */ if (vertex_infos->at(vertex_id).vclass != mve::VERTEX_CLASS_SIMPLE) { //std::cerr << "Complex/Border vertex in original mesh" << std::endl; disk_topology = false; break; } /* Check new topology and determine if vertex is now at the border. */ std::vector<std::size_t> const & adj_faces = vertex_infos->at(vertex_id).faces; std::set<std::size_t> const & adj_hole_faces = it->second; std::vector<std::pair<std::size_t, std::size_t> > fan; for (std::size_t k = 0; k < adj_faces.size(); ++k) { std::size_t adj_face = adj_faces[k]; if (graph.get_label(adj_faces[k]) == 0 && adj_hole_faces.find(adj_face) != adj_hole_faces.end()) { std::size_t curr = adj_faces[k]; std::size_t next = adj_faces[(k + 1) % adj_faces.size()]; std::pair<std::size_t, std::size_t> pair(curr, next); fan.push_back(pair); } } std::size_t gaps = 0; for (std::size_t k = 0; k < fan.size(); k++) { std::size_t curr = fan[k].first; std::size_t next = fan[(k + 1) % fan.size()].first; if (fan[k].second != next) { ++gaps; for (std::size_t l = 0; l < 3; ++l) { if(mesh_faces[curr * 3 + l] == vertex_id) { std::size_t second = mesh_faces[curr * 3 + (l + 2) % 3]; adj_verts_via_border[j].push_back(second); } if(mesh_faces[next * 3 + l] == vertex_id) { std::size_t first = mesh_faces[next * 3 + (l + 1) % 3]; adj_verts_via_border[j].push_back(first); } } } } is_border[j] = gaps == 1; /* Check if vertex is now complex. */ if (gaps > 1) { //std::cerr << "Complex vertex in hole" << std::endl; disk_topology = false; break; } if (is_border[j]) { idx[j] = num_border_vertices++; seed = vertex_id; } else { idx[j] = j - num_border_vertices; } } tmp.clear(); if (!disk_topology) continue; if (num_border_vertices == 0) { //std::cerr << "Genus zero topology" << std::endl; continue; } std::vector<std::size_t> border; border.reserve(num_border_vertices); std::size_t prev = seed; std::size_t curr = seed; while (prev == seed || curr != seed) { std::size_t next = std::numeric_limits<std::size_t>::max(); std::vector<std::size_t> const & adj_verts = adj_verts_via_border[g2l[curr]]; for (std::size_t adj_vert : adj_verts) { assert(is_border[g2l[adj_vert]]); if (adj_vert != prev && adj_vert != curr) { next = adj_vert; break; } } if (next != std::numeric_limits<std::size_t>::max()) { prev = curr; curr = next; border.push_back(next); } else { //std::cerr << "No new border vertex" << std::endl; border.clear(); break; } if (border.size() > num_border_vertices) { //std::cerr << "Loop within border" << std::endl; break; } } if (border.size() != num_border_vertices) { continue; } float total_length = 0.0f; float total_projection_length = 0.0f; for (std::size_t j = 0; j < border.size(); ++j) { std::size_t vi0 = border[j]; std::size_t vi1 = border[(j + 1) % border.size()]; std::vector<VertexProjectionInfo> const & vpi0 = vertex_projection_infos->at(vi0); std::vector<VertexProjectionInfo> const & vpi1 = vertex_projection_infos->at(vi0); /* According to the previous checks (vertex class within the origial * mesh and boundary) there already has to be at least one projection * of each border vertex. */ assert(!vpi0.empty() && !vpi1.empty()); math::Vec2f vp0(0.0f), vp1(0.0f); for (VertexProjectionInfo const & info0 : vpi0) { for (VertexProjectionInfo const & info1 : vpi1) { if (info0.texture_patch_id == info1.texture_patch_id) { vp0 = info0.projection; vp1 = info1.projection; break; } } } total_projection_length += (vp0 - vp1).norm(); math::Vec3f const & v0 = vertices[vi0]; math::Vec3f const & v1 = vertices[vi1]; total_length += (v0 - v1).norm(); } float radius = total_projection_length / (2.0f * MATH_PI); float length = 0.0f; std::vector<math::Vec2f> projections(num_vertices); for (std::size_t j = 0; j < border.size(); ++j) { float angle = 2.0f * MATH_PI * (length / total_length); projections[g2l[border[j]]] = math::Vec2f(std::cos(angle), std::sin(angle)); math::Vec3f const & v0 = vertices[border[j]]; math::Vec3f const & v1 = vertices[border[(j + 1) % border.size()]]; length += (v0 - v1).norm(); } typedef Eigen::Triplet<float, int> Triplet; std::vector<Triplet> coeff; std::size_t matrix_size = num_vertices - border.size(); Eigen::VectorXf xx(matrix_size), xy(matrix_size); if (matrix_size != 0) { Eigen::VectorXf bx(matrix_size); Eigen::VectorXf by(matrix_size); for (std::size_t j = 0; j < num_vertices; ++j) { if (is_border[j]) continue; std::size_t const vertex_id = l2g[j]; /* Calculate "Mean Value Coordinates" as proposed by Michael S. Floater */ std::map<std::size_t, float> weights; std::vector<std::size_t> const & adj_faces = vertex_infos->at(vertex_id).faces; for (std::size_t adj_face : adj_faces) { std::size_t v0 = mesh_faces[adj_face * 3]; std::size_t v1 = mesh_faces[adj_face * 3 + 1]; std::size_t v2 = mesh_faces[adj_face * 3 + 2]; if (v1 == vertex_id) std::swap(v1, v0); if (v2 == vertex_id) std::swap(v2, v0); math::Vec3f v01 = vertices[v1] - vertices[v0]; float v01n = v01.norm(); math::Vec3f v02 = vertices[v2] - vertices[v0]; float v02n = v02.norm(); float alpha = std::acos(v01.dot(v02) / (v01n * v02n)); weights[g2l[v1]] += std::tan(alpha / 2.0f) / v01n; weights[g2l[v2]] += std::tan(alpha / 2.0f) / v02n; } std::map<std::size_t, float>::iterator it; float sum = 0.0f; for (it = weights.begin(); it != weights.end(); ++it) sum += it->second; for (it = weights.begin(); it != weights.end(); ++it) it->second /= sum; bx[idx[j]] = 0.0f; by[idx[j]] = 0.0f; for (it = weights.begin(); it != weights.end(); ++it) { if (is_border[it->first]) { std::size_t border_vertex_id = border[idx[it->first]]; bx[idx[j]] += projections[g2l[border_vertex_id]][0] * it->second; by[idx[j]] += projections[g2l[border_vertex_id]][1] * it->second; } else { coeff.push_back(Triplet(idx[j], idx[it->first], -it->second)); } } } for (std::size_t j = 0; j < matrix_size; ++j) { coeff.push_back(Triplet(j, j, 1.0f)); } typedef Eigen::SparseMatrix<float> SpMat; SpMat A(matrix_size, matrix_size); A.setFromTriplets(coeff.begin(), coeff.end()); Eigen::SparseLU<SpMat> solver; solver.analyzePattern(A); solver.factorize(A); xx = solver.solve(bx); xy = solver.solve(by); } int image_size = std::floor(radius * 1.1f) * 2 + 4; int scale = image_size / 2 - texture_patch_border; for (std::size_t j = 0, k = 0; j < num_vertices; ++j) { if (!is_border[j]) { projections[j] = math::Vec2f(xx[k], xy[k]) * scale + image_size / 2; ++k; } else { projections[j] = projections[j] * scale + image_size / 2; } } mve::ByteImage::Ptr image = mve::ByteImage::create(image_size, image_size, 3); //DEBUG image->fill_color(*math::Vec4uc(0, 255, 0, 255)); std::vector<math::Vec2f> texcoords; texcoords.reserve(subgraph.size()); for (std::size_t const face_id : subgraph) { for (std::size_t j = 0; j < 3; ++j) { std::size_t const vertex_id = mesh_faces[face_id * 3 + j]; math::Vec2f const & projection = projections[g2l[vertex_id]]; texcoords.push_back(projection); } } TexturePatch texture_patch(0, subgraph, texcoords, image); std::size_t texture_patch_id; #pragma omp critical { texture_patches->push_back(texture_patch); texture_patch_id = num_patches++; num_hole_faces += subgraph.size(); ++num_holes; } for (std::size_t j = 0; j < num_vertices; ++j) { std::size_t const vertex_id = l2g[j]; std::vector<std::size_t> const & adj_faces = vertex_infos->at(vertex_id).faces; std::vector<std::size_t> faces; faces.reserve(adj_faces.size()); for (std::size_t adj_face : adj_faces) { if (graph.get_label(adj_face) == 0) { faces.push_back(adj_face); } } VertexProjectionInfo info = {texture_patch_id, projections[j], faces}; #pragma omp critical vertex_projection_infos->at(vertex_id).push_back(info); } } } merge_vertex_projection_infos(vertex_projection_infos); std::cout << "done. (Took " << timer.get_elapsed_sec() << "s)" << std::endl; std::cout << "\t" << num_patches << " texture patches." << std::endl; std::cout << "\t" << num_holes << " holes (" << num_hole_faces << " faces)." << std::endl; }