Skip to content
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
5 changes: 4 additions & 1 deletion include/manifold/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/cross_section_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
13 changes: 13 additions & 0 deletions test/manifold_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions test/utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,38 @@
#include <atomic>
#include <cmath>
#include <cstdint>
#include <limits>

#include "../src/atomic_compat.h"
#include "../src/shared.h"
#include "gtest/gtest.h"

using namespace manifold;

TEST(Utils, CosdLargeAngles) {
for (const double angle : {1e18, -1e18, 1e20, -1e20, 1e100, -1e100,
std::numeric_limits<double>::max(),
-std::numeric_limits<double>::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<double>::infinity(),
-std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::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.
Expand Down
Loading