|
|
@@ -1,10 +1,7 @@
|
|
1
|
|
-use cgmath::{Matrix3, Point3, Transform, vec3};
|
|
2
|
|
-use coord::{MapCoord, ScreenCoord, ScreenRect, TileCoord};
|
|
3
|
|
-use std::f32::consts::{PI, FRAC_1_PI};
|
|
4
|
|
-use std::f64;
|
|
|
1
|
+use coord::MapCoord;
|
|
5
|
2
|
|
|
6
|
3
|
|
|
7
|
|
-/// A view of a tiled map with a rectangular viewport and a zoom.
|
|
|
4
|
+/// A view of a map with a rectangular viewport and a zoom.
|
|
8
|
5
|
#[derive(Clone, Debug)]
|
|
9
|
6
|
pub struct MapView {
|
|
10
|
7
|
/// Width of the viewport.
|
|
|
@@ -23,13 +20,6 @@ pub struct MapView {
|
|
23
|
20
|
pub tile_zoom_offset: f64,
|
|
24
|
21
|
}
|
|
25
|
22
|
|
|
26
|
|
-/// The position and size of a specific tile on the screen.
|
|
27
|
|
-#[derive(Clone, Debug)]
|
|
28
|
|
-pub struct VisibleTile {
|
|
29
|
|
- pub tile: TileCoord,
|
|
30
|
|
- pub rect: ScreenRect,
|
|
31
|
|
-}
|
|
32
|
|
-
|
|
33
|
23
|
impl MapView {
|
|
34
|
24
|
/// Constructs a new `MapView`.
|
|
35
|
25
|
pub fn new(width: f64, height: f64, tile_size: u32, center: MapCoord, zoom: f64) -> MapView {
|
|
|
@@ -43,267 +33,9 @@ impl MapView {
|
|
43
|
33
|
}
|
|
44
|
34
|
}
|
|
45
|
35
|
|
|
46
|
|
- /// Constructs a new `MapView` centered at Null Island with an integer zoom that fills a screen
|
|
47
|
|
- /// with the given dimensions.
|
|
48
|
|
- pub fn with_filling_zoom(width: f64, height: f64, tile_size: u32) -> MapView {
|
|
49
|
|
- let min_dimension = width.min(height);
|
|
50
|
|
- let zoom = (min_dimension / f64::from(tile_size)).log2().ceil();
|
|
51
|
|
- MapView {
|
|
52
|
|
- width,
|
|
53
|
|
- height,
|
|
54
|
|
- tile_size,
|
|
55
|
|
- center: MapCoord::new(0.5, 0.5),
|
|
56
|
|
- zoom,
|
|
57
|
|
- tile_zoom_offset: 0.0,
|
|
58
|
|
- }
|
|
59
|
|
- }
|
|
60
|
|
-
|
|
61
|
|
- /// Returns the map coordinate that corresponds to the top-left corner of the viewport.
|
|
62
|
|
- pub fn top_left_coord(&self) -> MapCoord {
|
|
63
|
|
- let scale = f64::powf(2.0, -self.zoom) / f64::from(self.tile_size);
|
|
64
|
|
-
|
|
65
|
|
- let x = self.center.x + -0.5 * self.width * scale;
|
|
66
|
|
- let y = self.center.y + -0.5 * self.height * scale;
|
|
67
|
|
-
|
|
68
|
|
- MapCoord::new(x, y)
|
|
69
|
|
- }
|
|
70
|
|
-
|
|
71
|
|
- /// Returns the screen coordinate that corresponds to the given map coordinate.
|
|
72
|
|
- pub fn map_to_screen_coord(&self, map_coord: MapCoord) -> ScreenCoord {
|
|
73
|
|
- let scale = f64::powf(2.0, self.zoom) * f64::from(self.tile_size);
|
|
74
|
|
-
|
|
75
|
|
- let delta_x = map_coord.x - self.center.x;
|
|
76
|
|
- let delta_y = map_coord.y - self.center.y;
|
|
77
|
|
-
|
|
78
|
|
- ScreenCoord {
|
|
79
|
|
- x: 0.5 * self.width + delta_x * scale,
|
|
80
|
|
- y: 0.5 * self.height + delta_y * scale,
|
|
81
|
|
- }
|
|
82
|
|
- }
|
|
83
|
|
-
|
|
84
|
|
- /// Returns true if the viewport rectangle is fully inside the map.
|
|
85
|
|
- pub fn map_covers_viewport(&self) -> bool {
|
|
86
|
|
- let scale = f64::powf(2.0, -self.zoom) / f64::from(self.tile_size);
|
|
87
|
|
-
|
|
88
|
|
- let y_top = self.center.y + -0.5 * self.height * scale;
|
|
89
|
|
- let y_bottom = self.center.y + 0.5 * self.height * scale;
|
|
90
|
|
-
|
|
91
|
|
- y_top >= 0.0 && y_bottom <= 1.0
|
|
92
|
|
- }
|
|
93
|
|
-
|
|
94
|
|
- /// Returns true if the globe rendering covers the whole viewport.
|
|
95
|
|
- pub fn globe_covers_viewport(&self) -> bool {
|
|
96
|
|
- //TODO Add a little safety margin since the rendered globe is not a perfect sphere and its
|
|
97
|
|
- // screen area is underestimated by the tesselation.
|
|
98
|
|
- let globe_diameter = 2.0f64.powf(self.zoom) *
|
|
99
|
|
- (f64::consts::FRAC_1_PI * self.tile_size as f64);
|
|
100
|
|
-
|
|
101
|
|
- return (self.width * self.width) + (self.height * self.height) < globe_diameter * globe_diameter;
|
|
102
|
|
- }
|
|
103
|
|
-
|
|
104
|
|
- /// Returns the screen coordinate of the top-left corner of a tile.
|
|
105
|
|
- pub fn tile_screen_position(&self, tile: &TileCoord) -> ScreenCoord {
|
|
106
|
|
- self.map_to_screen_coord(tile.map_coord_north_west())
|
|
107
|
|
- }
|
|
108
|
|
-
|
|
109
|
|
- /// Returns a `Vec` of all tiles that are visible in the current viewport.
|
|
110
|
|
- pub fn visible_tiles(&self, snap_to_pixel: bool) -> Vec<VisibleTile> {
|
|
111
|
|
- let uzoom = self.tile_zoom();
|
|
112
|
|
- let top_left_tile = self.top_left_coord().on_tile_at_zoom(uzoom);
|
|
113
|
|
- let mut top_left_tile_screen_coord = self.tile_screen_position(&top_left_tile);
|
|
114
|
|
- let tile_screen_size = f64::powf(2.0, self.zoom - f64::from(uzoom)) * f64::from(self.tile_size);
|
|
115
|
|
-
|
|
116
|
|
- if snap_to_pixel {
|
|
117
|
|
- top_left_tile_screen_coord.snap_to_pixel();
|
|
118
|
|
- }
|
|
119
|
|
-
|
|
120
|
|
- let start_tile_x = top_left_tile.x;
|
|
121
|
|
- let start_tile_y = top_left_tile.y;
|
|
122
|
|
- let num_tiles_x = ((self.width - top_left_tile_screen_coord.x) / tile_screen_size).ceil().max(0.0) as i32;
|
|
123
|
|
- let num_tiles_y = ((self.height - top_left_tile_screen_coord.y) / tile_screen_size).ceil().max(0.0) as i32;
|
|
124
|
|
-
|
|
125
|
|
- let mut visible_tiles = Vec::with_capacity(num_tiles_x as usize * num_tiles_y as usize);
|
|
126
|
|
-
|
|
127
|
|
- for y in 0..num_tiles_y {
|
|
128
|
|
- for x in 0..num_tiles_x {
|
|
129
|
|
- let t = TileCoord::new(uzoom, start_tile_x + x, start_tile_y + y);
|
|
130
|
|
- if t.is_on_planet() {
|
|
131
|
|
- visible_tiles.push(
|
|
132
|
|
- VisibleTile {
|
|
133
|
|
- tile: t,
|
|
134
|
|
- rect: ScreenRect {
|
|
135
|
|
- x: top_left_tile_screen_coord.x + tile_screen_size * f64::from(x),
|
|
136
|
|
- y: top_left_tile_screen_coord.y + tile_screen_size * f64::from(y),
|
|
137
|
|
- width: tile_screen_size,
|
|
138
|
|
- height: tile_screen_size,
|
|
139
|
|
- }
|
|
140
|
|
- }
|
|
141
|
|
- );
|
|
142
|
|
- }
|
|
143
|
|
- }
|
|
144
|
|
- }
|
|
145
|
|
-
|
|
146
|
|
- visible_tiles
|
|
147
|
|
- }
|
|
148
|
|
-
|
|
149
|
|
- //TODO Put this in a new module with other "sphere things"
|
|
150
|
|
- //TODO Return the transformation matrix that is used here to avoid redundant calculation.
|
|
151
|
|
- /// Returns a `Vec` of all tiles that are visible in the current viewport.
|
|
152
|
|
- pub fn visible_globe_tiles(&self) -> Vec<TileCoord> {
|
|
153
|
|
- let uzoom = self.tile_zoom();
|
|
154
|
|
-
|
|
155
|
|
- match uzoom {
|
|
156
|
|
- 0 => return vec![TileCoord::new(0, 0, 0)],
|
|
157
|
|
- 1 => {
|
|
158
|
|
- // return every tile
|
|
159
|
|
- return vec![
|
|
160
|
|
- TileCoord::new(1, 0, 0),
|
|
161
|
|
- TileCoord::new(1, 0, 1),
|
|
162
|
|
- TileCoord::new(1, 1, 0),
|
|
163
|
|
- TileCoord::new(1, 1, 1),
|
|
164
|
|
- ]},
|
|
165
|
|
- _ => {},
|
|
166
|
|
- }
|
|
167
|
|
-
|
|
168
|
|
- let center_tile = self.center.on_tile_at_zoom(uzoom).globe_norm();
|
|
169
|
|
-
|
|
170
|
|
- let transform = self.globe_transformation_matrix();
|
|
171
|
|
-
|
|
172
|
|
- let add_tile_if_visible = |tc: TileCoord, vec: &mut Vec<TileCoord>| -> bool {
|
|
173
|
|
- let nearest = tc.nearest_inside_point(self.center).to_latlon_rad().to_sphere_point3();
|
|
174
|
|
- let screen_coord = transform.transform_point(nearest);
|
|
175
|
|
-
|
|
176
|
|
- let visible = screen_coord.x >= -1.0 && screen_coord.x <= 1.0 &&
|
|
177
|
|
- screen_coord.y >= -1.0 && screen_coord.y <= 1.0;
|
|
178
|
|
-
|
|
179
|
|
- if visible {
|
|
180
|
|
- vec.push(tc);
|
|
181
|
|
- true
|
|
182
|
|
- } else {
|
|
183
|
|
- false
|
|
184
|
|
- }
|
|
185
|
|
- };
|
|
186
|
|
-
|
|
187
|
|
- let mut tiles = vec![];
|
|
188
|
|
-
|
|
189
|
|
- {
|
|
190
|
|
- let zoom_level_tiles = TileCoord::get_zoom_level_tiles(uzoom);
|
|
191
|
|
-
|
|
192
|
|
- for dx in 0..(zoom_level_tiles / 2) {
|
|
193
|
|
- let v = add_tile_if_visible(TileCoord::new(uzoom, center_tile.x + dx, center_tile.y), &mut tiles);
|
|
194
|
|
- if !v {
|
|
195
|
|
- break;
|
|
196
|
|
- }
|
|
197
|
|
- }
|
|
198
|
|
- for dx in 1..(1 + zoom_level_tiles / 2) {
|
|
199
|
|
- let v = add_tile_if_visible(TileCoord::new(uzoom, center_tile.x - dx, center_tile.y), &mut tiles);
|
|
200
|
|
- if !v {
|
|
201
|
|
- break;
|
|
202
|
|
- }
|
|
203
|
|
- }
|
|
204
|
|
-
|
|
205
|
|
- // move south
|
|
206
|
|
- for y in (center_tile.y + 1)..zoom_level_tiles {
|
|
207
|
|
- let mut visible = false;
|
|
208
|
|
-
|
|
209
|
|
- for dx in 0..(zoom_level_tiles / 2) {
|
|
210
|
|
- let v = add_tile_if_visible(TileCoord::new(uzoom, center_tile.x + dx, y), &mut tiles);
|
|
211
|
|
- visible = visible || v;
|
|
212
|
|
- if !v {
|
|
213
|
|
- break;
|
|
214
|
|
- }
|
|
215
|
|
- }
|
|
216
|
|
- for dx in 1..(1 + zoom_level_tiles / 2) {
|
|
217
|
|
- let v = add_tile_if_visible(TileCoord::new(uzoom, center_tile.x - dx, y), &mut tiles);
|
|
218
|
|
- visible = visible || v;
|
|
219
|
|
- if !v {
|
|
220
|
|
- break;
|
|
221
|
|
- }
|
|
222
|
|
- }
|
|
223
|
|
-
|
|
224
|
|
- if !visible {
|
|
225
|
|
- break;
|
|
226
|
|
- }
|
|
227
|
|
- }
|
|
228
|
|
-
|
|
229
|
|
- // move north
|
|
230
|
|
- for y in (0..center_tile.y).rev() {
|
|
231
|
|
- let mut visible = false;
|
|
232
|
|
-
|
|
233
|
|
- for dx in 0..(zoom_level_tiles / 2) {
|
|
234
|
|
- let v = add_tile_if_visible(TileCoord::new(uzoom, center_tile.x + dx, y), &mut tiles);
|
|
235
|
|
- visible = visible || v;
|
|
236
|
|
- if !v {
|
|
237
|
|
- break;
|
|
238
|
|
- }
|
|
239
|
|
- }
|
|
240
|
|
- for dx in 1..(1 + zoom_level_tiles / 2) {
|
|
241
|
|
- let v = add_tile_if_visible(TileCoord::new(uzoom, center_tile.x - dx, y), &mut tiles);
|
|
242
|
|
- visible = visible || v;
|
|
243
|
|
- if !v {
|
|
244
|
|
- break;
|
|
245
|
|
- }
|
|
246
|
|
- }
|
|
247
|
|
-
|
|
248
|
|
- if !visible {
|
|
249
|
|
- break;
|
|
250
|
|
- }
|
|
251
|
|
- }
|
|
252
|
|
- }
|
|
253
|
|
-
|
|
254
|
|
- tiles
|
|
255
|
|
- }
|
|
256
|
|
-
|
|
257
|
|
- pub fn globe_transformation_matrix(&self) -> Matrix3<f32> {
|
|
258
|
|
- let (scale_x, scale_y) = {
|
|
259
|
|
- let factor = 2.0f32.powf(self.zoom as f32) *
|
|
260
|
|
- (FRAC_1_PI * self.tile_size as f32);
|
|
261
|
|
- (factor / self.width as f32, factor / self.height as f32)
|
|
262
|
|
- };
|
|
263
|
|
-
|
|
264
|
|
- let scale_mat: Matrix3<f32> = Matrix3::from_cols(
|
|
265
|
|
- vec3(scale_x, 0.0, 0.0),
|
|
266
|
|
- vec3(0.0, scale_y, 0.0),
|
|
267
|
|
- vec3(0.0, 0.0, 1.0),
|
|
268
|
|
- );
|
|
269
|
|
-
|
|
270
|
|
- let rot_mat_x: Matrix3<f32> = {
|
|
271
|
|
- let center_latlon = self.center.to_latlon_rad();
|
|
272
|
|
- let alpha = center_latlon.lon as f32 + (PI * 0.5);
|
|
273
|
|
- let cosa = alpha.cos();
|
|
274
|
|
- let sina = alpha.sin();
|
|
275
|
|
- Matrix3::from_cols(
|
|
276
|
|
- vec3(cosa, 0.0, -sina),
|
|
277
|
|
- vec3(0.0, 1.0, 0.0),
|
|
278
|
|
- vec3(sina, 0.0, cosa),
|
|
279
|
|
- )
|
|
280
|
|
- };
|
|
281
|
|
-
|
|
282
|
|
- let rot_mat_y: Matrix3<f32> = {
|
|
283
|
|
- let center_latlon = self.center.to_latlon_rad();
|
|
284
|
|
- let alpha = (-center_latlon.lat) as f32;
|
|
285
|
|
- let cosa = alpha.cos();
|
|
286
|
|
- let sina = alpha.sin();
|
|
287
|
|
- Matrix3::from_cols(
|
|
288
|
|
- vec3(1.0, 0.0, 0.0),
|
|
289
|
|
- vec3(0.0, cosa, sina),
|
|
290
|
|
- vec3(0.0, -sina, cosa),
|
|
291
|
|
- )
|
|
292
|
|
- };
|
|
293
|
|
-
|
|
294
|
|
- let transform = Transform::<Point3<f32>>::concat(&rot_mat_y, &rot_mat_x);
|
|
295
|
|
- let transform = Transform::<Point3<f32>>::concat(&scale_mat, &transform);
|
|
296
|
|
- transform
|
|
297
|
|
- }
|
|
298
|
|
-
|
|
299
|
|
- /// Returns the tile zoom value that is used for rendering with the current zoom.
|
|
300
|
|
- pub fn tile_zoom(&self) -> u32 {
|
|
301
|
|
- (self.zoom + self.tile_zoom_offset).floor().max(0.0) as u32
|
|
302
|
|
- }
|
|
303
|
|
-
|
|
304
|
36
|
/// Returns the tile zoom offset.
|
|
305
|
|
- pub fn tile_zoom_offset(&self) -> f64 {
|
|
306
|
|
- self.tile_zoom_offset
|
|
|
37
|
+ pub fn tile_zoom_offset(map_view: &MapView) -> f64 {
|
|
|
38
|
+ map_view.tile_zoom_offset
|
|
307
|
39
|
}
|
|
308
|
40
|
|
|
309
|
41
|
/// Set the tile zoom offset.
|
|
|
@@ -326,37 +58,4 @@ impl MapView {
|
|
326
|
58
|
pub fn zoom(&mut self, zoom_delta: f64) {
|
|
327
|
59
|
self.zoom += zoom_delta;
|
|
328
|
60
|
}
|
|
329
|
|
-
|
|
330
|
|
- /// Change zoom value by `zoom_delta` and zoom to a position given in screen coordinates.
|
|
331
|
|
- pub fn zoom_at(&mut self, pos: ScreenCoord, zoom_delta: f64) {
|
|
332
|
|
- let delta_x = pos.x - self.width * 0.5;
|
|
333
|
|
- let delta_y = pos.y - self.height * 0.5;
|
|
334
|
|
-
|
|
335
|
|
- let scale =
|
|
336
|
|
- (f64::powf(2.0, -self.zoom) - f64::powf(2.0, -self.zoom - zoom_delta))
|
|
337
|
|
- / f64::from(self.tile_size);
|
|
338
|
|
- self.zoom += zoom_delta;
|
|
339
|
|
-
|
|
340
|
|
- self.center.x += delta_x * scale;
|
|
341
|
|
- self.center.y += delta_y * scale;
|
|
342
|
|
- }
|
|
343
|
|
-
|
|
344
|
|
- /// Set a zoom value and zoom to a `position` given in screen coordinates.
|
|
345
|
|
- pub fn set_zoom_at(&mut self, pos: ScreenCoord, zoom: f64) {
|
|
346
|
|
- let delta_x = pos.x - self.width * 0.5;
|
|
347
|
|
- let delta_y = pos.y - self.height * 0.5;
|
|
348
|
|
-
|
|
349
|
|
- let scale = (f64::powf(2.0, -self.zoom) - f64::powf(2.0, -zoom)) / f64::from(self.tile_size);
|
|
350
|
|
- self.zoom = zoom;
|
|
351
|
|
-
|
|
352
|
|
- self.center.x += delta_x * scale;
|
|
353
|
|
- self.center.y += delta_y * scale;
|
|
354
|
|
- }
|
|
355
|
|
-
|
|
356
|
|
- /// Move the center of the viewport by (`delta_x`, `delta_y`) in screen coordinates.
|
|
357
|
|
- pub fn move_pixel(&mut self, delta_x: f64, delta_y: f64) {
|
|
358
|
|
- let scale = f64::powf(2.0, -self.zoom) / f64::from(self.tile_size);
|
|
359
|
|
- self.center.x += delta_x * scale;
|
|
360
|
|
- self.center.y += delta_y * scale;
|
|
361
|
|
- }
|
|
362
|
61
|
}
|