mln-rs-renderer 0.0.13

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use mln_math::fmaxf;
use mln_tile_id::geometry_tile_data::{GeometryCoordinate, GeometryCoordinates};
use mln_util::constants::EXTENT;
use mln_util::interpolate::Interpolate;

use crate::geometry::anchor::{Anchor, Anchors};
use crate::text::check_max_angle::check_max_angle;

/// The distance from `a` to `b` as C++ computes it: an `f64` square root of
/// the integer sum, narrowed to `f32`.
fn dist(a: GeometryCoordinate, b: GeometryCoordinate) -> f32 {
    let dx = f64::from(i32::from(b.x) - i32::from(a.x));
    let dy = f64::from(i32::from(b.y) - i32::from(a.y));
    (dx * dx + dy * dy).sqrt() as f32
}

/// The angle from `b` to `a`, in `f64`.
fn angle_to(a: GeometryCoordinate, b: GeometryCoordinate) -> f64 {
    f64::from(i32::from(a.y) - i32::from(b.y)).atan2(f64::from(i32::from(a.x) - i32::from(b.x)))
}

#[must_use]
pub(crate) fn get_angle_window_size(text_left: f32, text_right: f32, glyph_size: f32, box_scale: f32) -> f32 {
    if (text_left - text_right) == 0.0 { 0.0 } else { 3.0f32 / 5.0f32 * glyph_size * box_scale }
}

#[must_use]
pub(crate) fn get_line_length(line: &GeometryCoordinates) -> f32 {
    let mut line_length: f32 = 0.0;
    for segment in line.windows(2) {
        line_length += dist(segment[0], segment[1]);
    }
    line_length
}

#[expect(
    clippy::too_many_arguments,
    reason = "the line and the spacing, angle window and label length that pick the anchors"
)]
fn resample(
    line: &GeometryCoordinates,
    offset: f32,
    spacing: f32,
    angle_window_size: f32,
    max_angle: f32,
    label_length: f32,
    continued_line: bool,
    place_at_middle: bool,
) -> Anchors {
    let half_label_length = label_length / 2.0;
    let line_length = get_line_length(line);

    let mut distance: f32 = 0.0;
    let mut marked_distance: f32 = offset - spacing;

    let mut anchors = Anchors::new();

    debug_assert!(spacing > 0.0);

    for (i, segment) in line.windows(2).enumerate() {
        let a = &segment[0];
        let b = &segment[1];

        let segment_dist = dist(*a, *b);
        let angle = angle_to(*b, *a) as f32;

        while marked_distance + spacing < distance + segment_dist {
            marked_distance += spacing;

            let t = (marked_distance - distance) / segment_dist;
            let x = f32::from(a.x).interpolate_f32(&f32::from(b.x), t);
            let y = f32::from(a.y).interpolate_f32(&f32::from(b.y), t);

            // Check that the point is within the tile boundaries and that
            // the label would fit before the beginning and end of the line
            // if placed at this point.
            if x >= 0.0
                && x < EXTENT as f32
                && y >= 0.0
                && y < EXTENT as f32
                && marked_distance - half_label_length >= 0.0
                && marked_distance + half_label_length <= line_length
            {
                let anchor = Anchor::new(x.round(), y.round(), angle, Some(i));

                if angle_window_size == 0.0
                    || check_max_angle(line, &anchor, label_length, angle_window_size, max_angle)
                {
                    anchors.push(anchor);
                }
            }
        }

        distance += segment_dist;
    }

    if !place_at_middle && anchors.is_empty() && !continued_line {
        // The first attempt at finding anchors at which labels can be placed
        // failed. Try again, but this time just try placing one anchor at the
        // middle of the line. This has the most effect for short lines in
        // overscaled tiles, since the initial offset used in overscaled tiles
        // is calculated to align labels with positions in parent tiles instead
        // of placing the label as close to the beginning as possible.
        anchors =
            resample(line, distance / 2.0, spacing, angle_window_size, max_angle, label_length, continued_line, true);
    }

    anchors
}

#[expect(
    clippy::too_many_arguments,
    reason = "the line, the spacing and angle limit, and the text and icon extents and scales that size the labels"
)]
#[must_use]
pub(crate) fn get_anchors(
    line: &GeometryCoordinates,
    mut spacing: f32,
    max_angle: f32,
    text_left: f32,
    text_right: f32,
    icon_left: f32,
    icon_right: f32,
    glyph_size: f32,
    box_scale: f32,
    overscaling: f32,
) -> Anchors {
    if line.is_empty() {
        return Anchors::new();
    }

    // Resample a line to get anchor points for labels and check that each
    // potential label passes text-max-angle check and has enough froom to fit
    // on the line.

    let angle_window_size = get_angle_window_size(text_left, text_right, glyph_size, box_scale);

    let shaped_label_length = fmaxf(text_right - text_left, icon_right - icon_left);
    let label_length = shaped_label_length * box_scale;

    // Is the line continued from outside the tile boundary?
    let continued_line =
        line[0].x == 0 || i32::from(line[0].x) == EXTENT || line[0].y == 0 || i32::from(line[0].y) == EXTENT;

    // Is the label long, relative to the spacing?
    // If so, adjust the spacing so there is always a minimum space of `spacing / 4` between label edges.
    if spacing - label_length < spacing / 4.0 {
        spacing = label_length + spacing / 4.0;
    }

    // Offset the first anchor by:
    // Either half the label length plus a fixed extra offset if the line is not
    // continued Or half the spacing if the line is continued.

    // For non-continued lines, add a bit of fixed extra offset to avoid collisions at T intersections.
    let fixed_extra_offset = glyph_size * 2.0;

    // `std::fmod` on floats (fmodf) is `%`.
    let offset = if continued_line {
        (spacing / 2.0 * overscaling) % spacing
    } else {
        ((shaped_label_length / 2.0 + fixed_extra_offset) * box_scale * overscaling) % spacing
    };

    resample(line, offset, spacing, angle_window_size, max_angle, label_length, continued_line, false)
}

#[expect(
    clippy::too_many_arguments,
    reason = "the line, the angle limit, and the text and icon extents and scales that size the label"
)]
#[must_use]
pub(crate) fn get_center_anchor(
    line: &GeometryCoordinates,
    max_angle: f32,
    text_left: f32,
    text_right: f32,
    icon_left: f32,
    icon_right: f32,
    glyph_size: f32,
    box_scale: f32,
) -> Option<Anchor> {
    if line.is_empty() {
        return None;
    }

    let angle_window_size = get_angle_window_size(text_left, text_right, glyph_size, box_scale);
    let label_length = fmaxf(text_right - text_left, icon_right - icon_left) * box_scale;

    let mut prev_distance: f32 = 0.0;
    let center_distance = get_line_length(line) / 2.0;

    for (i, segment) in line.windows(2).enumerate() {
        let a = &segment[0];
        let b = &segment[1];

        let segment_distance = dist(*a, *b);

        if prev_distance + segment_distance > center_distance {
            // The center is on this segment
            let t = (center_distance - prev_distance) / segment_distance;
            let x = f32::from(a.x).interpolate_f32(&f32::from(b.x), t);
            let y = f32::from(a.y).interpolate_f32(&f32::from(b.y), t);

            let anchor = Anchor::new(x.round(), y.round(), angle_to(*b, *a) as f32, Some(i));

            if angle_window_size == 0.0 || check_max_angle(line, &anchor, label_length, angle_window_size, max_angle) {
                return Some(anchor);
            }

            return None;
        }

        prev_distance += segment_distance;
    }
    None
}

#[cfg(test)]
mod tests {
    use std::f32::consts::{FRAC_PI_4, PI};

    use geometry::Point;

    use super::*;

    fn make_line(shift: usize) -> GeometryCoordinates {
        let mut line = GeometryCoordinates::new();
        for i in shift..10 + shift {
            line.push(Point::new(1, i as i16));
        }
        line
    }

    fn continued_line() -> GeometryCoordinates {
        make_line(0)
    }

    fn non_continued_line() -> GeometryCoordinates {
        make_line(1)
    }

    const SMALL_SPACING: f32 = 2.0;
    const BIG_SPACING: f32 = 3.0;
    const TEXT_LEFT: f32 = -1.0;
    const TEXT_RIGHT: f32 = 1.0;
    const ICON_LEFT: f32 = -0.5;
    const ICON_RIGHT: f32 = 0.5;
    const GLYPH_SIZE: f32 = 0.1;
    const BOX_SCALE: f32 = 1.0;
    const OVERSCALING: f32 = 1.0;

    fn anchor(x: f32, y: f32, angle: f32, segment: usize) -> Anchor {
        Anchor::new(x, y, angle, Some(segment))
    }

    // gtest: getAnchors.NonContinuedLineShortLabels
    #[test]
    fn non_continued_line_short_labels() {
        let anchors = get_anchors(
            &non_continued_line(),
            BIG_SPACING,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            BOX_SCALE,
            OVERSCALING,
        );
        let expected_anchors =
            vec![anchor(1.0, 2.0, 1.570_796_4, 1), anchor(1.0, 5.0, 1.570_796_4, 4), anchor(1.0, 8.0, 1.570_796_4, 7)];

        assert_eq!(anchors, expected_anchors);
    }

    // gtest: getAnchors.NonContinuedLineLongLabels
    #[test]
    fn non_continued_line_long_labels() {
        let anchors = get_anchors(
            &non_continued_line(),
            SMALL_SPACING,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            BOX_SCALE,
            OVERSCALING,
        );
        let expected_anchors =
            vec![anchor(1.0, 2.0, 1.570_796_4, 1), anchor(1.0, 5.0, 1.570_796_4, 3), anchor(1.0, 7.0, 1.570_796_4, 6)];

        assert_eq!(anchors, expected_anchors);
    }

    // gtest: getAnchors.ContinuedLineShortLabels
    #[test]
    fn continued_line_short_labels() {
        let anchors = get_anchors(
            &continued_line(),
            BIG_SPACING,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            BOX_SCALE,
            OVERSCALING,
        );
        let expected_anchors =
            vec![anchor(1.0, 2.0, 1.570_796_4, 1), anchor(1.0, 5.0, 1.570_796_4, 4), anchor(1.0, 8.0, 1.570_796_4, 7)];

        assert_eq!(anchors, expected_anchors);
    }

    // gtest: getAnchors.ContinuedLineLongLabels
    #[test]
    fn continued_line_long_labels() {
        let anchors = get_anchors(
            &continued_line(),
            SMALL_SPACING,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            BOX_SCALE,
            OVERSCALING,
        );
        let expected_anchors =
            vec![anchor(1.0, 1.0, 1.570_796_4, 1), anchor(1.0, 4.0, 1.570_796_4, 3), anchor(1.0, 6.0, 1.570_796_4, 6)];

        assert_eq!(anchors, expected_anchors);
    }

    // gtest: getAnchors.OverscaledAnchorsInParent
    #[test]
    fn overscaled_anchors_in_parent() {
        let anchors = get_anchors(
            &non_continued_line(),
            BIG_SPACING,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            BOX_SCALE,
            OVERSCALING,
        );
        let child_anchors = get_anchors(
            &non_continued_line(),
            BIG_SPACING / 2.0,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            0.5, /* boxScale */
            2.0, /* overscaling */
        );
        for anchor in &anchors {
            assert!(child_anchors.contains(anchor));
        }
    }

    // gtest: getAnchors.UseMidpointForShortLine
    #[test]
    fn use_midpoint_for_short_line() {
        let short_line: GeometryCoordinates = vec![Point::new(1, 1), Point::new(1, 3)];
        let anchors = get_anchors(
            &short_line,
            SMALL_SPACING,
            PI,
            TEXT_LEFT,
            TEXT_RIGHT,
            ICON_LEFT,
            ICON_RIGHT,
            GLYPH_SIZE,
            BOX_SCALE,
            OVERSCALING,
        );
        let expected_anchors = vec![anchor(1.0, 2.0, 1.570_796_4, 0)];

        assert_eq!(anchors, expected_anchors);
    }

    // gtest: getAnchors.GetCenterAnchor
    #[test]
    fn get_center_anchor_test() {
        let line: GeometryCoordinates = vec![Point::new(1, 1), Point::new(1, 3), Point::new(3, 6), Point::new(4, 7)];
        let anchor_ = get_center_anchor(&line, PI, TEXT_LEFT, TEXT_RIGHT, ICON_LEFT, ICON_RIGHT, GLYPH_SIZE, BOX_SCALE);
        assert!(anchor_.is_some());
        assert_eq!(anchor_.unwrap(), anchor(2.0, 4.0, 0.982_793_75, 1));
    }

    // gtest: getAnchors.GetCenterAnchorOutsideTileBounds
    #[test]
    fn get_center_anchor_outside_tile_bounds() {
        let line: GeometryCoordinates = vec![Point::new(-10, -10), Point::new(5, 5)];
        let anchor_ = get_center_anchor(&line, PI, TEXT_LEFT, TEXT_RIGHT, ICON_LEFT, ICON_RIGHT, GLYPH_SIZE, BOX_SCALE);
        assert!(anchor_.is_some());
        assert_eq!(anchor_.unwrap(), anchor(-3.0, -3.0, FRAC_PI_4, 0));
    }

    // gtest: getAnchors.GetCenterAnchorFailMaxAngle
    #[test]
    fn get_center_anchor_fail_max_angle() {
        let line: GeometryCoordinates = vec![Point::new(1, 1), Point::new(1, 3), Point::new(3, 3)];
        let anchor_ =
            get_center_anchor(&line, PI / 4.0, TEXT_LEFT, TEXT_RIGHT, ICON_LEFT, ICON_RIGHT, GLYPH_SIZE, BOX_SCALE);
        assert!(anchor_.is_none());
    }
}