Skip to content

Commit

Permalink
fix: normalize the output vector of Polygon::getNormalVector (#106)
Browse files Browse the repository at this point in the history
* fix: normalize the output vector of Polygon::getNormalVector

* test: add unit test
  • Loading branch information
kyle-cochran authored Feb 1, 2024
1 parent 93d7dc5 commit 39a7665
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Vector3d Polygon::getNormalVector() const
throw ostk::core::error::runtime::Undefined("Polygon");
}

return xAxis_.cross(yAxis_);
return xAxis_.cross(yAxis_).normalized();
}

Size Polygon::getEdgeCount() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ TEST(OpenSpaceToolkit_Mathematics_Geometry_3D_Objects_Polygon, GetYAxis)

TEST(OpenSpaceToolkit_Mathematics_Geometry_3D_Objects_Polygon, GetNormalVector)
{
using ostk::core::types::Real;

using ostk::math::object::Vector3d;
using Polygon2d = ostk::math::geometry::d2::objects::Polygon;
using ostk::math::geometry::d3::objects::Point;
Expand All @@ -315,6 +317,23 @@ TEST(OpenSpaceToolkit_Mathematics_Geometry_3D_Objects_Polygon, GetNormalVector)
EXPECT_EQ(Vector3d(0.0, 0.0, +1.0), Polygon(polygon2d, origin, xAxis, yAxis).getNormalVector());
}

{
const Polygon2d polygon2d = {{{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}}};
const Point origin = {1.0, 2.0, 3.0};
const Vector3d xAxis = {1.0000000000000006, 0.0, 0.0};

const Vector3d yAxis = {0.0, 1.0000000000000006, 0.0};

// X and Y axes can be considered unitary while their cross product is not
EXPECT_TRUE(
(xAxis.norm() - 1.0) < Real::Epsilon() && (yAxis.norm() - 1.0) < Real::Epsilon() &&
(xAxis.cross(yAxis).norm() - 1.0) > Real::Epsilon()
);

// The `getNormalVector` function normalizes the output
EXPECT_TRUE((Polygon(polygon2d, origin, xAxis, yAxis).getNormalVector().norm() - 1.0) < Real::Epsilon());
}

{
EXPECT_ANY_THROW(Polygon::Undefined().getNormalVector());
}
Expand Down

0 comments on commit 39a7665

Please sign in to comment.