Line data Source code
1 :
2 : #ifndef AMREX_RealBox_H_
3 : #define AMREX_RealBox_H_
4 : #include <AMReX_Config.H>
5 :
6 : #include <AMReX_Array.H>
7 : #include <AMReX_Vector.H>
8 : #include <AMReX_REAL.H>
9 : #include <AMReX_Box.H>
10 : #include <AMReX_RealVect.H>
11 : #include <AMReX_SPACE.H>
12 :
13 : #include <iosfwd>
14 : #include <array>
15 :
16 : namespace amrex {
17 :
18 : //!A Box with real dimensions. A RealBox is OK iff volume >= 0.
19 :
20 : class RealBox
21 : {
22 : public:
23 : //! The default constructor. Builds invalid RealBox.
24 : constexpr RealBox () noexcept = default;
25 :
26 : //! Construct region given diagonal points.
27 : AMREX_GPU_HOST_DEVICE
28 : RealBox (const Real* a_lo, const Real* a_hi) noexcept
29 : : xlo{AMREX_D_DECL(a_lo[0],a_lo[1],a_lo[2])}, xhi{AMREX_D_DECL(a_hi[0],a_hi[1],a_hi[2])} {}
30 :
31 : RealBox (const std::array<Real,AMREX_SPACEDIM>& a_lo,
32 : const std::array<Real,AMREX_SPACEDIM>& a_hi) noexcept;
33 :
34 : /**
35 : * \brief Construct region given index box, cell spacing
36 : * and physical location of index (0,0,0).
37 : */
38 : RealBox (const Box& bx, const Real* dx, const Real* base) noexcept;
39 : //! Explicit dimension specific constructors.
40 : AMREX_GPU_HOST_DEVICE
41 : RealBox (AMREX_D_DECL(Real x0, Real y0, Real z0),
42 : AMREX_D_DECL(Real x1, Real y1, Real z1)) noexcept
43 : : xlo{AMREX_D_DECL(x0,y0,z0)}, xhi{AMREX_D_DECL(x1,y1,z1)} {}
44 : //! Returns lo side.
45 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
46 1057510 : const Real* lo () const& noexcept { return xlo; }
47 : AMREX_GPU_HOST_DEVICE
48 : const Real* lo () && = delete;
49 : //! Returns hide side.
50 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
51 100 : const Real* hi () const& noexcept { return xhi; }
52 : AMREX_GPU_HOST_DEVICE
53 : const Real* hi () && = delete;
54 : //! Returns length in specified direction.
55 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
56 80 : Real lo (int dir) const noexcept { return xlo[dir]; }
57 : //! Returns hi side in specified direction.
58 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
59 40 : Real hi (int dir) const noexcept { return xhi[dir]; }
60 : //! Returns length in specified direction.
61 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
62 : Real length (int dir) const noexcept { return xhi[dir]-xlo[dir]; }
63 : //! Sets lo side.
64 : void setLo (const Real* a_lo) noexcept { AMREX_D_EXPR(xlo[0] = a_lo[0], xlo[1] = a_lo[1], xlo[2] = a_lo[2]); }
65 : //! Sets lo side.
66 : void setLo (const Vector<Real>& a_lo) noexcept { AMREX_D_EXPR(xlo[0] = a_lo[0], xlo[1] = a_lo[1], xlo[2] = a_lo[2]); }
67 : //! Sets lo side.
68 : void setLo (const RealVect& a_lo) noexcept { AMREX_D_EXPR(xlo[0] = a_lo[0], xlo[1] = a_lo[1], xlo[2] = a_lo[2]); }
69 : //! Sets lo side in specified direction.
70 : void setLo (int dir, Real a_lo) noexcept { BL_ASSERT(dir >= 0 && dir < AMREX_SPACEDIM); xlo[dir] = a_lo; }
71 : //! Sets hi side.
72 : void setHi (const Real* a_hi) noexcept { AMREX_D_EXPR(xhi[0] = a_hi[0], xhi[1] = a_hi[1], xhi[2] = a_hi[2]); }
73 : //! Sets hi side.
74 : void setHi (const Vector<Real>& a_hi) noexcept { AMREX_D_EXPR(xhi[0] = a_hi[0], xhi[1] = a_hi[1], xhi[2] = a_hi[2]); }
75 : //! Sets hi side.
76 : void setHi (const RealVect& a_hi) noexcept { AMREX_D_EXPR(xhi[0] = a_hi[0], xhi[1] = a_hi[1], xhi[2] = a_hi[2]); }
77 : //! Sets hi side in specified direction.
78 : void setHi (int dir, Real a_hi) noexcept { BL_ASSERT(dir >= 0 && dir < AMREX_SPACEDIM); xhi[dir] = a_hi; }
79 : //! Is the RealBox OK; i.e. does it have non-negative volume?
80 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
81 : bool ok () const noexcept {
82 : return (length(0) >= 0.0)
83 : #if (AMREX_SPACEDIM > 1)
84 : && (length(1) >= 0.0)
85 : #endif
86 : #if (AMREX_SPACEDIM > 2)
87 : && (length(2) >= 0.0)
88 : #endif
89 : ;
90 : }
91 :
92 : //! Returns the volume of the RealBox. If this RealBox is invalid,
93 : //! it's volume is considered to be zero.
94 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
95 : Real volume () const noexcept {
96 : if (ok()) { return AMREX_D_TERM(length(0), *length(1), *length(2)); }
97 : return 0.0;
98 : }
99 :
100 : //! Is the specified point contained in the RealBox?
101 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
102 : bool contains (const Real* point,
103 : Real eps = 0.0) const noexcept {
104 : return AMREX_D_TERM((xlo[0]-eps < point[0]) && (point[0] < xhi[0]+eps),
105 : && (xlo[1]-eps < point[1]) && (point[1] < xhi[1]+eps),
106 : && (xlo[2]-eps < point[2]) && (point[2] < xhi[2]+eps));
107 : }
108 :
109 : //! Is the specified point contained in the RealBox?
110 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
111 : bool contains (XDim3 point, Real eps = 0.0) const noexcept {
112 : return AMREX_D_TERM((xlo[0]-eps < point.x) && (point.x < xhi[0]+eps),
113 : && (xlo[1]-eps < point.y) && (point.y < xhi[1]+eps),
114 : && (xlo[2]-eps < point.z) && (point.z < xhi[2]+eps));
115 : }
116 :
117 : //! Is the specified RealVect contained in this RealBox?
118 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
119 : bool contains (const RealVect& rv,
120 : Real eps=0.0) const noexcept { return contains(rv.dataPtr(), eps); }
121 :
122 : //! Is the specified RealBox contained in this RealBox?
123 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
124 : bool contains (const RealBox& rb,
125 : Real eps = 0.0) const noexcept {
126 : return contains(rb.xlo, eps) && contains(rb.xhi, eps);
127 : }
128 :
129 : //! Does the specified RealBox intersect with this RealBox?
130 : [[nodiscard]] AMREX_GPU_HOST_DEVICE
131 : bool intersects (const RealBox& bx) const noexcept {
132 : return ! (AMREX_D_TERM((xlo[0] > bx.xhi[0]) || (xhi[0] < bx.xlo[0]),
133 : || (xlo[1] > bx.xhi[1]) || (xhi[1] < bx.xlo[1]),
134 : || (xlo[2] > bx.xhi[2]) || (xhi[2] < bx.xlo[2])));
135 : }
136 :
137 : private:
138 : //
139 : // The data.
140 : //
141 : Real xlo[AMREX_SPACEDIM] = {AMREX_D_DECL(Real(0.),Real(0.),Real(0.))};
142 : Real xhi[AMREX_SPACEDIM] = {AMREX_D_DECL(Real(-1.),Real(-1.),Real(-1.))};
143 : };
144 :
145 : //
146 : //! Nice ASCII output.
147 : std::ostream& operator<< (std::ostream&, const RealBox&);
148 : //
149 : //! Nice ASCII input.
150 : std::istream& operator>> (std::istream&, RealBox&);
151 :
152 : //! Check for equality of real boxes within a certain tolerance
153 : bool AlmostEqual (const RealBox& box1,
154 : const RealBox& box2,
155 : Real eps = 0.0) noexcept;
156 :
157 : }
158 :
159 : #endif /*_RealBox_H_*/
|