openmesh - impl - Remove Duplicated Vertices

关于openmesh元素删除实现的介绍参见:openmesh - src - trimesh delete and add elements - grassofsky - 博客园 (cnblogs.com)

重复点删除的主要步骤如下:

  • 找到所有的重复顶点,并设定每组重复顶点中需要保留的顶点;
  • 记录这些重复顶点对应的三角形;(因为下一步在顶点删除的时候,会将顶点周围关联的三角形都删除,并将顶点的状态设置为deleted);
  • 删除所有的重复的顶点;(此时会删除关联的三角形和半边);
  • 可能出现孤立点,删除孤立点;(此步骤可选);
  • 回收,边和面的资源;(此步骤为了后续添加关联面做准备);
  • 遍历所有的重复顶点关联的三角形,如果这个三角形中仅含有一个重复点,那么添加该三角形,并将重复点替换为对应的保留的点;
  • 恢复重新添加的保留的顶点的删除状态;
  • 资源回收;

具体实现代码如下:

#include <iostream>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/Timer.hh> #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h" using namespace OpenMesh; struct MyTraits : public OpenMesh::DefaultTraits
{
#if 1
typedef OpenMesh::Vec3f Point;
typedef OpenMesh::Vec3f Normal;
#else
typedef OpenMesh::Vec3d Point;
typedef OpenMesh::Vec3d Normal;
#endif
}; typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> TriMesh; //----------------------------------------------------------------------------- bool HasDuplicatedVertex(const TriMesh& mesh)
{
std::map<TriMesh::Point, int> mapPoints;
for (auto iter = mesh.vertices_begin(); iter != mesh.vertices_end(); ++iter)
{
auto iterPt = mapPoints.find(mesh.point(*iter));
if (iterPt == mapPoints.end())
{
mapPoints.insert_or_assign(mesh.point(*iter), 1);
}
else
{
return true;
}
} return false;
} int RemoveDuplicatedVertex(TriMesh& mesh)
{
// Find all duplicated vertices
int iOriginalNumVertices = static_cast<int>(mesh.n_vertices());
std::map<TriMesh::Point, TriMesh::VertexHandle> mapPoints;
std::map<TriMesh::VertexHandle, std::set<TriMesh::VertexHandle>> mapDuplicatedVertices; for (auto iter = mesh.vertices_begin(); iter != mesh.vertices_end(); ++iter)
{
auto& pt = mesh.point(*iter);
if (mapPoints.find(pt) != mapPoints.end())
{
auto& vh = mapPoints[pt];
auto iterDuplicatedVertices = mapDuplicatedVertices.find(vh);
if (iterDuplicatedVertices == mapDuplicatedVertices.end())
{
mapDuplicatedVertices.insert(std::make_pair(vh, std::set<TriMesh::VertexHandle>{ vh, *iter }));
}
else
{
iterDuplicatedVertices->second.insert(*iter);
}
}
else
{
mapPoints.insert(std::make_pair(pt, *iter));
}
} // Record the duplicated vertices related faces
std::map<TriMesh::VertexHandle, std::set<std::array<TriMesh::VertexHandle, 3>>> mapVertexFaces;
for (auto iter = mapDuplicatedVertices.begin(); iter != mapDuplicatedVertices.end(); ++iter)
{
auto pair = mapVertexFaces.insert(std::make_pair(iter->first, std::set<std::array<TriMesh::VertexHandle, 3>>{}));
auto& vecFaces = pair.first->second;
for (auto v_it = iter->second.begin(); v_it != iter->second.end(); ++v_it)
{
for (auto vf_it = mesh.vf_iter(*v_it); vf_it.is_valid(); ++vf_it)
{
std::array<TriMesh::VertexHandle, 3> face;
int i = 0;
auto fv_it = mesh.fv_begin(*vf_it);
face[i++] = *(fv_it++);
face[i++] = *(fv_it++);
face[i++] = *(fv_it++);
vecFaces.insert(face);
}
}
} if (!mesh.has_vertex_status()) mesh.request_vertex_status();
if (!mesh.has_face_status()) mesh.request_face_status();
if (!mesh.has_edge_status()) mesh.request_edge_status(); // remove all duplicated vertices
for (auto iter = mapDuplicatedVertices.begin(); iter != mapDuplicatedVertices.end(); ++iter)
{
for (auto v_it = iter->second.begin(); v_it != iter->second.end(); ++v_it)
{
mesh.delete_vertex(*v_it);
}
} mesh.delete_isolated_vertices(); // garbage_collection edge and face
mesh.garbage_collection(false, true, true); // add not degenereated faces
std::set<TriMesh::VertexHandle> setRemainVertices;
for (auto iter = mapVertexFaces.begin(); iter != mapVertexFaces.end(); ++iter)
{
for (auto f_it = iter->second.begin(); f_it != iter->second.end(); ++f_it)
{
std::array<TriMesh::VertexHandle, 3> face = *f_it;
unsigned short result = 0;
for (auto iter = mapDuplicatedVertices.begin(); iter != mapDuplicatedVertices.end(); ++iter)
{
// bit pos records if the vertex is duplicated vertex or not
result = 0;
result |= (iter->second.count(face[0]) == 0 ? 0 : 1);
result |= (iter->second.count(face[1]) == 0 ? 0 : 2);
result |= (iter->second.count(face[2]) == 0 ? 0 : 4);
if (result == 1 || result == 2 || result == 4)
{
// replace the duplicated vertex as remaining vertex
face[result / 2] = iter->first;
mesh.add_face(face[0], face[1], face[2]);
setRemainVertices.insert(face[0]);
setRemainVertices.insert(face[1]);
setRemainVertices.insert(face[2]);
break;
}
}
}
} // recover status of remain vertex
for (auto v_it = setRemainVertices.begin(); v_it != setRemainVertices.end(); ++v_it)
{
mesh.status(*v_it).set_deleted(false);
} // garbage_collection vertex
mesh.garbage_collection(); if (!mesh.has_vertex_status()) mesh.release_vertex_status();
if (!mesh.has_face_status()) mesh.release_vertex_status();
if (!mesh.has_edge_status()) mesh.release_vertex_status(); return iOriginalNumVertices - static_cast<int>(mesh.n_vertices());
} //----------------------------------------------------------------------------- TEST_CASE("testing delete duplicated vertex")
{
TriMesh mesh;
// Add some vertices
TriMesh::VertexHandle vhandle[7];
vhandle[0] = mesh.add_vertex(TriMesh::Point(0, 0, 0));
vhandle[1] = mesh.add_vertex(TriMesh::Point(0, 2, 0));
vhandle[2] = mesh.add_vertex(TriMesh::Point(2, 2, 0));
vhandle[3] = mesh.add_vertex(TriMesh::Point(2, 0, 0));
vhandle[4] = mesh.add_vertex(TriMesh::Point(1, 1, 0));
vhandle[5] = mesh.add_vertex(TriMesh::Point(1, 1, 0));
vhandle[6] = mesh.add_vertex(TriMesh::Point(1, 1, 0)); // Add faces
mesh.add_face(vhandle[0], vhandle[1], vhandle[4]);
mesh.add_face(vhandle[0], vhandle[4], vhandle[6]);
mesh.add_face(vhandle[0], vhandle[6], vhandle[3]);
mesh.add_face(vhandle[1], vhandle[5], vhandle[4]);
mesh.add_face(vhandle[1], vhandle[2], vhandle[5]);
mesh.add_face(vhandle[2], vhandle[6], vhandle[5]);
mesh.add_face(vhandle[2], vhandle[3], vhandle[6]);
mesh.add_face(vhandle[4], vhandle[5], vhandle[6]); auto iNDeleted = RemoveDuplicatedVertex(mesh);
CHECK(iNDeleted == 2);
CHECK(mesh.n_faces() == 4);
CHECK(mesh.n_vertices() == 5);
CHECK_FALSE(HasDuplicatedVertex(mesh));
}

openmesh - impl - Remove Duplicated Vertices的更多相关文章

  1. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  2. remove duplicated gene pair using awk

    cat input.txt TRINITY_DN106621_c0_g1_i1 TRINITY_DN129833_c0_g1_i2 TRINITY_DN106621_c0_g1_i1 TRINITY_ ...

  3. CG&CAD resource

    Computational Geometry The Geometry Center (UIUC) Computational Geometry Pages (UIUC) Geometry in Ac ...

  4. 大数据技术之_19_Spark学习_05_Spark GraphX 应用解析 + Spark GraphX 概述、解析 + 计算模式 + Pregel API + 图算法参考代码 + PageRank 实例

    第1章 Spark GraphX 概述1.1 什么是 Spark GraphX1.2 弹性分布式属性图1.3 运行图计算程序第2章 Spark GraphX 解析2.1 存储模式2.1.1 图存储模式 ...

  5. 07-THREE.JS 各种形状的几何图形

    <!DOCTYPE html> <html> <head> <title>Example 02.04 - Geometries</title> ...

  6. 2. Spark GraphX解析

    2.1 存储模式 2.1.1 图存储模式 巨型图的存储总体上有边分割和点分割两种存储方式 1)边分割(Edge-Cut):每个顶点都存储一次,但有的边会被打断分到两台机器上.这样做的好处是节省存储空间 ...

  7. Spark GraphX从入门到实战

      第1章 Spark GraphX 概述 1.1 什么是 Spark GraphX   Spark GraphX 是一个分布式图处理框架,它是基于 Spark 平台提供对图计算和图挖掘简洁易用的而丰 ...

  8. arcmap Command

    The information in this document is useful if you are trying to programmatically find a built-in com ...

  9. python查找并删除相同文件-UNIQ File-wxPython-v6

    相比第一版,新增:菜单,对话框,文件过滤器,操作结果保存,配置功能(自己写了一个读写配置文件的功能),提示语优化,模块分化更合理. 截图: 源代码: UniqFile-wxPython-v6.py: ...

随机推荐

  1. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  2. java Map集合类

    ---恢复内容开始--- Map提供了一个更通用的元素存储方法,Map集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值. 了解Map接口和方法 ...

  3. grep命令输出显示高亮字

    grep命令执行后,终端上输出显示颜色可以加"--color=auto"的参数. 另外的两个办法是: 1.设置环境变量: export GREP_OPTIONS="--c ...

  4. Python循环控制

    一.比较符 和算术操作符一样,布尔操作符也有操作顺序.在所有算术和比较操作符求值后,Python 先求值 not 操作符,然后是 and 操作符,然后是 or 操作符. 二.if控制 if name ...

  5. 【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https

    问题描述 在上篇博文"[Azure 应用服务]App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)"中,实现了通过 HT ...

  6. Pytorch入门上 —— Dataset、Tensorboard、Transforms、Dataloader

    本节内容参照小土堆的pytorch入门视频教程.学习时建议多读源码,通过源码中的注释可以快速弄清楚类或函数的作用以及输入输出类型. Dataset 借用Dataset可以快速访问深度学习需要的数据,例 ...

  7. [BUUCTF]PWN——picoctf_2018_rop chain

    picoctf_2018_rop chain 附件 步骤: 例行检查,32位,开启了NX保护 试运行一下程序,看到输入太长数据会崩溃 32位ida载入,习惯性的检索程序里的字符串,看见了flag.tx ...

  8. 用 shell 脚本做自动化测试

    前言 项目中有一个功能,需要监控本地文件系统的变更,例如文件的增.删.改名.文件数据变动等等.之前只在 windows 上有实现,采用的是 iocp + ReadDirectoryChanges 方案 ...

  9. 阿里云ilogtail收集自建Kubernetes容器日志文件

    背景 1,k8s属于自建. 2,需要收集应用服务容器里面指定目录的日志. 3,计划收集所有私有云php和nginx日志. 4,日志格式化处理. 思考 1,一个私有云一个Project,还是统一放入一个 ...

  10. JAVA删除某个文件夹(递归删除文件夹的所有文件)

    /** * 递归删除文件夹下所有内容 最后删除该文件夹 * @param filePath 要删除的文件夹路径 * @return */ public boolean deleteFiles(Stri ...