Skip to main content

maliput_sys/api/objects/
mod.rs

1// BSD 3-Clause License
2//
3// Copyright (c) 2026, 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
31#[cxx::bridge(namespace = "maliput::api::objects")]
32#[allow(clippy::needless_lifetimes)] // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/5787
33pub mod ffi {
34    /// Shared struct for `RoadObject` pointers.
35    /// This is needed because `*const` can't be used directly in the CxxVector collection.
36    struct ConstRoadObjectPtr {
37        pub road_object: *const RoadObject,
38    }
39    /// Shared struct for `Outline` pointers.
40    /// This is needed because `*const` can't be used directly in the CxxVector collection.
41    struct ConstOutlinePtr {
42        pub outline: *const Outline,
43    }
44    /// Shared struct for outline corner data.
45    /// This is a flat representation of `OutlineCorner` to avoid a new opaque CXX type.
46    /// `height` is 0.0 when `has_height` is false.
47    struct OutlineCornerData {
48        pub x: f64,
49        pub y: f64,
50        pub z: f64,
51        pub has_height: bool,
52        pub height: f64,
53    }
54    /// Shared struct for pairs in a properties map.
55    /// This is needed because maps can't be bound directly.
56    struct StringPair {
57        pub key: String,
58        pub value: String,
59    }
60
61    /// Shared enum representing different types of road objects.
62    /// This is needed to access the enum variant from Rust API since the C++ enum has an opaque implementation.
63    /// The order of these variants must match with the order of the enum class defined in maliput C++ API.
64    #[repr(i32)]
65    enum RoadObjectType {
66        kUnknown = 0,
67        kBarrier,
68        kBuilding,
69        kCrosswalk,
70        kGantry,
71        kObstacle,
72        kParkingSpace,
73        kPole,
74        kRoadMark,
75        kRoadSurface,
76        kStopLine,
77        kTrafficIsland,
78        kTree,
79        kVegetation,
80    }
81
82    unsafe extern "C++" {
83        include!("api/objects/objects.h");
84        include!("cxx_utils/error_handling.h");
85
86        // Forward declarations from parent namespaces.
87        #[namespace = "maliput::api"]
88        type InertialPosition = crate::api::ffi::InertialPosition;
89        #[namespace = "maliput::api"]
90        type Rotation = crate::api::ffi::Rotation;
91        #[namespace = "maliput::math"]
92        type Vector3 = crate::math::ffi::Vector3;
93        #[namespace = "maliput::math"]
94        type BoundingBox = crate::math::ffi::BoundingBox;
95        // StringWrapper is reused from the rules bridge to avoid duplication.
96        #[namespace = "maliput::api::rules"]
97        type StringWrapper = crate::api::rules::ffi::StringWrapper;
98
99        // RoadObjectType opaque type - this is needed to prevent CXX from redefining the enum (it'll use a `using` alias instead).
100        type RoadObjectType;
101
102        // RoadObjectBook opaque type and bindings.
103        type RoadObjectBook;
104        fn RoadObjectBook_RoadObjects(book: &RoadObjectBook) -> UniquePtr<CxxVector<ConstRoadObjectPtr>>;
105        fn RoadObjectBook_GetRoadObject(book: &RoadObjectBook, id: &String) -> *const RoadObject;
106        fn RoadObjectBook_FindByType(
107            book: &RoadObjectBook,
108            obj_type: RoadObjectType,
109        ) -> UniquePtr<CxxVector<ConstRoadObjectPtr>>;
110        fn RoadObjectBook_FindByLane(
111            book: &RoadObjectBook,
112            lane_id: &String,
113        ) -> UniquePtr<CxxVector<ConstRoadObjectPtr>>;
114        fn RoadObjectBook_FindInRadius(
115            book: &RoadObjectBook,
116            x: f64,
117            y: f64,
118            z: f64,
119            radius: f64,
120        ) -> UniquePtr<CxxVector<ConstRoadObjectPtr>>;
121
122        // RoadObject opaque type and bindings.
123        type RoadObject;
124        fn RoadObject_id(obj: &RoadObject) -> String;
125        fn RoadObject_name(obj: &RoadObject) -> UniquePtr<StringWrapper>;
126        // This method could be bound as `fn type(self: &RoadObject) -> RoadObjectType` but it
127        // causes a conflict with the `type` keyword in Rust.
128        fn RoadObject_object_type(obj: &RoadObject) -> RoadObjectType;
129        fn RoadObject_subtype(obj: &RoadObject) -> UniquePtr<StringWrapper>;
130        fn RoadObject_position_inertial(obj: &RoadObject) -> UniquePtr<InertialPosition>;
131        fn RoadObject_position_has_lane_position(obj: &RoadObject) -> bool;
132        fn RoadObject_position_lane_id(obj: &RoadObject) -> String;
133        fn RoadObject_position_lane_s(obj: &RoadObject) -> f64;
134        fn RoadObject_position_lane_r(obj: &RoadObject) -> f64;
135        fn RoadObject_position_lane_h(obj: &RoadObject) -> f64;
136        fn RoadObject_orientation(obj: &RoadObject) -> UniquePtr<Rotation>;
137        fn RoadObject_bounding_box(obj: &RoadObject) -> UniquePtr<BoundingBox>;
138        fn is_dynamic(self: &RoadObject) -> bool;
139        fn RoadObject_related_lanes(obj: &RoadObject) -> Vec<String>;
140        fn num_outlines(self: &RoadObject) -> i32;
141        fn RoadObject_outlines(obj: &RoadObject) -> UniquePtr<CxxVector<ConstOutlinePtr>>;
142        fn RoadObject_properties(obj: &RoadObject) -> Vec<StringPair>;
143
144        // Outline opaque type and bindings.
145        type Outline;
146        fn Outline_id(outline: &Outline) -> String;
147        fn Outline_is_closed(outline: &Outline) -> bool;
148        fn Outline_num_corners(outline: &Outline) -> i32;
149        fn Outline_corners(outline: &Outline) -> Vec<OutlineCornerData>;
150    }
151}