rmf_site_mesh 0.0.2

Mesh building utilties for rmf_site_editor
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
 * Copyright (C) 2022 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use bevy_color::Color;
use bevy_math::prelude::*;
use bevy_math::{Affine3A, ops::abs, primitives};
use bevy_render::prelude::*;

use bevy_render::primitives::Aabb;
use std::collections::{BTreeMap, HashMap};

use crate::*;

#[derive(Clone, Copy, Debug)]
pub struct OffsetCircle {
    pub radius: f32,
    pub height: f32,
}

impl OffsetCircle {
    pub fn flip_height(mut self) -> Self {
        self.height = -self.height;
        self
    }
}

impl From<(f32, f32)> for OffsetCircle {
    fn from((radius, height): (f32, f32)) -> Self {
        Self { radius, height }
    }
}

pub fn make_circles(
    circles: impl IntoIterator<Item = OffsetCircle>,
    resolution: u32,
    gap: f32,
) -> impl Iterator<Item = [f32; 3]> {
    return [0..resolution]
        .into_iter()
        .cycle()
        .zip(circles.into_iter())
        .flat_map(move |(range, circle)| {
            range.into_iter().map(move |i| {
                let theta = (i as f32) / (resolution as f32 - 1.) * (std::f32::consts::TAU - gap);
                let r = circle.radius;
                let h = circle.height;
                [r * theta.cos(), r * theta.sin(), h]
            })
        });
}

pub fn make_smooth_wrap(circles: [OffsetCircle; 2], resolution: u32) -> MeshBuffer {
    let (bottom_circle, top_circle) = if circles[0].height < circles[1].height {
        (circles[0], circles[1])
    } else {
        (circles[1], circles[0])
    };

    let positions: Vec<[f32; 3]> =
        make_circles([bottom_circle, top_circle], resolution, 0.).collect();

    let top_start = resolution;
    let indices = [[0, 1, top_start + 1, 0, top_start + 1, top_start]]
        .into_iter()
        .cycle()
        .enumerate()
        .flat_map(|(i, values)| values.into_iter().map(move |s| s + i as u32))
        .take(6 * (resolution - 1) as usize)
        .collect();

    let mut normals = Vec::new();
    normals.resize(positions.len(), [0., 0., 1.]);
    for i in 0..resolution {
        let theta = (i as f32) / (resolution as f32 - 1.) * 2. * std::f32::consts::PI;
        let dr = top_circle.radius - bottom_circle.radius;
        let dh = top_circle.height - bottom_circle.height;
        let phi = dr.atan2(dh);
        let r_y = Affine3A::from_rotation_y(phi);
        let r_z = Affine3A::from_rotation_z(theta);
        let n = (r_z * r_y).transform_vector3([1., 0., 0.].into());
        normals[i as usize] = n.into();
        normals[(i + top_start) as usize] = n.into();
    }

    return MeshBuffer::new(positions, normals, indices);
}

pub fn make_wall_mesh(
    p_start: Vec3,
    p_end: Vec3,
    thickness: f32,
    height: f32,
    texture_height: Option<f32>,
    texture_width: Option<f32>,
) -> MeshBuffer {
    let dp = p_end - p_start;
    let length = dp.length();
    let yaw = dp.y.atan2(dp.x);
    let center = (p_start + p_end) / 2.0;
    let texture_height = texture_height.unwrap_or(height);
    let texture_width = texture_width.unwrap_or(1.0);

    // The default UV coordinates made by bevy do not work well for walls,
    // so we customize them here
    let uv = vec![
        // Top
        [0., 0.], // 0
        [0., 0.], // 1
        [0., 0.], // 2
        [0., 0.], // 3
        // Bottom
        [0., height / texture_height], // 4
        [0., height / texture_height], // 5
        [0., height / texture_height], // 6
        [0., height / texture_height], // 7
        // Right
        [length / texture_width, height / texture_height], // 8
        [0., height / texture_height],                     // 9
        [0., 0.],                                          // 10
        [length / texture_width, 0.],                      // 11
        // Left
        [0., 0.],                                          // 12
        [length / texture_width, 0.],                      // 13
        [length / texture_width, height / texture_height], // 14
        [0., height / texture_height],                     // 15
        // Front
        [0., height / texture_height],                     // 16
        [length / texture_width, height / texture_height], // 17
        [length / texture_width, 0.],                      // 18
        [0., 0.],                                          // 19
        // Back
        [length / texture_width, 0.],                      // 20
        [0., 0.],                                          // 21
        [0., height / texture_height],                     // 22
        [length / texture_width, height / texture_height], // 23
    ];
    make_cuboid(length, thickness, height)
        .with_uv(uv)
        .transform_by(
            Affine3A::from_translation(Vec3::new(center.x, center.y, height / 2.0))
                * Affine3A::from_rotation_z(yaw),
        )
}

pub fn make_top_circle(circle: OffsetCircle, resolution: u32) -> MeshBuffer {
    let positions: Vec<[f32; 3]> = make_circles([circle], resolution, 0.)
        .take(resolution as usize) // skip the vertex which would close the circle
        .chain([[0., 0., circle.height]].into_iter())
        .collect();

    let peak = positions.len() as u32 - 1;
    let indices: Vec<u32> = (0..=peak - 2)
        .into_iter()
        .flat_map(|i| [i, i + 1, peak].into_iter())
        .chain([peak - 1, 0, peak])
        .collect();

    let normals: Vec<[f32; 3]> = [[0., 0., 1.]]
        .into_iter()
        .cycle()
        .take(positions.len())
        .collect();

    return MeshBuffer::new(positions, normals, indices);
}

pub fn make_bottom_circle(circle: OffsetCircle, resolution: u32) -> MeshBuffer {
    let positions: Vec<[f32; 3]> = make_circles([circle], resolution, 0.)
        .take(resolution as usize) // skip the vertex which would close the circle
        .chain([[0., 0., circle.height]].into_iter())
        .collect();

    let peak = positions.len() as u32 - 1;
    let indices: Vec<u32> = (0..=peak - 2)
        .into_iter()
        .flat_map(|i| [i, peak, i + 1].into_iter())
        .chain([peak - 1, peak, 0])
        .collect();

    let normals: Vec<[f32; 3]> = [[0., 0., -1.]]
        .into_iter()
        .cycle()
        .take(positions.len())
        .collect();

    return MeshBuffer::new(positions, normals, indices);
}

pub fn make_flat_disk(circle: OffsetCircle, resolution: u32) -> MeshBuffer {
    make_top_circle(circle, resolution).merge_with(make_bottom_circle(circle, resolution))
}

pub fn make_cylinder(height: f32, radius: f32) -> MeshBuffer {
    let top_circle = OffsetCircle {
        height: height / 2.0,
        radius,
    };
    let mid_circle = OffsetCircle {
        height: 0.0,
        radius,
    };
    let bottom_circle = OffsetCircle {
        height: -height / 2.0,
        radius,
    };
    let resolution = 32;
    make_smooth_wrap([top_circle, bottom_circle], resolution)
        .merge_with(
            make_bottom_circle(mid_circle, resolution)
                .transform_by(Affine3A::from_translation([0.0, 0., -height / 2.0].into())),
        )
        .merge_with(make_bottom_circle(mid_circle, resolution).transform_by(
            Affine3A::from_translation([0., 0., height / 2.0].into())
                * Affine3A::from_rotation_x(180_f32.to_radians()),
        ))
}

pub fn flat_arc(
    pivot: Vec3,
    outer_radius: f32,
    inner_thickness: f32,
    initial_angle: impl Into<Radians>,
    sweep: impl Into<Radians>,
    vertices_per_degree: f32,
) -> MeshBuffer {
    let initial_angle: Radians = initial_angle.into();
    let sweep = sweep.into();
    let (initial_angle, sweep) = if sweep < 0.0 {
        (initial_angle + sweep, -sweep)
    } else {
        (initial_angle, sweep)
    };

    let resolution = (Degrees::from(sweep) * vertices_per_degree).0 as u32;
    let positions: Vec<[f32; 3]> = make_circles(
        [
            (outer_radius - inner_thickness, 0.).into(),
            (outer_radius, 0.).into(),
        ],
        resolution,
        std::f32::consts::TAU - *sweep,
    )
    .collect();

    let normals: Vec<[f32; 3]> = {
        let mut normals = Vec::new();
        normals.resize(positions.len(), [0.0, 0.0, 1.0]);
        normals
    };

    let indices: Vec<u32> = if resolution >= 1 {
        [[0, resolution, resolution + 1, 0, resolution + 1, 1]]
            .into_iter()
            .cycle()
            .enumerate()
            .flat_map(|(segment, values)| values.map(|s| segment as u32 + s))
            .take(6 * (resolution as usize - 1))
            .collect()
    } else {
        Vec::new()
    };

    let outline: Vec<u32> = if resolution >= 1 {
        [[0, 1, resolution, resolution + 1]]
            .into_iter()
            .cycle()
            .enumerate()
            .flat_map(|(segment, values)| values.map(|s| segment as u32 + s))
            .take(4 * (resolution as usize - 1))
            .collect()
    } else {
        Vec::new()
    };

    let ratio = abs(*sweep / 180_f32.to_radians());

    // Maps UV coordinates along the arc
    let inner_radius = outer_radius - inner_thickness;
    let uvs: Vec<[f32; 2]> = positions
        .iter()
        .map(|pos| {
            let p_vec = Vec2::new(pos[0], pos[1]);
            let distance = p_vec.length();
            let mut angle = p_vec.y.atan2(p_vec.x);

            if angle < 0.0 {
                angle += 2.0 * std::f32::consts::PI;
            }

            let u = (*sweep - angle) / *sweep;
            let v = (distance - inner_radius) / inner_thickness;

            [u * ratio, v]
        })
        .collect();

    MeshBuffer::new(positions, normals, indices)
        .with_outline(outline)
        .with_uv(uvs)
        .transform_by(Affine3A::from_rotation_translation(
            Quat::from_rotation_z(*initial_angle),
            pivot,
        ))
}

pub fn line_stroke_mesh(start: Vec3, end: Vec3, thickness: f32) -> MeshBuffer {
    let positions: Vec<[f32; 3]> = vec![
        [-0.5, -0.5, 0.], // 0
        [0.5, -0.5, 0.],  // 1
        [0.5, 0.5, 0.],   // 2
        [-0.5, 0.5, 0.],  // 3
    ];

    let normals: Vec<[f32; 3]> = {
        let mut normals = Vec::new();
        normals.resize(positions.len(), [0.0, 0.0, 1.0]);
        normals
    };

    let indices: Vec<u32> = vec![0, 1, 2, 0, 2, 3];
    let outline: Vec<u32> = vec![0, 1, 1, 2, 2, 3, 3, 0];

    let uvs: Vec<[f32; 2]> = vec![[1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0]];

    let center = (start + end) / 2.0;
    let dp = end - start;
    let yaw = dp.y.atan2(dp.x);

    MeshBuffer::new(positions, normals, indices)
        .with_outline(outline)
        .with_uv(uvs)
        .transform_by(Affine3A::from_scale_rotation_translation(
            Vec3::new(dp.length(), thickness, 1.),
            Quat::from_rotation_z(yaw),
            center,
        ))
}

pub fn line_stroke_away_from(
    start: Vec3,
    direction: Radians,
    length: f32,
    thickness: f32,
) -> MeshBuffer {
    let end = start
        + Affine3A::from_rotation_z(direction.0).transform_vector3(Vec3::new(length, 0.0, 0.0));

    line_stroke_mesh(start, end, thickness)
}

pub fn make_physical_camera_mesh() -> Mesh {
    let scale = 0.1;
    let lens_hood_protrusion = 0.8;

    // Main body
    let mut mesh: Mesh = primitives::Cuboid::new(scale, scale, scale).into();
    mesh.remove_attribute(Mesh::ATTRIBUTE_UV_0);

    // Outside of the lens hood
    make_pyramid(
        OffsetCircle {
            radius: scale,
            height: 0.,
        },
        [0., 0., scale],
        4,
    )
    .transform_by(
        Affine3A::from_translation([lens_hood_protrusion * scale, 0., 0.].into())
            * Affine3A::from_rotation_y(-90_f32.to_radians())
            * Affine3A::from_rotation_z(45_f32.to_radians()),
    )
    .merge_into(&mut mesh);

    // Inside of the lens hood
    make_pyramid(
        OffsetCircle {
            radius: scale,
            height: scale,
        },
        [0., 0., 0.],
        4,
    )
    .transform_by(
        Affine3A::from_translation([-(1.0 - lens_hood_protrusion) * scale, 0., 0.].into())
            * Affine3A::from_rotation_y(90_f32.to_radians())
            * Affine3A::from_rotation_z(45_f32.to_radians()),
    )
    .merge_into(&mut mesh);

    mesh
}

pub fn make_diamond(tip: f32, width: f32) -> MeshBuffer {
    make_pyramid(
        OffsetCircle {
            radius: width,
            height: 0.0,
        },
        [0.0, 0.0, tip],
        4,
    )
    .merge_with(
        make_pyramid(
            OffsetCircle {
                radius: width,
                height: 0.0,
            },
            [0.0, 0.0, tip],
            4,
        )
        .transform_by(Affine3A::from_rotation_x(180_f32.to_radians())),
    )
}

pub fn make_flat_square_mesh(extent: f32) -> MeshBuffer {
    return make_flat_rect_mesh(extent, extent);
}

pub fn make_flat_rect_mesh(x_size: f32, y_size: f32) -> MeshBuffer {
    let x = x_size / 2.0;
    let y = y_size / 2.0;
    let positions: Vec<[f32; 3]> = [[-x, -y, 0.], [x, -y, 0.], [x, y, 0.], [-x, y, 0.]]
        .into_iter()
        .cycle()
        .take(8)
        .collect();

    let indices = [0, 1, 2, 0, 2, 3, 4, 6, 5, 4, 7, 6].into_iter().collect();

    let normals: Vec<[f32; 3]> = [[0., 0., 1.]]
        .into_iter()
        .cycle()
        .take(4)
        .chain([[0., 0., -1.]].into_iter().cycle().take(4))
        .collect();

    let uv: Vec<[f32; 2]> = [[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0]]
        .into_iter()
        .cycle()
        .take(8)
        .collect();

    let outline = [0, 1, 1, 2, 2, 3, 3, 0].into_iter().collect();

    return MeshBuffer::new(positions, normals, indices)
        .with_uv(uv)
        .with_outline(outline);
}

pub fn make_flat_mesh_for_aabb(aabb: Aabb) -> MeshBuffer {
    make_flat_rect_mesh(2.0 * aabb.half_extents.x, 2.0 * aabb.half_extents.y)
        .transform_by(Affine3A::from_translation(aabb.center.into()))
}

pub fn make_ring(inner_radius: f32, outer_radius: f32, resolution: usize) -> MeshBuffer {
    let positions: Vec<[f32; 3]> = make_circles(
        [(inner_radius, 0.).into(), (outer_radius, 0.).into()],
        resolution as u32,
        0.0,
    )
    .collect();

    let normals: Vec<[f32; 3]> = [[0., 0., 1.0]]
        .into_iter()
        .cycle()
        .take(positions.len())
        .collect();

    let r = resolution as u32;
    let indices = [[0, r, r + 1, 0, r + 1, 1]]
        .into_iter()
        .cycle()
        .enumerate()
        .flat_map(|(cycle, values)| values.map(|s| s + cycle as u32))
        .take(6 * (resolution - 1))
        .chain([r - 1, 2 * r - 1, r, r - 1, r, 0])
        .collect();

    MeshBuffer::new(positions, normals, indices)
}

pub fn make_location_icon(radius: f32, height: f32, segments: usize) -> MeshBuffer {
    let height = 2.0 * height;
    let angle = (360.0 / (2.0 * segments as f32)).to_radians();
    let p0 = radius * Vec3::X;
    let p1 = Affine3A::from_rotation_z(angle).transform_vector3(p0);
    let width = (p1 - p0).length();
    make_flat_square_mesh(width).transform_by(Affine3A::from_translation(Vec3::new(
        radius + width / 2.0,
        0.0,
        height / 2.0,
    )))
}

pub fn make_closed_path_outline(mut initial_positions: Vec<[f32; 3]>) -> MeshBuffer {
    let num_positions = initial_positions.len() as u32;
    let mut positions = Vec::new();
    let mut normals = Vec::new();
    let mut uv = Vec::new();
    let mut indices = Vec::new();
    let z2 = [0.0, 0.0];

    // Close the loop by repeating the first and last values at the end and start, respectively
    if let Some(pf) = initial_positions.last() {
        let pf = *pf;
        if let Some(pi) = initial_positions.first() {
            let pi = *pi;
            initial_positions.push(pi);
        }
        initial_positions.insert(0, pf);
    }

    // for (i, [p0, p1, p2]) in initial_positions.array_windows::<3>().enumerate() {
    for (i, window) in initial_positions.windows(3).enumerate() {
        let i = i as u32;
        let p0 = window[0];
        let p1 = window[1];
        let p2 = window[2];

        let p = p1;
        let p0 = Vec3::new(p0[0], p0[1], 0.0);
        let p1 = Vec3::new(p1[0], p1[1], 0.0);
        let p2 = Vec3::new(p2[0], p2[1], 0.0);
        let v0 = (p1 - p0).normalize_or_zero();
        let v1 = (p2 - p1).normalize_or_zero();

        // n: normal
        let n = Vec3::Z;
        let u = n.cross(v0).normalize();
        let w = n.cross(v1).normalize();

        // b: bisector
        let b = match (u + w).try_normalize() {
            Some(b) => b,
            None => {
                // This means that u and w are pointing in opposite directions,
                // so the next vertex is in a perfect 180 back towards the
                // previous vertex. We can simply use v0 as the bisecting
                // vector.
                v0
            }
        };

        positions.extend([p, p, p, p, p, p, p, p]);
        normals.extend([u, -u, w, -w, b, -b, n, -n].map(Into::<[f32; 3]>::into));
        uv.extend([z2, z2, z2, z2, z2, z2, z2, z2]);

        let u0 = 0;
        let u1 = 1;
        let w0 = 2;
        let w1 = 3;
        let b0 = 4;
        let b1 = 5;
        let n0 = 6;
        let n1 = 7;
        let i_delta = 8;

        // Current base index
        let c = i_delta * i;
        // Next base index
        let f = if i == num_positions - 1 {
            // We have reached the last iteration so we should wrap around and
            // connect to the first set of vertices.
            0
        } else {
            i_delta * (i + 1)
        };

        if w.cross(b).dot(n) < 0.0 {
            // left turn
            indices.extend([
                c + u1,
                c + b1,
                c + n0,
                c + b1,
                c + w1,
                c + n0,
                c + u1,
                c + n1,
                c + b1,
                c + b1,
                c + n1,
                c + w1,
            ]);
        } else {
            // right turn
            indices.extend([
                c + u0,
                c + n0,
                c + b0,
                c + b0,
                c + n0,
                c + w0,
                c + u0,
                c + b0,
                c + n1,
                c + b0,
                c + w0,
                c + n1,
            ]);
        }

        indices.extend([
            c + w0,
            c + n0,
            f + n0,
            c + w0,
            f + n0,
            f + u0,
            c + w1,
            f + n0,
            c + n0,
            c + w1,
            f + u1,
            f + n0,
            c + w0,
            f + u0,
            f + n1,
            c + w0,
            f + n1,
            c + n1,
            c + w1,
            f + n1,
            f + u1,
            c + w1,
            c + n1,
            f + n1,
        ]);
    }

    MeshBuffer::new(positions, normals, indices)
        .with_uv(uv)
        .copy_outline_normals()
}

const X_AXIS_COLOR: Color = Color::srgb(1.0, 0.2, 0.2);
const Y_AXIS_COLOR: Color = Color::srgb(0.2, 1.0, 0.2);
const NEG_X_AXIS_COLOR: Color = Color::srgb(0.5, 0.0, 0.0);
const NEG_Y_AXIS_COLOR: Color = Color::srgb(0.0, 0.5, 0.0);

const POLYLINE_SEPARATOR: Vec3 = Vec3::splat(std::f32::NAN);

pub fn make_finite_grid(
    scale: f32,
    count: u32,
    color: Color,
    weights: BTreeMap<u32, f32>,
) -> Vec<(BoxedPolyline3d, PolylineMaterial)> {
    let d_max = count as f32 * scale;
    let depth_bias = -0.0001;
    let perspective = true;

    let make_point = |i, j, d, w| {
        let mut p = Vec3::ZERO;
        p[i] = w;
        p[j] = d;
        p
    };

    let make_points = |i, j, d| [make_point(i, j, d, d_max), make_point(i, j, d, -d_max)];

    let mut vec_of_lines: HashMap<u32, Vec<Vec3>> = HashMap::new();

    let mut result = {
        let Some(width) = weights.values().last().copied() else {
            return Vec::new();
        };
        let mut axes: Vec<(BoxedPolyline3d, PolylineMaterial)> = Vec::new();

        for (sign, x_axis_color, y_axis_color) in [
            (1.0, X_AXIS_COLOR, Y_AXIS_COLOR),
            (-1.0, NEG_X_AXIS_COLOR, NEG_Y_AXIS_COLOR),
        ] {
            for (i, j, color) in [(0, 1, x_axis_color), (1, 0, y_axis_color)] {
                let p0 = Vec3::ZERO;
                let p1 = make_point(i, j, 0.0, sign * d_max);
                let polyline: BoxedPolyline3d = BoxedPolyline3d::new([p0, p1]);
                let material = PolylineMaterial {
                    width,
                    color: color.into(),
                    depth_bias,
                    perspective,
                };
                axes.push((polyline, material));
            }
        }

        axes
    };

    for n in 1..=count {
        let d = n as f32 * scale;
        let polyline = {
            let Some(weight_key) = weights.keys().rev().find(|k| n % **k == 0) else {
                continue;
            };
            vec_of_lines.entry(*weight_key).or_insert(Vec::default())
        };

        for (i, j) in [(0, 1), (1, 0)] {
            polyline.extend(make_points(i, j, d));
            polyline.push(POLYLINE_SEPARATOR);
            polyline.extend(make_points(i, j, -d));
            polyline.push(POLYLINE_SEPARATOR);
        }
    }

    result.extend(vec_of_lines.into_iter().map(|(n, polyline)| {
        let width = *weights.get(&n).unwrap();
        let material = PolylineMaterial {
            width,
            color: color.into(),
            depth_bias,
            perspective,
        };
        (BoxedPolyline3d::new(polyline), material)
    }));
    result
}

pub fn make_metric_finite_grid(
    scale: f32,
    count: u32,
    color: Color,
) -> Vec<(BoxedPolyline3d, PolylineMaterial)> {
    make_finite_grid(scale, count, color, [(1, 0.5), (5, 1.0), (10, 1.5)].into())
}