Skip to content

Commit

Permalink
Add bounding zone (#1093)
Browse files Browse the repository at this point in the history
* Add bounding zones
* Define 'bound' enum and use lo/hi instead of inside/outside
* Rename 'clip' to 'shrink'
* Add 'grow' function
* Use zones with surface clipper
* Address review feedbcak
  • Loading branch information
sethrj committed Feb 2, 2024
1 parent 3ff4abc commit 91b41d6
Show file tree
Hide file tree
Showing 13 changed files with 955 additions and 387 deletions.
84 changes: 64 additions & 20 deletions src/orange/BoundingBox.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "corecel/math/Algorithms.hh"
#include "corecel/math/NumericLimits.hh"

#include "OrangeTypes.hh"
#include "Types.hh"

namespace celeritas
{
Expand Down Expand Up @@ -65,23 +65,38 @@ class BoundingBox
//// ACCESSORS ////

//! Lower bbox coordinate
CELER_CONSTEXPR_FUNCTION Real3 const& lower() const { return lower_; }
CELER_CONSTEXPR_FUNCTION Real3 const& lower() const
{
return this->point(Bound::lo);
}

//! Upper bbox coordinate
CELER_CONSTEXPR_FUNCTION Real3 const& upper() const { return upper_; }
CELER_CONSTEXPR_FUNCTION Real3 const& upper() const
{
return this->point(Bound::hi);
}

//! Access a bounding point
CELER_CONSTEXPR_FUNCTION Real3 const& point(Bound b) const
{
return points_[to_int(b)];
}

// Whether the bbox is non-null
CELER_CONSTEXPR_FUNCTION explicit operator bool() const;

//// MUTATORS ////

// Intersect in place with a half-space
// Reduce the bounding box's extent along an axis
CELER_CONSTEXPR_FUNCTION void
shrink(Bound bnd, Axis axis, real_type position);

// Increase the bounding box's extent along an axis
CELER_CONSTEXPR_FUNCTION void
clip(Sense sense, Axis axis, real_type position);
grow(Bound bnd, Axis axis, real_type position);

private:
Real3 lower_;
Real3 upper_;
Array<Real3, 2> points_; //!< lo/hi points

// Implementation of 'from_unchecked' (true type 'tag')
CELER_CONSTEXPR_FUNCTION
Expand Down Expand Up @@ -179,8 +194,8 @@ template<class T>
CELER_CONSTEXPR_FUNCTION BoundingBox<T>::BoundingBox()
{
constexpr real_type inf = numeric_limits<real_type>::infinity();
lower_ = {inf, inf, inf};
upper_ = {-inf, -inf, -inf};
points_[to_int(Bound::lo)] = {inf, inf, inf};
points_[to_int(Bound::hi)] = {-inf, -inf, -inf};
}

//---------------------------------------------------------------------------//
Expand All @@ -192,13 +207,14 @@ CELER_CONSTEXPR_FUNCTION BoundingBox<T>::BoundingBox()
*/
template<class T>
CELER_FUNCTION BoundingBox<T>::BoundingBox(Real3 const& lo, Real3 const& hi)
: lower_(lo), upper_(hi)
: points_{{lo, hi}}
{
if constexpr (CELERITAS_DEBUG)
{
for (auto ax : {Axis::x, Axis::y, Axis::z})
{
CELER_EXPECT(lower_[to_int(ax)] <= upper_[to_int(ax)]);
CELER_EXPECT(this->lower()[to_int(ax)]
<= this->upper()[to_int(ax)]);
}
}
CELER_ENSURE(*this);
Expand All @@ -211,7 +227,7 @@ CELER_FUNCTION BoundingBox<T>::BoundingBox(Real3 const& lo, Real3 const& hi)
template<class T>
CELER_CONSTEXPR_FUNCTION
BoundingBox<T>::BoundingBox(std::true_type, Real3 const& lo, Real3 const& hi)
: lower_(lo), upper_(hi)
: points_{{lo, hi}}
{
}

Expand All @@ -222,27 +238,55 @@ BoundingBox<T>::BoundingBox(std::true_type, Real3 const& lo, Real3 const& hi)
template<class T>
CELER_CONSTEXPR_FUNCTION BoundingBox<T>::operator bool() const
{
return lower_[to_int(Axis::x)] <= upper_[to_int(Axis::x)]
&& lower_[to_int(Axis::y)] <= upper_[to_int(Axis::y)]
&& lower_[to_int(Axis::z)] <= upper_[to_int(Axis::z)];
return this->lower()[0] <= this->upper()[0]
&& this->lower()[1] <= this->upper()[1]
&& this->lower()[2] <= this->upper()[2];
}

//---------------------------------------------------------------------------//
/*!
* Reduce (clip) the bounding box's extent along an axis.
*
* If the point is inside the box, the box is clipped so the given boundary is
* on that point. Otherwise no change is made.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION void
BoundingBox<T>::shrink(Bound bnd, Axis axis, real_type position)
{
real_type p = points_[to_int(bnd)][to_int(axis)];
if (bnd == Bound::lo)
{
p = ::celeritas::max(p, position);
}
else
{
p = ::celeritas::min(p, position);
}
points_[to_int(bnd)][to_int(axis)] = p;
}

//---------------------------------------------------------------------------//
/*!
* Intersect in place with a half-space.
* Increase (expand) the bounding box's extent along an axis.
*
* If the point is outside the box, the box is expanded so the given boundary
* is on that point. Otherwise no change is made.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION void
BoundingBox<T>::clip(Sense sense, Axis axis, real_type position)
BoundingBox<T>::grow(Bound bnd, Axis axis, real_type position)
{
if (sense == Sense::inside)
real_type p = points_[to_int(bnd)][to_int(axis)];
if (bnd == Bound::lo)
{
upper_[to_int(axis)] = ::celeritas::min(upper_[to_int(axis)], position);
p = ::celeritas::min(p, position);
}
else
{
lower_[to_int(axis)] = ::celeritas::max(lower_[to_int(axis)], position);
p = ::celeritas::max(p, position);
}
points_[to_int(bnd)][to_int(axis)] = p;
}

//---------------------------------------------------------------------------//
Expand Down
3 changes: 2 additions & 1 deletion src/orange/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ list(APPEND SOURCES
detail/SurfacesRecordBuilder.cc
detail/UnitInserter.cc
detail/UniverseInserter.cc
orangeinp/detail/BoundingZone.cc
orangeinp/detail/CsgUnitBuilder.cc
surf/ConeAligned.cc
surf/CylAligned.cc
Expand All @@ -40,10 +41,10 @@ list(APPEND SOURCES
surf/SimpleQuadric.cc
surf/Sphere.cc
surf/SoftSurfaceEqual.cc
surf/SurfaceClipper.cc
surf/SurfaceIO.cc
surf/SurfaceSimplifier.cc
surf/VariantSurface.cc
surf/detail/SurfaceClipperImpl.cc
surf/detail/SurfaceTranslator.cc
surf/detail/SurfaceTransformer.cc
transform/SignedPermutation.cc
Expand Down
24 changes: 23 additions & 1 deletion src/orange/Types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ enum class Axis
size_ //!< Sentinel value for looping over axes
};

//---------------------------------------------------------------------------//
/*!
* Which of two bounding points, faces, etc.
*
* Here, lo/hi correspond to left/right, back/front, bottom/top. It's used for
* the two points in a bounding box.
*/
enum class Bound : bool
{
lo,
hi
};

//---------------------------------------------------------------------------//
// STRUCTS
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -86,21 +99,30 @@ struct Propagation
};

//---------------------------------------------------------------------------//
// HELPER FUNCTIONS (HOST)
// HELPER FUNCTIONS
//---------------------------------------------------------------------------//
//! Convert Axis enum value to int
CELER_CONSTEXPR_FUNCTION int to_int(Axis a)
{
return static_cast<int>(a);
}

//---------------------------------------------------------------------------//
//! Convert int to Axis enum value
inline CELER_FUNCTION Axis to_axis(int a)
{
CELER_EXPECT(a >= 0 && a < 3);
return static_cast<Axis>(a);
}

//---------------------------------------------------------------------------//
//! Convert Bound enum value to int
CELER_CONSTEXPR_FUNCTION int to_int(Bound b)
{
return static_cast<int>(b);
}

//---------------------------------------------------------------------------//
//! Get the lowercase name of the axis.
inline constexpr char to_char(Axis ax)
{
Expand Down

0 comments on commit 91b41d6

Please sign in to comment.