Skip to content

Commit 92d7fdb

Browse files
authored
Use min/max position members in C++ BoundingBox class (#3699)
1 parent 3f06a42 commit 92d7fdb

File tree

8 files changed

+68
-65
lines changed

8 files changed

+68
-65
lines changed

include/openmc/bounding_box.h

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ namespace openmc {
1313
//==============================================================================
1414

1515
struct BoundingBox {
16-
double xmin = -INFTY;
17-
double xmax = INFTY;
18-
double ymin = -INFTY;
19-
double ymax = INFTY;
20-
double zmin = -INFTY;
21-
double zmax = INFTY;
16+
Position min = {-INFTY, -INFTY, -INFTY};
17+
Position max = {INFTY, INFTY, INFTY};
18+
19+
// Constructors
20+
BoundingBox() = default;
21+
BoundingBox(Position min_, Position max_) : min {min_}, max {max_} {}
22+
23+
// Static factory methods
24+
static BoundingBox infinite() { return {}; }
25+
static BoundingBox inverted()
26+
{
27+
return {{INFTY, INFTY, INFTY}, {-INFTY, -INFTY, -INFTY}};
28+
}
2229

2330
inline BoundingBox operator&(const BoundingBox& other)
2431
{
@@ -35,29 +42,26 @@ struct BoundingBox {
3542
// intersect operator
3643
inline BoundingBox& operator&=(const BoundingBox& other)
3744
{
38-
xmin = std::max(xmin, other.xmin);
39-
xmax = std::min(xmax, other.xmax);
40-
ymin = std::max(ymin, other.ymin);
41-
ymax = std::min(ymax, other.ymax);
42-
zmin = std::max(zmin, other.zmin);
43-
zmax = std::min(zmax, other.zmax);
45+
min.x = std::max(min.x, other.min.x);
46+
min.y = std::max(min.y, other.min.y);
47+
min.z = std::max(min.z, other.min.z);
48+
max.x = std::min(max.x, other.max.x);
49+
max.y = std::min(max.y, other.max.y);
50+
max.z = std::min(max.z, other.max.z);
4451
return *this;
4552
}
4653

4754
// union operator
4855
inline BoundingBox& operator|=(const BoundingBox& other)
4956
{
50-
xmin = std::min(xmin, other.xmin);
51-
xmax = std::max(xmax, other.xmax);
52-
ymin = std::min(ymin, other.ymin);
53-
ymax = std::max(ymax, other.ymax);
54-
zmin = std::min(zmin, other.zmin);
55-
zmax = std::max(zmax, other.zmax);
57+
min.x = std::min(min.x, other.min.x);
58+
min.y = std::min(min.y, other.min.y);
59+
min.z = std::min(min.z, other.min.z);
60+
max.x = std::max(max.x, other.max.x);
61+
max.y = std::max(max.y, other.max.y);
62+
max.z = std::max(max.z, other.max.z);
5663
return *this;
5764
}
58-
59-
inline Position min() const { return {xmin, ymin, zmin}; }
60-
inline Position max() const { return {xmax, ymax, zmax}; }
6165
};
6266

6367
} // namespace openmc

include/openmc/mesh.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ class Mesh {
244244
//! \return Bounding box of mesh
245245
BoundingBox bounding_box() const
246246
{
247-
auto ll = this->lower_left();
248-
auto ur = this->upper_right();
249-
return {ll.x, ur.x, ll.y, ur.y, ll.z, ur.z};
247+
return {this->lower_left(), this->upper_right()};
250248
}
251249

252250
virtual Position lower_left() const = 0;

src/cell.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,14 +1334,14 @@ extern "C" int openmc_cell_bounding_box(
13341334
bbox = c->bounding_box();
13351335

13361336
// set lower left corner values
1337-
llc[0] = bbox.xmin;
1338-
llc[1] = bbox.ymin;
1339-
llc[2] = bbox.zmin;
1337+
llc[0] = bbox.min.x;
1338+
llc[1] = bbox.min.y;
1339+
llc[2] = bbox.min.z;
13401340

13411341
// set upper right corner values
1342-
urc[0] = bbox.xmax;
1343-
urc[1] = bbox.ymax;
1344-
urc[2] = bbox.zmax;
1342+
urc[0] = bbox.max.x;
1343+
urc[1] = bbox.max.y;
1344+
urc[2] = bbox.max.z;
13451345

13461346
return 0;
13471347
}

src/dagmc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ BoundingBox DAGCell::bounding_box() const
753753
double min[3], max[3];
754754
rval = dagmc_ptr_->getobb(vol, min, max);
755755
MB_CHK_ERR_CONT(rval);
756-
return {min[0], max[0], min[1], max[1], min[2], max[2]};
756+
return {{min[0], min[1], min[2]}, {max[0], max[1], max[2]}};
757757
}
758758

759759
//==============================================================================

src/geometry.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,14 @@ extern "C" int openmc_global_bounding_box(double* llc, double* urc)
480480
auto bbox = model::universes.at(model::root_universe)->bounding_box();
481481

482482
// set lower left corner values
483-
llc[0] = bbox.xmin;
484-
llc[1] = bbox.ymin;
485-
llc[2] = bbox.zmin;
483+
llc[0] = bbox.min.x;
484+
llc[1] = bbox.min.y;
485+
llc[2] = bbox.min.z;
486486

487487
// set upper right corner values
488-
urc[0] = bbox.xmax;
489-
urc[1] = bbox.ymax;
490-
urc[2] = bbox.zmax;
488+
urc[0] = bbox.max.x;
489+
urc[1] = bbox.max.y;
490+
urc[2] = bbox.max.z;
491491

492492
return 0;
493493
}

src/mesh.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,10 @@ void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
359359
std::array<int, 3> n_rays = {nx, ny, nz};
360360

361361
// Determine effective width of rays
362-
Position width((nx > 0) ? (bbox.xmax - bbox.xmin) / nx : 0.0,
363-
(ny > 0) ? (bbox.ymax - bbox.ymin) / ny : 0.0,
364-
(nz > 0) ? (bbox.zmax - bbox.zmin) / nz : 0.0);
362+
Position width = bbox.max - bbox.min;
363+
width.x = (nx > 0) ? width.x / nx : 0.0;
364+
width.y = (ny > 0) ? width.y / ny : 0.0;
365+
width.z = (nz > 0) ? width.z / nz : 0.0;
365366

366367
// Set flag for mesh being contained within model
367368
bool out_of_model = false;
@@ -380,15 +381,15 @@ void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
380381
for (int axis = 0; axis < 3; ++axis) {
381382
// Set starting position and direction
382383
site.r = {0.0, 0.0, 0.0};
383-
site.r[axis] = bbox.min()[axis];
384+
site.r[axis] = bbox.min[axis];
384385
site.u = {0.0, 0.0, 0.0};
385386
site.u[axis] = 1.0;
386387

387388
// Determine width of rays and number of rays in other directions
388389
int ax1 = (axis + 1) % 3;
389390
int ax2 = (axis + 2) % 3;
390-
double min1 = bbox.min()[ax1];
391-
double min2 = bbox.min()[ax2];
391+
double min1 = bbox.min[ax1];
392+
double min2 = bbox.min[ax2];
392393
double d1 = width[ax1];
393394
double d2 = width[ax2];
394395
int n1 = n_rays[ax1];
@@ -433,7 +434,7 @@ void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
433434
while (true) {
434435
// Ray trace from r_start to r_end
435436
Position r0 = p.r();
436-
double max_distance = bbox.max()[axis] - r0[axis];
437+
double max_distance = bbox.max[axis] - r0[axis];
437438

438439
// Find the distance to the nearest boundary
439440
BoundaryInfo boundary = distance_to_boundary(p);
@@ -2415,14 +2416,14 @@ extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
24152416
BoundingBox bbox = model::meshes[index]->bounding_box();
24162417

24172418
// set lower left corner values
2418-
ll[0] = bbox.xmin;
2419-
ll[1] = bbox.ymin;
2420-
ll[2] = bbox.zmin;
2419+
ll[0] = bbox.min.x;
2420+
ll[1] = bbox.min.y;
2421+
ll[2] = bbox.min.z;
24212422

24222423
// set upper right corner values
2423-
ur[0] = bbox.xmax;
2424-
ur[1] = bbox.ymax;
2425-
ur[2] = bbox.zmax;
2424+
ur[0] = bbox.max.x;
2425+
ur[1] = bbox.max.y;
2426+
ur[2] = bbox.max.z;
24262427
return 0;
24272428
}
24282429

src/surface.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ void SurfaceXPlane::to_hdf5_inner(hid_t group_id) const
251251
BoundingBox SurfaceXPlane::bounding_box(bool pos_side) const
252252
{
253253
if (pos_side) {
254-
return {x0_, INFTY, -INFTY, INFTY, -INFTY, INFTY};
254+
return {{x0_, -INFTY, -INFTY}, {INFTY, INFTY, INFTY}};
255255
} else {
256-
return {-INFTY, x0_, -INFTY, INFTY, -INFTY, INFTY};
256+
return {{-INFTY, -INFTY, -INFTY}, {x0_, INFTY, INFTY}};
257257
}
258258
}
259259

@@ -291,9 +291,9 @@ void SurfaceYPlane::to_hdf5_inner(hid_t group_id) const
291291
BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const
292292
{
293293
if (pos_side) {
294-
return {-INFTY, INFTY, y0_, INFTY, -INFTY, INFTY};
294+
return {{-INFTY, y0_, -INFTY}, {INFTY, INFTY, INFTY}};
295295
} else {
296-
return {-INFTY, INFTY, -INFTY, y0_, -INFTY, INFTY};
296+
return {{-INFTY, -INFTY, -INFTY}, {INFTY, y0_, INFTY}};
297297
}
298298
}
299299

@@ -331,9 +331,9 @@ void SurfaceZPlane::to_hdf5_inner(hid_t group_id) const
331331
BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const
332332
{
333333
if (pos_side) {
334-
return {-INFTY, INFTY, -INFTY, INFTY, z0_, INFTY};
334+
return {{-INFTY, -INFTY, z0_}, {INFTY, INFTY, INFTY}};
335335
} else {
336-
return {-INFTY, INFTY, -INFTY, INFTY, -INFTY, z0_};
336+
return {{-INFTY, -INFTY, -INFTY}, {INFTY, INFTY, z0_}};
337337
}
338338
}
339339

@@ -492,8 +492,8 @@ void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
492492
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
493493
{
494494
if (!pos_side) {
495-
return {-INFTY, INFTY, y0_ - radius_, y0_ + radius_, z0_ - radius_,
496-
z0_ + radius_};
495+
return {{-INFTY, y0_ - radius_, z0_ - radius_},
496+
{INFTY, y0_ + radius_, z0_ + radius_}};
497497
} else {
498498
return {};
499499
}
@@ -535,8 +535,8 @@ void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
535535
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
536536
{
537537
if (!pos_side) {
538-
return {x0_ - radius_, x0_ + radius_, -INFTY, INFTY, z0_ - radius_,
539-
z0_ + radius_};
538+
return {{x0_ - radius_, -INFTY, z0_ - radius_},
539+
{x0_ + radius_, INFTY, z0_ + radius_}};
540540
} else {
541541
return {};
542542
}
@@ -579,8 +579,8 @@ void SurfaceZCylinder::to_hdf5_inner(hid_t group_id) const
579579
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
580580
{
581581
if (!pos_side) {
582-
return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_, -INFTY,
583-
INFTY};
582+
return {{x0_ - radius_, y0_ - radius_, -INFTY},
583+
{x0_ + radius_, y0_ + radius_, INFTY}};
584584
} else {
585585
return {};
586586
}
@@ -657,8 +657,8 @@ void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
657657
BoundingBox SurfaceSphere::bounding_box(bool pos_side) const
658658
{
659659
if (!pos_side) {
660-
return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_,
661-
z0_ - radius_, z0_ + radius_};
660+
return {{x0_ - radius_, y0_ - radius_, z0_ - radius_},
661+
{x0_ + radius_, y0_ + radius_, z0_ + radius_}};
662662
} else {
663663
return {};
664664
}

src/universe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ bool Universe::find_cell(GeometryState& p) const
6161

6262
BoundingBox Universe::bounding_box() const
6363
{
64-
BoundingBox bbox = {INFTY, -INFTY, INFTY, -INFTY, INFTY, -INFTY};
64+
BoundingBox bbox = BoundingBox::inverted();
6565
if (cells_.size() == 0) {
6666
return {};
6767
} else {

0 commit comments

Comments
 (0)