Skip to content

Commit

Permalink
refactor: Early exit for CylinderBounds::inside (acts-project#3207)
Browse files Browse the repository at this point in the history
Move code to the left and by leveraging early return statements. Have a clear unconditional return statement at the end of the function.

Pulled out of acts-project#3170
  • Loading branch information
andiwand authored and EleniXoch committed May 31, 2024
1 parent aa6ba05 commit 79bb921
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions Core/src/Surfaces/CylinderBounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,49 @@ bool Acts::CylinderBounds::inside(const Vector2& lposition,

double halfLengthZ = get(eHalfLengthZ);
double halfPhi = get(eHalfPhiSector);
if (bevelMinZ != 0. && bevelMaxZ != 0.) {
double radius = get(eR);
// Beleved sides will unwrap to a trapezoid
///////////////////////////////////
// ________
// /| . . |\ r/phi
// \|______|/ r/phi
// -Z 0 Z
///////////////////////////////////
float localx =
lposition[0] > radius ? 2 * radius - lposition[0] : lposition[0];
Vector2 shiftedlposition = shifted(lposition);
if ((std::fabs(shiftedlposition[0]) <= halfPhi &&
std::fabs(shiftedlposition[1]) <= halfLengthZ)) {
return true;
} else if ((lposition[1] >=
-(localx * std::tan(bevelMinZ) + halfLengthZ)) &&
(lposition[1] <= (localx * std::tan(bevelMaxZ) + halfLengthZ))) {
return true;
} else {
// check within tolerance
auto boundaryCheck = bcheck.transformed(jacobian());

Vector2 lowerLeft = {-radius, -halfLengthZ};
Vector2 middleLeft = {0., -(halfLengthZ + radius * std::tan(bevelMinZ))};
Vector2 upperLeft = {radius, -halfLengthZ};
Vector2 upperRight = {radius, halfLengthZ};
Vector2 middleRight = {0., (halfLengthZ + radius * std::tan(bevelMaxZ))};
Vector2 lowerRight = {-radius, halfLengthZ};
Vector2 vertices[] = {lowerLeft, middleLeft, upperLeft,
upperRight, middleRight, lowerRight};
Vector2 closestPoint =
boundaryCheck.computeClosestPointOnPolygon(lposition, vertices);

return boundaryCheck.isTolerated(closestPoint - lposition);
}
} else {

if (bevelMinZ == 0. || bevelMaxZ == 0.) {
return bcheck.transformed(jacobian())
.isInside(shifted(lposition), Vector2(-halfPhi, -halfLengthZ),
Vector2(halfPhi, halfLengthZ));
}

double radius = get(eR);
// Beleved sides will unwrap to a trapezoid
///////////////////////////////////
// ________
// /| . . |\ r/phi
// \|______|/ r/phi
// -Z 0 Z
///////////////////////////////////
float localx =
lposition[0] > radius ? 2 * radius - lposition[0] : lposition[0];
Vector2 shiftedlposition = shifted(lposition);
if ((std::fabs(shiftedlposition[0]) <= halfPhi &&
std::fabs(shiftedlposition[1]) <= halfLengthZ)) {
return true;
}

if ((lposition[1] >= -(localx * std::tan(bevelMinZ) + halfLengthZ)) &&
(lposition[1] <= (localx * std::tan(bevelMaxZ) + halfLengthZ))) {
return true;
}

// check within tolerance
auto boundaryCheck = bcheck.transformed(jacobian());

Vector2 lowerLeft = {-radius, -halfLengthZ};
Vector2 middleLeft = {0., -(halfLengthZ + radius * std::tan(bevelMinZ))};
Vector2 upperLeft = {radius, -halfLengthZ};
Vector2 upperRight = {radius, halfLengthZ};
Vector2 middleRight = {0., (halfLengthZ + radius * std::tan(bevelMaxZ))};
Vector2 lowerRight = {-radius, halfLengthZ};
Vector2 vertices[] = {lowerLeft, middleLeft, upperLeft,
upperRight, middleRight, lowerRight};
Vector2 closestPoint =
boundaryCheck.computeClosestPointOnPolygon(lposition, vertices);

return boundaryCheck.isTolerated(closestPoint - lposition);
}

bool Acts::CylinderBounds::inside3D(const Vector3& position,
Expand Down

0 comments on commit 79bb921

Please sign in to comment.