Chi-Tech
SnapToPlaneMeshModifier.cc
Go to the documentation of this file.
2
3#include "ChiObjectFactory.h"
4
5#include "chi_runtime.h"
6#include "chi_mpi.h"
7#include "chi_log.h"
8
11
12namespace chi_mesh
13{
14
16
18{
20
22 "Modifier that will snap nodes, that are within a tolerated distance of a "
23 "plane, to the plane. This modifier is useful for straitening a boundary "
24 "edge or aligning vertices that misaligned during meshing.");
25 params.SetDocGroup("DocMeshModifiers");
26
28 "normal", "The normal of the plane to which the nodes are to be snapped.");
29
30 params.AddRequiredParameterArray("point", "The anchor point of the plane.");
31
33 "boundaries_only",
34 true,
35 "If set to true, only boundary nodes will be snapped.");
36
38 "check_face_alignment",
39 false,
40 "If set, only faces that match the plane normal (within tolerance) will "
41 "have their nodes snapped.");
42
44 "tolerance",
45 1.0e-5,
46 "Tolerance per dimension within which a face/edge will be aligned");
47
48 return params;
49}
50
52 const chi::InputParameters& params)
53 : MeshModifier(params),
54 normal_(params.GetParamVectorValue<double>("normal")),
55 point_(params.GetParamVectorValue<double>("point")),
56 boundary_nodes_only_(params.GetParamValue<bool>("boundaries_only")),
57 check_face_alignment_(params.GetParamValue<bool>("check_face_alignment")),
58 tol_(params.GetParamValue<double>("tolerance"))
59{
60 ChiLogicalErrorIf(Chi::mpi.process_count != 1,
61 "Cannot only be used in serial");
62}
63
65{
66 auto& grid = *chi_mesh::GetCurrentHandler().GetGrid();
67
68 std::vector<uint64_t> snapped_vertex_ids;
69 std::set<uint64_t> cell_ids_modified;
70
72 for (const auto& cell : grid.local_cells)
73 {
74 for (const auto& face : cell.faces_)
75 {
76 if (boundary_nodes_only_ and face.has_neighbor_) continue;
77
78 bool matches = true;
79 for (size_t d = 0; d < 3; ++d)
80 if (std::fabs(face.normal_[d] - normal_[d]) >= tol_) matches = false;
81
82 if (matches)
83 for (uint64_t vid : face.vertex_ids_)
84 {
85 const double d = (grid.vertices[vid] - point_).Dot(normal_);
86
87 if (std::fabs(d) < tol_)
88 {
89 grid.vertices[vid] -= d * normal_;
90 snapped_vertex_ids.push_back(vid);
91 cell_ids_modified.insert(cell.local_id_);
92 }
93 }
94 } // for face
95 }//for cell
96 else
97 for (const auto& cell : grid.local_cells)
98 {
99 for (const uint64_t vid : cell.vertex_ids_)
100 {
101 const double d = (grid.vertices[vid] - point_).Dot(normal_);
102
103 if (std::fabs(d) < tol_)
104 {
105 grid.vertices[vid] -= d * normal_;
106 snapped_vertex_ids.push_back(vid);
107 cell_ids_modified.insert(cell.local_id_);
108 }
109 }
110 } // for cell
111
112 // Modifying cells
113 for (const uint64_t cell_local_id : cell_ids_modified)
114 {
115 grid.local_cells[cell_local_id].RecomputeCentroidsAndNormals(grid);
116 //for (const auto& face : grid.local_cells[cell_local_id_].faces_)
117 // chi::log.Log() << face.normal_.PrintStr();
118 }
119
120 Chi::log.Log0Verbose1() << "Number of cells modified "
121 << cell_ids_modified.size();
122}
123
124} // namespace chi_mesh
#define ChiLogicalErrorIf(condition, message)
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
static chi::InputParameters GetInputParameters()
Definition: ChiObject.cc:4
LogStream Log0Verbose1()
Definition: chi_log.h:234
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameterArray(const std::string &name, const std::string &doc_string)
void AddOptionalParameter(const std::string &name, T value, const std::string &doc_string)
void SetGeneralDescription(const std::string &description)
chi_mesh::MeshContinuumPtr & GetGrid() const
static chi::InputParameters GetInputParameters()
SnapToPlaneMeshModifier(const chi::InputParameters &params)
double Dot(const VecDbl &x, const VecDbl &y)
RegisterChiObject(chi_mesh, BooleanLogicalVolume)
MeshHandler & GetCurrentHandler()