@@ -143,38 +143,47 @@ caf::Wall_t sbn::GetWallCross(const geo::BoxBoundedGeo &volume, const TVector3 p
143143 TVector3 direction = (p1 - p0) * ( 1 . / (p1 - p0).Mag ());
144144 std::vector<TVector3> intersections = volume.GetIntersections (p0, direction);
145145
146- assert (intersections.size () == 2 );
146+ /*
147+ * There are either two, or one, or zero intersection points.
148+ * As per larcorealg/Geometry/BoxBoundedGeo.h documentation:
149+ * If the return std::vector is empty the trajectory does not intersect with the box.
150+ * Normally the return value should have one (if the trajectory originates in the box) or two (else) entries.
151+ * If the return value has two entries the first represents the entry point and the second the exit point
152+ */
153+
154+ if ( intersections.empty () ) return caf::kWallNone ;
147155
148156 // get the intersection point closer to p0
149- int intersection_i = ((intersections[0 ] - p0).Mag () < (intersections[1 ] - p0).Mag ()) ? 0 : 1 ;
157+ TVector3 closestIntersection;
158+ double minDistance2 = std::numeric_limits<double >::max ();
159+
160+ for ( TVector3 const & point : intersections ) {
161+ const double d2 = (point - p0).Mag2 ();
162+ if ( d2 > minDistance2 ) continue ;
163+ minDistance2 = d2;
164+ closestIntersection = point;
165+ }
150166
151167 double eps = 1e-3 ;
152- if (abs (intersections[intersection_i].X () - volume.MinX ()) < eps) {
153- // std::cout << "Left\n";
168+ if (abs (closestIntersection.X () - volume.MinX ()) < eps) {
154169 return caf::kWallLeft ;
155170 }
156- else if (abs (intersections[intersection_i].X () - volume.MaxX ()) < eps) {
157- // std::cout << "Right\n";
171+ else if (abs (closestIntersection.X () - volume.MaxX ()) < eps) {
158172 return caf::kWallRight ;
159173 }
160- else if (abs (intersections[intersection_i].Y () - volume.MinY ()) < eps) {
161- // std::cout << "Bottom\n";
174+ else if (abs (closestIntersection.Y () - volume.MinY ()) < eps) {
162175 return caf::kWallBottom ;
163176 }
164- else if (abs (intersections[intersection_i].Y () - volume.MaxY ()) < eps) {
165- // std::cout << "Top\n";
177+ else if (abs (closestIntersection.Y () - volume.MaxY ()) < eps) {
166178 return caf::kWallTop ;
167179 }
168- else if (abs (intersections[intersection_i].Z () - volume.MinZ ()) < eps) {
169- // std::cout << "Front\n";
180+ else if (abs (closestIntersection.Z () - volume.MinZ ()) < eps) {
170181 return caf::kWallFront ;
171182 }
172- else if (abs (intersections[intersection_i].Z () - volume.MaxZ ()) < eps) {
173- // std::cout << "Back\n";
183+ else if (abs (closestIntersection.Z () - volume.MaxZ ()) < eps) {
174184 return caf::kWallBack ;
175185 }
176186 else assert (false );
177- // std::cout << "None\n";
178187
179188 return caf::kWallNone ;
180189}// GetWallCross
0 commit comments