Skip to content

Commit b9f411c

Browse files
committed
deal with ellipses not aligned on axis
1 parent 0e96dd8 commit b9f411c

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/geode/geometry/distance.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,15 +1336,22 @@ namespace geode
13361336
{
13371337
std::tuple< double, Point< dimension > > result;
13381338
std::array< double, dimension > point_coordinates;
1339+
const Vector< dimension > center_to_point{ ellipse.center(), point };
13391340
for( const auto i : geode::LRange{ dimension } )
13401341
{
1341-
point_coordinates[i] = point.value( i );
1342+
point_coordinates[i] = center_to_point.dot(
1343+
ellipse.axis().direction( i ).normalize() );
13421344
}
13431345
auto& [distance, closest_point] = result;
13441346
const auto [squared_distance, closest_point_result] =
13451347
SquaredDistance< dimension >( ellipse, point_coordinates );
13461348
distance = std::sqrt( squared_distance );
1347-
closest_point = closest_point_result;
1349+
closest_point = ellipse.center();
1350+
for( const auto i : geode::LRange{ dimension } )
1351+
{
1352+
closest_point += ellipse.axis().direction( i ).normalize()
1353+
* closest_point_result.value( i );
1354+
}
13481355
return result;
13491356
}
13501357

tests/geometry/test-distance.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,21 @@ void test_point_ellipse_distance_2d()
11231123
"point" );
11241124
}
11251125

1126+
void test_point_ellipse_distance_2d_not_aligned()
1127+
{
1128+
const geode::Point2D center{ { 0.0, 0.0 } };
1129+
const geode::Vector2D first_axis{ { 1.0, 1.0 } };
1130+
const geode::Vector2D second_axis{ { -1.0, 1.0 } };
1131+
const geode::Frame2D frame{ { first_axis, second_axis } };
1132+
const geode::Ellipse2D ellipse{ center, frame };
1133+
const geode::Point2D point{ { 2.0, 2.0 } };
1134+
const auto [distance, closest_point] =
1135+
geode::point_ellipse_distance( point, ellipse );
1136+
const geode::Point2D result{ { 1.0, 1.0 } };
1137+
OPENGEODE_EXCEPTION( closest_point.inexact_equal( result ),
1138+
"[Test] Wrong result for point_ellipse_distance_2d_not_aligned" );
1139+
}
1140+
11261141
void test_point_ellipse_distance_3d()
11271142
{
11281143
const geode::Point3D center{ { 0.0, 0.0, 0.0 } };
@@ -1169,23 +1184,25 @@ void test_point_ellipse_distance_3d()
11691184

11701185
void test_point_ellipse_distance()
11711186
{
1187+
geode::Logger::set_level( geode::Logger::LEVEL::trace );
11721188
test_point_ellipse_distance_2d();
11731189
test_point_ellipse_distance_3d();
1190+
test_point_ellipse_distance_2d_not_aligned();
11741191
}
11751192

11761193
void test()
11771194
{
1178-
test_point_segment_distance();
1179-
test_segment_segment_distance();
1180-
test_segment_line_distance();
1181-
test_point_line_distance();
1182-
test_point_triangle_distance();
1183-
test_point_tetrahedron_distance();
1184-
test_point_plane_distance();
1185-
test_point_sphere_distance();
1186-
test_point_circle_distance();
1187-
test_line_triangle_distance();
1188-
test_segment_triangle_distance();
1195+
// test_point_segment_distance();
1196+
// test_segment_segment_distance();
1197+
// test_segment_line_distance();
1198+
// test_point_line_distance();
1199+
// test_point_triangle_distance();
1200+
// test_point_tetrahedron_distance();
1201+
// test_point_plane_distance();
1202+
// test_point_sphere_distance();
1203+
// test_point_circle_distance();
1204+
// test_line_triangle_distance();
1205+
// test_segment_triangle_distance();
11891206
test_point_ellipse_distance();
11901207
}
11911208

0 commit comments

Comments
 (0)