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;
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
}
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);
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 {
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();
}
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;
let continued_line =
line[0].x == 0 || i32::from(line[0].x) == EXTENT || line[0].y == 0 || i32::from(line[0].y) == EXTENT;
if spacing - label_length < spacing / 4.0 {
spacing = label_length + spacing / 4.0;
}
let fixed_extra_offset = glyph_size * 2.0;
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 {
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))
}
#[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);
}
#[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);
}
#[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);
}
#[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);
}
#[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,
2.0,
);
for anchor in &anchors {
assert!(child_anchors.contains(anchor));
}
}
#[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);
}
#[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));
}
#[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));
}
#[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());
}
}