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

Add octile distance for tripoints. #75964

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,14 @@ inline int octile_dist( const coords::coord_point_ob<Point, Origin, Scale> &loc1
return octile_dist( loc1.raw(), loc2.raw(), multiplier );
}


template<typename Point, coords::origin Origin, coords::scale Scale>
inline float octile_dist_exact( const coords::coord_point<Point, Origin, Scale> &loc1,
const coords::coord_point<Point, Origin, Scale> &loc2 )
{
return octile_dist_exact( loc1.raw(), loc2.raw() );
}

template<typename Point, coords::origin Origin, coords::scale Scale>
direction direction_from( const coords::coord_point_ob<Point, Origin, Scale> &loc1,
const coords::coord_point_ob<Point, Origin, Scale> &loc2 )
Expand Down
13 changes: 13 additions & 0 deletions src/line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,19 @@ float octile_dist_exact( const point &loc1, const point &loc2 )
return d.x + d.y - 2 * mind + mind * M_SQRT2;
}

float octile_dist_exact( const tripoint &from, const tripoint &to )
{
const tripoint d = ( from - to ).abs();
const int min = std::min( d.x, std::min( d.y, d.z ) );
const int max = std::max( d.x, std::max( d.y, d.z ) );
const int mid = d.x + d.y + d.z - min - max;

constexpr int one_axis = 1;
constexpr float two_axis = M_SQRT2;
constexpr float three_axis = 1.73205f;
return ( three_axis - two_axis ) * min + ( two_axis - one_axis ) * mid + one_axis * max;
}

units::angle atan2( const point &p )
{
return units::atan2( p.y, p.x );
Expand Down
3 changes: 2 additions & 1 deletion src/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ float rl_dist_exact( const tripoint &loc1, const tripoint &loc2 );
int manhattan_dist( const point &loc1, const point &loc2 );

// Travel distance between 2 points on a square grid, assuming diagonal moves
// cost sqrt(2) and cardinal moves cost 1.
// cost sqrt(2) or sqrt(3) and cardinal moves cost 1.
int octile_dist( const point &loc1, const point &loc2, int multiplier = 1 );
float octile_dist_exact( const point &loc1, const point &loc2 );
float octile_dist_exact( const tripoint &from, const tripoint &to );

// get angle of direction represented by point
units::angle atan2( const point & );
Expand Down
49 changes: 49 additions & 0 deletions tests/line_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,52 @@
CHECK( raw_line[i] == coord_line[i].raw() );
}
}

TEST_CASE( "octile_dist_exact_tripoints" )
{
constexpr int one_axis = 1;
constexpr float two_axis = M_SQRT2;
constexpr float three_axis = 1.73205f;
constexpr float eps = 0.05f;

// Check that unit distances are all correct.
CHECK( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 0, 0, 0 } ) == 0 );

Check failure on line 477 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]

Check failure on line 477 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 0, 0, 1 } ),

Check failure on line 478 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]

Check failure on line 478 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_above' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]
Catch::Matchers::WithinRel( one_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 0, 1, 0 } ),

Check failure on line 480 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]

Check failure on line 480 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_south' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]
Catch::Matchers::WithinRel( one_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 1, 0, 0 } ),

Check failure on line 482 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]

Check failure on line 482 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_east' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]
Catch::Matchers::WithinRel( one_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 10, 30, 20 }, tripoint{ 11, 30, 20 } ),
Catch::Matchers::WithinRel( 1, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 10, 30, 20 }, tripoint{ 10, 31, 20 } ),
Catch::Matchers::WithinRel( 1, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 10, 30, 21 }, tripoint{ 10, 30, 21 } ),
Catch::Matchers::WithinRel( 1, eps ) );

Check failure on line 489 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

0.0f and 1 are within 5% of each other

// Check that 2d unit distances are correct.
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 0, 1, 1 } ),

Check failure on line 492 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]
Catch::Matchers::WithinRel( two_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 1, 0, 1 } ),

Check failure on line 494 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / build (other)

Prefer constructing 'tripoint' from named constant 'tripoint_zero' rather than explicit integer arguments. [cata-use-named-point-constants,-warnings-as-errors]
Catch::Matchers::WithinRel( two_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 1, 1, 0 } ),
Catch::Matchers::WithinRel( two_axis, eps ) );

// Check that 3d unit distances are correct.
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 1, 1, 1 } ),
Catch::Matchers::WithinRel( three_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 2, 4, 6 }, tripoint{ 3, 5, 7 } ),
Catch::Matchers::WithinRel( three_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 26, 32, 75 }, tripoint{ 27, 33, 76 } ),
Catch::Matchers::WithinRel( three_axis, eps ) );

// Check a few non-unit distances.
CHECK_THAT( octile_dist_exact( tripoint{ 0, 0, 0 }, tripoint{ 2, 2, 0 } ),
Catch::Matchers::WithinRel( 2 * two_axis, eps ) );
CHECK_THAT( octile_dist_exact( tripoint{ 1, 2, 3 }, tripoint{ 4, 1, 2 } ),
Catch::Matchers::WithinRel( 3.316f, eps ) );

Check failure on line 511 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

3.73205f and 3.316 are within 5% of each other
CHECK_THAT( octile_dist_exact( tripoint{ 6, 9, 4 }, tripoint{ 8, 1, 1 } ),
Catch::Matchers::WithinRel( 8.775f, eps ) );

Check failure on line 513 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

9.87831f and 8.775 are within 5% of each other
CHECK_THAT( octile_dist_exact( tripoint{ -3, -6, -2 }, tripoint{ -10, -8, 4 } ),
Catch::Matchers::WithinRel( 9.434f, eps ) );

Check failure on line 515 in tests/line_test.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

10.12095f and 9.434 are within 5% of each other
}
Loading