17#include <lagrange/MeshGeometry.h>
18#include <lagrange/utils/assert.h>
21template <
typename GeometryType>
25 using Index =
typename GeometryType::Index;
26 using IndexList = std::vector<Index>;
27 using AdjacencyList = std::vector<IndexList>;
30 : m_initialized(
false)
33 void initialize(
const GeometryType& geometry)
39 auto remove_duplicate_entries = [](IndexList& arr) {
40 std::sort(arr.begin(), arr.end());
41 const auto end_itr = std::unique(arr.begin(), arr.end());
42 arr.resize(std::distance(arr.begin(), end_itr));
45 auto extract_duplicate_entries = [](IndexList& arr) {
46 std::sort(arr.begin(), arr.end());
47 IndexList duplicate_entries;
48 Index curr = arr.back() + 1;
49 for (
const auto& item : arr) {
51 (duplicate_entries.empty() || item != duplicate_entries.back())) {
52 duplicate_entries.push_back(item);
57 arr.swap(duplicate_entries);
60 const Index num_vertices = geometry.get_num_vertices();
61 const Index num_facets = geometry.get_num_facets();
62 const Index vertex_per_facet = geometry.get_vertex_per_facet();
63 m_v2v.resize(num_vertices);
64 m_v2f.resize(num_vertices);
65 m_f2f.resize(num_facets);
66 for (
auto& arr : m_v2v) arr.reserve(16);
67 for (
auto& arr : m_v2f) arr.reserve(16);
68 for (
auto& arr : m_f2f) arr.reserve(16);
70 const auto& facets = geometry.get_facets();
72 for (Index i = 0; i < num_facets; i++) {
73 for (Index j = 0; j < vertex_per_facet; j++) {
74 Index curr = facets(i, j);
75 Index next = facets(i, (j + 1) % vertex_per_facet);
76 Index prev = facets(i, (j + vertex_per_facet - 1) % vertex_per_facet);
77 m_v2v[curr].push_back(next);
78 m_v2v[curr].push_back(prev);
79 m_v2f[curr].push_back(i);
82 std::for_each(m_v2v.begin(), m_v2v.end(), remove_duplicate_entries);
83 std::for_each(m_v2f.begin(), m_v2f.end(), remove_duplicate_entries);
85 for (Index i = 0; i < num_facets; i++) {
86 for (Index j = 0; j < vertex_per_facet; j++) {
87 Index vi = facets(i, j);
88 const auto& adj_facets = m_v2f[vi];
89 m_f2f[i].insert(m_f2f[i].end(), adj_facets.begin(), adj_facets.end());
92 std::for_each(m_f2f.begin(), m_f2f.end(), extract_duplicate_entries);
95 for (Index i = 0; i < num_facets; i++) {
96 const auto itr = std::find(m_f2f[i].begin(), m_f2f[i].end(), i);
101 m_initialized =
true;
104 bool is_initialized()
const {
return m_initialized; }
105 const AdjacencyList& get_vertex_vertex_adjacency()
const {
return m_v2v; }
106 const AdjacencyList& get_vertex_facet_adjacency()
const {
return m_v2f; }
107 const AdjacencyList& get_facet_facet_adjacency()
const {
return m_f2f; }
108 const IndexList& get_vertices_adjacent_to_vertex(Index vi)
const {
return m_v2v[vi]; }
109 const IndexList& get_facets_adjacent_to_vertex(Index vi)
const {
return m_v2f[vi]; }
110 const IndexList& get_facets_adjacent_to_facet(Index fi)
const {
return m_f2f[fi]; }
119template <
typename GeometryType>
120std::unique_ptr<Connectivity<GeometryType>> compute_connectivity(
const GeometryType& geometry)
122 auto ptr = std::make_unique<Connectivity<GeometryType>>();
124 ptr->initialize(geometry);
Definition: Connectivity.h:23
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30