Skip to content

Commit

Permalink
Bindings for LaneEndSet abstraction.
Browse files Browse the repository at this point in the history
Signed-off-by: Franco Cipollone <[email protected]>
  • Loading branch information
francocipollone committed Apr 16, 2024
1 parent f122b7e commit 243a9f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
9 changes: 9 additions & 0 deletions maliput-sys/src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <sstream>
#include <vector>

#include <maliput/api/branch_point.h>
#include <maliput/api/junction.h>
#include <maliput/api/lane.h>
#include <maliput/api/lane_data.h>
Expand Down Expand Up @@ -274,5 +275,13 @@ std::unique_ptr<LaneEnd> LaneEnd_new(const Lane* lane, bool start) {
return std::make_unique<LaneEnd>(lane, start ? LaneEnd::kStart : LaneEnd::kFinish);
}

const Lane* LaneEnd_lane(const LaneEnd& lane_end) {
return lane_end.lane;
}

bool LaneEnd_is_start(const LaneEnd& lane_end) {
return lane_end.end == LaneEnd::kStart;
}

} // namespace api
} // namespace maliput
6 changes: 6 additions & 0 deletions maliput-sys/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ pub mod ffi {
/// # Safety
/// This function is unsafe because it dereferences `lane` pointer.
unsafe fn LaneEnd_new(lane: *const Lane, start: bool) -> UniquePtr<LaneEnd>;
fn LaneEnd_lane(lane_end: &LaneEnd) -> *const Lane;
fn LaneEnd_is_start(lane_end: &LaneEnd) -> bool;

// LaneEndSet bindings definitions
type LaneEndSet;
fn size(self: &LaneEndSet) -> i32;
fn get(self: &LaneEndSet, index: i32) -> &LaneEnd;
}
impl UniquePtr<RoadNetwork> {}
impl UniquePtr<LanePosition> {}
Expand Down
32 changes: 30 additions & 2 deletions maliput/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,37 @@ impl LaneSRange {
/// This is analogous to the C++ maliput::api::LaneEnd implementation.
pub enum LaneEnd<'a> {
/// The start of the Lane. ("s == 0")
Start(&'a Lane<'a>),
Start(Lane<'a>),
/// The end of the Lane. ("s == length")
Finish(&'a Lane<'a>),
Finish(Lane<'a>),
}

/// A set of LaneEnds.
pub struct LaneEndSet<'a> {
lane_end_set: &'a maliput_sys::api::ffi::LaneEndSet,
}

impl<'a> LaneEndSet<'a> {
/// Obtain the size of the LaneEndSet.
pub fn size(&self) -> i32 {
self.lane_end_set.size()
}
/// Get the LaneEnd at the given index.
pub fn get(&self, index: i32) -> LaneEnd {
let lane_end = self.lane_end_set.get(index);
// Obtain end type and lane reference.
let is_start = maliput_sys::api::ffi::LaneEnd_is_start(lane_end);
let lane_ref = unsafe {
maliput_sys::api::ffi::LaneEnd_lane(lane_end)
.as_ref()
.expect("Underlying LaneEnd is null")
};
// Create a LaneEnd enum variant.
match is_start {
true => LaneEnd::Start(Lane { lane: lane_ref }),
false => LaneEnd::Finish(Lane { lane: lane_ref }),
}
}
}

mod tests {
Expand Down
11 changes: 8 additions & 3 deletions maliput/tests/lane_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,20 @@ fn lane_end_test() {
let road_geometry = road_network.road_geometry();

let lane_id = String::from("0_0_1");
let lane = road_geometry.get_lane(&lane_id);
let lane_end_start = maliput::api::LaneEnd::Start(&lane);
let lane_end_start = maliput::api::LaneEnd::Start(road_geometry.get_lane(&lane_id));
match lane_end_start {
maliput::api::LaneEnd::Start(lane) => assert_eq!(lane.id(), lane_id),
maliput::api::LaneEnd::Finish(_) => panic!("Expected Start, got Finish"),
}
let lane_end_end = maliput::api::LaneEnd::Finish(&lane);
let lane_end_end = maliput::api::LaneEnd::Finish(road_geometry.get_lane(&lane_id));
match lane_end_end {
maliput::api::LaneEnd::Start(_) => panic!("Expected Finish, got Start"),
maliput::api::LaneEnd::Finish(lane) => assert_eq!(lane.id(), lane_id),
}
}

#[test]
fn lane_end_set_test() {
// TODO: Implement this test once BranchPoint is implemented as we
// the latter provides methods to get LaneEndSet.
}

0 comments on commit 243a9f6

Please sign in to comment.