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
use gltf::Semantic;
use petgraph::{graph::NodeIndex, visit::EdgeRef};
use thiserror::Error;

use crate::graph::{
    gltf::{accessor::iter::AccessorIterCreateError, Accessor, GltfEdge},
    Edge, Graph, GraphNodeEdges, Property,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MorphTargetEdge {
    Attribute(Semantic),
}

impl<'a> TryFrom<&'a Edge> for &'a MorphTargetEdge {
    type Error = ();
    fn try_from(value: &'a Edge) -> Result<Self, Self::Error> {
        match value {
            Edge::Gltf(GltfEdge::MorphTarget(edge)) => Ok(edge),
            _ => Err(()),
        }
    }
}

impl From<MorphTargetEdge> for Edge {
    fn from(edge: MorphTargetEdge) -> Self {
        Self::Gltf(GltfEdge::MorphTarget(edge))
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MorphTarget(pub NodeIndex);

impl From<NodeIndex> for MorphTarget {
    fn from(index: NodeIndex) -> Self {
        Self(index)
    }
}

impl From<MorphTarget> for NodeIndex {
    fn from(primitive: MorphTarget) -> Self {
        primitive.0
    }
}

impl GraphNodeEdges<MorphTargetEdge> for MorphTarget {}
impl Property for MorphTarget {}

impl MorphTarget {
    pub fn attributes(&self, graph: &Graph) -> Vec<(Semantic, Accessor)> {
        graph
            .edges_directed(self.0, petgraph::Direction::Outgoing)
            .filter_map(|edge| {
                if let Edge::Gltf(GltfEdge::MorphTarget(MorphTargetEdge::Attribute(semantic))) =
                    edge.weight()
                {
                    Some((semantic.clone(), Accessor(edge.target())))
                } else {
                    None
                }
            })
            .collect()
    }
    pub fn attribute(&self, graph: &Graph, semantic: &Semantic) -> Option<Accessor> {
        self.find_edge_target(graph, &MorphTargetEdge::Attribute(semantic.clone()))
    }
    pub fn set_attribute(
        &self,
        graph: &mut Graph,
        semantic: &Semantic,
        accessor: Option<Accessor>,
    ) {
        self.set_edge_target(
            graph,
            MorphTargetEdge::Attribute(semantic.clone()),
            accessor,
        );
    }
}

#[derive(Debug, Error)]
pub enum MorphTargetIterError {
    #[error(transparent)]
    AccessorIterCreateError(#[from] AccessorIterCreateError),
}