1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::*;

/// Describes components of a [`RigidBody`].
#[derive(Clone, Debug)]
pub struct Shape {
    /// If true, the mass is distributed along the surface of the shape.
    pub hollow: Option<bool>,
    /// The mass of the shape.
    /// If not provided, it is derived from density x shape volume.
    pub mass: Option<f32>,
    /// The density of the shape.
    /// If not provided, it is derived from mass/shape volume.
    pub density: Option<f32>,
    /// The [`PhysicsMaterial`] used for this shape.
    pub physics_material: Option<Box<DefInstance<PhysicsMaterial>>>,
    /// The geometry of the shape ([`Plane`], [`Sphere`], [`Mesh`], etc.).
    pub geom: ShapeGeom,
    /// Transformation for the shape. Any combination of these elements in any order.
    /// See [`Node`] for additional information.
    pub transform: Vec<RigidTransform>,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for Shape {
    const NAME: &'static str = "shape";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(Shape {
            hollow: parse_opt("hollow", &mut it, parse_elem)?,
            mass: parse_opt("mass", &mut it, parse_elem)?,
            density: parse_opt("density", &mut it, parse_elem)?,
            physics_material: parse_opt_many(&mut it, DefInstance::parse)?.map(Box::new),
            transform: parse_list_many(&mut it, RigidTransform::parse)?,
            geom: parse_one_many(&mut it, ShapeGeom::parse)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// The geometry of a shape. This can be either an inline definition
/// using [`Plane`], [`Sphere`] etc, or a [`Instance<Geometry>`],
/// which can reference other geometry types.
#[derive(Clone, Debug)]
pub enum ShapeGeom {
    /// Defines an infinite plane primitive.
    Plane(Plane),
    /// Declares an axis-aligned, centered box primitive.
    Box(BoxShape),
    /// Describes a centered sphere primitive.
    Sphere(Sphere),
    /// Declares a cylinder primitive that is centered on, and aligned with, the local y axis.
    Cylinder(Cylinder),
    /// Describes a tapered cylinder primitive that is centered on and aligned with the local y axis.
    TaperedCylinder(TaperedCylinder),
    /// Declares a capsule primitive that is centered on and aligned with the local y axis.
    Capsule(Capsule),
    /// Describes a tapered capsule primitive that is centered on, and aligned with, the local y axis.
    TaperedCapsule(TaperedCapsule),
    /// A geometry instance using the [`Instance<Geometry>`] element, which
    /// references any [`GeometryElement`] ([`Mesh`] or [`Spline`]).
    Geom(Instance<Geometry>),
}

impl ShapeGeom {
    /// Parse a [`ShapeGeom`] from an XML element.
    pub fn parse(e: &Element) -> Result<Option<Self>> {
        match e.name() {
            Plane::NAME => Ok(Some(Self::Plane(Plane::parse(e)?))),
            BoxShape::NAME => Ok(Some(Self::Box(BoxShape::parse(e)?))),
            Sphere::NAME => Ok(Some(Self::Sphere(Sphere::parse(e)?))),
            Cylinder::NAME => Ok(Some(Self::Cylinder(Cylinder::parse(e)?))),
            TaperedCylinder::NAME => Ok(Some(Self::TaperedCylinder(TaperedCylinder::parse(e)?))),
            Capsule::NAME => Ok(Some(Self::Capsule(Capsule::parse(e)?))),
            TaperedCapsule::NAME => Ok(Some(Self::TaperedCapsule(TaperedCapsule::parse(e)?))),
            Geometry::INSTANCE => Ok(Some(Self::Geom(Instance::parse(e)?))),
            _ => Ok(None),
        }
    }
}

/// Defines an infinite plane primitive.
#[derive(Clone, Debug)]
pub struct Plane {
    /// The coefficients for the plane’s equation: `Ax + By + Cz + D = 0`.
    pub equation: [f32; 4],
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for Plane {
    const NAME: &'static str = "plane";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(Plane {
            equation: *parse_one("equation", &mut it, parse_array_n)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// Declares an axis-aligned, centered box primitive.
/// (Note: The type is not called `Box` to avoid the name clash with the Rust builtin type.)
#[derive(Clone, Debug)]
pub struct BoxShape {
    /// The extents of the box.
    pub half_extents: [f32; 3],
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for BoxShape {
    const NAME: &'static str = "box";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(BoxShape {
            half_extents: *parse_one("half_extents", &mut it, parse_array_n)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// Describes a centered sphere primitive.
#[derive(Clone, Debug)]
pub struct Sphere {
    /// The radius of the sphere.
    pub radius: f32,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for Sphere {
    const NAME: &'static str = "sphere";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(Sphere {
            radius: parse_one("radius", &mut it, parse_elem)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// Declares a cylinder primitive that is centered on, and aligned with, the local y axis.
#[derive(Clone, Debug)]
pub struct Cylinder {
    /// The length of the cylinder along the y axis.
    pub height: f32,
    /// The radii of the cylinder (it may be elliptical).
    pub radius: [f32; 2],
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for Cylinder {
    const NAME: &'static str = "cylinder";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(Cylinder {
            height: parse_one("height", &mut it, parse_elem)?,
            radius: *parse_one("radius", &mut it, parse_array_n)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// Describes a tapered cylinder primitive that is centered on and aligned with the local y axis.
#[derive(Clone, Debug)]
pub struct TaperedCylinder {
    /// The length of the cylinder along the y axis.
    pub height: f32,
    /// The radii of the tapered cylinder at the positive (height/2) Y value.
    /// Both ends of the tapered cylinder may be elliptical.
    pub radius1: [f32; 2],
    /// The radii of the tapered cylinder at the negative (height/2) Y value.
    /// Both ends of the tapered cylinder may be elliptical.
    pub radius2: [f32; 2],
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for TaperedCylinder {
    const NAME: &'static str = "tapered_cylinder";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(TaperedCylinder {
            height: parse_one("height", &mut it, parse_elem)?,
            radius1: *parse_one("radius1", &mut it, parse_array_n)?,
            radius2: *parse_one("radius2", &mut it, parse_array_n)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// Declares a capsule primitive that is centered on and aligned with the local y axis.
#[derive(Clone, Debug)]
pub struct Capsule {
    /// The length of the line segment connecting the centers of the capping hemispheres.
    pub height: f32,
    /// The radii of the capsule (it may be elliptical).
    pub radius: [f32; 2],
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for Capsule {
    const NAME: &'static str = "capsule";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(Capsule {
            height: parse_one("height", &mut it, parse_elem)?,
            radius: *parse_one("radius", &mut it, parse_array_n)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

/// Describes a tapered capsule primitive that is centered on, and aligned with, the local y axis.
#[derive(Clone, Debug)]
pub struct TaperedCapsule {
    /// The length of the line segment connecting the centers of the capping hemispheres.
    pub height: f32,
    /// The radii of the tapered capsule at the positive (height/2) Y value.
    /// Both ends of the tapered capsule may be elliptical.
    pub radius1: [f32; 2],
    /// The radii of the tapered capsule at the negative (height/2) Y value.
    /// Both ends of the tapered capsule may be elliptical.
    pub radius2: [f32; 2],
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for TaperedCapsule {
    const NAME: &'static str = "tapered_capsule";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(TaperedCapsule {
            height: parse_one("height", &mut it, parse_elem)?,
            radius1: *parse_one("radius1", &mut it, parse_array_n)?,
            radius2: *parse_one("radius2", &mut it, parse_array_n)?,
            extra: Extra::parse_many(it)?,
        })
    }
}