Skip to content

Commit

Permalink
Adds zone method to rules api. (#122)
Browse files Browse the repository at this point in the history
Signed-off-by: Franco Cipollone <[email protected]>
  • Loading branch information
francocipollone committed Jun 25, 2024
1 parent 80d6188 commit 4463b6d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions maliput-sys/src/api/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub mod ffi {
type InertialPosition = crate::api::ffi::InertialPosition;
#[namespace = "maliput::api"]
type Rotation = crate::api::ffi::Rotation;
#[namespace = "maliput::api"]
type LaneSRoute = crate::api::ffi::LaneSRoute;
#[namespace = "maliput::math"]
type Vector3 = crate::math::ffi::Vector3;

Expand Down Expand Up @@ -181,6 +183,7 @@ pub mod ffi {
fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
fn DiscreteValueRule_zone(rule: &DiscreteValueRule) -> UniquePtr<LaneSRoute>;

// RangeValueRule::Range bindings definitions.
type RangeValueRuleRange;
Expand All @@ -195,6 +198,7 @@ pub mod ffi {
type RangeValueRule;
fn RangeValueRule_id(rule: &RangeValueRule) -> String;
fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
fn RangeValueRule_zone(rule: &RangeValueRule) -> UniquePtr<LaneSRoute>;
fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
}
}
8 changes: 8 additions & 0 deletions maliput-sys/src/api/rules/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ rust::String DiscreteValueRule_type_id(const DiscreteValueRule& rule) {
return rule.type_id().string();
}

std::unique_ptr<LaneSRoute> DiscreteValueRule_zone(const DiscreteValueRule& rule) {
return std::make_unique<LaneSRoute>(rule.zone());
}

rust::String RangeValueRuleRange_description(const RangeValueRuleRange& range) {
return rust::String(range.description);
}
Expand Down Expand Up @@ -273,6 +277,10 @@ rust::String RangeValueRule_type_id(const RangeValueRule& rule) {
return rule.type_id().string();
}

std::unique_ptr<LaneSRoute> RangeValueRule_zone(const RangeValueRule& rule) {
return std::make_unique<LaneSRoute>(rule.zone());
}

std::unique_ptr<RangeValueRule> RoadRulebook_GetRangeValueRule(const RoadRulebook& road_rulebook, const rust::String& id) {
return std::make_unique<RangeValueRule>(road_rulebook.GetRangeValueRule(Rule::Id{std::string(id)}));
}
Expand Down
10 changes: 6 additions & 4 deletions maliput/src/api/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,9 @@ impl DiscreteValueRule {
maliput_sys::api::rules::ffi::DiscreteValueRule_type_id(&self.discrete_value_rule)
}
/// Returns a [LaneSRoute] that represents the zone that the rule applies to.
pub fn zone(&self) {
unimplemented!("Not yet implemented")
pub fn zone(&self) -> crate::api::LaneSRoute {
let lane_s_route = maliput_sys::api::rules::ffi::DiscreteValueRule_zone(&self.discrete_value_rule);
crate::api::LaneSRoute { lane_s_route }
}
/// Returns the states of the rule.
pub fn states(&self) -> Vec<DiscreteValue> {
Expand Down Expand Up @@ -602,8 +603,9 @@ impl RangeValueRule {
maliput_sys::api::rules::ffi::RangeValueRule_type_id(&self.range_value_rule)
}
/// Returns a [LaneSRoute] that represents the zone that the rule applies to.
pub fn zone(&self) {
unimplemented!("Not yet implemented")
pub fn zone(&self) -> crate::api::LaneSRoute {
let lane_s_route = maliput_sys::api::rules::ffi::RangeValueRule_zone(&self.range_value_rule);
crate::api::LaneSRoute { lane_s_route }
}
/// Returns the states of the rule.
pub fn states(&self) -> Vec<Range> {
Expand Down
4 changes: 4 additions & 0 deletions maliput/tests/discrete_value_rule_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod common;

#[test]
fn discrete_value_rule_test_api() {
static TOLERANCE: f64 = 1e-9;
use maliput::api::rules::RuleState;

let road_network = common::create_loop_road_pedestrian_crosswalk_road_network_with_books();
Expand All @@ -45,6 +46,9 @@ fn discrete_value_rule_test_api() {
let rule = book.get_discrete_value_rule(&expected_rule_id);
assert_eq!(rule.id(), expected_rule_id);
assert_eq!(rule.type_id(), expected_type_id);
let zone = rule.zone();
let expected_zone_length = 15.;
assert!((zone.length() - expected_zone_length).abs() < TOLERANCE);

let states = rule.states();
assert_eq!(states.len(), 2); // Go and Stop
Expand Down
4 changes: 4 additions & 0 deletions maliput/tests/range_value_rule_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod common;

#[test]
fn range_value_rule_test_api() {
static TOLERANCE: f64 = 1e-9;
use maliput::api::rules::RuleState;

let road_network = common::create_loop_road_pedestrian_crosswalk_road_network_with_books();
Expand All @@ -45,6 +46,9 @@ fn range_value_rule_test_api() {
let rule = book.get_range_value_rule(&expected_rule_id);
assert_eq!(rule.id(), expected_rule_id);
assert_eq!(rule.type_id(), expected_type_id);
let zone = rule.zone();
let expected_zone_length = 15.;
assert!((zone.length() - expected_zone_length).abs() < TOLERANCE);

let states = rule.states();
assert_eq!(states.len(), 1); // Only one speed limit state
Expand Down

0 comments on commit 4463b6d

Please sign in to comment.