CGAL边折叠edge_collapse的问题

使用edge_collapse对一个模型简化,之后回收垃圾,collect_garbage

处理之前的顶点和三角形数量:

number_of_vertices: 955730  number_of_faces: 1903410
num_vertices: 955730  num_faces: 1903410


处理之后的顶点和三角形数量:
number_of_vertices: 479309  number_of_faces: 950633
num_vertices: 479309  num_faces: 950633
可以看到顶点数和三角形数都减少了

然后遍历每个三角形,计算每个点的法线:

for (auto face : mesh.faces()) {

        auto h = mesh.halfedge(face);
        auto v0 = mesh.target(h);
        auto v1 = mesh.target(mesh.next(h));
        auto v2 = mesh.target(mesh.prev(h));

}

奇怪的事情发生了,当读取到ID为5623的面时,其第一个顶点v0 值为v953550,超出了number_of_vertices的大小,引起程序崩溃

测试程序如下:
 


#include <CGAL/Simple_cartesian.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/IO/polygon_mesh_io.h>
#include <CGAL/IO/STL.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_2.h>#include <CGAL/Polygon_mesh_processing/compute_normal.h>#include <CGAL/Aff_transformation_3.h>
#include <CGAL/Aff_transformation_2.h>#include <CGAL/IO/polygon_soup_io.h>
#include <CGAL/Polygon_mesh_processing/repair_polygon_soup.h>#include <CGAL/Filtered_kernel/internal/Static_filters/Is_degenerate_3.h>#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/repair.h>
#include <CGAL/Polygon_mesh_processing/stitch_borders.h>
#include <CGAL/Polygon_mesh_processing/repair_degeneracies.h>
#include <CGAL/Polygon_mesh_processing/remesh.h>#include <CGAL/Surface_mesh_simplification/edge_collapse.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/LindstromTurk_cost.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Edge_count_stop_predicate.h>
#include <CGAL/Surface_mesh_simplification/Edge_collapse_visitor_base.h>#include <CGAL/Simple_cartesian.h>  // 或者你使用的其他内核
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>typedef std::function<void(std::size_t, std::size_t)> _callback;namespace SMS = CGAL::Surface_mesh_simplification;typedef std::function<void(std::size_t, std::size_t)> _callback;typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Exact_predicates_exact_constructions_kernel Exact_kernel;
typedef K::Point_2 Point2;
typedef K::Point_3 Point3;
typedef K::Triangle_3 Triangle3;
typedef K::Vector_3 Vector3;
typedef K::Vector_2 Vector2;
typedef K::Plane_3 Plane3;
typedef CGAL::Bbox_3 Box3;
typedef CGAL::Bbox_2 Box2;
typedef K::Segment_2 Segment2;
typedef K::Segment_3 Segment3;
typedef CGAL::Polygon_2<K> Polygon2;
typedef CGAL::Surface_mesh<Point3> Mesh;
typedef std::shared_ptr<Mesh>	Mesh_ptr;typedef boost::graph_traits<Mesh> GT;
typedef typename GT::face_descriptor face_descriptor;
typedef typename GT::edge_descriptor edge_descriptor;
typedef typename GT::halfedge_descriptor halfedge_descriptor;
typedef typename GT::vertex_descriptor vertex_descriptor;
typedef typename GT::face_iterator face_iterator;
typedef typename GT::edge_iterator edge_iterator;
typedef typename GT::vertex_iterator vertex_iterator;void compute_vertex_normals_manual(const Mesh& mesh,std::vector<Vector3>& vertex_normals) {// 初始化顶点法向量为0int size = mesh.num_vertices();vertex_normals.resize(mesh.num_vertices(), Vector3(0, 0, 0));std::cout << "vertex_normals size " << size << std::endl;// 用于记录每个顶点相邻的面数std::vector<int> vertex_face_count(mesh.num_vertices(), 0);// 遍历所有面片std::ofstream ofs("compute_vertex_normals_manual.txt");ofs << "num_faces " << (int)mesh.num_faces() << std::endl;ofs << "num_vertices " << (int)mesh.num_vertices() << std::endl;for (auto face : mesh.faces()) {ofs << face.id() << std::endl;if (5623 == face.id()) ofs << "a" << std::endl;if (5623 == face.id()) {bool del = mesh.is_removed(face);ofs << "is_removed " << del << std::endl;}// 获取面片的三个顶点auto h = mesh.halfedge(face);if (5623 == face.id()) ofs << "halfedge " << h << std::endl;if (5623 == face.id()) ofs << "halfedge is_removed " << mesh.is_removed(h)<< std::endl;auto v0 = mesh.target(h);auto v1 = mesh.target(mesh.next(h));auto v2 = mesh.target(mesh.prev(h));if (5623 == face.id()) ofs << "mesh.point" << std::endl;if (5623 == face.id()) ofs << v0 << " " << v1 << " " << v2 << std::endl;if (5623 == face.id()) {/*bool del1 = mesh.is_removed(v0);bool del2 = mesh.is_removed(v1);bool del3 = mesh.is_removed(v2);ofs << "is_removed del1 " << del1 << std::endl;ofs << "is_removed del2 " << del2 << std::endl;ofs << "is_removed del3 " << del3 << std::endl;
*/}// 计算面片法向量Point3 p0 = mesh.point(v0);Point3 p1 = mesh.point(v1);Point3 p2 = mesh.point(v2);if (5623 == face.id()) ofs << "p0" << p0.x() << " " << p0.y() << " " << p0.z() << std::endl;if (5623 == face.id()) ofs << "p1" << p1.x() << " " << p1.y() << " " << p1.z() << std::endl;if (5623 == face.id()) ofs << "p2" << p2.x() << " " << p2.y() << " " << p2.z() << std::endl;if (5623 == face.id()) ofs << "c" << std::endl;Vector3 edge1 = p1 - p0;Vector3 edge2 = p2 - p0;Vector3 face_normal = CGAL::cross_product(edge1, edge2);if (5623 == face.id()) ofs << "d" << std::endl;// 归一化面片法向量(可选)double len = CGAL::sqrt(face_normal.squared_length());if (len > 0) face_normal = face_normal / len;if (5623 == face.id()) ofs << "e" << std::endl;// 累加到顶点法向量vertex_normals[v0.idx()] = vertex_normals[v0.idx()] + face_normal;vertex_normals[v1.idx()] = vertex_normals[v1.idx()] + face_normal;vertex_normals[v2.idx()] = vertex_normals[v2.idx()] + face_normal;if (5623 == face.id()) ofs << "f" << std::endl;vertex_face_count[v0.idx()]++;vertex_face_count[v1.idx()]++;vertex_face_count[v2.idx()]++;if (5623 == face.id()) ofs << "g" << std::endl;}// 平均法向量并归一化for (size_t i = 0; i < vertex_normals.size(); ++i) {if (vertex_face_count[i] > 0) {vertex_normals[i] = vertex_normals[i] / vertex_face_count[i];double len = CGAL::sqrt(vertex_normals[i].squared_length());if (len > 0) vertex_normals[i] = vertex_normals[i] / len;}}
}int main()
{typedef CGAL::Point_3<CGAL::Simple_cartesian<float>> STL_point;typedef CGAL::array<int, 3> STL_tringle;std::cout << "begin" << std::endl;std::string file_path = "F:\\WORK\\STL\\Print-test\\test-bug\\1\\Groot_Planter_-_v2Groot_Plante.stl";std::vector<STL_point> points;std::vector<STL_tringle> faces;Mesh_ptr mesh = std::make_shared<Mesh>();//box_top = Box3();try {CGAL::IO::read_polygon_soup(file_path, points, faces);/*STL_FILE::read_STL(_file_path, points, faces, [&](std::size_t a, std::size_t b) {aggregator.updateSectionProgress(a, b);});
*/CGAL::Polygon_mesh_processing::repair_polygon_soup(points, faces);std::cout << "repair_polygon_soup" << std::endl;std::vector <Mesh::vertex_index>vHandles;vHandles.reserve(points.size());mesh->reserve(points.size(), points.size() + faces.size(), faces.size());for (auto& pt : points) {auto vh = mesh->add_vertex(Point3((double)pt[0], (double)pt[1], (double)pt[2]));vHandles.push_back(vh);//box_top += Box3(pt[0], pt[1], pt[2], pt[0], pt[1], pt[2]);}int i = 0;for (auto& tri : faces) {int v0 = tri[0];int v1 = tri[1];int v2 = tri[2];if (v0 == v1 || v1 == v2 || v0 == v2)continue;auto vh0 = vHandles[v0];auto vh1 = vHandles[v1];auto vh2 = vHandles[v2];auto fh = mesh->add_face(vh0, vh1, vh2);if (!fh.is_valid()) {Point3 p0 = mesh->point(vh0);Point3 p1 = mesh->point(vh1);Point3 p2 = mesh->point(vh2);auto vh3 = mesh->add_vertex(p0);auto vh4 = mesh->add_vertex(p1);auto vh5 = mesh->add_vertex(p2);mesh->add_face(vh3, vh4, vh5);}++i;}}catch (const std::exception& e) {std::cerr << "load_STL" << e.what() << std::endl;}Mesh& _mesh = *mesh;size_t total_edges = _mesh.number_of_edges();CGAL::Surface_mesh_simplification::Edge_count_stop_predicate<Mesh> stop(total_edges / 2 - 1); // 目标边数std::cout << "************start***************" << std::endl;//num_vertices 955730 number_of_vertices 479307std::cout << "number_of_vertices: " << _mesh.number_of_vertices() << "  number_of_faces: " << _mesh.number_of_faces() << std::endl;std::cout << "num_vertices: " << _mesh.num_vertices() << "  num_faces: " << _mesh.num_faces() << std::endl;SMS::edge_collapse(_mesh, stop);if (!CGAL::is_valid(_mesh)) {//CGAL::Polygon_mesh_processing::remove_degenerate_faces(_mesh);//CGAL::Polygon_mesh_processing::stitch_borders(_mesh);}std::cout << "************edge_collapse***************"   << std::endl;//num_vertices 955730 number_of_vertices 479307std::cout << "number_of_vertices: " << _mesh.number_of_vertices() << "  number_of_faces: " << _mesh.number_of_faces() << std::endl;std::cout << "num_vertices: " << _mesh.num_vertices() << "  num_faces: " << _mesh.num_faces() << std::endl;_mesh.collect_garbage();//_mesh.collect_garbage();std::cout << "************collect_garbage***************" << std::endl;//num_vertices 955730 number_of_vertices 479307std::cout << "number_of_vertices: " << _mesh.number_of_vertices() << "  number_of_faces: " << _mesh.number_of_faces() << std::endl;std::cout << "num_vertices: " << _mesh.num_vertices() << "  num_faces: " << _mesh.num_faces() << std::endl;////auto vertex_normals = _mesh.add_property_map<vertex_descriptor, Vector3>("v:normal").first;//CGAL::Polygon_mesh_processing::compute_vertex_normals(_mesh, vertex_normals);//std::vector<Vector3> vertex_normals;compute_vertex_normals_manual(_mesh, vertex_normals);std::cout << "compute_vertex_normals" << std::endl;
}

日志:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/77450.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用c语言实现——顺序队列支持用户输入交互、入队、出队、查找、遍历、计算队列长度等功能。确定判断判满的方法为:牺牲一个存储单元方式

一、知识介绍 1.基本原理 在顺序队列中&#xff0c;我们使用一个固定大小的数组来存储队列中的元素&#xff0c;并使用两个指针&#xff08;front 和 rear&#xff09;来分别表示队头和队尾的位置。 队列为空的条件&#xff1a;front rear 队列满的条件&#xff1a;rear 1…

JVM 系列:JVM 内存结构深度解析

你点赞了吗&#xff1f;你关注了吗&#xff1f;每天分享干货好文。 高并发解决方案与架构设计。 海量数据存储和性能优化。 通用框架/组件设计与封装。 如何设计合适的技术架构&#xff1f; 如何成功转型架构设计与技术管理&#xff1f; 在竞争激烈的大环境下&#xff0c…

手机上的APN是什么,该怎么设置

网上说改个APN就可以让网速快几倍&#xff0c;那到底APN是个什么东西&#xff0c;真的能让网速快几倍吗&#xff1f; APN的作用 网络连接基础&#xff1a;APN&#xff08;接入点名称&#xff09;是手机连接移动网络的“桥梁”&#xff0c;负责识别运营商网络类型&#xff08;…

微服务治理与可观测性

服务注册与发现 核心功能 服务实例动态变化&#xff1a;实例可能因扩缩容、故障或迁移导致IP变动。服务依赖解耦&#xff1a;调用方无需硬编码服务地址&#xff0c;降低耦合度。负载均衡&#xff1a;自动选择健康实例&#xff0c;提升系统可用性。 核心组件 服务注册中心&am…

嵌入式linux系统中内存管理的方法与实现

第一:linux内核管理详解图形 第二:Linux内存管理详细分析 深入剖析Linux内核内存管理 作为嵌入式系统开发者,理解Linux内核的内存管理对于开发高效、稳定的系统至关重要。在这篇文章中,我们将详细解析Linux内核如何划分物理内存和虚拟内存,页表、MMU(内存管理单元)与TL…

【dataframe显示不全问题】打开一个行列超多的excel转成df之后行列显示不全

出现问题如下图&#xff1a; 解决方案&#xff5e; display.width解决列显示不全 pd.set_option(display.max_columns,1000) pd.set_option(display.width, 1000) pd.set_option(display.max_colwidth,1000) pd.set_option(display.max_rows,1000)

Linux——Shell编程之正则表达式与文本处理器(笔记)

目录 基础正则表达式 1:基础正则表达式示例 &#xff08;4&#xff09;查找任意一个字符“.”与重新字符“*” &#xff08;5&#xff09;查找连续字符范围“{ }” 文本处理器 一、sed工具 二、awk工具 &#xff08;1&#xff09;按行输出文本 &#xff08;2&#xff0…

OpenHarmony系统-源码下载,环境搭建,编译,烧录,调试

获取源码 以OpenHarmony5.0.3为例 repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-5.0.3-Release --no-repo-verify repo sync -c repo forall -c git lfs pull搭建环境 安装必要的工具和命令 apt-get install -y apt-utils binutils bison flex bc …

Vue3 本地打包启动白屏解决思路!! !

“为什么我访问 http://127.0.0.1:5501/index.html 白屏&#xff0c;删了 index.html 再访问 / 就又活过来了&#xff1f;” —— 你的项目与 SPA 路由的“宫斗大戏” 一、问题复现 场景 本地通过 VSCode Live Server&#xff08;或其他静态服务器&#xff09;启动了打包后的 V…

数字人(2):数字人技术全景透视(2025演进版)

随着人工智能技术的迅猛发展,数字人技术发展也是一日千里。站在当下,着眼未来,我们一起在回眸透视过去的基础上,一起共同眺望数字人技术的未来。 一、数字人技术体系重构 我们可以用三维定义对数字人技术进行框架重构 维度 技术内涵 典型特征 物理层 人体数字化建模技术 …

小刚说C语言刷题——1035 判断成绩等级

1.题目描述 输入某学生成绩&#xff0c;如果 86分以上(包括 86分&#xff09;则输出 VERY GOOD &#xff0c;如果在 60到 85之间的则输出 GOOD (包括 60和 85)&#xff0c;小于 60 的则输出 BAD。 输入 输入只有一行&#xff0c;包括 1个整数。 输出 输出只有一行&#xf…

React-在使用map循环数组渲染列表时须指定唯一且稳定值的key

在渲染列表的时候&#xff0c;我们须给组件或者元素分配一个唯一值的key, key是一个特殊的属性&#xff0c;不会最终加在元素上面&#xff0c;也无法通过props.key来获取&#xff0c;仅在react内部使用。react中的key本质是服务于diff算法, 它的默认值是null, 在diff算法过程中…

Zookeeper的通知机制是什么?

大家好&#xff0c;我是锋哥。今天分享关于【Zookeeper的通知机制是什么&#xff1f;】面试题。希望对大家有帮助&#xff1b; Zookeeper的通知机制是什么&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 Zookeeper 的通知机制是其核心特性之一&#xf…

【LangChain实战】构建下一代智能问答系统:从RAG架构到生产级优化

打破传统问答系统的次元壁 当ChatGPT在2022年掀起AI革命时&#xff0c;开发者们很快发现一个残酷现实&#xff1a;通用大模型在专业领域的表现如同拿着地图的盲人&#xff0c;既无法理解企业私有数据&#xff0c;也无法保证事实准确性。这催生了RAG&#xff08;检索增强生成&a…

UDS中功能寻址可以请求多帧数据嘛?当ECU响应首帧后,诊断仪是通过物理寻址发送流控帧嘛?

文章目录 1. 前言📢1.1 功能寻址是否支持请求多帧数据?1.2 ECU发送首帧(FF)后,诊断仪如何发送流控帧(FC)?1.3 协议依据(ISO 14229-1)1.4 实际应用注意事项总结1. 前言📢 在UDS(Unified Diagnostic Services)协议中,功能寻址与物理寻址的使用规则以及多帧数据传…

PHP异常处理__Throwable

在 PHP 里&#xff0c;Throwable 是一个极为关键的接口&#xff0c;自 PHP 7 起被引入。它为错误和异常处理构建了一个统一的框架。下面会详细介绍 Throwable 的相关内容。 1. 基本概念 Throwable 是 Exception 和 Error 的父接口。在 PHP 7 之前&#xff0c;异常&#xff08…

无需训练的具身导航探索!TRAVEL:零样本视觉语言导航中的检索与对齐

作者&#xff1a; Navid Rajabi, Jana Kosecka 单位&#xff1a;乔治梅森大学计算机科学系 论文标题&#xff1a;TRAVEL: Training-Free Retrieval and Alignment for Vision-and-Language Navigation 论文链接&#xff1a;https://arxiv.org/pdf/2502.07306 主要贡献 提出…

Vue3+Vite+TypeScript+Element Plus开发-22.客制Table组件

系列文档目录 Vue3ViteTypeScript安装 Element Plus安装与配置 主页设计与router配置 静态菜单设计 Pinia引入 Header响应式菜单缩展 Mockjs引用与Axios封装 登录设计 登录成功跳转主页 多用户动态加载菜单 Pinia持久化 动态路由 -动态增加路由 动态路由-动态删除…

Java读取JSON文件并将其中元素转为JSON对象输出

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Java读取JSON文件并将其中元素转为JSON对象输…

Spring Boot自动配置原理深度解析:从条件注解到spring.factories

大家好&#xff01;今天我们来深入探讨Spring Boot最神奇的特性之一——自动配置(Auto-configuration)。这个功能让Spring Boot如此受欢迎&#xff0c;因为它大大简化了我们的开发工作。让我们一起来揭开它的神秘面纱吧&#xff01;&#x1f440; &#x1f31f; 什么是自动配置…