diff --git a/include/manifold/common.h b/include/manifold/common.h index a56cb610f7..0a66836265 100644 --- a/include/manifold/common.h +++ b/include/manifold/common.h @@ -136,7 +136,10 @@ inline double sind(double x) { * * @param x Angle in degrees. */ -inline double cosd(double x) { return sind(x + 90.0); } +inline double cosd(double x) { + if (!la::isfinite(x)) return NAN; + return sind(90.0 - std::fmod(std::fabs(x), 360.0)); +} /** @} */ /** @addtogroup Structs diff --git a/test/cross_section_test.cpp b/test/cross_section_test.cpp index bfae5e8ce0..e72122124a 100644 --- a/test/cross_section_test.cpp +++ b/test/cross_section_test.cpp @@ -2655,6 +2655,13 @@ TEST(CrossSection, Rect) { EXPECT_TRUE(Rect().IsEmpty()); } +TEST(CrossSection, LargeRotationsPreserveArea) { + const CrossSection rectangle = CrossSection::Square({2, 3}); + for (const double angle : {1e18, -1e18, 1e20, -1e20}) { + EXPECT_NEAR(rectangle.Rotate(angle).Area(), rectangle.Area(), 1e-10); + } +} + TEST(CrossSection, Transform) { auto sq = CrossSection::Square({10., 10.}); auto a = sq.Rotate(45).Scale({2, 3}).Translate({4, 5}); diff --git a/test/manifold_test.cpp b/test/manifold_test.cpp index 18b6c23f79..71194074f4 100644 --- a/test/manifold_test.cpp +++ b/test/manifold_test.cpp @@ -359,6 +359,19 @@ TEST(Manifold, NormalsRotateBeforeCalc) { EXPECT_EQ(bad, 0); } +TEST(Manifold, LargeRotationsPreserveVolume) { + const Manifold cube = Manifold::Cube({2, 3, 4}); + for (const double angle : {1e18, -1e18, 1e20, -1e20}) { + for (int axis = 0; axis < 3; ++axis) { + vec3 angles(0.0); + angles[axis] = angle; + const Manifold rotated = cube.Rotate(angles.x, angles.y, angles.z); + ASSERT_EQ(rotated.Status(), Manifold::Error::NoError); + EXPECT_NEAR(rotated.Volume(), cube.Volume(), 1e-10); + } + } +} + TEST(Manifold, NormalsRotateAfterCalc) { // Rotation *after* CalculateNormals: Impl::Transform eager-transforms the // stored slot 0..2 so it tracks the new orientation. diff --git a/test/utils_test.cpp b/test/utils_test.cpp index 99af22807e..9624be2fdd 100644 --- a/test/utils_test.cpp +++ b/test/utils_test.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "../src/atomic_compat.h" #include "../src/shared.h" @@ -22,6 +23,30 @@ using namespace manifold; +TEST(Utils, CosdLargeAngles) { + for (const double angle : {1e18, -1e18, 1e20, -1e20, 1e100, -1e100, + std::numeric_limits::max(), + -std::numeric_limits::max()}) { + SCOPED_TRACE(testing::Message() << "angle = " << angle); + const double reduced = std::remainder(angle, 360.0); + EXPECT_NEAR(cosd(angle), std::cos(radians(reduced)), 1e-15); + EXPECT_NEAR(sind(angle) * sind(angle) + cosd(angle) * cosd(angle), 1.0, + 1e-15); + } +} + +TEST(Utils, CosdQuadrantBoundaries) { + const double expected[] = {1.0, 0.0, -1.0, 0.0}; + for (int quarter = -8; quarter <= 8; ++quarter) { + EXPECT_DOUBLE_EQ(cosd(quarter * 90.0), expected[(quarter + 8) % 4]); + } + for (const double angle : {std::numeric_limits::infinity(), + -std::numeric_limits::infinity(), + std::numeric_limits::quiet_NaN()}) { + EXPECT_TRUE(std::isnan(cosd(angle))); + } +} + TEST(Utils, BarycentricAllWeightsSnappedToZero) { // Both points are within tolerance of every edge, but not any vertex. // Scaling both the triangle and tolerance must preserve the equal fallback.