Skip to content

Commit c660c64

Browse files
committed
Compute closest intersection in a loop
1 parent e5bf134 commit c660c64

1 file changed

Lines changed: 20 additions & 29 deletions

File tree

sbncode/CAFMaker/FillTrue.cxx

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,58 +1260,49 @@ caf::Wall_t caf::GetWallCross(const geo::BoxBoundedGeo &volume, const TVector3 p
12601260
TVector3 direction = (p1 - p0) * ( 1. / (p1 - p0).Mag());
12611261
std::vector<TVector3> intersections = volume.GetIntersections(p0, direction);
12621262

1263-
//assert(intersections.size() == 2);
12641263
/*
1265-
* There are either infinity, two, or one, or zero intersection points.
1264+
* There are either two, or one, or zero intersection points.
12661265
* As per larcorealg/Geometry/BoxBoundedGeo.h documentation:
12671266
* If the return std::vector is empty the trajectory does not intersect with the box.
12681267
* Normally the return value should have one (if the trajectory originates in the box) or two (else) entries.
12691268
* If the return value has two entries the first represents the entry point and the second the exit point
12701269
*/
1271-
switch( intersections.size() ) {
1272-
case 0: // Set kWallNone and return
1273-
return caf::kWallNone;
1274-
break;
1275-
case 1: // Set the second intersection to be the same as the first, and fall through
1276-
intersections.emplace_back(intersections.front());
1277-
[[fallthrough]];
1278-
case 2:
1279-
break;
1280-
default: // There are infinity points to consider. Just use the first two.
1281-
intersections.resize(2);
1282-
break;
1283-
}
1270+
1271+
if( intersections.empty() ) return caf::kWallNone;
1272+
// ensure intersections has two points. No op if already 2
1273+
intersections.resize( 2, intersections.front() );
12841274

12851275
// get the intersection point closer to p0
1286-
int intersection_i = ((intersections[0] - p0).Mag() < (intersections[1] - p0).Mag()) ? 0 : 1;
1276+
TVector3 closestIntersection;
1277+
double minDistance2 = std::numeric_limits<double>::max();
1278+
1279+
for( TVector3 const & point : intersections ) {
1280+
const double d2 = (point - p0).Mag2();
1281+
if( d2 > minDistance2 ) continue;
1282+
minDistance2 = d2;
1283+
closestIntersection = point;
1284+
}
12871285

12881286
double eps = 1e-3;
1289-
if (abs(intersections[intersection_i].X() - volume.MinX()) < eps) {
1290-
//std::cout << "Left\n";
1287+
if (abs(closestIntersection.X() - volume.MinX()) < eps) {
12911288
return caf::kWallLeft;
12921289
}
1293-
else if (abs(intersections[intersection_i].X() - volume.MaxX()) < eps) {
1294-
//std::cout << "Right\n";
1290+
else if (abs(closestIntersection.X() - volume.MaxX()) < eps) {
12951291
return caf::kWallRight;
12961292
}
1297-
else if (abs(intersections[intersection_i].Y() - volume.MinY()) < eps) {
1298-
//std::cout << "Bottom\n";
1293+
else if (abs(closestIntersection.Y() - volume.MinY()) < eps) {
12991294
return caf::kWallBottom;
13001295
}
1301-
else if (abs(intersections[intersection_i].Y() - volume.MaxY()) < eps) {
1302-
//std::cout << "Top\n";
1296+
else if (abs(closestIntersection.Y() - volume.MaxY()) < eps) {
13031297
return caf::kWallTop;
13041298
}
1305-
else if (abs(intersections[intersection_i].Z() - volume.MinZ()) < eps) {
1306-
//std::cout << "Front\n";
1299+
else if (abs(closestIntersection.Z() - volume.MinZ()) < eps) {
13071300
return caf::kWallFront;
13081301
}
1309-
else if (abs(intersections[intersection_i].Z() - volume.MaxZ()) < eps) {
1310-
//std::cout << "Back\n";
1302+
else if (abs(closestIntersection.Z() - volume.MaxZ()) < eps) {
13111303
return caf::kWallBack;
13121304
}
13131305
else assert(false);
1314-
//std::cout << "None\n";
13151306

13161307
return caf::kWallNone;
13171308
}//GetWallCross

0 commit comments

Comments
 (0)