Chi-Tech
cutmesh_2D_utils.cc
Go to the documentation of this file.
1#include "meshcutting.h"
2
4
5//###################################################################
6/**Makes a unique edge from a regular edge. A unique edge is an edge
7 * with its 1st vertex-id the smallest of the two vertex-ids.*/
9 MakeUniqueEdge(const Edge &edge)
10{
11 return std::make_pair(std::min(edge.first, edge.second),
12 std::max(edge.first, edge.second));
13}
14
15//###################################################################
16/**Make an edge for a polygon given its edge index.*/
17std::pair<uint64_t,uint64_t> chi_mesh::mesh_cutting::
18 MakeEdgeFromPolygonEdgeIndex(const std::vector<uint64_t>& vertex_ids,
19 size_t edge_index)
20{
21 const int e = static_cast<int>(edge_index);
22 const int num_verts = static_cast<int>(vertex_ids.size());
23
24 int next_v = (e < (num_verts-1)) ? e+1 : 0;
25 uint64_t v0_id = vertex_ids[e];
26 uint64_t v1_id = vertex_ids[next_v];
27
28 return std::make_pair(v0_id,v1_id);
29}
30
31//###################################################################
32/**Computes the centroid of an edge.*/
34 GetEdgeCentroid(const Edge &edge,
35 const chi_mesh::MeshContinuum &grid)
36{
37 auto& v0 = grid.vertices[edge.first];
38 auto& v1 = grid.vertices[edge.second];
39
40 return 0.5*(v0 + v1);
41}
42
43//###################################################################
44/***/
47 const std::vector<uint64_t> &vertex_ids,
48 chi_mesh::Cell &cell)
49{
50 cell.faces_.clear();
51 cell.faces_.reserve(vertex_ids.size());
52 cell.vertex_ids_ = vertex_ids;
53
54 cell.centroid_ = chi_mesh::Vector3(0.0, 0.0, 0.0);
55 for (uint64_t vid : cell.vertex_ids_)
56 cell.centroid_ += mesh.vertices[vid];
57 cell.centroid_ /= double(cell.vertex_ids_.size());
58
59 size_t num_verts = vertex_ids.size();
60 for (size_t v=0; v<num_verts; ++v)
61 {
62 size_t v1_ref = (v < (num_verts-1))? v+1 : 0;
63
64 uint64_t v0id = cell.vertex_ids_[v];
65 uint64_t v1id = cell.vertex_ids_[v1_ref];
66
67 const auto& v0 = mesh.vertices[v0id];
68 const auto& v1 = mesh.vertices[v1id];
69
70 chi_mesh::Vector3 v01 = v1 - v0;
71
73 face.vertex_ids_ = {v0id, v1id};
74 face.normal_ = v01.Cross(chi_mesh::Normal(0.0, 0.0, 1.0)).Normalized();
75 face.centroid_ = (v0 + v1) / 2.0;
76
77 cell.faces_.push_back(face);
78 }
79}
80
81//###################################################################
82/**Performs a quality check of a given polygon. The simple quality requirement
83 * is that, if we form a triangle with the cell-centroid and the vertices of
84 * each edge (in ccw orientation), no inverted triangles are present.*/
87 const chi_mesh::Cell &cell,
88 const bool check_convexity/*=false*/)
89{
90 const chi_mesh::Vector3 khat(0.0,0.0,1.0);
91
92 auto& v0 = cell.centroid_;
93 size_t num_edges = cell.vertex_ids_.size();
94
95 for (size_t e=0; e<num_edges; ++e)
96 {
97 auto edge = MakeEdgeFromPolygonEdgeIndex(cell.vertex_ids_, e);
98
99 const auto& v1 = mesh.vertices[edge.first];
100 const auto& v2 = mesh.vertices[edge.second];
101
102 auto v01 = v1-v0;
103 auto v02 = v2-v0;
104
105 if (v01.Cross(v02).Dot(khat)<0.0)
106 return false;
107 }//for edge
108
109 //============================================= Optional convexity check
110 if (check_convexity)
111 {
112
113 }
114
115 return true;
116}
117
Normal normal_
A list of the vertices.
Definition: cell.h:39
std::vector< uint64_t > vertex_ids_
Definition: cell.h:38
Vertex centroid_
The face centroid.
Definition: cell.h:40
std::vector< CellFace > faces_
Definition: cell.h:82
Vertex centroid_
Definition: cell.h:78
std::vector< uint64_t > vertex_ids_
Definition: cell.h:81
VectorN< 3 > Vector3
void PopulatePolygonFromVertices(const MeshContinuum &mesh, const std::vector< uint64_t > &vertex_ids, chi_mesh::Cell &cell)
Edge MakeUniqueEdge(const Edge &edge)
Edge MakeEdgeFromPolygonEdgeIndex(const std::vector< uint64_t > &vertex_ids, size_t edge_index)
bool CheckPolygonQuality(const MeshContinuum &mesh, const chi_mesh::Cell &cell, bool check_convexity=false)
chi_mesh::Vector3 GetEdgeCentroid(const Edge &edge, const chi_mesh::MeshContinuum &grid)
std::pair< uint64_t, uint64_t > Edge
Definition: meshcutting.h:11
Vector3 Cross(const Vector3 &that) const