@@ -213,20 +213,35 @@ impl TileTraits for Tile {
213213 }
214214
215215 fn intersects ( & self , other : & Tile ) -> bool {
216+
217+ // Note: It is easier to match tiles on their ids. This seems elegant
218+ // because we don't need to care about rotation. This was how it
219+ // was first implemented but it required self and other to be on
220+ // the same grid. This was not enforced so you would get weird
221+ // results if this was not upheld. Rather than forcing the grids
222+ // to be aligned we use the tile corner coordinates. This way
223+ // any tile can be checked for intersection with any other tile
224+ // purely based on location, regardless of the host grid. This
225+ // does have the downside that we need to account for rotated
226+ // grids where the corner that was originally the on the left
227+ // side might now be the rightmost corner.
216228 let corners_self = self . corners ( ) ;
217229 let corners_other = other. corners ( ) ;
218230
219- let left_self = corners_self[ Ix2 ( 3 , 0 ) ] ;
220- let bottom_self = corners_self[ Ix2 ( 3 , 1 ) ] ;
221- let right_self = corners_self[ Ix2 ( 1 , 0 ) ] ;
222- let top_self = corners_self[ Ix2 ( 1 , 1 ) ] ;
223-
224- let left_other = corners_other[ Ix2 ( 3 , 0 ) ] ;
225- let bottom_other = corners_other[ Ix2 ( 3 , 1 ) ] ;
226- let right_other = corners_other[ Ix2 ( 1 , 0 ) ] ;
227- let top_other = corners_other[ Ix2 ( 1 , 1 ) ] ;
228- return !(
229- left_self >= right_other
231+ // Get min and max values.
232+ // Note: This is a thorn in the eye as .min() does not work
233+ // on ndarrays of dtype float because of nan and inf values.
234+ let left_self = corners_self. slice ( s ! [ .., 0 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: min) . unwrap ( ) ;
235+ let bottom_self = corners_self. slice ( s ! [ .., 1 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: min) . unwrap ( ) ;
236+ let right_self = corners_self. slice ( s ! [ .., 0 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: max) . unwrap ( ) ;
237+ let top_self = corners_self. slice ( s ! [ .., 1 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: max) . unwrap ( ) ;
238+
239+ let left_other = corners_other. slice ( s ! [ .., 0 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: min) . unwrap ( ) ;
240+ let bottom_other = corners_other. slice ( s ! [ .., 1 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: min) . unwrap ( ) ;
241+ let right_other = corners_other. slice ( s ! [ .., 0 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: max) . unwrap ( ) ;
242+ let top_other = corners_other. slice ( s ! [ .., 1 ] ) . iter ( ) . cloned ( ) . reduce ( f64:: max) . unwrap ( ) ;
243+
244+ return !( left_self >= right_other
230245 || right_self <= left_other
231246 || bottom_self >= top_other
232247 || top_self <= bottom_other
0 commit comments