VTK Learning Thirty - vtkPolyData to CGAL Surface_mesh

Description

vtkPolyData转换成CGAL::Surface_mesh,并利用CGAL的布尔运算对数据进行处理,
然后再将处理后的CGAL::Surface_mesh转换成vtkPolyData进行可视化渲染。

定义 CGAL::Surface_mesh


typedef CGAL::Exact_predicates_inexact_constructions_kernel EPICK;
typedef EPICK::Point_3 Point_3;
typedef CGAL::Surface_mesh<Point_3> SMesh;
typedef boost::property_traits<boost::property_map<SMesh,CGAL::vertex_point_t>::type>::value_type Point;

vtkPolyData to CGAL::Surface_mesh


bool vtkPointSet_to_polygon_mesh(vtkPointSet *poly_data, SMesh &tmesh)
{typedef typename boost::property_map<SMesh, CGAL::vertex_point_t>::type VPMap;typedef typename boost::property_map_value<SMesh, CGAL::vertex_point_t>::type Point_3;typedef typename boost::graph_traits<SMesh>::vertex_descriptor vertex_descriptor;VPMap vpmap = get(CGAL::vertex_point, tmesh);// get nb of points and cellsvtkIdType nb_points = poly_data->GetNumberOfPoints();vtkIdType nb_cells = poly_data->GetNumberOfCells();//extract pointsstd::vector<vertex_descriptor> vertex_map(nb_points);for (vtkIdType i = 0; i<nb_points; ++i){double coords[3];poly_data->GetPoint(i, coords);vertex_descriptor v = CGAL::add_vertex(tmesh);put(vpmap, v, Point_3(coords[0], coords[1], coords[2]));vertex_map[i]=v;}for (vtkIdType i = 0; i<nb_cells; ++i){if(poly_data->GetCellType(i) != 5&& poly_data->GetCellType(i) != 7&& poly_data->GetCellType(i) != 9) //only supported cells are triangles, quads and polygonscontinue;vtkCell* cell_ptr = poly_data->GetCell(i);vtkIdType nb_vertices = cell_ptr->GetNumberOfPoints();if (nb_vertices < 3)return false;std::vector<vertex_descriptor> vr(nb_vertices);for (vtkIdType k=0; k<nb_vertices; ++k)vr[k]=vertex_map[cell_ptr->GetPointId(k)];CGAL::Euler::add_face(vr, tmesh);}return true;
}

CGAL::Surface_mesh to vtkPolyData

首先,转换成vtkUnstructuredGrid格式,然后再转成vtkPolyData


vtkSmartPointer<vtkUnstructuredGrid> polygon_mesh_to_vtkUnstructured(const SMesh &pmesh)
{typedef typename boost::graph_traits<SMesh>::vertex_descriptor   vertex_descriptor;typedef typename boost::graph_traits<SMesh>::face_descriptor     face_descriptor;typedef typename boost::graph_traits<SMesh>::halfedge_descriptor halfedge_descriptor;typedef typename boost::property_map<SMesh, CGAL::vertex_point_t>::const_type VPMap;typedef typename boost::property_map_value<SMesh, CGAL::vertex_point_t>::type Point_3;VPMap vpmap = get(CGAL::vertex_point, pmesh);vtkPoints* const vtk_points = vtkPoints::New();vtkCellArray* const vtk_cells = vtkCellArray::New();vtk_points->Allocate(CGAL::num_vertices(pmesh));vtk_cells->Allocate(CGAL::num_faces(pmesh));std::map<vertex_descriptor, vtkIdType> Vids;vtkIdType inum = 0;for(vertex_descriptor v : CGAL::vertices(pmesh)){const Point_3& p = get(vpmap, v);vtk_points->InsertNextPoint(CGAL::to_double(p.x()),CGAL::to_double(p.y()),CGAL::to_double(p.z()));Vids[v] = inum++;}for(face_descriptor f : CGAL::faces(pmesh)){vtkIdList* cell = vtkIdList::New();for(halfedge_descriptor h :CGAL::halfedges_around_face(CGAL::halfedge(f, pmesh), pmesh)){cell->InsertNextId(Vids[CGAL::target(h, pmesh)]);}vtk_cells->InsertNextCell(cell);cell->Delete();}vtkSmartPointer<vtkUnstructuredGrid> usg =vtkSmartPointer<vtkUnstructuredGrid>::New();usg->SetPoints(vtk_points);vtk_points->Delete();usg->SetCells(5,vtk_cells);vtk_cells->Delete();return usg;
}
// vtkUnstructuredGrid->vtkPolyData
vtkSmartPointer<vtkPolyData> ugToPolyData(vtkSmartPointer<vtkUnstructuredGrid> ug)
{vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter =vtkSmartPointer<vtkDataSetSurfaceFilter>::New();surfaceFilter->SetInputData(ug);surfaceFilter->Update();return surfaceFilter->GetOutput();
}

利用CGAL进行Mesh的布尔运算

//! op
//! enum bool_op {CRF_UNION(并), CRF_INTER(交), CRF_MINUS(差), CRF_MINUS_OP(差或)};
//!@return 布尔运算的结果MeshSMesh *apply_corefine_and_bool_op(SMesh *oneMesh, SMesh *twoMesh, bool_op op)
{if(! CGAL::is_triangle_mesh(*oneMesh)) {return nullptr;}if(! CGAL::is_triangle_mesh(*twoMesh)) {return nullptr;}SMesh* new_poly = new SMesh();QString str_op;SMesh P, Q;switch(op){case CRF_UNION:P = *oneMesh, Q = *twoMesh;if (! PMP::corefine_and_compute_union(P, Q, *new_poly, params::throw_on_self_intersection(true)) ){delete new_poly;return nullptr;}str_op = "Union";break;case CRF_INTER:P = *oneMesh, Q = *twoMesh;if (! PMP::corefine_and_compute_intersection(P, Q, *new_poly, params::throw_on_self_intersection(true)) ){delete new_poly;return nullptr;}str_op = "Intersection";break;case CRF_MINUS_OP:std::swap(oneMesh, twoMesh);CGAL_FALLTHROUGH;case CRF_MINUS:P = *oneMesh, Q = *twoMesh;if (! PMP::corefine_and_compute_difference(P, Q, *new_poly, params::throw_on_self_intersection(true)) ){delete new_poly;return nullptr;}str_op = "Difference";}return new_poly;
}

Usage

    SMesh* meshOne = new SMesh();vtkPointSet_to_polygon_mesh(one,*meshOne);SMesh* meshTwo = new SMesh();vtkPointSet_to_polygon_mesh(two,*meshTwo);SMesh* meshThree=apply_corefine_and_bool_op(meshOne,meshTwo,op);vtkSmartPointer<vtkUnstructuredGrid>ug=polygon_mesh_to_vtkUnstructured(*meshThree);vtkSmartPointer<vtkPolyData>outPolyData= ugToPolyData(ug);delete meshOne;delete meshTwo;delete meshThree;

Result


VTK Learning Thirty - vtkPolyData to CGAL Surface_mesh相关推荐

  1. VTK Learning Thirteen - VTK Label Three

    VTK Learning Thirteen - VTK Label Three Description 使用vtkLabeledDataMapper显示属性标签.相关类vtkIdFilter和vtkC ...

  2. VTK:使用 vtkPolyData 结构为单元格分配颜色查找表用法实战

    VTK:使用 vtkPolyData 结构为单元格分配颜色查找表用法实战 程序输出 程序完整源代码 程序输出 程序完整源代码 #include <vtkActor.h> #include ...

  3. 【CGAL】表面细化

    [CGAL ]表面细化 cgla提供了四种细化方式,这是第一天测试结果,明天四种都测试完在更新这一块,这是其中一种细化方法 // 01frame includes #include "cga ...

  4. VTK修炼之道56:图形基本操作进阶_表面重建技术(三维点云曲面重建)

    1.点云重建 虽然Delaunay三角剖分算法可以实现网格曲面重建,但是其应用主要在二维剖分,在三维空间网格生成中遇到了问题.因为在三维点云曲面重建中,Delaunay条件不在满足,不仅基于最大最小角 ...

  5. VTK修炼之道53:图形基本操作进阶_多分辨率策略(模型细化的三种方法)

    1.模型细化 vtk中实现网格细化的累有vtkLinearSubdivisionFilter.vtkLoopsubdivisionFilter.vtkButterflySubdivisionFilte ...

  6. VTK修炼之道52:图形基本操作进阶_多分辨率策略(模型抽取的三种方法)

    1.多分辨率处理策略 模型抽取(Decimation)和细化(Subdivision)是两个相反的操作,是三角形网格模型多分辨处理中的两个重要操作.使用这两个操作可以在保持模型拓扑结构的同时,得到不同 ...

  7. 三维数据平滑处理_VTK图像处理(二)--vtkPolyData数据处理

    前言 vtkPolyData数据是一种广泛使用的vtk数据结构,可以用来表示很多常用的数据结构,如点云数据.面片模型等.本文章先分析vtkPolyData数据的基本组成,创建方法和显示管线,结果介绍了 ...

  8. CGAL例程:地理信息系统----点云数据生成DSM、DTM、等高线和数据分类

    作者:西蒙·吉罗多 链接:CGAL 5.4 - Manual: GIS (Geographic Information System) 目录 1 概述 2 不规则三角形网数据表示:TIN 3 数字表面 ...

  9. VTK:对输入的三维模型在某个方向等间距提取模型的切面轮廓线

    测试的三维模型为bunny,即斯坦福兔子 代码示例: #include <vtkOBJReader.h> #include <vtkSmartPointer.h> #inclu ...

  10. CGAL表面网格降采样

    CGAL表面网格降采样 1. CGAL Surface_mesh_simplification模块 2. 源为stl格式数据 1. CGAL Surface_mesh_simplification模块 ...

最新文章

  1. 静态路由和默认路由的配置实例
  2. CTO集体怒吼:我到底要不要继续写代码(上篇)
  3. Android实践 -- 监听应用程序的安装、卸载
  4. 数据中心网络架构 — 云数据中心网络 — 二层架构设计示例
  5. IPC 之 Binder 初识
  6. c语言在管理系统中的应用,C语言应用——学生管理系统的制作
  7. php mongodb 别名,PHP mongo与mongodb扩展 | 码路春哥
  8. configParser模块详谈
  9. html表单实验总结,HTML表单总结
  10. linux iptables_linux 开启独立iptables日志
  11. KDD Cup 2020多模态召回比赛亚军方案与搜索推荐业务的业务应用
  12. Mac上有什么实用的必备软件?
  13. oracle建表语句6,Oracle建表语句
  14. 樱桃键盘驱动linux,樱桃键盘mx board9.0驱动
  15. 数据库中索引原理及填充因子
  16. 表达式的LenB(123程序设计ABC)的值是
  17. 设计一个权限系统-RBAC
  18. 前端一(HTML、CSS)
  19. 笔记-Codeforces比赛
  20. uCLinux LINUX区别

热门文章

  1. 如何用ps制作GIF动图
  2. php word 生成图片,PHP导出成word,带图片样式
  3. 凡泰极客:远程办公,你礼貌吗?
  4. 登高,A股想过重阳节,但是令人遗憾的是,遍插茱萸少一人啊
  5. 用python来开发webgame服务端(1)
  6. win10 +cude9.2+相匹配的cudnn+相匹配的tensorflow+ssd学习之路(问题百出1)
  7. 腾讯云内容分发网络 CDN 产品认证课程笔记(二)——腾讯云CDN介绍
  8. 心理学第二周学习笔记:心理学的历史和流派
  9. unix bsd linux shell bash GNU之间的联系,歪讲Linux(一)
  10. linux 10247 java_linux kubernetes