OGS
MeshToolsLib/MeshGenerators/AddFaultToVoxelGrid.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <Eigen/Geometry>
7#include <algorithm>
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "GeoLib/AABB.h"
13#include "InfoLib/GitInfo.h"
14#include "MathLib/Point3d.h"
18#include "MeshLib/Mesh.h"
19#include "MeshLib/Node.h"
20
21namespace
22{
23// tests if a plane and an AABB are intersecting
24// (based on Christer Ericson "Real Time Collision Detection" 5.2.3)
25bool testAABBIntersectingPlane(Eigen::Vector3d const& aabb_centre,
26 Eigen::Vector3d const& aabb_extent,
27 Eigen::Vector3d const& plane_normal,
28 double const pd)
29{
30 double const r = aabb_extent.dot(plane_normal.cwiseAbs());
31 double const s = plane_normal.dot(aabb_centre) - pd;
32 return std::abs(s) > r;
33}
34// tests if a triangle and an AABB are intersecting
35// (based on Christer Ericson "Real Time Collision Detection" 5.2.9)
37 MeshLib::Node const& n1,
38 MeshLib::Node const& n2,
39 Eigen::Vector3d const& c,
40 Eigen::Vector3d const& e)
41{
42 // Translate triangle as conceptually moving AABB to origin
43 Eigen::Matrix3d v;
44 v << n0.asEigenVector3d() - c, n1.asEigenVector3d() - c,
45 n2.asEigenVector3d() - c;
46
47 // Test the three axes corresponding to the face normals of AABB b
48 if (((v.rowwise().minCoeff() - e).array() > 0).any() ||
49 ((v.rowwise().maxCoeff() + e).array() < 0).any())
50 {
51 return false;
52 }
53
54 // separating axes
55 std::array<Eigen::Vector3d, 3> tri_edge{
56 {v.col(1) - v.col(0), v.col(2) - v.col(1), v.col(0) - v.col(2)}};
57 std::array<Eigen::Vector3d, 9> const axx{
58 {Eigen::Vector3d({0, -tri_edge[0].z(), tri_edge[0].y()}),
59 Eigen::Vector3d({0, -tri_edge[1].z(), tri_edge[1].y()}),
60 Eigen::Vector3d({0, -tri_edge[2].z(), tri_edge[2].y()}),
61 Eigen::Vector3d({tri_edge[0].z(), 0, -tri_edge[0].x()}),
62 Eigen::Vector3d({tri_edge[1].z(), 0, -tri_edge[1].x()}),
63 Eigen::Vector3d({tri_edge[2].z(), 0, -tri_edge[2].x()}),
64 Eigen::Vector3d({-tri_edge[0].y(), tri_edge[0].x(), 0}),
65 Eigen::Vector3d({-tri_edge[1].y(), tri_edge[1].x(), 0}),
66 Eigen::Vector3d({-tri_edge[2].y(), tri_edge[2].x(), 0})}};
67
68 // Separating axis tests to check if there's a plane separating the
69 // projections of the AABB and the triangle according to the Separating Axis
70 // Theorem (see C. Ericson "Real Time Collision Detection" for details)
71 for (auto const& a : axx)
72 {
73 Eigen::Vector3d p = v.transpose() * a;
74 double const r = e.dot(a.cwiseAbs());
75 if (std::max(-p.maxCoeff(), p.minCoeff()) > r)
76 {
77 return false;
78 }
79 }
80
81 // Test separating axis corresponding to triangle face normal
82 Eigen::Vector3d const plane_normal(tri_edge[0].cross(tri_edge[1]));
83 double const pd = plane_normal.dot(v.row(0));
84 return testAABBIntersectingPlane(c, e, plane_normal, pd);
85}
86// mark all cells of the voxel grid that intersect with fault
87void markFaults(MeshLib::Mesh& mesh, MeshLib::Mesh const& fault,
88 int const fault_id, Eigen::Vector3d const& half_cell_size)
89{
90 auto const& elems = mesh.getElements();
91 std::size_t const n_elems = mesh.getNumberOfElements();
92 auto mat_ids = MeshLib::materialIDs(mesh);
93 auto const& fnodes = fault.getNodes();
94 auto const& felems = fault.getElements();
95 GeoLib::AABB const fault_aabb(fnodes.cbegin(), fnodes.cend());
96 auto [min_pnt, max_pnt] = fault_aabb.getMinMaxPoints();
97
98 // get bounding box of fault + voxel extent
99 min_pnt -= half_cell_size;
100 max_pnt += half_cell_size;
101
102 std::array<Eigen::Vector3d, 2> const fault_extent{{min_pnt, max_pnt}};
103 GeoLib::AABB const fault_aabb_ext(fault_extent.cbegin(),
104 fault_extent.cend());
105
106 // test each voxel grid element vs each fault triangle
107 Eigen::Vector3d const extent{half_cell_size};
108 for (std::size_t j = 0; j < n_elems; ++j)
109 {
110 // test if bounding box of fault is intersecting voxel
111 auto const& centre_pnt = MeshLib::getCenterOfGravity(*elems[j]);
112 if (!fault_aabb_ext.containsPoint(centre_pnt, 0))
113 {
114 continue;
115 }
116
117 // test if voxel is intersecting triangle
118 auto const& c(centre_pnt.asEigenVector3d());
119 for (auto const* const fault_elem : felems)
120 {
121 if (fault_elem->getDimension() != 2)
122 {
123 continue;
124 }
125
127 *fault_elem->getNode(0), *fault_elem->getNode(1),
128 *fault_elem->getNode(2), c, extent))
129 {
130 (*mat_ids)[j] = fault_id;
131 break;
132 }
133
134 if (fault_elem->getGeomType() == MeshLib::MeshElemType::QUAD)
135 {
137 *fault_elem->getNode(0), *fault_elem->getNode(2),
138 *fault_elem->getNode(3), c, extent))
139 {
140 (*mat_ids)[j] = fault_id;
141 break;
142 }
143 }
144 }
145 }
146}
147} // namespace
149{
150bool isVoxelGrid(MeshLib::Mesh const& mesh)
151{
152 auto const& elements = mesh.getElements();
153 if (std::any_of(elements.cbegin(), elements.cend(),
154 [&](auto const& e) {
155 return (e->getGeomType() !=
156 MeshLib::MeshElemType::HEXAHEDRON);
157 }))
158 {
159 return false;
160 }
161 auto is_voxel = [](auto const& e)
162 {
163 auto const n = e->getNodes();
164 return ((*n[0])[2] != (*n[1])[2] || (*n[1])[2] != (*n[2])[2] ||
165 (*n[4])[2] != (*n[5])[2] || (*n[5])[2] != (*n[6])[2] ||
166 (*n[1])[0] != (*n[2])[0] || (*n[2])[0] != (*n[5])[0] ||
167 (*n[0])[0] != (*n[3])[0] || (*n[3])[0] != (*n[7])[0]);
168 };
169
170 if (std::any_of(elements.cbegin(), elements.cend(), is_voxel))
171 {
172 ERR("Input mesh needs to be voxel grid (i.e. equally sized axis "
173 "aligned hexahedra).");
174 return false;
175 }
176 return true;
177}
178} // namespace MeshToolsLib::MeshGenerator::AddFaultToVoxelGrid
180{
182 MeshLib::Mesh const* fault,
183 int const fault_id)
184{
185 if (mesh == nullptr)
186 {
187 ERR("Input mesh not found...");
188 return false;
189 }
190 if (!isVoxelGrid(*mesh))
191 {
192 ERR("The input mesh is not a voxel grid. The input mesh must be "
193 "a voxel grid (i.e. an equally sized axis "
194 "aligned hexahedra mesh).");
195 return false;
196 }
197
198 if (fault == nullptr)
199 {
200 ERR("Fault mesh not found...");
201 return false;
202 }
203 if (fault->getDimension() != 2)
204 {
205 ERR("Fault needs to be a 2D mesh.");
206 return false;
207 }
208
209 Eigen::Vector3d half_cell_size;
210 {
211 auto const n = *mesh->getElement(0)->getNode(0);
212 auto const c = MeshLib::getCenterOfGravity(*mesh->getElement(0));
213 half_cell_size[0] = std::abs(c[0] - n[0]);
214 half_cell_size[1] = std::abs(c[1] - n[1]);
215 half_cell_size[2] = std::abs(c[2] - n[2]);
216 }
217
218 markFaults(*mesh, *fault, fault_id, half_cell_size);
219
220 return true;
221}
222} // namespace MeshToolsLib::MeshGenerator::AddFaultToVoxelGrid
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:45
bool containsPoint(T const &pnt, double eps) const
Definition AABB.h:132
MinMaxPoints getMinMaxPoints() const
Definition AABB.h:163
Eigen::Vector3d const & asEigenVector3d() const
Definition Point3d.h:55
virtual const Node * getNode(unsigned idx) const =0
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:79
const Element * getElement(std::size_t idx) const
Get the element with the given index.
Definition Mesh.h:85
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:88
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:258
MathLib::Point3d getCenterOfGravity(Element const &element)
Calculates the center of gravity for the mesh element.
Definition Element.cpp:131
bool addFaultToVoxelGrid(MeshLib::Mesh *mesh, MeshLib::Mesh const *fault, int const fault_id)
bool testAABBIntersectingPlane(Eigen::Vector3d const &aabb_centre, Eigen::Vector3d const &aabb_extent, Eigen::Vector3d const &plane_normal, double const pd)
void markFaults(MeshLib::Mesh &mesh, MeshLib::Mesh const &fault, int const fault_id, Eigen::Vector3d const &half_cell_size)
bool testTriangleIntersectingAABB(MeshLib::Node const &n0, MeshLib::Node const &n1, MeshLib::Node const &n2, Eigen::Vector3d const &c, Eigen::Vector3d const &e)