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
56        #[namespace = "maliput::math"]
57        type Vector3 = crate::math::ffi::Vector3;
58        #[namespace = "maliput::math"]
59        type Quaternion = crate::math::ffi::Quaternion;
60        #[namespace = "maliput::math"]
61        type Matrix3 = crate::math::ffi::Matrix3;
62        #[namespace = "maliput::math"]
63        type RollPitchYaw = crate::math::ffi::RollPitchYaw;
64        #[namespace = "maliput::api::rules"]
65        type RoadRulebook = crate::api::rules::ffi::RoadRulebook;
66        #[namespace = "maliput::api::rules"]
67        type TrafficLightBook = crate::api::rules::ffi::TrafficLightBook;
68
69        #[namespace = "maliput::api"]
70        // RoadNetwork bindings definitions.
71        type RoadNetwork;
72        fn road_geometry(self: &RoadNetwork) -> *const RoadGeometry;
73        fn intersection_book(self: Pin<&mut RoadNetwork>) -> *mut IntersectionBook;
74        fn traffic_light_book(self: &RoadNetwork) -> *const TrafficLightBook;
75        fn rulebook(self: &RoadNetwork) -> *const RoadRulebook;
76
77        // RoadGeometry bindings definitions.
78        type RoadGeometry;
79        fn RoadGeometry_id(road_geometry: &RoadGeometry) -> String;
80        fn num_junctions(self: &RoadGeometry) -> i32;
81        fn linear_tolerance(self: &RoadGeometry) -> f64;
82        fn angular_tolerance(self: &RoadGeometry) -> f64;
83        fn num_branch_points(self: &RoadGeometry) -> i32;
84        fn RoadGeometry_ToRoadPosition(
85            rg: &RoadGeometry,
86            inertial_position: &InertialPosition,
87        ) -> UniquePtr<RoadPositionResult>;
88        fn RoadGeometry_GetLane(rg: &RoadGeometry, lane_id: &String) -> ConstLanePtr;
89        fn RoadGeometry_GetLanes(rg: &RoadGeometry) -> UniquePtr<CxxVector<ConstLanePtr>>;
90        fn RoadGeometry_GetSegment(rg: &RoadGeometry, segment_id: &String) -> *const Segment;
91        fn RoadGeometry_GetJunction(rg: &RoadGeometry, junction_id: &String) -> *const Junction;
92        fn RoadGeometry_GetBranchPoint(rg: &RoadGeometry, branch_point_id: &String) -> *const BranchPoint;
93        fn RoadGeometry_BackendCustomCommand(rg: &RoadGeometry, command: &String) -> String;
94        // LanePosition bindings definitions.
95        type LanePosition;
96        fn LanePosition_new(s: f64, r: f64, h: f64) -> UniquePtr<LanePosition>;
97        fn s(self: &LanePosition) -> f64;
98        fn r(self: &LanePosition) -> f64;
99        fn h(self: &LanePosition) -> f64;
100        fn set_s(self: Pin<&mut LanePosition>, s: f64);
101        fn set_r(self: Pin<&mut LanePosition>, r: f64);
102        fn set_h(self: Pin<&mut LanePosition>, h: f64);
103        fn srh(self: &LanePosition) -> &Vector3;
104        fn set_srh(self: Pin<&mut LanePosition>, srh: &Vector3);
105        fn LanePosition_to_str(lane_pos: &LanePosition) -> String;
106
107        // InertialPosition bindings definitions
108        type InertialPosition;
109        fn InertialPosition_new(x: f64, y: f64, z: f64) -> UniquePtr<InertialPosition>;
110        fn x(self: &InertialPosition) -> f64;
111        fn y(self: &InertialPosition) -> f64;
112        fn z(self: &InertialPosition) -> f64;
113        fn set_x(self: Pin<&mut InertialPosition>, x: f64);
114        fn set_y(self: Pin<&mut InertialPosition>, y: f64);
115        fn set_z(self: Pin<&mut InertialPosition>, z: f64);
116        fn xyz(self: &InertialPosition) -> &Vector3;
117        fn set_xyz(self: Pin<&mut InertialPosition>, xyz: &Vector3);
118        fn length(self: &InertialPosition) -> f64;
119        fn Distance(self: &InertialPosition, other: &InertialPosition) -> f64;
120        fn InertialPosition_to_str(inertial_pos: &InertialPosition) -> String;
121        fn InertialPosition_operator_eq(lhs: &InertialPosition, rhs: &InertialPosition) -> bool;
122        fn InertialPosition_operator_sum(lhs: &InertialPosition, rhs: &InertialPosition)
123            -> UniquePtr<InertialPosition>;
124        fn InertialPosition_operator_sub(lhs: &InertialPosition, rhs: &InertialPosition)
125            -> UniquePtr<InertialPosition>;
126        fn InertialPosition_operator_mul_scalar(lhs: &InertialPosition, scalar: f64) -> UniquePtr<InertialPosition>;
127
128        // Lane bindings definitions
129        type Lane;
130        fn to_left(self: &Lane) -> *const Lane;
131        fn to_right(self: &Lane) -> *const Lane;
132        fn index(self: &Lane) -> i32;
133        fn length(self: &Lane) -> f64;
134        fn Contains(self: &Lane, lane_position: &LanePosition) -> bool;
135        fn segment(self: &Lane) -> *const Segment;
136        fn Lane_id(lane: &Lane) -> String;
137        fn Lane_lane_bounds(lane: &Lane, s: f64) -> UniquePtr<RBounds>;
138        fn Lane_segment_bounds(lane: &Lane, s: f64) -> UniquePtr<RBounds>;
139        fn Lane_elevation_bounds(lane: &Lane, s: f64, r: f64) -> UniquePtr<HBounds>;
140        fn Lane_GetOrientation(lane: &Lane, lane_position: &LanePosition) -> UniquePtr<Rotation>;
141        fn Lane_ToInertialPosition(lane: &Lane, lane_position: &LanePosition) -> UniquePtr<InertialPosition>;
142        fn Lane_ToLanePosition(lane: &Lane, inertial_position: &InertialPosition) -> UniquePtr<LanePositionResult>;
143        fn Lane_ToSegmentPosition(lane: &Lane, inertial_position: &InertialPosition) -> UniquePtr<LanePositionResult>;
144        fn Lane_GetBranchPoint(lane: &Lane, start: bool) -> *const BranchPoint;
145        fn Lane_GetConfluentBranches(lane: &Lane, start: bool) -> *const LaneEndSet;
146        fn Lane_GetOngoingBranches(lane: &Lane, start: bool) -> *const LaneEndSet;
147        fn Lane_GetDefaultBranch(lane: &Lane, start: bool) -> UniquePtr<LaneEnd>;
148        fn Lane_EvalMotionDerivatives(
149            lane: &Lane,
150            lane_position: &LanePosition,
151            sigma_v: f64,
152            rho_v: f64,
153            eta_v: f64,
154        ) -> UniquePtr<LanePosition>;
155
156        // Segment bindings definitions
157        type Segment;
158        fn num_lanes(self: &Segment) -> i32;
159        fn junction(self: &Segment) -> *const Junction;
160        fn lane(self: &Segment, index: i32) -> *const Lane;
161        fn Segment_id(segment: &Segment) -> String;
162
163        // Junction bindings definitions
164        type Junction;
165        fn road_geometry(self: &Junction) -> *const RoadGeometry;
166        fn num_segments(self: &Junction) -> i32;
167        fn segment(self: &Junction, index: i32) -> *const Segment;
168        fn Junction_id(junction: &Junction) -> String;
169
170        // RoadPosition bindings definitions
171        type RoadPosition;
172        /// # Safety
173        ///
174        /// This function is unsafe because it dereferences `lane` pointers.
175        unsafe fn RoadPosition_new(lane: *const Lane, lane_pos: &LanePosition) -> UniquePtr<RoadPosition>;
176        fn RoadPosition_ToInertialPosition(road_position: &RoadPosition) -> UniquePtr<InertialPosition>;
177        fn RoadPosition_lane(road_position: &RoadPosition) -> *const Lane;
178        fn RoadPosition_pos(road_position: &RoadPosition) -> UniquePtr<LanePosition>;
179
180        // RoadPositionResult bindings definitions
181        type RoadPositionResult;
182        fn RoadPositionResult_road_position(result: &RoadPositionResult) -> UniquePtr<RoadPosition>;
183        fn RoadPositionResult_nearest_position(result: &RoadPositionResult) -> UniquePtr<InertialPosition>;
184        fn RoadPositionResult_distance(result: &RoadPositionResult) -> f64;
185
186        // LanePositionResult bindings definitions
187        type LanePositionResult;
188        fn LanePositionResult_road_position(result: &LanePositionResult) -> UniquePtr<LanePosition>;
189        fn LanePositionResult_nearest_position(result: &LanePositionResult) -> UniquePtr<InertialPosition>;
190        fn LanePositionResult_distance(result: &LanePositionResult) -> f64;
191
192        // Rotation bindings definitions
193        type Rotation;
194        fn Rotation_new() -> UniquePtr<Rotation>;
195        fn Rotation_from_quat(q: &Quaternion) -> UniquePtr<Rotation>;
196        fn Rotation_from_rpy(rpy: &RollPitchYaw) -> UniquePtr<Rotation>;
197        fn roll(self: &Rotation) -> f64;
198        fn pitch(self: &Rotation) -> f64;
199        fn yaw(self: &Rotation) -> f64;
200        fn quat(self: &Rotation) -> &Quaternion;
201        fn Distance(self: &Rotation, other: &Rotation) -> f64;
202        fn Rotation_set_quat(r: Pin<&mut Rotation>, q: &Quaternion);
203        fn Rotation_rpy(r: &Rotation) -> UniquePtr<RollPitchYaw>;
204        fn Rotation_matrix(r: &Rotation) -> UniquePtr<Matrix3>;
205        fn Rotation_Apply(r: &Rotation, ip: &InertialPosition) -> UniquePtr<InertialPosition>;
206        fn Rotation_Reverse(r: &Rotation) -> UniquePtr<Rotation>;
207
208        // RBounds bindings definitions
209        type RBounds;
210        fn min(self: &RBounds) -> f64;
211        fn max(self: &RBounds) -> f64;
212
213        // HBounds bindings definitions
214        type HBounds;
215        fn min(self: &HBounds) -> f64;
216        fn max(self: &HBounds) -> f64;
217
218        // SRange bindings definitions
219        type SRange;
220        fn SRange_new(s0: f64, s1: f64) -> UniquePtr<SRange>;
221        fn s0(self: &SRange) -> f64;
222        fn s1(self: &SRange) -> f64;
223        fn set_s0(self: Pin<&mut SRange>, s0: f64);
224        fn set_s1(self: Pin<&mut SRange>, s1: f64);
225        fn size(self: &SRange) -> f64;
226        fn WithS(self: &SRange) -> bool;
227        fn Intersects(self: &SRange, other: &SRange, tolerance: f64) -> bool;
228        fn Contains(self: &SRange, s_range: &SRange, tolerance: f64) -> bool;
229        fn SRange_GetIntersection(s_range: &SRange, other: &SRange, tolerance: f64) -> UniquePtr<SRange>;
230
231        // LaneSRange bindings definitions
232        type LaneSRange;
233        fn LaneSRange_new(lane_id: &String, s_range: &SRange) -> UniquePtr<LaneSRange>;
234        fn length(self: &LaneSRange) -> f64;
235        fn Intersects(self: &LaneSRange, other: &LaneSRange, tolerance: f64) -> bool;
236        fn Contains(self: &LaneSRange, lane_s_range: &LaneSRange, tolerance: f64) -> bool;
237        fn LaneSRange_lane_id(lane_s_range: &LaneSRange) -> String;
238        fn LaneSRange_s_range(lane_s_range: &LaneSRange) -> UniquePtr<SRange>;
239        fn LaneSRange_GetIntersection(
240            lane_s_range: &LaneSRange,
241            other: &LaneSRange,
242            tolerance: f64,
243        ) -> UniquePtr<LaneSRange>;
244
245        // LaneSRoute bindings definitions
246        type LaneSRoute;
247        fn LaneSRoute_new(ranges: &CxxVector<ConstLaneSRangeRef>) -> UniquePtr<LaneSRoute>;
248        fn length(self: &LaneSRoute) -> f64;
249        fn Intersects(self: &LaneSRoute, other: &LaneSRoute, tolerance: f64) -> bool;
250        fn ranges(self: &LaneSRoute) -> &CxxVector<LaneSRange>;
251
252        // LaneEnd bindings definitions
253        type LaneEnd;
254        // maliput::api Rust will have its own LaneEnd enum.
255        // However, this LaneEnd_new is expected to be used on methods that return a Cpp LaneEnd
256        // and a conversion to Rust LaneEnd is needed.
257        /// # Safety
258        /// This function is unsafe because it dereferences `lane` pointer.
259        unsafe fn LaneEnd_new(lane: *const Lane, start: bool) -> UniquePtr<LaneEnd>;
260        fn LaneEnd_lane(lane_end: &LaneEnd) -> *const Lane;
261        fn LaneEnd_is_start(lane_end: &LaneEnd) -> bool;
262
263        // LaneEndSet bindings definitions
264        type LaneEndSet;
265        fn size(self: &LaneEndSet) -> i32;
266        fn get(self: &LaneEndSet, index: i32) -> &LaneEnd;
267
268        // BranchPoint bindings definitions
269        type BranchPoint;
270        fn BranchPoint_id(branch_point: &BranchPoint) -> String;
271        fn road_geometry(self: &BranchPoint) -> *const RoadGeometry;
272        fn GetConfluentBranches(self: &BranchPoint, end: &LaneEnd) -> *const LaneEndSet;
273        fn GetOngoingBranches(self: &BranchPoint, end: &LaneEnd) -> *const LaneEndSet;
274        fn GetASide(self: &BranchPoint) -> *const LaneEndSet;
275        fn GetBSide(self: &BranchPoint) -> *const LaneEndSet;
276        fn BranchPoint_GetDefaultBranch(branch_point: &BranchPoint, end: &LaneEnd) -> UniquePtr<LaneEnd>;
277
278        // Intersection bindings definitions
279        type Intersection;
280        fn Intersection_id(intersection: &Intersection) -> String;
281
282        // IntersectionBook bindings definitions
283        type IntersectionBook;
284        fn IntersectionBook_GetIntersection(book: Pin<&mut IntersectionBook>, id: &String) -> MutIntersectionPtr;
285        fn IntersectionBook_GetIntersections(
286            book: Pin<&mut IntersectionBook>,
287        ) -> UniquePtr<CxxVector<MutIntersectionPtr>>;
288
289    }
290    impl UniquePtr<RoadNetwork> {}
291    impl UniquePtr<LanePosition> {}
292}