MeshCGALExtensions.h
Go to the documentation of this file.
1#pragma once
2
3#pragma GCC diagnostic push
4#pragma GCC diagnostic ignored "-Wpedantic"
5#include <CGAL/Polyhedron_3.h>
6#include <CGAL/Surface_mesh/Surface_mesh.h>
7#include <CGAL/boost/graph/iterator.h>
8#pragma GCC diagnostic pop
9
10#include "Mesh.h"
11
12namespace armarx::viz
13{
14 template <class T>
15 inline Mesh&
17 {
18 using sm_t = CGAL::Surface_mesh<T>;
19 using vidx_t = typename sm_t::Vertex_index;
20
21 auto& vertices = data_->vertices;
22 auto& faces = data_->faces;
23 vertices.clear();
24 faces.clear();
25 vertices.reserve(sm.number_of_vertices());
26 faces.reserve(sm.number_of_faces());
27 std::map<vidx_t, std::size_t> index;
28 for (const auto& vidx : sm.vertices())
29 {
30 index[vidx] = vertices.size();
31 const auto& p = sm.point(vidx);
32 armarx::Vector3f visp;
33 visp.e0 = p.x();
34 visp.e1 = p.y();
35 visp.e2 = p.z();
36 vertices.emplace_back(visp);
37 }
38 for (const auto& fidx : sm.faces())
39 {
40 data::Face f;
41 const auto hf = sm.halfedge(fidx);
42 std::size_t i = 0;
43 for (auto hi : CGAL::halfedges_around_face(hf, sm))
44 {
45 const auto vidx = CGAL::target(hi, sm);
46 switch (i++)
47 {
48 case 0:
49 f.v0 = index.at(vidx);
50 break;
51 case 1:
52 f.v1 = index.at(vidx);
53 break;
54 case 2:
55 f.v2 = index.at(vidx);
56 break;
57 default:
58 break; // error handling below
59 }
60 }
61 ARMARX_CHECK_EQUAL(3, i) << "One face is no triangle!";
62 faces.emplace_back(f);
63 }
64 return *this;
65 }
66
67 template <class PolyhedronTraits_3,
68 class PolyhedronItems_3,
69 template <class T, class I, class A>
70 class T_HDS,
71 class Alloc>
72 inline Mesh&
74 {
75 auto& vertices = data_->vertices;
76 auto& faces = data_->faces;
77 vertices.clear();
78 faces.clear();
79 vertices.reserve(p3.size_of_vertices());
80 faces.reserve(p3.size_of_facets());
81 auto vbeg = p3.vertices_begin();
82 for (const auto& v : CGAL::Iterator_range{vbeg, p3.vertices_end()})
83 {
84 const auto& p = v.point();
85 armarx::Vector3f visp;
86 visp.e0 = p.x();
87 visp.e1 = p.y();
88 visp.e2 = p.z();
89 vertices.emplace_back(visp);
90 }
91 for (const auto& fidx : CGAL::Iterator_range{p3.facets_begin(), p3.facets_end()})
92 {
93 auto circ = fidx.facet_begin();
94 ARMARX_CHECK_EQUAL(3, CGAL::circulator_size(circ)) << "One face is no triangle!";
95 data::Face f;
96 f.v0 = std::distance(vbeg, circ->vertex());
97 ++circ;
98 f.v1 = std::distance(vbeg, circ->vertex());
99 ++circ;
100 f.v2 = std::distance(vbeg, circ->vertex());
101 ++circ;
102 ARMARX_CHECK_EXPRESSION(circ == fidx.facet_begin())
103 << "Internal error while circulating a facet";
104 faces.emplace_back(f);
105 }
106 return *this;
107 }
108} // namespace armarx::viz
#define hi(x)
uint8_t index
IceInternal::Handle< data::ElementMesh > data_
Definition ElementOps.h:315
Mesh & vertices(const Eigen::Vector3f *vs, std::size_t size)
Definition Mesh.h:34
Mesh & faces(const data::Face *fs, std::size_t size)
Definition Mesh.h:83
Mesh & mesh(const CGAL::Surface_mesh< T > &sm)
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
This file is part of ArmarX.