Skip to content

Commit edae136

Browse files
committed
increase robustness of cylindrical and surface robustness checks
1 parent fd1bc26 commit edae136

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/surface.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ double axis_aligned_cylinder_evaluate(
391391
{
392392
const double r1 = r.get<i1>() - offset1;
393393
const double r2 = r.get<i2>() - offset2;
394-
return r1 * r1 + r2 * r2 - radius * radius;
394+
return std::sqrt(r1 * r1 + r2 * r2) - radius;
395395
}
396396

397397
// The first template parameter indicates which axis the cylinder is aligned to.
@@ -408,14 +408,15 @@ double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
408408
const double r2 = r.get<i2>() - offset1;
409409
const double r3 = r.get<i3>() - offset2;
410410
const double k = r2 * u.get<i2>() + r3 * u.get<i3>();
411-
const double c = r2 * r2 + r3 * r3 - radius * radius;
412-
const double quad = k * k - a * c;
411+
const double R = std::sqrt(r2 * r2 + r3 * r3);
412+
const double quad = k * k - a * (R - radius) * (R + radius);
413413

414414
if (quad < 0.0) {
415415
// No intersection with cylinder.
416416
return INFTY;
417417

418-
} else if (coincident || std::abs(c) < FP_COINCIDENT) {
418+
} else if (coincident ||
419+
std::abs(R - radius) < FP_COINCIDENT * (1.0 + radius)) {
419420
// Particle is on the cylinder, thus one distance is positive/negative
420421
// and the other is zero. The sign of k determines if we are facing in or
421422
// out.
@@ -425,7 +426,7 @@ double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
425426
return (-k + sqrt(quad)) / a;
426427
}
427428

428-
} else if (c < 0.0) {
429+
} else if (R < radius) {
429430
// Particle is inside the cylinder, thus one distance must be negative
430431
// and one must be positive. The positive distance will be the one with
431432
// negative sign on sqrt(quad).
@@ -601,7 +602,7 @@ double SurfaceSphere::evaluate(Position r) const
601602
const double x = r.x - x0_;
602603
const double y = r.y - y0_;
603604
const double z = r.z - z0_;
604-
return x * x + y * y + z * z - radius_ * radius_;
605+
return std::sqrt(x * x + y * y + z * z) - radius_;
605606
}
606607

607608
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
@@ -610,14 +611,15 @@ double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
610611
const double y = r.y - y0_;
611612
const double z = r.z - z0_;
612613
const double k = x * u.x + y * u.y + z * u.z;
613-
const double c = x * x + y * y + z * z - radius_ * radius_;
614-
const double quad = k * k - c;
614+
const double R = std::sqrt(x * x + y * y + z * z);
615+
const double quad = k * k - (R - radius_) * (R + radius_);
615616

616617
if (quad < 0.0) {
617618
// No intersection with sphere.
618619
return INFTY;
619620

620-
} else if (coincident || std::abs(c) < FP_COINCIDENT) {
621+
} else if (coincident ||
622+
std::abs(R - radius_) < FP_COINCIDENT * (1.0 + radius_)) {
621623
// Particle is on the sphere, thus one distance is positive/negative and
622624
// the other is zero. The sign of k determines if we are facing in or out.
623625
if (k >= 0.0) {
@@ -626,7 +628,7 @@ double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
626628
return -k + sqrt(quad);
627629
}
628630

629-
} else if (c < 0.0) {
631+
} else if (R < radius_) {
630632
// Particle is inside the sphere, thus one distance must be negative and
631633
// one must be positive. The positive distance will be the one with
632634
// negative sign on sqrt(quad)

0 commit comments

Comments
 (0)