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 all 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
2 changes: 1 addition & 1 deletion bindings/python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile bindings/python/requirements.in
# pip-compile --pre bindings/python/requirements.in
#
3 changes: 2 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,8 @@
.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_length )

namespace geode
{
Expand Down
18 changes: 13 additions & 5 deletions include/geode/basic/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,22 @@ namespace geode
/*!
* Modify the container by removing every duplicated values inside
* @tparam Container Type of container.
* @param[in] in container in which perform the search.
* @param[in] container container in which perform the search.
*/
template < typename Container >
void sort_unique( Container& in )
void sort_unique( Container& container )
{
absl::c_sort( in );
const auto last = std::unique( in.begin(), in.end() );
in.erase( last, in.end() );
absl::c_sort( container );
const auto last = std::unique( container.begin(), container.end() );
container.erase( last, container.end() );
}

template < typename Container, typename Comparison >
void sort_unique( Container& container, Comparison comp )
{
absl::c_sort( container, comp );
const auto last = std::unique( container.begin(), container.end() );
container.erase( last, container.end() );
}

/*!
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