Block-Structured AMR Software Framework
AMReX_EB_STL_utils.H
Go to the documentation of this file.
1 #ifndef AMREX_EB_STL_UTILS_H_
2 #define AMREX_EB_STL_UTILS_H_
3 
4 #include <AMReX_Config.H>
5 #include <AMReX_Geometry.H>
6 #include <AMReX_MultiFab.H>
7 #include <AMReX_Dim3.H>
8 #include <AMReX_EB2_Graph.H>
9 
10 #include <algorithm>
11 #include <cstdint>
12 #include <limits>
13 #include <utility>
14 
15 namespace amrex
16 {
17 
18 class STLtools
19 {
20 public:
21  struct Triangle {
23 
24  [[nodiscard]] Real cent (int d) const
25  {
26  static_assert(sizeof(XDim3) == sizeof(Real)*3);
27  return Real(1./3.)*((&v1.x)[d] + (&v2.x)[d] + (&v3.x)[d]);
28  }
29 
30  [[nodiscard]] std::pair<Real,Real> minmax (int d) const
31  {
32  static_assert(sizeof(XDim3) == sizeof(Real)*3);
33  return std::minmax({(&v1.x)[d], (&v2.x)[d], (&v3.x)[d]});
34  }
35  };
36 
37  template <int M, int N>
38  struct BVHNodeT
39  {
43  AMREX_D_DECL(std::numeric_limits<Real>::lowest(),
44  std::numeric_limits<Real>::lowest(),
45  std::numeric_limits<Real>::lowest())};
48  int children[N];
49  std::int8_t ntriangles = 0;
50  std::int8_t nchildren = 0;
51  };
52 
53  static constexpr int m_bvh_max_size = 4; // max # of triangles in a leaf node
54  static constexpr int m_bvh_max_splits = 4; // max # of children
55  static constexpr int m_bvh_max_stack_size = 12; // max depth of the tree
56 
58 
59  static constexpr int allregular = -1;
60  static constexpr int mixedcells = 0;
61  static constexpr int allcovered = 1;
62 
63  void setBVHOptimization (bool flag) { m_bvh_optimization = flag; }
64 
65  void read_stl_file (std::string const& fname, Real scale, Array<Real,3> const& center,
66  int reverse_normal);
67 
68  void fill (MultiFab& mf, IntVect const& nghost, Geometry const& geom,
69  Real outside_value = -1._rt, Real inside_value = 1._rt) const;
70 
71  [[nodiscard]] int getBoxType (Box const& box, Geometry const& geom, RunOn) const;
72 
73  static constexpr bool isGPUable () noexcept { return true; }
74 
75  void fillFab (BaseFab<Real>& levelset, const Geometry& geom, RunOn,
76  Box const& bounding_box) const;
77 
78  void getIntercept (Array<Array4<Real>,AMREX_SPACEDIM> const& inter_arr,
79  Array<Array4<EB2::Type_t const>,AMREX_SPACEDIM> const& type_arr,
80  Array4<Real const> const& lst, Geometry const& geom, RunOn,
81  Box const& bounding_box) const;
82 
83  static void updateIntercept (Array<Array4<Real>,AMREX_SPACEDIM> const& inter_arr,
84  Array<Array4<EB2::Type_t const>,AMREX_SPACEDIM> const& type_arr,
85  Array4<Real const> const& lst, Geometry const& geom) ;
86 
87  void prepare (Gpu::PinnedVector<Triangle> a_tri_pts); // public for cuda
88 
89 private:
90 
91  bool m_bvh_optimization = true;
92 
96 
97  int m_num_tri=0;
98 
99  XDim3 m_ptmin; // All triangles are inside the bounding box defined by
100  XDim3 m_ptmax; // m_ptmin and m_ptmax.
101  XDim3 m_ptref; // The reference point is slightly outside the bounding box.
102  bool m_boundry_is_outside; // Is the bounding box boundary outside or inside the object?
103 
104  void read_ascii_stl_file (std::string const& fname, Real scale,
105  Array<Real,3> const& center, int reverse_normal,
106  Gpu::PinnedVector<Triangle>& a_tri_pts);
107  void read_binary_stl_file (std::string const& fname, Real scale,
108  Array<Real,3> const& center, int reverse_normal,
109  Gpu::PinnedVector<Triangle>& a_tri_pts);
110 
111  static void build_bvh (Triangle* begin, Triangle * end, Gpu::PinnedVector<Node>& bvh_nodes);
112  static void bvh_size (int ntri, std::size_t& nnodes);
113 };
114 
115 }
116 #endif
#define AMREX_D_DECL(a, b, c)
Definition: AMReX_SPACE.H:104
Rectangular problem domain geometry.
Definition: AMReX_Geometry.H:73
A collection (stored as an array) of FArrayBox objects.
Definition: AMReX_MultiFab.H:38
Definition: AMReX_PODVector.H:246
A Box with real dimensions. A RealBox is OK iff volume >= 0.
Definition: AMReX_RealBox.H:21
Definition: AMReX_EB_STL_utils.H:19
int m_num_tri
Definition: AMReX_EB_STL_utils.H:97
static void bvh_size(int ntri, std::size_t &nnodes)
Definition: AMReX_EB_STL_utils.cpp:607
static constexpr int allregular
Definition: AMReX_EB_STL_utils.H:59
static constexpr int m_bvh_max_stack_size
Definition: AMReX_EB_STL_utils.H:55
XDim3 m_ptmax
Definition: AMReX_EB_STL_utils.H:100
static constexpr int m_bvh_max_size
Definition: AMReX_EB_STL_utils.H:53
static constexpr int m_bvh_max_splits
Definition: AMReX_EB_STL_utils.H:54
Gpu::DeviceVector< Node > m_bvh_nodes
Definition: AMReX_EB_STL_utils.H:95
int getBoxType(Box const &box, Geometry const &geom, RunOn) const
Definition: AMReX_EB_STL_utils.cpp:700
bool m_bvh_optimization
Definition: AMReX_EB_STL_utils.H:91
void prepare(Gpu::PinnedVector< Triangle > a_tri_pts)
Definition: AMReX_EB_STL_utils.cpp:348
void fill(MultiFab &mf, IntVect const &nghost, Geometry const &geom, Real outside_value=-1._rt, Real inside_value=1._rt) const
Definition: AMReX_EB_STL_utils.cpp:624
void read_binary_stl_file(std::string const &fname, Real scale, Array< Real, 3 > const &center, int reverse_normal, Gpu::PinnedVector< Triangle > &a_tri_pts)
Definition: AMReX_EB_STL_utils.cpp:222
XDim3 m_ptref
Definition: AMReX_EB_STL_utils.H:101
void read_stl_file(std::string const &fname, Real scale, Array< Real, 3 > const &center, int reverse_normal)
Definition: AMReX_EB_STL_utils.cpp:195
void setBVHOptimization(bool flag)
Definition: AMReX_EB_STL_utils.H:63
Gpu::DeviceVector< XDim3 > m_tri_normals_d
Definition: AMReX_EB_STL_utils.H:94
static constexpr int mixedcells
Definition: AMReX_EB_STL_utils.H:60
static constexpr bool isGPUable() noexcept
Definition: AMReX_EB_STL_utils.H:73
static constexpr int allcovered
Definition: AMReX_EB_STL_utils.H:61
void getIntercept(Array< Array4< Real >, AMREX_SPACEDIM > const &inter_arr, Array< Array4< EB2::Type_t const >, AMREX_SPACEDIM > const &type_arr, Array4< Real const > const &lst, Geometry const &geom, RunOn, Box const &bounding_box) const
Definition: AMReX_EB_STL_utils.cpp:876
void read_ascii_stl_file(std::string const &fname, Real scale, Array< Real, 3 > const &center, int reverse_normal, Gpu::PinnedVector< Triangle > &a_tri_pts)
Definition: AMReX_EB_STL_utils.cpp:276
static void updateIntercept(Array< Array4< Real >, AMREX_SPACEDIM > const &inter_arr, Array< Array4< EB2::Type_t const >, AMREX_SPACEDIM > const &type_arr, Array4< Real const > const &lst, Geometry const &geom)
Definition: AMReX_EB_STL_utils.cpp:1060
bool m_boundry_is_outside
Definition: AMReX_EB_STL_utils.H:102
XDim3 m_ptmin
Definition: AMReX_EB_STL_utils.H:99
void fillFab(BaseFab< Real > &levelset, const Geometry &geom, RunOn, Box const &bounding_box) const
Definition: AMReX_EB_STL_utils.cpp:808
static void build_bvh(Triangle *begin, Triangle *end, Gpu::PinnedVector< Node > &bvh_nodes)
Definition: AMReX_EB_STL_utils.cpp:523
Gpu::DeviceVector< Triangle > m_tri_pts_d
Definition: AMReX_EB_STL_utils.H:93
@ max
Definition: AMReX_ParallelReduce.H:17
static constexpr int M
Definition: AMReX_OpenBC.H:13
Definition: AMReX_Amr.cpp:49
RunOn
Definition: AMReX_GpuControl.H:69
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 end(BoxND< dim > const &box) noexcept
Definition: AMReX_Box.H:1890
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE IntVectND< dim > scale(const IntVectND< dim > &p, int s) noexcept
Returns a IntVectND obtained by multiplying each of the components of this IntVectND by s.
Definition: AMReX_IntVect.H:1006
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 begin(BoxND< dim > const &box) noexcept
Definition: AMReX_Box.H:1881
std::array< T, N > Array
Definition: AMReX_Array.H:24
Definition: AMReX_Array4.H:61
Definition: AMReX_EB_STL_utils.H:39
XDim3 trinorm[M]
Definition: AMReX_EB_STL_utils.H:47
int children[N]
Definition: AMReX_EB_STL_utils.H:48
std::int8_t nchildren
Definition: AMReX_EB_STL_utils.H:50
std::int8_t ntriangles
Definition: AMReX_EB_STL_utils.H:49
STLtools::Triangle triangles[M]
Definition: AMReX_EB_STL_utils.H:46
RealBox boundingbox
Definition: AMReX_EB_STL_utils.H:40
Definition: AMReX_EB_STL_utils.H:21
std::pair< Real, Real > minmax(int d) const
Definition: AMReX_EB_STL_utils.H:30
Real cent(int d) const
Definition: AMReX_EB_STL_utils.H:24
XDim3 v1
Definition: AMReX_EB_STL_utils.H:22
XDim3 v2
Definition: AMReX_EB_STL_utils.H:22
XDim3 v3
Definition: AMReX_EB_STL_utils.H:22
Definition: AMReX_Dim3.H:13
Real x
Definition: AMReX_Dim3.H:13