Skip to content

Commit 67868d1

Browse files
Fix the math for inverted shapes (including cylinders)
1 parent 4a64fe3 commit 67868d1

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

src/geometry/Circle.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ namespace geometry {
5252
return std::numeric_limits<Scalar>::quiet_NaN();
5353
}
5454

55-
// Valid below the horizon
55+
// Below the horizon with positive height
5656
if (h > 0 && phi_n < M_PI_2) { return std::atan((2 * r / k + h * std::tan(phi_n)) / h); }
57-
// Valid above the horizon
57+
// Above the horizon with negative height
5858
else if (h < 0 && phi_n > M_PI_2) {
59-
return M_PI - std::atan((2 * r / k - h * std::tan(M_PI - phi_n)) / -h);
59+
return M_PI - phi(phi_n, -h);
6060
}
6161
// Other situations are invalid so return NaN
6262
else {
@@ -77,11 +77,11 @@ namespace geometry {
7777
// If we are beyond our max distance return nan
7878
if (std::abs(h) * tan(phi > M_PI_2 ? M_PI - phi : phi) > d) { return std::numeric_limits<Scalar>::quiet_NaN(); }
7979

80-
// Valid below the horizon
80+
// Below the horizon with positive height
8181
if (h > 0 && phi < M_PI_2) { return 2 * std::asin(r / (h * std::tan(phi) + r)) / k; }
82-
// Valid above the horizon
82+
// Above the horizon with negative height
8383
else if (h < 0 && phi > M_PI_2) {
84-
return 2 * std::asin(r / (-h * std::tan(M_PI - phi) + r)) / k;
84+
return theta(M_PI - phi, -h);
8585
}
8686
// Other situations are invalid so return NaN
8787
else {

src/geometry/Sphere.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ namespace geometry {
5252
return std::numeric_limits<Scalar>::quiet_NaN();
5353
}
5454

55-
// Our effective radius for the number of intersections
56-
Scalar er = (h / 2) * (1 - std::pow(1 - 2 * r / h, 1 / k));
57-
58-
// Valid below the horizon
55+
// Below the horizon with positive height
5956
if (h > 0 && phi_n < M_PI_2) {
57+
// Our effective radius for the number of intersections
58+
Scalar er = (h / 2) * (1 - std::pow(1 - 2 * r / h, 1 / k));
6059
return 2 * std::atan(er / (std::cos(phi_n) * (h - er)) + std::tan(phi_n)) - phi_n;
6160
}
62-
// Valid above the horizon
61+
// Above the horizon with negative height
6362
else if (h < 0 && phi_n > M_PI_2) {
64-
return 2 * std::atan(er / (std::cos(M_PI - phi_n) * (-h - er)) + std::tan(M_PI - phi_n)) - M_PI - phi_n;
63+
return M_PI - phi(M_PI - phi_n, -h);
6564
}
6665
// Other situations are invalid so return NaN
6766
else {
@@ -84,11 +83,11 @@ namespace geometry {
8483
return std::numeric_limits<Scalar>::quiet_NaN();
8584
}
8685

87-
// Valid below the horizon
86+
// Below the horizon with positive height
8887
if (h > 0 && phi < M_PI_2) { return 2 * std::asin(r / ((h - r) * std::tan(phi))) / k; }
89-
// Valid above the horizon
88+
// Above the horizon with negative height
9089
else if (h < 0 && phi > M_PI_2) {
91-
return 2 * std::asin(r / (-(h - r) * std::tan(M_PI - phi))) / k;
90+
return theta(M_PI - phi, -h);
9291
}
9392
// Other situations are invalid so return NaN
9493
else {

0 commit comments

Comments
 (0)