robot_description_builder/link/geometry/
geometry_shape_data.rs

1use super::{
2	mesh_geometry::MeshGeometry, BoxGeometry, CylinderGeometry, GeometryInterface, SphereGeometry,
3};
4use crate::transform::Transform;
5
6#[cfg(feature = "urdf")]
7use crate::to_rdf::to_urdf::ToURDF;
8
9#[derive(Debug, PartialEq, Clone)]
10pub struct GeometryShapeData {
11	/// The transform from the frame/origin of the parent `Link` to the frame/origin of this `Geometry`.
12	///
13	/// This is the reference for the placement of the `geometry`.
14	///
15	// TODO: Maybe remove the last line
16	/// In URDF this field is refered to as `<origin>`.
17	pub transform: Transform,
18	pub geometry: GeometryShapeContainer,
19}
20
21impl GeometryShapeData {
22	/// X Y Z Bounding box sizes from center of the origin of the shape.
23	pub fn bounding_box(&self) -> (f32, f32, f32) {
24		match &self.geometry {
25			GeometryShapeContainer::Box(g) => g.bounding_box(),
26			GeometryShapeContainer::Cylinder(g) => g.bounding_box(),
27			GeometryShapeContainer::Sphere(g) => g.bounding_box(),
28			GeometryShapeContainer::Mesh(g) => g.bounding_box(),
29		}
30	}
31}
32
33#[derive(Debug, PartialEq, Clone)]
34#[non_exhaustive]
35pub enum GeometryShapeContainer {
36	Box(BoxGeometry),
37	Cylinder(CylinderGeometry),
38	Sphere(SphereGeometry),
39	// Capsule(String),
40	Mesh(MeshGeometry),
41}
42
43#[cfg(feature = "urdf")]
44impl ToURDF for GeometryShapeContainer {
45	fn to_urdf(
46		&self,
47		writer: &mut quick_xml::Writer<std::io::Cursor<Vec<u8>>>,
48		urdf_config: &crate::to_rdf::to_urdf::URDFConfig,
49	) -> Result<(), quick_xml::Error> {
50		match self {
51			GeometryShapeContainer::Box(box_geometry) => box_geometry.to_urdf(writer, urdf_config),
52			GeometryShapeContainer::Cylinder(cylinder_geometry) => {
53				cylinder_geometry.to_urdf(writer, urdf_config)
54			}
55			GeometryShapeContainer::Sphere(sphere_geometry) => {
56				sphere_geometry.to_urdf(writer, urdf_config)
57			}
58			GeometryShapeContainer::Mesh(mesh_geometry) => {
59				mesh_geometry.to_urdf(writer, urdf_config)
60			}
61		}
62	}
63}
64
65impl From<BoxGeometry> for GeometryShapeContainer {
66	fn from(value: BoxGeometry) -> Self {
67		Self::Box(value)
68	}
69}
70
71impl From<CylinderGeometry> for GeometryShapeContainer {
72	fn from(value: CylinderGeometry) -> Self {
73		Self::Cylinder(value)
74	}
75}
76
77impl From<SphereGeometry> for GeometryShapeContainer {
78	fn from(value: SphereGeometry) -> Self {
79		Self::Sphere(value)
80	}
81}
82
83impl From<MeshGeometry> for GeometryShapeContainer {
84	fn from(value: MeshGeometry) -> Self {
85		Self::Mesh(value)
86	}
87}