@@ -1260,38 +1260,47 @@ 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 );
1263+ /*
1264+ * There are either two, or one, or zero intersection points.
1265+ * As per larcorealg/Geometry/BoxBoundedGeo.h documentation:
1266+ * If the return std::vector is empty the trajectory does not intersect with the box.
1267+ * Normally the return value should have one (if the trajectory originates in the box) or two (else) entries.
1268+ * If the return value has two entries the first represents the entry point and the second the exit point
1269+ */
1270+
1271+ if ( intersections.empty () ) return caf::kWallNone ;
12641272
12651273 // get the intersection point closer to p0
1266- int intersection_i = ((intersections[0 ] - p0).Mag () < (intersections[1 ] - p0).Mag ()) ? 0 : 1 ;
1274+ TVector3 closestIntersection;
1275+ double minDistance2 = std::numeric_limits<double >::max ();
1276+
1277+ for ( TVector3 const & point : intersections ) {
1278+ const double d2 = (point - p0).Mag2 ();
1279+ if ( d2 > minDistance2 ) continue ;
1280+ minDistance2 = d2;
1281+ closestIntersection = point;
1282+ }
12671283
12681284 double eps = 1e-3 ;
1269- if (abs (intersections[intersection_i].X () - volume.MinX ()) < eps) {
1270- // std::cout << "Left\n";
1285+ if (abs (closestIntersection.X () - volume.MinX ()) < eps) {
12711286 return caf::kWallLeft ;
12721287 }
1273- else if (abs (intersections[intersection_i].X () - volume.MaxX ()) < eps) {
1274- // std::cout << "Right\n";
1288+ else if (abs (closestIntersection.X () - volume.MaxX ()) < eps) {
12751289 return caf::kWallRight ;
12761290 }
1277- else if (abs (intersections[intersection_i].Y () - volume.MinY ()) < eps) {
1278- // std::cout << "Bottom\n";
1291+ else if (abs (closestIntersection.Y () - volume.MinY ()) < eps) {
12791292 return caf::kWallBottom ;
12801293 }
1281- else if (abs (intersections[intersection_i].Y () - volume.MaxY ()) < eps) {
1282- // std::cout << "Top\n";
1294+ else if (abs (closestIntersection.Y () - volume.MaxY ()) < eps) {
12831295 return caf::kWallTop ;
12841296 }
1285- else if (abs (intersections[intersection_i].Z () - volume.MinZ ()) < eps) {
1286- // std::cout << "Front\n";
1297+ else if (abs (closestIntersection.Z () - volume.MinZ ()) < eps) {
12871298 return caf::kWallFront ;
12881299 }
1289- else if (abs (intersections[intersection_i].Z () - volume.MaxZ ()) < eps) {
1290- // std::cout << "Back\n";
1300+ else if (abs (closestIntersection.Z () - volume.MaxZ ()) < eps) {
12911301 return caf::kWallBack ;
12921302 }
12931303 else assert (false );
1294- // std::cout << "None\n";
12951304
12961305 return caf::kWallNone ;
12971306}// GetWallCross
0 commit comments