Skip to content

Commit

Permalink
Merge pull request #70 from LLNL/feature/zagaris2/bvh-updates
Browse files Browse the repository at this point in the history
Improve BVH robustness
  • Loading branch information
gzagaris committed Aug 7, 2019
2 parents cbc05a2 + ec89866 commit 9592cbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/axom/spin/BVH.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ class BVH
*/
~BVH();

/*!
* \brief Sets the scale factor for scaling the supplied bounding boxes.
* \param [in] scale_factor the scale factor
*
* \note The default scale factor is set to 1.001
*/
void setScaleFactor( FloatType scale_factor )
{ m_scaleFactor = scale_factor; };

/*!
* \brief Generates the BVH
* \return status set to BVH_BUILD_OK on success.
Expand Down Expand Up @@ -267,10 +276,12 @@ class BVH
/// \name Private Members
/// @{

FloatType m_scaleFactor;
IndexType m_numItems;
const FloatType* m_boxes;
internal::linear_bvh::BVHData< FloatType,NDIMS > m_bvh;

static constexpr FloatType DEFAULT_SCALE_FACTOR = 1.001;
/// @}

DISABLE_COPY_AND_ASSIGNMENT(BVH);
Expand Down Expand Up @@ -365,6 +376,7 @@ bool InRight( const internal::linear_bvh::Vec< FloatType, 2>& point,
template< int NDIMS, typename ExecSpace, typename FloatType >
BVH< NDIMS, ExecSpace, FloatType >::BVH( const FloatType* boxes,
IndexType numItems ) :
m_scaleFactor( DEFAULT_SCALE_FACTOR ),
m_numItems( numItems ),
m_boxes( boxes )
{
Expand Down Expand Up @@ -418,7 +430,7 @@ int BVH< NDIMS, ExecSpace, FloatType >::build()
internal::linear_bvh::RadixTree< FloatType, NDIMS > radix_tree;
internal::linear_bvh::AABB< FloatType, NDIMS > global_bounds;
internal::linear_bvh::build_radix_tree< ExecSpace >(
boxesptr, numBoxes, global_bounds, radix_tree );
boxesptr, numBoxes, global_bounds, radix_tree, m_scaleFactor );

// STEP 3: emit the BVH data-structure from the radix tree
m_bvh.m_bounds = global_bounds;
Expand Down
28 changes: 18 additions & 10 deletions src/axom/spin/internal/linear_bvh/build_radix_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ axom::int64 morton64_encode( axom::float32 x,
template < typename ExecSpace, typename FloatType >
void transform_boxes( const FloatType *boxes,
AABB<FloatType,3> *aabbs,
int32 size)
int32 size,
FloatType scale_factor )
{
constexpr int NDIMS = 3;
constexpr int STRIDE = 2 * NDIMS;
Expand All @@ -126,6 +127,8 @@ void transform_boxes( const FloatType *boxes,

aabb.include(min_point);
aabb.include(max_point);
aabb.scale( scale_factor );

aabbs[i] = aabb;
} );

Expand All @@ -135,7 +138,8 @@ void transform_boxes( const FloatType *boxes,
template < typename ExecSpace, typename FloatType >
void transform_boxes( const FloatType *boxes,
AABB<FloatType,2> *aabbs,
int32 size)
int32 size,
FloatType scale_factor )
{
constexpr int NDIMS = 2;
constexpr int STRIDE = 2 * NDIMS;
Expand All @@ -156,6 +160,8 @@ void transform_boxes( const FloatType *boxes,

aabb.include(min_point);
aabb.include(max_point);
aabb.scale( scale_factor );

aabbs[ i ] = aabb;
} );

Expand Down Expand Up @@ -374,12 +380,12 @@ void custom_sort( ExecSpace, uint32*& mcodes, int32 size, int32* iter )
{
array_counting< ExecSpace >(iter, size, 0, 1);

std::sort(iter,
iter + size,
[=](int32 i1, int32 i2)
{
return mcodes[i1] < mcodes[i2];
} );
std::stable_sort( iter,
iter + size,
[=](int32 i1, int32 i2)
{
return mcodes[i1] < mcodes[i2];
} );


reorder< ExecSpace >(iter, mcodes, size);
Expand Down Expand Up @@ -653,7 +659,8 @@ template < typename ExecSpace, typename FloatType, int NDIMS >
void build_radix_tree( const FloatType* boxes,
int size,
AABB< FloatType, NDIMS >& bounds,
RadixTree< FloatType, NDIMS >& radix_tree )
RadixTree< FloatType, NDIMS >& radix_tree,
FloatType scale_factor )
{
// sanity checks
SLIC_ASSERT( boxes !=nullptr );
Expand All @@ -662,7 +669,8 @@ void build_radix_tree( const FloatType* boxes,
radix_tree.allocate( size );

// copy so we don't reorder the input
transform_boxes< ExecSpace >(boxes, radix_tree.m_leaf_aabbs, size);
transform_boxes< ExecSpace >( boxes, radix_tree.m_leaf_aabbs,
size, scale_factor );

// evaluate global bounds
bounds = reduce< ExecSpace >(radix_tree.m_leaf_aabbs, size);
Expand Down

0 comments on commit 9592cbc

Please sign in to comment.