maliput_sys/api/
mod.rs

1// BSD 3-Clause License
2//
3// Copyright (c) 2024, Woven by Toyota.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// * Redistributions of source code must retain the above copyright notice, this
10//   list of conditions and the following disclaimer.
11//
12// * Redistributions in binary form must reproduce the above copyright notice,
13//   this list of conditions and the following disclaimer in the documentation
14//   and/or other materials provided with the distribution.
15//
16// * Neither the name of the copyright holder nor the names of its
17//   contributors may be used to endorse or promote products derived from
18//   this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31pub mod rules;
32
33#[cxx::bridge(namespace = "maliput::api")]
34#[allow(clippy::needless_lifetimes)] // Remove after rust 1.87 is used. https://github.com/rust-lang/rust-clippy/issues/14441
35#[allow(clippy::missing_safety_doc)]
36pub mod ffi {
37    /// Shared struct for `Lane` pointers.
38    /// This is needed because `*const Lane` can't be used directly in the CxxVector collection.
39    struct ConstLanePtr {
40        pub lane: *const Lane,
41    }
42    /// Shared struct for `Intersection` pointers.
43    /// This is needed because `*mut Intersection` can't be used directly in the CxxVector collection.
44    struct MutIntersectionPtr {
45        pub intersection: *mut Intersection,
46    }
47    /// Shared struct for `LaneSRange` references.
48    /// This is needed because `&f` can't be used directly in the CxxVector collection.
49    struct ConstLaneSRangeRef<'a> {
50        pub lane_s_range: &'a LaneSRange,
51    }
52
53    unsafe extern "C++" {
54        include!("api/api.h");
55        include!("cxx_utils/error_handling.h");
56
57        #[namespace = "maliput::math"]
58        type Vector3 = crate::math::ffi::Vector3;
59        #[namespace = "maliput::math"]
60        type Quaternion = crate::math::ffi::Quaternion;
61        #[namespace = "maliput::math"]
62        type Matrix3 = crate::math::ffi::Matrix3;
63        #[namespace = "maliput::math"]
64        type RollPitchYaw = crate::math::ffi::RollPitchYaw;
65        #[namespace = "maliput::api::rules"]
66        type RoadRulebook = crate::api::rules::ffi::RoadRulebook;
67        #[namespace = "maliput::api::rules"]
68        type TrafficLightBook = crate::api::rules::ffi::TrafficLightBook;
69
70        #[namespace = "maliput::api"]
71        // RoadNetwork bindings definitions.
72        type RoadNetwork;
73        fn road_geometry(self: &RoadNetwork) -> *const RoadGeometry;
74        fn intersection_book(self: Pin<&mut RoadNetwork>) -> *mut IntersectionBook;
75        fn traffic_light_book(self: &RoadNetwork) -> *const TrafficLightBook;
76        fn rulebook(self: &RoadNetwork) -> *const RoadRulebook;
77
78        // RoadGeometry bindings definitions.
79        type RoadGeometry;
80        fn RoadGeometry_id(road_geometry: &RoadGeometry) -> String;
81        fn num_junctions(self: &RoadGeometry) -> i32;
82        fn linear_tolerance(self: &RoadGeometry) -> f64;
83        fn angular_tolerance(self: &RoadGeometry) -> f64;
84        fn num_branch_points(self: &RoadGeometry) -> i32;
85        fn RoadGeometry_ToRoadPosition(
86            rg: &RoadGeometry,
87            inertial_position: &InertialPosition,
88        ) -> Result<UniquePtr<RoadPositionResult>>;
89        fn RoadGeometry_GetLane(rg: &RoadGeometry, lane_id: &String) -> ConstLanePtr;
90        fn RoadGeometry_GetLanes(rg: &RoadGeometry) -> UniquePtr<CxxVector<ConstLanePtr>>;
91        fn RoadGeometry_GetSegment(rg: &RoadGeometry, segment_id: &String) -> *const Segment;
92        fn RoadGeometry_GetJunction(rg: &RoadGeometry, junction_id: &String) -> *const Junction;
93        fn RoadGeometry_GetBranchPoint(rg: &RoadGeometry, branch_point_id: &String) -> *const BranchPoint;
94        fn RoadGeometry_BackendCustomCommand(rg: &RoadGeometry, command: &String) -> Result<String>;
95        fn RoadGeometry_GeoReferenceInfo(rg: &RoadGeometry) -> String;
96        // LanePosition bindings definitions.
97        type LanePosition;
98        fn LanePosition_new(s: f64, r: f64, h: f64) -> UniquePtr<LanePosition>;
99        fn s(self: &LanePosition) -> f64;
100        fn r(self: &LanePosition) -> f64;
101        fn h(self: &LanePosition) -> f64;
102        fn set_s(self: Pin<&mut LanePosition>, s: f64);
103        fn set_r(self: Pin<&mut LanePosition>, r: f64);
104        fn set_h(self: Pin<&mut LanePosition>, h: f64);
105        fn srh(self: &LanePosition) -> &Vector3;
106        fn set_srh(self: Pin<&mut LanePosition>, srh: &Vector3);
107        fn LanePosition_to_str(lane_pos: &LanePosition) -> String;
108
109        // InertialPosition bindings definitions
110        type InertialPosition;
111        fn InertialPosition_new(x: f64, y: f64, z: f64) -> UniquePtr<InertialPosition>;
112        fn x(self: &InertialPosition) -> f64;
113        fn y(self: &InertialPosition) -> f64;
114        fn z(self: &InertialPosition) -> f64;
115        fn set_x(self: Pin<&mut InertialPosition>, x: f64);
116        fn set_y(self: Pin<&mut InertialPosition>, y: f64);
117        fn set_z(self: Pin<&mut InertialPosition>, z: f64);
118        fn xyz(self: &InertialPosition) -> &Vector3;
119        fn set_xyz(self: Pin<&mut InertialPosition>, xyz: &Vector3);
120        fn length(self: &InertialPosition) -> f64;
121        fn Distance(self: &InertialPosition, other: &InertialPosition) -> f64;
122        fn InertialPosition_to_str(inertial_pos: &InertialPosition) -> String;
123        fn InertialPosition_operator_eq(lhs: &InertialPosition, rhs: &InertialPosition) -> bool;
124        fn InertialPosition_operator_sum(lhs: &InertialPosition, rhs: &InertialPosition)
125            -> UniquePtr<InertialPosition>;
126        fn InertialPosition_operator_sub(lhs: &InertialPosition, rhs: &InertialPosition)
127            -> UniquePtr<InertialPosition>;
128        fn InertialPosition_operator_mul_scalar(lhs: &InertialPosition, scalar: f64) -> UniquePtr<InertialPosition>;
129
130        // Lane bindings definitions
131        type Lane;
132        fn to_left(self: &Lane) -> *const Lane;
133        fn to_right(self: &Lane) -> *const Lane;
134        fn index(self: &Lane) -> i32;
135        fn length(self: &Lane) -> f64;
136        fn Contains(self: &Lane, lane_position: &LanePosition) -> bool;
137        fn segment(self: &Lane) -> *const Segment;
138        fn Lane_id(lane: &Lane) -> String;
139        fn Lane_lane_bounds(lane: &Lane, s: f64) -> Result<UniquePtr<RBounds>>;
140        fn Lane_segment_bounds(lane: &Lane, s: f64) -> Result<UniquePtr<RBounds>>;
141        fn Lane_elevation_bounds(lane: &Lane, s: f64, r: f64) -> Result<UniquePtr<HBounds>>;
142        fn Lane_GetOrientation(lane: &Lane, lane_position: &LanePosition) -> Result<UniquePtr<Rotation>>;
143        fn Lane_ToInertialPosition(lane: &Lane, lane_position: &LanePosition) -> Result<UniquePtr<InertialPosition>>;
144        fn Lane_ToLanePosition(
145            lane: &Lane,
146            inertial_position: &InertialPosition,
147        ) -> Result<UniquePtr<LanePositionResult>>;
148        fn Lane_ToSegmentPosition(
149            lane: &Lane,
150            inertial_position: &InertialPosition,
151        ) -> Result<UniquePtr<LanePositionResult>>;
152        fn Lane_GetBranchPoint(lane: &Lane, start: bool) -> *const BranchPoint;
153        fn Lane_GetConfluentBranches(lane: &Lane, start: bool) -> Result<*const LaneEndSet>;
154        fn Lane_GetOngoingBranches(lane: &Lane, start: bool) -> Result<*const LaneEndSet>;
155        fn Lane_GetDefaultBranch(lane: &Lane, start: bool) -> UniquePtr<LaneEnd>;
156        fn Lane_EvalMotionDerivatives(
157            lane: &Lane,
158            lane_position: &LanePosition,
159            sigma_v: f64,
160            rho_v: f64,
161            eta_v: f64,
162        ) -> UniquePtr<LanePosition>;
163
164        // Segment bindings definitions
165        type Segment;
166        fn num_lanes(self: &Segment) -> i32;
167        fn junction(self: &Segment) -> Result<*const Junction>;
168        fn lane(self: &Segment, index: i32) -> Result<*const Lane>;
169        fn Segment_id(segment: &Segment) -> String;
170
171        // Junction bindings definitions
172        type Junction;
173        fn road_geometry(self: &Junction) -> *const RoadGeometry;
174        fn num_segments(self: &Junction) -> i32;
175        fn segment(self: &Junction, index: i32) -> Result<*const Segment>;
176        fn Junction_id(junction: &Junction) -> String;
177
178        // RoadPosition bindings definitions
179        type RoadPosition;
180        /// # Safety
181        ///
182        /// This function is unsafe because it dereferences `lane` pointers.
183        unsafe fn RoadPosition_new(lane: *const Lane, lane_pos: &LanePosition) -> UniquePtr<RoadPosition>;
184        fn RoadPosition_ToInertialPosition(road_position: &RoadPosition) -> UniquePtr<InertialPosition>;
185        fn RoadPosition_lane(road_position: &RoadPosition) -> *const Lane;
186        fn RoadPosition_pos(road_position: &RoadPosition) -> UniquePtr<LanePosition>;
187
188        // RoadPositionResult bindings definitions
189        type RoadPositionResult;
190        fn RoadPositionResult_road_position(result: &RoadPositionResult) -> UniquePtr<RoadPosition>;
191        fn RoadPositionResult_nearest_position(result: &RoadPositionResult) -> UniquePtr<InertialPosition>;
192        fn RoadPositionResult_distance(result: &RoadPositionResult) -> f64;
193
194        // LanePositionResult bindings definitions
195        type LanePositionResult;
196        fn LanePositionResult_road_position(result: &LanePositionResult) -> UniquePtr<LanePosition>;
197        fn LanePositionResult_nearest_position(result: &LanePositionResult) -> UniquePtr<InertialPosition>;
198        fn LanePositionResult_distance(result: &LanePositionResult) -> f64;
199
200        // Rotation bindings definitions
201        type Rotation;
202        fn Rotation_new() -> UniquePtr<Rotation>;
203        fn Rotation_from_quat(q: &Quaternion) -> UniquePtr<Rotation>;
204        fn Rotation_from_rpy(rpy: &RollPitchYaw) -> UniquePtr<Rotation>;
205        fn roll(self: &Rotation) -> f64;
206        fn pitch(self: &Rotation) -> f64;
207        fn yaw(self: &Rotation) -> f64;
208        fn quat(self: &Rotation) -> &Quaternion;
209        fn Distance(self: &Rotation, other: &Rotation) -> f64;
210        fn Rotation_set_quat(r: Pin<&mut Rotation>, q: &Quaternion);
211        fn Rotation_rpy(r: &Rotation) -> UniquePtr<RollPitchYaw>;
212        fn Rotation_matrix(r: &Rotation) -> UniquePtr<Matrix3>;
213        fn Rotation_Apply(r: &Rotation, ip: &InertialPosition) -> UniquePtr<InertialPosition>;
214        fn Rotation_Reverse(r: &Rotation) -> UniquePtr<Rotation>;
215
216        // RBounds bindings definitions
217        type RBounds;
218        fn min(self: &RBounds) -> f64;
219        fn max(self: &RBounds) -> f64;
220
221        // HBounds bindings definitions
222        type HBounds;
223        fn min(self: &HBounds) -> f64;
224        fn max(self: &HBounds) -> f64;
225
226        // SRange bindings definitions
227        type SRange;
228        fn SRange_new(s0: f64, s1: f64) -> UniquePtr<SRange>;
229        fn s0(self: &SRange) -> f64;
230        fn s1(self: &SRange) -> f64;
231        fn set_s0(self: Pin<&mut SRange>, s0: f64);
232        fn set_s1(self: Pin<&mut SRange>, s1: f64);
233        fn size(self: &SRange) -> f64;
234        fn WithS(self: &SRange) -> bool;
235        fn Intersects(self: &SRange, other: &SRange, tolerance: f64) -> bool;
236        fn Contains(self: &SRange, s_range: &SRange, tolerance: f64) -> bool;
237        fn SRange_GetIntersection(s_range: &SRange, other: &SRange, tolerance: f64) -> UniquePtr<SRange>;
238
239        // LaneSRange bindings definitions
240        type LaneSRange;
241        fn LaneSRange_new(lane_id: &String, s_range: &SRange) -> UniquePtr<LaneSRange>;
242        fn length(self: &LaneSRange) -> f64;
243        fn Intersects(self: &LaneSRange, other: &LaneSRange, tolerance: f64) -> bool;
244        fn Contains(self: &LaneSRange, lane_s_range: &LaneSRange, tolerance: f64) -> bool;
245        fn LaneSRange_lane_id(lane_s_range: &LaneSRange) -> String;
246        fn LaneSRange_s_range(lane_s_range: &LaneSRange) -> UniquePtr<SRange>;
247        fn LaneSRange_GetIntersection(
248            lane_s_range: &LaneSRange,
249            other: &LaneSRange,
250            tolerance: f64,
251        ) -> UniquePtr<LaneSRange>;
252
253        // LaneSRoute bindings definitions
254        type LaneSRoute;
255        fn LaneSRoute_new(ranges: &CxxVector<ConstLaneSRangeRef>) -> UniquePtr<LaneSRoute>;
256        fn length(self: &LaneSRoute) -> f64;
257        fn Intersects(self: &LaneSRoute, other: &LaneSRoute, tolerance: f64) -> bool;
258        fn ranges(self: &LaneSRoute) -> &CxxVector<LaneSRange>;
259
260        // LaneEnd bindings definitions
261        type LaneEnd;
262        // maliput::api Rust will have its own LaneEnd enum.
263        // However, this LaneEnd_new is expected to be used on methods that return a Cpp LaneEnd
264        // and a conversion to Rust LaneEnd is needed.
265        /// # Safety
266        /// This function is unsafe because it dereferences `lane` pointer.
267        unsafe fn LaneEnd_new(lane: *const Lane, start: bool) -> UniquePtr<LaneEnd>;
268        fn LaneEnd_lane(lane_end: &LaneEnd) -> *const Lane;
269        fn LaneEnd_is_start(lane_end: &LaneEnd) -> bool;
270
271        // LaneEndSet bindings definitions
272        type LaneEndSet;
273        fn size(self: &LaneEndSet) -> i32;
274        fn get(self: &LaneEndSet, index: i32) -> Result<&LaneEnd>;
275
276        // BranchPoint bindings definitions
277        type BranchPoint;
278        fn BranchPoint_id(branch_point: &BranchPoint) -> String;
279        fn road_geometry(self: &BranchPoint) -> *const RoadGeometry;
280        fn GetConfluentBranches(self: &BranchPoint, end: &LaneEnd) -> Result<*const LaneEndSet>;
281        fn GetOngoingBranches(self: &BranchPoint, end: &LaneEnd) -> Result<*const LaneEndSet>;
282        fn GetASide(self: &BranchPoint) -> *const LaneEndSet;
283        fn GetBSide(self: &BranchPoint) -> *const LaneEndSet;
284        fn BranchPoint_GetDefaultBranch(branch_point: &BranchPoint, end: &LaneEnd) -> UniquePtr<LaneEnd>;
285
286        // Intersection bindings definitions
287        type Intersection;
288        fn Intersection_id(intersection: &Intersection) -> String;
289
290        // IntersectionBook bindings definitions
291        type IntersectionBook;
292        fn IntersectionBook_GetIntersection(book: Pin<&mut IntersectionBook>, id: &String) -> MutIntersectionPtr;
293        fn IntersectionBook_GetIntersections(
294            book: Pin<&mut IntersectionBook>,
295        ) -> UniquePtr<CxxVector<MutIntersectionPtr>>;
296
297    }
298    impl UniquePtr<RoadNetwork> {}
299    impl UniquePtr<LanePosition> {}
300}