maliput 0.37.0

Rust API for maliput
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
// BSD 3-Clause License
//
// Copyright (c) 2026, Woven by Toyota.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
//   list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
//   contributors may be used to endorse or promote products derived from
//   this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mod common;

// TwoRoadsWithRoadObjects.xodr road layout (viewed from above):
//
//     Road 1 (100m, hdg=0)              Road 2 (100m, hdg=0)
//   ============================      ============================
//           lane 1_0_1                        lane 2_0_1
//   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─    ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//   (0,0) ──────────── (100,0)       (100,0) ──────────── (200,0)
//   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─    ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//           lane 1_0_-1                       lane 2_0_-1
//   ============================      ============================
//
// Objects on Road 1:
//   obj_vegetation  (StarBush)     : type=vegetation, s=0,   t=4,  zOffset=0.0, star outline (6 corners, closed)
//   obj_barrier     (GuardRail)    : type=barrier,    s=20,  t=3,  zOffset=0.5, validity fromLane=-1 toLane=-1
//   obj_building    (Warehouse)    : type=building,   s=50,  t=-5, zOffset=0.0, radius=4.0
//   obj_obstacle    (RoadObstacle) : type=obstacle,   s=100, t=0,  zOffset=0.0, validity fromLane=-1 toLane=1, rect outline (4 corners, closed)
//
// RoadWithStopLine.xodr road layout:
//
//     Road 1 (100m, hdg=0)
//   ============================
//           lane 1_0_1
//   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//   (0,0) ──────────── (100,0)
//   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//           lane 1_0_-1
//   ============================
//
// Objects on Road 1:
//   obj_stop_line_name   : type=roadMark, name=stopLine   → kStopLine
//   obj_stop_line_subtype: type=roadMark, subtype=stopLine → kStopLine
//   obj_road_mark        : type=roadMark, name=TurnArrow  → kRoadMark

const OBJ_VEGETATION_ID: &str = "obj_vegetation";
const OBJ_BARRIER_ID: &str = "obj_barrier";
const OBJ_BUILDING_ID: &str = "obj_building";
const OBJ_OBSTACLE_ID: &str = "obj_obstacle";

#[test]
fn road_object_book_api() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    let objects = book.road_objects();
    assert_eq!(objects.len(), 4, "Expected 4 road objects");

    let ids: Vec<String> = objects.iter().map(|o| o.id()).collect();
    assert!(
        ids.contains(&OBJ_VEGETATION_ID.to_string()),
        "Missing {OBJ_VEGETATION_ID}"
    );
    assert!(ids.contains(&OBJ_BARRIER_ID.to_string()), "Missing {OBJ_BARRIER_ID}");
    assert!(ids.contains(&OBJ_BUILDING_ID.to_string()), "Missing {OBJ_BUILDING_ID}");
    assert!(ids.contains(&OBJ_OBSTACLE_ID.to_string()), "Missing {OBJ_OBSTACLE_ID}");

    // get_road_object returns Some for known IDs.
    assert!(book.get_road_object(&OBJ_VEGETATION_ID.to_string()).is_some());
    assert!(book.get_road_object(&OBJ_BARRIER_ID.to_string()).is_some());
    assert!(book.get_road_object(&OBJ_BUILDING_ID.to_string()).is_some());
    assert!(book.get_road_object(&OBJ_OBSTACLE_ID.to_string()).is_some());

    // get_road_object returns None for an unknown ID.
    assert!(book.get_road_object(&String::from("nonexistent_id")).is_none());
}

#[test]
fn road_object_api() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    let vegetation = book
        .get_road_object(&OBJ_VEGETATION_ID.to_string())
        .expect("obj_vegetation not found");
    assert_eq!(vegetation.id(), OBJ_VEGETATION_ID);
    assert_eq!(vegetation.name(), Some("StarBush".to_string()));
    assert_eq!(
        vegetation.object_type(),
        maliput::api::objects::RoadObjectType::Vegetation
    );
    assert_eq!(vegetation.subtype(), None);
    assert!(!vegetation.is_dynamic());
    assert!(!vegetation.is_movable());

    let barrier = book
        .get_road_object(&OBJ_BARRIER_ID.to_string())
        .expect("obj_barrier not found");
    assert_eq!(barrier.id(), OBJ_BARRIER_ID);
    assert_eq!(barrier.name(), Some("GuardRail".to_string()));
    assert_eq!(barrier.object_type(), maliput::api::objects::RoadObjectType::GuardRail);
    assert_eq!(barrier.subtype(), Some("guardRail".to_string()));
    assert!(!barrier.is_dynamic());
    assert!(!barrier.is_movable());

    let building = book
        .get_road_object(&OBJ_BUILDING_ID.to_string())
        .expect("obj_building not found");
    assert_eq!(building.id(), OBJ_BUILDING_ID);
    assert_eq!(building.name(), Some("Warehouse".to_string()));
    assert_eq!(building.object_type(), maliput::api::objects::RoadObjectType::Building);
    assert_eq!(building.subtype(), None);
    assert!(!building.is_dynamic());
    assert!(!building.is_movable());

    let obstacle = book
        .get_road_object(&OBJ_OBSTACLE_ID.to_string())
        .expect("obj_obstacle not found");
    assert_eq!(obstacle.id(), OBJ_OBSTACLE_ID);
    assert_eq!(obstacle.name(), Some("RoadObstacle".to_string()));
    assert_eq!(obstacle.object_type(), maliput::api::objects::RoadObjectType::Obstacle);
    assert_eq!(obstacle.subtype(), None);
    assert!(!obstacle.is_dynamic());
    assert!(!obstacle.is_movable());
}

#[test]
fn road_object_position() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();
    let tol = 1e-2; // Equal to the linear_tolerance set in [common::create_malidrive_road_network].

    // obj_vegetation: s=0, t=4, zOffset=0 on hdg=0 road → inertial ≈ (0, 4, 0).
    let veg = book.get_road_object(&OBJ_VEGETATION_ID.to_string()).unwrap();
    let veg_pos = veg.position();
    assert!(
        (veg_pos.inertial_position.x() - 0.0).abs() < tol,
        "veg x: got {}",
        veg_pos.inertial_position.x()
    );
    assert!(
        (veg_pos.inertial_position.y() - 4.0).abs() < tol,
        "veg y: got {}",
        veg_pos.inertial_position.y()
    );
    assert!(
        (veg_pos.inertial_position.z() - 0.0).abs() < tol,
        "veg z: got {}",
        veg_pos.inertial_position.z()
    );

    // obj_barrier: s=20, t=3, zOffset=0.5 → inertial ≈ (20, 3, 0.5).
    let barrier = book.get_road_object(&OBJ_BARRIER_ID.to_string()).unwrap();
    let barrier_pos = barrier.position();
    assert!(
        (barrier_pos.inertial_position.x() - 20.0).abs() < tol,
        "barrier x: got {}",
        barrier_pos.inertial_position.x()
    );
    assert!(
        (barrier_pos.inertial_position.y() - 3.0).abs() < tol,
        "barrier y: got {}",
        barrier_pos.inertial_position.y()
    );
    assert!(
        (barrier_pos.inertial_position.z() - 0.5).abs() < tol,
        "barrier z: got {}",
        barrier_pos.inertial_position.z()
    );

    // obj_building: s=50, t=-5, zOffset=0 → inertial ≈ (50, -5, 0).
    let building = book.get_road_object(&OBJ_BUILDING_ID.to_string()).unwrap();
    let building_pos = building.position();
    assert!(
        (building_pos.inertial_position.x() - 50.0).abs() < tol,
        "building x: got {}",
        building_pos.inertial_position.x()
    );
    assert!(
        (building_pos.inertial_position.y() - (-5.0)).abs() < tol,
        "building y: got {}",
        building_pos.inertial_position.y()
    );
    assert!(
        (building_pos.inertial_position.z() - 0.0).abs() < tol,
        "building z: got {}",
        building_pos.inertial_position.z()
    );
}

#[test]
fn road_object_bounding_box() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    // Every RoadObject should have a valid bounding box with 8 vertices.
    let barrier = book.get_road_object(&OBJ_BARRIER_ID.to_string()).unwrap();
    assert_eq!(barrier.bounding_box().get_vertices().len(), 8);

    let building = book.get_road_object(&OBJ_BUILDING_ID.to_string()).unwrap();
    assert_eq!(building.bounding_box().get_vertices().len(), 8);

    let obstacle = book.get_road_object(&OBJ_OBSTACLE_ID.to_string()).unwrap();
    assert_eq!(obstacle.bounding_box().get_vertices().len(), 8);
}

#[test]
fn road_object_outlines() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    // obj_vegetation has 1 outline with 6 corners (star shape, closed).
    let veg = book.get_road_object(&OBJ_VEGETATION_ID.to_string()).unwrap();
    assert_eq!(veg.num_outlines(), 1);
    let veg_outlines = veg.outlines();
    assert_eq!(veg_outlines.len(), 1);
    assert!(veg_outlines[0].is_closed());
    assert_eq!(veg_outlines[0].num_corners(), 6);
    assert_eq!(veg_outlines[0].corners().len(), 6);
    // All corners have a height attribute (height="2.0" in the XODR cornerLocal).
    assert!(
        veg_outlines[0].corners().iter().all(|c| c.height.is_some()),
        "All vegetation corners should have height"
    );

    // obj_obstacle has 1 outline with 4 corners (closed, using cornerRoad).
    let obstacle = book.get_road_object(&OBJ_OBSTACLE_ID.to_string()).unwrap();
    assert_eq!(obstacle.num_outlines(), 1);
    let obstacle_outlines = obstacle.outlines();
    assert_eq!(obstacle_outlines.len(), 1);
    assert!(obstacle_outlines[0].is_closed());
    assert_eq!(obstacle_outlines[0].num_corners(), 4);
    assert_eq!(obstacle_outlines[0].corners().len(), 4);

    // obj_barrier has no outlines.
    let barrier = book.get_road_object(&OBJ_BARRIER_ID.to_string()).unwrap();
    assert_eq!(barrier.num_outlines(), 0);
    assert!(barrier.outlines().is_empty());

    // obj_building has no outlines.
    let building = book.get_road_object(&OBJ_BUILDING_ID.to_string()).unwrap();
    assert_eq!(building.num_outlines(), 0);
    assert!(building.outlines().is_empty());
}

#[test]
fn road_object_related_lanes() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    // obj_barrier: validity fromLane=-1 toLane=-1 → related to lane "1_0_-1" only.
    let barrier = book.get_road_object(&OBJ_BARRIER_ID.to_string()).unwrap();
    let barrier_lanes = barrier.related_lanes();
    assert!(
        barrier_lanes.contains(&"1_0_-1".to_string()),
        "obj_barrier related lanes: {:?}",
        barrier_lanes
    );
    assert!(
        !barrier_lanes.contains(&"1_0_1".to_string()),
        "obj_barrier should not be related to 1_0_1: {:?}",
        barrier_lanes
    );

    // obj_obstacle: validity fromLane=-1 toLane=1 → related to lanes "1_0_-1" and "1_0_1".
    let obstacle = book.get_road_object(&OBJ_OBSTACLE_ID.to_string()).unwrap();
    let obstacle_lanes = obstacle.related_lanes();
    assert!(
        obstacle_lanes.contains(&"1_0_-1".to_string()),
        "obj_obstacle related lanes: {:?}",
        obstacle_lanes
    );
    assert!(
        obstacle_lanes.contains(&"1_0_1".to_string()),
        "obj_obstacle related lanes: {:?}",
        obstacle_lanes
    );
}

#[test]
fn road_object_book_find_by_type() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    let vegetation_objects = book.find_by_type(&maliput::api::objects::RoadObjectType::Vegetation);
    assert_eq!(vegetation_objects.len(), 1);
    assert_eq!(vegetation_objects[0].id(), OBJ_VEGETATION_ID);

    let guard_rail_objects = book.find_by_type(&maliput::api::objects::RoadObjectType::GuardRail);
    assert_eq!(guard_rail_objects.len(), 1);
    assert_eq!(guard_rail_objects[0].id(), OBJ_BARRIER_ID);

    let building_objects = book.find_by_type(&maliput::api::objects::RoadObjectType::Building);
    assert_eq!(building_objects.len(), 1);
    assert_eq!(building_objects[0].id(), OBJ_BUILDING_ID);

    let obstacle_objects = book.find_by_type(&maliput::api::objects::RoadObjectType::Obstacle);
    assert_eq!(obstacle_objects.len(), 1);
    assert_eq!(obstacle_objects[0].id(), OBJ_OBSTACLE_ID);

    // No Pole objects in this map.
    let pole_objects = book.find_by_type(&maliput::api::objects::RoadObjectType::Pole);
    assert!(
        pole_objects.is_empty(),
        "Expected no Pole objects, got {:?}",
        pole_objects.len()
    );
}

#[test]
fn road_object_book_find_by_lane() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    // Lane "1_0_-1": obj_barrier (validity -1 to -1) and obj_obstacle (validity -1 to 1).
    let objects_1_0_minus1 = book.find_by_lane(&String::from("1_0_-1"));
    let ids: Vec<String> = objects_1_0_minus1.iter().map(|o| o.id()).collect();
    assert!(
        ids.contains(&OBJ_BARRIER_ID.to_string()),
        "Expected obj_barrier in lane 1_0_-1, got: {:?}",
        ids
    );
    assert!(
        ids.contains(&OBJ_OBSTACLE_ID.to_string()),
        "Expected obj_obstacle in lane 1_0_-1, got: {:?}",
        ids
    );

    // Lane "1_0_1": obj_obstacle only (validity -1 to 1 includes +1, barrier is -1 only).
    let objects_1_0_1 = book.find_by_lane(&String::from("1_0_1"));
    let ids: Vec<String> = objects_1_0_1.iter().map(|o| o.id()).collect();
    assert!(
        ids.contains(&OBJ_OBSTACLE_ID.to_string()),
        "Expected obj_obstacle in lane 1_0_1, got: {:?}",
        ids
    );
    assert!(
        !ids.contains(&OBJ_BARRIER_ID.to_string()),
        "obj_barrier should not be in lane 1_0_1, got: {:?}",
        ids
    );

    // Unknown lane returns empty.
    let none_objects = book.find_by_lane(&String::from("nonexistent_lane"));
    assert!(none_objects.is_empty());
}

#[test]
fn road_object_book_find_in_radius() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    // obj_building is at approximately (50, -5, 0); radius 2.0 should find it.
    let results = book.find_in_radius(50.0, -5.0, 0.0, 2.0);
    let ids: Vec<String> = results.iter().map(|o| o.id()).collect();
    assert!(
        ids.contains(&OBJ_BUILDING_ID.to_string()),
        "Expected obj_building near (50, -5, 0), got: {:?}",
        ids
    );

    // Searching far away with a tiny radius returns nothing.
    let far_results = book.find_in_radius(500.0, 500.0, 500.0, 0.1);
    assert!(
        far_results.is_empty(),
        "Expected empty result far away, got: {:?}",
        far_results.len()
    );
}

#[test]
fn road_object_properties() {
    let road_network = common::create_malidrive_road_network("TwoRoadsWithRoadObjects.xodr", None, None);
    let book = road_network.road_object_book();

    // Verify properties() is callable and returns a HashMap without panicking.
    for obj in book.road_objects() {
        let _props = obj.properties();
    }
}

// ArcLaneOffsetWithGuardRail.xodr road layout:
//
// Road "1" is a single 100m arc (curvature=0.025 -> R=40m), starting at (0,0) with hdg=0,
// curving left about C=(0,40). No elevation profile, so z=0 everywhere.
//
// Continuous (repeat-based) objects on Road 1:
//   guardrail_right_boundary  : s=[0,50],   t=-0.5 constant,     width=0.3 constant,        height=1.0
//   guardrail_left_boundary   : s=[50,100], t=4.5 -> 5.0,        width=0.3 -> 1.3,          height=1.0
//   guardrail_detached_boundary: same repeat as guardrail_right_boundary but
//                                 detachFromReferenceLine=true (samples a chord, not the arc)
//
// Expected values below were cross-checked against maliput_malidrive's own
// `ContinuousObjectRepeatSamplingTest` regression tests, using
// `continuous_object_sampling_distance=5.0` so that each 50m repeat yields exactly 11 samples.
const GUARDRAIL_RIGHT_ID: &str = "guardrail_right_boundary";
const GUARDRAIL_LEFT_ID: &str = "guardrail_left_boundary";
const GUARDRAIL_DETACHED_ID: &str = "guardrail_detached_boundary";

fn create_arc_guard_rail_road_network(continuous_object_sampling_distance: &str) -> maliput::api::RoadNetwork {
    let rm = maliput::ResourceManager::new();
    let xodr_path = rm
        .get_resource_path_by_name("maliput_malidrive", "ArcLaneOffsetWithGuardRail.xodr")
        .unwrap();

    let properties = std::collections::HashMap::from([
        ("road_geometry_id", "my_rg_from_rust"),
        ("opendrive_file", xodr_path.to_str().unwrap()),
        ("omit_nondrivable_lanes", "false"),
        (
            "continuous_object_sampling_distance",
            continuous_object_sampling_distance,
        ),
    ]);
    let rn_res = maliput::api::RoadNetwork::new(maliput::api::RoadNetworkBackend::MaliputMalidrive, &properties);
    assert!(
        rn_res.is_ok(),
        "Expected RoadNetwork to be created successfully with ArcLaneOffsetWithGuardRail.xodr"
    );
    rn_res.unwrap()
}

#[test]
fn road_object_continuous_properties_attached_guard_rail() {
    let tol = 1e-2;
    let road_network = create_arc_guard_rail_road_network("5.0");
    let book = road_network.road_object_book();

    let right = book.get_road_object(&GUARDRAIL_RIGHT_ID.to_string()).unwrap();
    let samples = right.continuous_properties();
    assert_eq!(samples.len(), 11, "Expected 11 samples for a 50m repeat at 5m spacing");

    // width/height are constant across this repeat (widthStart == widthEnd == 0.3, heightStart == heightEnd == 1.0).
    for (i, sample) in samples.iter().enumerate() {
        assert!(
            (sample.width - 0.3).abs() < tol,
            "sample {i} width: got {}",
            sample.width
        );
        assert!(
            (sample.height - 1.0).abs() < tol,
            "sample {i} height: got {}",
            sample.height
        );
    }

    // First sample: s=0, t=-0.5 -> matches the object's own declared position exactly.
    let first = &samples[0];
    assert!(
        (first.point_sample.x() - 0.0).abs() < tol,
        "first sample x: got {}",
        first.point_sample.x()
    );
    assert!(
        (first.point_sample.y() - (-0.5)).abs() < tol,
        "first sample y: got {}",
        first.point_sample.y()
    );
    assert!(
        (first.point_sample.z() - 0.0).abs() < tol,
        "first sample z: got {}",
        first.point_sample.z()
    );

    // Last sample: s=50, t=-0.5 (end of the repeat, on the arc, not the road's s=100 end).
    let last = samples.last().unwrap();
    assert!(
        (last.point_sample.x() - 38.43388).abs() < tol,
        "last sample x: got {}",
        last.point_sample.x()
    );
    assert!(
        (last.point_sample.y() - 27.22944).abs() < tol,
        "last sample y: got {}",
        last.point_sample.y()
    );
    assert!(
        (last.point_sample.z() - 0.0).abs() < tol,
        "last sample z: got {}",
        last.point_sample.z()
    );
}

#[test]
fn road_object_continuous_properties_left_guard_rail_interpolates() {
    let tol = 1e-2;
    let road_network = create_arc_guard_rail_road_network("5.0");
    let book = road_network.road_object_book();

    let left = book.get_road_object(&GUARDRAIL_LEFT_ID.to_string()).unwrap();
    let samples = left.continuous_properties();
    assert_eq!(samples.len(), 11, "Expected 11 samples for a 50m repeat at 5m spacing");

    // First sample: s=50, t=4.5, widthStart=0.3.
    let first = &samples[0];
    assert!(
        (first.width - 0.3).abs() < tol,
        "first sample width: got {}",
        first.width
    );
    assert!(
        (first.point_sample.x() - 33.68895).abs() < tol,
        "first sample x: got {}",
        first.point_sample.x()
    );
    assert!(
        (first.point_sample.y() - 28.80606).abs() < tol,
        "first sample y: got {}",
        first.point_sample.y()
    );

    // Last sample: s=100, t=5.0, widthEnd=1.3.
    let last = samples.last().unwrap();
    assert!((last.width - 1.3).abs() < tol, "last sample width: got {}", last.width);
    assert!(
        (last.point_sample.x() - 20.94653).abs() < tol,
        "last sample x: got {}",
        last.point_sample.x()
    );
    assert!(
        (last.point_sample.y() - 68.04003).abs() < tol,
        "last sample y: got {}",
        last.point_sample.y()
    );

    // height is untouched by the width ramp (heightStart == heightEnd == 1.0).
    assert!(
        (first.height - 1.0).abs() < tol && (last.height - 1.0).abs() < tol,
        "expected constant height, got first={}, last={}",
        first.height,
        last.height
    );
}

#[test]
fn road_object_continuous_properties_detached_matches_attached_endpoints() {
    let tol = 1e-2;
    let road_network = create_arc_guard_rail_road_network("5.0");
    let book = road_network.road_object_book();

    // guardrail_detached_boundary shares the same repeat span/lateral offset as
    // guardrail_right_boundary, but detachFromReferenceLine=true makes it sample a
    // chord instead of the arc. Endpoints must still coincide since ratio=0/1 collapse
    // the chord interpolation onto the same start/end points used by the attached repeat.
    let detached = book.get_road_object(&GUARDRAIL_DETACHED_ID.to_string()).unwrap();
    let detached_samples = detached.continuous_properties();
    assert_eq!(detached_samples.len(), 11);

    let detached_first = &detached_samples[0];
    let detached_last = detached_samples.last().unwrap();
    assert!((detached_first.point_sample.x() - 0.0).abs() < tol);
    assert!((detached_first.point_sample.y() - (-0.5)).abs() < tol);
    assert!((detached_last.point_sample.x() - 38.43388).abs() < tol);
    assert!((detached_last.point_sample.y() - 27.22944).abs() < tol);

    // Interior points diverge from the attached (on-arc) samples: the midpoint of the
    // chord must sit roughly halfway between the endpoints, unlike the attached repeat's
    // arc-following midpoint.
    let expected_mid_x = (detached_first.point_sample.x() + detached_last.point_sample.x()) / 2.0;
    let expected_mid_y = (detached_first.point_sample.y() + detached_last.point_sample.y()) / 2.0;
    let mid = &detached_samples[detached_samples.len() / 2];
    assert!(
        (mid.point_sample.x() - expected_mid_x).abs() < tol,
        "chord midpoint x: got {}, expected {}",
        mid.point_sample.x(),
        expected_mid_x
    );
    assert!(
        (mid.point_sample.y() - expected_mid_y).abs() < tol,
        "chord midpoint y: got {}, expected {}",
        mid.point_sample.y(),
        expected_mid_y
    );
}

#[test]
fn road_object_book_find_in_radius_matches_continuous_object_sample() {
    let tol = 1.0;
    let road_network = create_arc_guard_rail_road_network("5.0");
    let book = road_network.road_object_book();

    // The last sample of guardrail_right_boundary is at (38.43388, 27.22944, 0), far away
    // from the object's own declared position (s=0, t=-0.5 -> inertial (0, -0.5, 0)).
    // find_in_radius must match it via the continuous sample, not just the base position.
    let near_last_sample = book.find_in_radius(38.43388, 27.22944, 0.0, tol);
    let ids: Vec<String> = near_last_sample.iter().map(|o| o.id()).collect();
    assert!(
        ids.contains(&GUARDRAIL_RIGHT_ID.to_string()),
        "Expected {GUARDRAIL_RIGHT_ID} to be found near its last continuous sample, got: {ids:?}"
    );

    // The first sample coincides with the object's own declared position, (0, -0.5, 0).
    let near_first_sample = book.find_in_radius(0.0, -0.5, 0.0, tol);
    let ids: Vec<String> = near_first_sample.iter().map(|o| o.id()).collect();
    assert!(
        ids.contains(&GUARDRAIL_RIGHT_ID.to_string()),
        "Expected {GUARDRAIL_RIGHT_ID} to be found near its first continuous sample, got: {ids:?}"
    );

    // Far away from every sample and every base position, nothing should match.
    let far_results = book.find_in_radius(500.0, 500.0, 500.0, 0.1);
    assert!(
        far_results.is_empty(),
        "Expected empty result far away, got: {:?}",
        far_results.len()
    );
}