@@ -203,16 +203,16 @@ impl TileSchema {
203203 let pix_bound = ( self . bounds . x_max ( ) - self . origin . x ( ) ) / resolution;
204204 let floored = pix_bound. floor ( ) ;
205205 if ( pix_bound - floored) . abs ( ) < 0.1 {
206- ( floored / self . tile_width as f64 ) as i32 - 1
206+ ( pix_bound / self . tile_width as f64 ) as i32 - 1
207207 } else {
208- ( floored / self . tile_width as f64 ) as i32
208+ ( pix_bound / self . tile_width as f64 ) as i32
209209 }
210210 }
211211
212212 fn min_y_index ( & self , resolution : f64 ) -> i32 {
213213 match self . y_direction {
214214 VerticalDirection :: TopToBottom => {
215- ( ( self . bounds . y_min ( ) + self . origin . y ( ) ) / resolution / self . tile_height as f64 )
215+ ( ( self . origin . y ( ) - self . bounds . y_max ( ) ) / resolution / self . tile_height as f64 )
216216 . floor ( ) as i32
217217 }
218218 VerticalDirection :: BottomToTop => {
@@ -224,14 +224,15 @@ impl TileSchema {
224224
225225 fn max_y_index ( & self , resolution : f64 ) -> i32 {
226226 let pix_bound = match self . y_direction {
227- VerticalDirection :: TopToBottom => ( self . bounds . y_max ( ) + self . origin . y ( ) ) / resolution,
227+ VerticalDirection :: TopToBottom => ( self . origin . y ( ) - self . bounds . y_min ( ) ) / resolution,
228228 VerticalDirection :: BottomToTop => ( self . bounds . y_max ( ) - self . origin . y ( ) ) / resolution,
229229 } ;
230+
230231 let floored = pix_bound. floor ( ) ;
231232 if ( pix_bound - floored) . abs ( ) < 0.1 {
232- ( floored / self . tile_height as f64 ) as i32 - 1
233+ ( pix_bound / self . tile_width as f64 ) as i32 - 1
233234 } else {
234- ( floored / self . tile_height as f64 ) as i32
235+ ( pix_bound / self . tile_width as f64 ) as i32
235236 }
236237 }
237238}
@@ -397,4 +398,97 @@ mod tests {
397398 4
398399 ) ;
399400 }
401+
402+ #[ test]
403+ fn iter_tiles_origin_out_of_bounds ( ) {
404+ let schema = TileSchema {
405+ origin : Point2 :: new ( 0.0 , 0.0 ) ,
406+ bounds : Rect :: new ( 1000.0 , 1000.0 , 2000.0 , 2000.0 ) ,
407+ lods : Arc :: new ( vec ! [ 30.0 , 10.0 , 1.0 ] ) ,
408+ tile_width : 10 ,
409+ tile_height : 10 ,
410+ y_direction : VerticalDirection :: BottomToTop ,
411+ wrap_x : false ,
412+ } ;
413+
414+ let tiles: Vec < _ > = schema
415+ . iter_tiles_over_bbox ( 10.0 , Rect :: new ( 0.0 , 0.0 , 500.0 , 500.0 ) )
416+ . unwrap ( )
417+ . collect ( ) ;
418+ assert ! (
419+ tiles. is_empty( ) ,
420+ "Expected empty tiles iter, but got: {tiles:?}"
421+ ) ;
422+
423+ let tiles: Vec < _ > = schema
424+ . iter_tiles_over_bbox ( 10.0 , Rect :: new ( 900.0 , 900.0 , 1100.0 , 1100.0 ) )
425+ . unwrap ( )
426+ . collect ( ) ;
427+
428+ assert_eq ! ( tiles. len( ) , 1 ) ;
429+ assert_eq ! ( tiles[ 0 ] , WrappingTileIndex :: new( 10 , 10 , 1 ) ) ;
430+
431+ let tiles: Vec < _ > = schema
432+ . iter_tiles_over_bbox ( 30.0 , Rect :: new ( 900.0 , 900.0 , 950.0 , 950.0 ) )
433+ . unwrap ( )
434+ . collect ( ) ;
435+
436+ assert_eq ! ( tiles. len( ) , 1 ) ;
437+ assert_eq ! ( tiles[ 0 ] , WrappingTileIndex :: new( 3 , 3 , 0 ) ) ;
438+ }
439+
440+ #[ test]
441+ fn iter_tiles_origin_out_of_bounds_top_to_bottom ( ) {
442+ let schema = TileSchema {
443+ origin : Point2 :: new ( 0.0 , 3000.0 ) ,
444+ bounds : Rect :: new ( 1000.0 , 1000.0 , 2000.0 , 2000.0 ) ,
445+ lods : Arc :: new ( vec ! [ 30.0 , 10.0 , 1.0 ] ) ,
446+ tile_width : 10 ,
447+ tile_height : 10 ,
448+ y_direction : VerticalDirection :: TopToBottom ,
449+ wrap_x : false ,
450+ } ;
451+
452+ let tiles: Vec < _ > = schema
453+ . iter_tiles_over_bbox ( 10.0 , Rect :: new ( 0.0 , 0.0 , 500.0 , 500.0 ) )
454+ . unwrap ( )
455+ . collect ( ) ;
456+ assert ! (
457+ tiles. is_empty( ) ,
458+ "Expected empty tiles iter, but got: {tiles:?}"
459+ ) ;
460+
461+ let tiles: Vec < _ > = schema
462+ . iter_tiles_over_bbox ( 10.0 , Rect :: new ( 900.0 , 900.0 , 1099.0 , 1099.0 ) )
463+ . unwrap ( )
464+ . collect ( ) ;
465+
466+ println ! ( "{tiles:?}" ) ;
467+ assert_eq ! ( tiles. len( ) , 1 ) ;
468+ assert_eq ! ( tiles[ 0 ] , WrappingTileIndex :: new( 10 , 19 , 1 ) ) ;
469+
470+ let tiles: Vec < _ > = schema
471+ . iter_tiles_over_bbox ( 30.0 , Rect :: new ( 900.0 , 900.0 , 950.0 , 950.0 ) )
472+ . unwrap ( )
473+ . collect ( ) ;
474+
475+ assert_eq ! ( tiles. len( ) , 1 ) ;
476+ assert_eq ! ( tiles[ 0 ] , WrappingTileIndex :: new( 3 , 6 , 0 ) ) ;
477+ }
478+
479+ #[ test]
480+ fn tile_bbox_origin_out_of_bounds ( ) {
481+ let schema = TileSchema {
482+ origin : Point2 :: new ( 0.0 , 0.0 ) ,
483+ bounds : Rect :: new ( 1000.0 , 1000.0 , 2000.0 , 2000.0 ) ,
484+ lods : Arc :: new ( vec ! [ 300.0 , 100.0 , 10.0 , 1.0 ] ) ,
485+ tile_width : 10 ,
486+ tile_height : 10 ,
487+ y_direction : VerticalDirection :: BottomToTop ,
488+ wrap_x : false ,
489+ } ;
490+
491+ let bbox = schema. tile_bbox ( WrappingTileIndex :: new ( 10 , 10 , 2 ) ) . unwrap ( ) ;
492+ assert_eq ! ( bbox, Rect :: new( 1000.0 , 1000.0 , 1100.0 , 1100.0 ) ) ;
493+ }
400494}
0 commit comments