OGS
removeMeshElements.cpp File Reference

Detailed Description

Remove mesh elements.

Author
Norihiro Watanabe
Date
2013/10/15

Definition in file removeMeshElements.cpp.

Include dependency graph for removeMeshElements.cpp:

Go to the source code of this file.

Functions

template<typename PROPERTY_TYPE >
void searchByPropertyValue (std::string const &property_name, std::vector< PROPERTY_TYPE > const &property_values, MeshLib::ElementSearch &searcher)
 
void searchByPropertyRange (std::string const &property_name, double const &min_value, double const &max_value, bool const &outside, MeshLib::ElementSearch &searcher)
 
void outputAABB (MeshLib::Mesh const &mesh)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 82 of file removeMeshElements.cpp.

83{
84 TCLAP::CmdLine cmd(
85 "Removes mesh elements based on element type, element volume, scalar "
86 "arrays, or bounding box . The documentation is available at "
87 "https://docs.opengeosys.org/docs/tools/meshing/"
88 "remove-mesh-elements.\n\n"
89 "OpenGeoSys-6 software, version " +
91 ".\n"
92 "Copyright (c) 2012-2025, OpenGeoSys Community "
93 "(http://www.opengeosys.org)",
95
96 // Bounding box params
97 TCLAP::SwitchArg invert_bounding_box_arg(
98 "", "invert", "inverts the specified bounding box", false);
99 cmd.add(invert_bounding_box_arg);
100 TCLAP::ValueArg<double> zLargeArg(
101 "", "z-max", "largest allowed extent in z-dimension", false,
102 std::numeric_limits<double>::max(), "MAX_EXTENT_Z");
103 cmd.add(zLargeArg);
104 TCLAP::ValueArg<double> zSmallArg(
105 "", "z-min", "smallest allowed extent in z-dimension", false,
106 -1 * std::numeric_limits<double>::max(), "MIN_EXTENT_Z");
107 cmd.add(zSmallArg);
108 TCLAP::ValueArg<double> yLargeArg(
109 "", "y-max", "largest allowed extent in y-dimension", false,
110 std::numeric_limits<double>::max(), "MAX_EXTENT_Y");
111 cmd.add(yLargeArg);
112 TCLAP::ValueArg<double> ySmallArg(
113 "", "y-min", "smallest allowed extent in y-dimension", false,
114 -1 * std::numeric_limits<double>::max(), "MIN_EXTENT_Y");
115 cmd.add(ySmallArg);
116 TCLAP::ValueArg<double> xLargeArg(
117 "", "x-max", "largest allowed extent in x-dimension", false,
118 std::numeric_limits<double>::max(), "MAX_EXTENT_X");
119 cmd.add(xLargeArg);
120 TCLAP::ValueArg<double> xSmallArg(
121 "", "x-min", "smallest allowed extent in x-dimension", false,
122 -1 * std::numeric_limits<double>::max(), "MIN_EXTENT_X");
123 cmd.add(xSmallArg);
124
125 // Non-bounding-box params
126 TCLAP::SwitchArg zveArg("z", "zero-volume", "remove zero volume elements",
127 false);
128 cmd.add(zveArg);
129
130 std::vector<std::string> allowed_ele_types{
131 "point", "line", "tri", "quad", "hex", "prism", "tet", "pyramid"};
132 TCLAP::ValuesConstraint<std::string> allowedVals{allowed_ele_types};
133 TCLAP::MultiArg<std::string> eleTypeArg(
134 "t", "element-type", "element type to be removed", false, &allowedVals);
135 cmd.add(eleTypeArg);
136
137 // scalar array params
138 TCLAP::ValueArg<std::string> property_name_arg(
139 "n", "property-name", "name of property in the mesh", false,
140 "MaterialIDs", "PROPERTY_NAME");
141 cmd.add(property_name_arg);
142
143 TCLAP::MultiArg<int> property_arg(
144 "", "property-value", "value of selected property to be removed", false,
145 "PROPERTY_VALUE");
146 cmd.add(property_arg);
147
148 TCLAP::ValueArg<double> min_property_arg(
149 "", "min-value", "minimum value of range for selected property", false,
150 0, "MIN_RANGE");
151 cmd.add(min_property_arg);
152
153 TCLAP::ValueArg<double> max_property_arg(
154 "", "max-value", "maximum value of range for selected property", false,
155 0, "MAX_RANGE");
156 cmd.add(max_property_arg);
157
158 TCLAP::SwitchArg outside_property_arg(
159 "", "outside", "remove all elements outside the given property range");
160 cmd.add(outside_property_arg);
161
162 TCLAP::SwitchArg inside_property_arg(
163 "", "inside", "remove all elements inside the given property range");
164 cmd.add(inside_property_arg);
165
166 // I/O params
167 TCLAP::ValueArg<std::string> mesh_out(
168 "o", "mesh-output-file",
169 "Output (.vtu). The name of the file the mesh will be written to", true,
170 "", "OUTPUT_FILE");
171 cmd.add(mesh_out);
172 TCLAP::ValueArg<std::string> mesh_in(
173 "i", "mesh-input-file",
174 "Input (.vtu). The name of the file containing the input mesh", true,
175 "", "INPUT_FILE");
176 cmd.add(mesh_in);
177 cmd.parse(argc, argv);
178
179 BaseLib::MPI::Setup mpi_setup(argc, argv);
180
181 std::unique_ptr<MeshLib::Mesh const> mesh(
182 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
183 if (mesh == nullptr)
184 {
185 return EXIT_FAILURE;
186 }
187
188 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
189 mesh->getNumberOfElements());
190 MeshLib::ElementSearch searcher(*mesh);
191
192 // search elements IDs to be removed
193 if (zveArg.isSet())
194 {
195 INFO("{:d} zero volume elements found.", searcher.searchByContent());
196 }
197 if (eleTypeArg.isSet())
198 {
199 const std::vector<std::string> eleTypeNames = eleTypeArg.getValue();
200 for (const auto& typeName : eleTypeNames)
201 {
202 const MeshLib::MeshElemType type =
205 {
206 continue;
207 }
208 INFO("{:d} {:s} elements found.",
209 searcher.searchByElementType(type), typeName);
210 }
211 }
212
213 if (property_name_arg.isSet() || property_arg.isSet() ||
214 min_property_arg.isSet() || max_property_arg.isSet())
215 {
216 if ((property_arg.isSet() || min_property_arg.isSet() ||
217 max_property_arg.isSet()) &&
218 !property_name_arg.isSet())
219 {
220 ERR("Specify a property name for the value/range selected.");
221 return EXIT_FAILURE;
222 }
223
224 if (property_name_arg.isSet() &&
225 !((min_property_arg.isSet() && max_property_arg.isSet()) ||
226 property_arg.isSet()))
227 {
228 ERR("Specify a value or range ('-min-value' and '-max_value') for "
229 "the property selected.");
230 return EXIT_FAILURE;
231 }
232
233 // name + value
234 if (property_arg.isSet() && property_name_arg.isSet())
235 {
236 searchByPropertyValue(property_name_arg.getValue(),
237 property_arg.getValue(), searcher);
238 }
239
240 // name + range
241 if (property_name_arg.isSet() && min_property_arg.isSet() &&
242 max_property_arg.isSet())
243 {
244 if ((!outside_property_arg.isSet() &&
245 !inside_property_arg.isSet()) ||
246 (outside_property_arg.isSet() && inside_property_arg.isSet()))
247 {
248 ERR("Specify if the inside or the outside of the selected "
249 "range should be removed.");
250 return EXIT_FAILURE;
251 }
252
253 bool const outside = outside_property_arg.isSet();
255 property_name_arg.getValue(), min_property_arg.getValue(),
256 max_property_arg.getValue(), outside, searcher);
257 }
258 }
259
260 if (xSmallArg.isSet() || xLargeArg.isSet() || ySmallArg.isSet() ||
261 yLargeArg.isSet() || zSmallArg.isSet() || zLargeArg.isSet())
262 {
263 outputAABB(*mesh);
264 bool aabb_error(false);
265 if (xSmallArg.getValue() >= xLargeArg.getValue())
266 {
267 ERR("Minimum x-extent larger than maximum x-extent.");
268 aabb_error = true;
269 }
270 if (ySmallArg.getValue() >= yLargeArg.getValue())
271 {
272 ERR("Minimum y-extent larger than maximum y-extent.");
273 aabb_error = true;
274 }
275 if (zSmallArg.getValue() >= zLargeArg.getValue())
276 {
277 ERR("Minimum z-extent larger than maximum z-extent.");
278 aabb_error = true;
279 }
280 if (aabb_error)
281 {
282 return EXIT_FAILURE;
283 }
284
285 std::array<MathLib::Point3d, 2> extent(
286 {{MathLib::Point3d(std::array<double, 3>{{xSmallArg.getValue(),
287 ySmallArg.getValue(),
288 zSmallArg.getValue()}}),
289 MathLib::Point3d(std::array<double, 3>{
290 {xLargeArg.getValue(), yLargeArg.getValue(),
291 zLargeArg.getValue()}})}});
292 INFO("{:d} elements found.",
293 searcher.searchByBoundingBox(
294 GeoLib::AABB(extent.begin(), extent.end()),
295 invert_bounding_box_arg.getValue()));
296 }
297
298 // remove the elements and create a new mesh object.
299 std::unique_ptr<MeshLib::Mesh const> new_mesh(MeshToolsLib::removeElements(
300 *mesh, searcher.getSearchedElementIDs(), mesh->getName()));
301
302 if (new_mesh == nullptr)
303 {
304 return EXIT_FAILURE;
305 }
306
307 // write into a file
308 MeshLib::IO::writeMeshToFile(*new_mesh, mesh_out.getValue());
309
310 return EXIT_SUCCESS;
311}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:56
Element search class.
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
MeshElemType String2MeshElemType(const std::string &s)
Given a string of the shortened name of the element type, this returns the corresponding MeshElemType...
Definition MeshEnums.cpp:95
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:48
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)
void outputAABB(MeshLib::Mesh const &mesh)
void searchByPropertyValue(std::string const &property_name, std::vector< PROPERTY_TYPE > const &property_values, MeshLib::ElementSearch &searcher)
void searchByPropertyRange(std::string const &property_name, double const &min_value, double const &max_value, bool const &outside, MeshLib::ElementSearch &searcher)

References ERR(), MeshLib::ElementSearch::getSearchedElementIDs(), INFO(), MeshLib::INVALID, GitInfoLib::GitInfo::ogs_version, outputAABB(), MeshLib::IO::readMeshFromFile(), MeshToolsLib::removeElements(), MeshLib::ElementSearch::searchByBoundingBox(), MeshLib::ElementSearch::searchByContent(), MeshLib::ElementSearch::searchByElementType(), searchByPropertyRange(), searchByPropertyValue(), MeshLib::String2MeshElemType(), and MeshLib::IO::writeMeshToFile().

◆ outputAABB()

void outputAABB ( MeshLib::Mesh const & mesh)

Definition at line 71 of file removeMeshElements.cpp.

72{
74 auto const [min, max] = aabb.getMinMaxPoints();
75
76 INFO(
77 "Bounding box of \"{:s}\" is\nx = [{:f},{:f}]\ny = [{:f},{:f}]\nz = "
78 "[{:f},{:f}]",
79 mesh.getName(), min.x(), max.x(), min.y(), max.y(), min.z(), max.z());
80}
static GeoLib::AABB getBoundingBox(const MeshLib::Mesh &mesh)
Returns the bounding box of the mesh.

References MeshToolsLib::MeshInformation::getBoundingBox(), MeshLib::Mesh::getName(), and INFO().

Referenced by main().

◆ searchByPropertyRange()

void searchByPropertyRange ( std::string const & property_name,
double const & min_value,
double const & max_value,
bool const & outside,
MeshLib::ElementSearch & searcher )

Definition at line 50 of file removeMeshElements.cpp.

54{
55 std::size_t n_marked_elements = searcher.searchByPropertyValueRange<double>(
56 property_name, min_value, max_value, outside);
57
58 if (n_marked_elements == 0)
59 {
60 n_marked_elements = searcher.searchByPropertyValueRange<int>(
61 property_name, static_cast<int>(min_value),
62 static_cast<int>(max_value), outside);
63 }
64
65 // add checks for other data types here (if n_marked_elements remains 0)
66
67 INFO("{:d} elements in range [{:s}, {:s}] found.", n_marked_elements,
68 std::to_string(min_value), std::to_string(max_value));
69}
std::size_t searchByPropertyValueRange(std::string const &property_name, PROPERTY_TYPE const min_property_value, PROPERTY_TYPE const max_property_value, bool outside_of)

References INFO(), and MeshLib::ElementSearch::searchByPropertyValueRange().

Referenced by main().

◆ searchByPropertyValue()

template<typename PROPERTY_TYPE >
void searchByPropertyValue ( std::string const & property_name,
std::vector< PROPERTY_TYPE > const & property_values,
MeshLib::ElementSearch & searcher )

Definition at line 31 of file removeMeshElements.cpp.

34{
35 for (auto const& property_value : property_values)
36 {
37 std::size_t n_marked_elements = searcher.searchByPropertyValue<double>(
38 property_name, property_value);
39 if (n_marked_elements == 0)
40 {
41 n_marked_elements = searcher.searchByPropertyValue<int>(
42 property_name, property_value);
43 }
44
45 INFO("{:d} elements with property value {:s} found.", n_marked_elements,
46 std::to_string(property_value));
47 }
48}
std::size_t searchByPropertyValue(std::string const &property_name, PROPERTY_TYPE const property_value)

References INFO(), and MeshLib::ElementSearch::searchByPropertyValue().

Referenced by main().