Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Bounding Box): add method to get the largest axe of the BBox #1003

Merged
merged 9 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bindings/python/src/geometry/bounding_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
.def( "max", &BoundingBox##dimension##D::max ) \
.def( "center", &BoundingBox##dimension##D::center ) \
.def( "diagonal", &BoundingBox##dimension##D::diagonal ) \
.def( "smallest_length", &BoundingBox##dimension##D::smallest_length )
.def( "smallest_length", &BoundingBox##dimension##D::smallest_length ) \
.def( \
"largest_length", &BoundingBox##dimension##D::largest_dimension )

namespace geode
{
Expand Down
8 changes: 8 additions & 0 deletions include/geode/basic/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ namespace geode
in.erase( last, in.end() );
}

template < typename Container, typename Comparison >
void sort_unique( Container& in, Comparison comp )
panquez marked this conversation as resolved.
Show resolved Hide resolved
{
absl::c_sort( in, comp );
const auto last = std::unique( in.begin(), in.end(), comp );
in.erase( last, in.end() );
}

/*!
* Concatenate tuples into a single tuple.
*/
Expand Down
4 changes: 3 additions & 1 deletion include/geode/geometry/bounding_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ namespace geode

Vector< dimension > diagonal() const;

double smallest_length() const;
std::tuple< local_index_t, double > smallest_length() const;

std::tuple< local_index_t, double > largest_length() const;

private:
Point< dimension > min_;
Expand Down
42 changes: 35 additions & 7 deletions src/geode/geometry/bounding_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ namespace
}
return line.origin().value( 0 ) > box.max().value( 0 );
}

template < geode::index_t dimension, typename Condition >
std::tuple< geode::local_index_t, double > test_axes(
const geode::Vector< dimension >& diagonal,
panquez marked this conversation as resolved.
Show resolved Hide resolved
double length,
Condition predicate )
{
geode::local_index_t axis{ 0 };
for( const auto i : geode::LRange{ dimension } )
{
if( predicate( diagonal.value( i ), length ) )
{
length = diagonal.value( i );
axis = i;
}
}
return std::make_tuple( axis, length );
}
} // namespace

namespace geode
Expand Down Expand Up @@ -408,15 +426,25 @@ namespace geode
}

template < index_t dimension >
double BoundingBox< dimension >::smallest_length() const
std::tuple< local_index_t, double >
BoundingBox< dimension >::smallest_length() const
{
auto length = std::numeric_limits< double >::max();
const auto diag = diagonal();
for( const auto i : LRange{ dimension } )
{
length = std::min( length, diag.value( i ) );
}
return length;
return test_axes(
diagonal(), length, []( double diagonal_value, double min_value ) {
return diagonal_value < min_value;
} );
}

template < index_t dimension >
std::tuple< local_index_t, double >
BoundingBox< dimension >::largest_length() const
{
double length{ 0 };
return test_axes(
diagonal(), length, []( double diagonal_value, double max_value ) {
return diagonal_value > max_value;
} );
}

template < index_t dimension >
Expand Down
Loading