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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use crate::*;

#[cfg(feature = "nalgebra")]
use nalgebra::Matrix4;

/// Embodies the entire set of information that can be visualized
/// from the contents of a COLLADA resource.
#[derive(Clone, Default, Debug)]
pub struct Scene {
    /// The instantiated [`PhysicsScene`] elements describe
    /// any physics being applied to the scene.
    pub instance_physics_scene: Vec<Instance<PhysicsScene>>,
    /// The scene graph is built from the [`VisualScene`] elements instantiated under [`Scene`].
    pub instance_visual_scene: Option<Instance<VisualScene>>,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl Scene {
    /// Construct a new `Scene` from a [`VisualScene`] instance.
    pub fn new(instance_visual_scene: Instance<VisualScene>) -> Self {
        Self {
            instance_physics_scene: vec![],
            instance_visual_scene: Some(instance_visual_scene),
            extra: vec![],
        }
    }
}

impl XNode for Scene {
    const NAME: &'static str = "scene";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(Scene {
            instance_physics_scene: Instance::parse_list(&mut it)?,
            instance_visual_scene: Instance::parse_opt(&mut it)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

impl XNodeWrite for Scene {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let e = Self::elem().start(w)?;
        self.instance_physics_scene.write_to(w)?;
        self.instance_visual_scene.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

/// Embodies the entire set of information that can be visualized
/// from the contents of a COLLADA resource.
#[derive(Clone, Debug)]
pub struct VisualScene {
    /// A text string containing the unique identifier of the element.
    pub id: Option<String>,
    /// The text string name of this element.
    pub name: Option<String>,
    /// Asset management information about this element.
    pub asset: Option<Box<Asset>>,
    /// The scene graph subtrees.
    pub nodes: Vec<Node>,
    /// The [`EvaluateScene`] element declares information
    /// specifying how to evaluate this [`VisualScene`].
    pub evaluate_scene: Vec<EvaluateScene>,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl VisualScene {
    /// Create a new empty `VisualScene`.
    pub fn new(id: impl Into<String>, name: Option<String>) -> Self {
        Self {
            id: Some(id.into()),
            name,
            asset: None,
            nodes: vec![],
            evaluate_scene: vec![],
            extra: vec![],
        }
    }
}

impl XNode for VisualScene {
    const NAME: &'static str = "visual_scene";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        Ok(VisualScene {
            id: element.attr("id").map(Into::into),
            name: element.attr("name").map(Into::into),
            asset: Asset::parse_opt_box(&mut it)?,
            nodes: Node::parse_list(&mut it)?,
            evaluate_scene: EvaluateScene::parse_list(&mut it)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

impl XNodeWrite for VisualScene {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.opt_attr("id", &self.id);
        e.opt_attr("name", &self.name);
        let e = e.start(w)?;
        self.asset.write_to(w)?;
        self.nodes.write_to(w)?;
        self.evaluate_scene.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

/// Embodies the hierarchical relationship of elements in a scene.
///
/// The [`Node`] element declares a point of interest in a scene.
/// A node denotes one point on a branch of the scene graph.
/// The [`Node`] element is essentially the root of a subgraph of the entire scene graph.
#[derive(Clone, Debug)]
pub struct Node {
    /// A text string containing the unique identifier of the element.
    pub id: Option<String>,
    /// The text string name of this element.
    pub name: Option<String>,
    /// A text string value containing the subidentifier of this element.
    /// This value must be unique within the scope of the parent element.
    pub sid: Option<String>,
    /// The type of the [`Node`] element.
    pub ty: NodeType,
    /// The layers to which this node belongs.
    pub layer: Vec<String>,
    /// Asset management information about this element.
    pub asset: Option<Box<Asset>>,
    /// Any combination of geometric transforms.
    pub transforms: Vec<Transform>,
    /// Allows the node to instantiate a camera object.
    pub instance_camera: Vec<Instance<Camera>>,
    /// Allows the node to instantiate a controller object.
    pub instance_controller: Vec<Instance<Controller>>,
    /// Allows the node to instantiate a geometry object.
    pub instance_geometry: Vec<Instance<Geometry>>,
    /// Allows the node to instantiate a light object.
    pub instance_light: Vec<Instance<Light>>,
    /// Allows the node to instantiate a hierarchy of other nodes.
    pub instance_node: Vec<Instance<Node>>,
    /// Allows the node to recursively define hierarchy.
    pub children: Vec<Node>,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for Node {
    const NAME: &'static str = "node";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        let extra;
        Ok(Node {
            id: element.attr("id").map(Into::into),
            name: element.attr("name").map(Into::into),
            sid: element.attr("sid").map(Into::into),
            ty: parse_attr(element.attr("type"))?.unwrap_or_default(),
            layer: element.attr("layer").map_or_else(Vec::new, |s| {
                s.split_ascii_whitespace().map(|s| s.to_owned()).collect()
            }),
            asset: Asset::parse_opt_box(&mut it)?,
            transforms: parse_list_many(&mut it, Transform::parse)?,
            instance_camera: Instance::parse_list(&mut it)?,
            instance_controller: Instance::parse_list(&mut it)?,
            instance_geometry: Instance::parse_list(&mut it)?,
            instance_light: Instance::parse_list(&mut it)?,
            instance_node: Instance::parse_list(&mut it)?,
            children: {
                // Note: this is nonconforming, COLLADA spec says `extra` should come
                // after `children`.
                // However FBX Collada exporter has been witnessed producing such files
                extra = Extra::parse_list(&mut it)?;
                Node::parse_list(&mut it)?
            },
            extra: Extra::parse_append_many(extra, it)?,
        })
    }
}

impl XNodeWrite for Node {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.opt_attr("id", &self.id);
        e.opt_attr("name", &self.name);
        e.opt_attr("sid", &self.sid);
        e.def_print_attr("type", self.ty, Default::default());
        if let Some(s) = arr_to_string(&self.layer) {
            e.attr("layer", &s)
        }
        if self.is_empty() {
            e.end(w)
        } else {
            let e = e.start(w)?;
            self.asset.write_to(w)?;
            self.transforms.write_to(w)?;
            self.instance_camera.write_to(w)?;
            self.instance_controller.write_to(w)?;
            self.instance_geometry.write_to(w)?;
            self.instance_light.write_to(w)?;
            self.instance_node.write_to(w)?;
            self.children.write_to(w)?;
            self.extra.write_to(w)?;
            e.end(w)
        }
    }
}

impl CollectLocalMaps for Node {
    fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
        maps.insert(self);
        self.children.collect_local_maps(maps);
    }
}

impl Node {
    /// Construct a new default node with the given `id` and `name`.
    pub fn new(id: impl Into<String>, name: Option<String>) -> Self {
        Self {
            id: Some(id.into()),
            name,
            sid: Default::default(),
            ty: Default::default(),
            layer: Default::default(),
            asset: Default::default(),
            transforms: Default::default(),
            instance_camera: Default::default(),
            instance_controller: Default::default(),
            instance_geometry: Default::default(),
            instance_light: Default::default(),
            instance_node: Default::default(),
            children: Default::default(),
            extra: Default::default(),
        }
    }

    /// Add a transform to this node's transformation stack.
    pub fn push_transform(&mut self, transform: impl Into<Transform>) {
        self.transforms.push(transform.into())
    }

    fn on_children<'a, E>(
        &'a self,
        f: &mut impl FnMut(&'a Self) -> Result<(), E>,
    ) -> Result<(), E> {
        f(self)?;
        for child in &self.children {
            child.on_children(f)?
        }
        Ok(())
    }

    /// Returns true if this node has no sub-elements.
    pub fn is_empty(&self) -> bool {
        self.asset.is_none()
            && self.transforms.is_empty()
            && self.instance_camera.is_empty()
            && self.instance_controller.is_empty()
            && self.instance_geometry.is_empty()
            && self.instance_light.is_empty()
            && self.instance_node.is_empty()
            && self.children.is_empty()
            && self.extra.is_empty()
    }
}

impl Traversable for Node {
    fn traverse<'a, E>(
        doc: &'a Document,
        mut f: impl FnMut(&'a Node) -> Result<(), E>,
    ) -> Result<(), E> {
        doc.library.iter().try_for_each(|elem| match elem {
            LibraryElement::Nodes(lib) => lib.items.iter().try_for_each(|e| e.on_children(&mut f)),
            LibraryElement::VisualScenes(lib) => lib
                .items
                .iter()
                .try_for_each(|e| e.nodes.iter().try_for_each(|e| e.on_children(&mut f))),
            _ => Ok(()),
        })
    }
}

#[cfg(feature = "nalgebra")]
impl Node {
    /// Apply the transformation stack on this node to a [`Matrix4`].
    /// If `mat` beforehand is the transformation applying at the parent of the
    /// current node, then `mat` after this call is the transformation that
    /// applies to everything in this node and in the children.
    pub fn prepend_transforms(&self, mat: &mut Matrix4<f32>) {
        for t in &self.transforms {
            t.prepend_to_matrix(mat)
        }
    }

    /// Apply the transformation stack on this node to a [`Matrix4`].
    /// If `mat` before this call is a transformation applying to a child of this node,
    /// then `mat` afterward is the transformation situating the child in the parent frame.
    pub fn append_transforms(&self, mat: &mut Matrix4<f32>) {
        for t in self.transforms.iter().rev() {
            t.append_to_matrix(mat)
        }
    }

    /// Convert this node's transformation stack to a [`Matrix4`].
    pub fn transform_as_matrix(&self) -> Matrix4<f32> {
        let mut mat = Matrix4::identity();
        self.prepend_transforms(&mut mat);
        mat
    }
}

/// The type of a [`Node`] element.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NodeType {
    /// A regular node.
    Node,
    /// A joint. See [`Joints`].
    Joint,
}

impl Default for NodeType {
    fn default() -> Self {
        Self::Node
    }
}

impl FromStr for NodeType {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "NODE" => Ok(Self::Node),
            "JOINT" => Ok(Self::Joint),
            _ => Err(()),
        }
    }
}

impl NodeType {
    /// The XML name of a value in this enumeration.
    pub fn to_str(self) -> &'static str {
        match self {
            Self::Node => "NODE",
            Self::Joint => "JOINT",
        }
    }
}

impl Display for NodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self.to_str(), f)
    }
}

/// Declares information specifying how to evaluate a [`VisualScene`].
#[derive(Clone, Debug)]
pub struct EvaluateScene {
    /// The text string name of this element.
    pub name: Option<String>,
    /// Describes the effect passes to evaluate a scene.
    pub render: Vec<Render>,
}

impl EvaluateScene {
    /// Construct a new `EvaluateScene` with the given effect passes.
    pub fn new(render: Vec<Render>) -> Self {
        assert!(!render.is_empty());
        Self { name: None, render }
    }
}

impl XNode for EvaluateScene {
    const NAME: &'static str = "evaluate_scene";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        let res = EvaluateScene {
            name: element.attr("name").map(Into::into),
            render: Render::parse_list_n::<1>(&mut it)?,
        };
        finish(res, it)
    }
}

impl XNodeWrite for EvaluateScene {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.opt_attr("name", &self.name);
        let e = e.start(w)?;
        self.render.write_to(w)?;
        e.end(w)
    }
}