1 module sbylib.wrapper.assimp.node;
2 
3 import bindbc.assimp.types;
4 import sbylib.wrapper.assimp.mesh : Mesh;
5 import sbylib.wrapper.assimp.metadata : MetaData;
6 import sbylib.wrapper.assimp.functions : toRegularString, toSbylibMatrix, toArray;
7 import sbylib.wrapper.assimp.scene : Scene;
8 import sbylib.math : mat4;
9 import std.typecons : nullable, Nullable;
10 
11 struct Node {
12 
13     private const(aiNode)* node;
14 
15     string name() const {
16         return node.mName.toRegularString();
17     }
18 
19     mat4 transformation() const {
20         return node.mTransformation.toSbylibMatrix();
21     }
22 
23     Node parent() {
24         return Node(node.mParent);
25     }
26 
27     auto children() {
28         return toArray!((const(aiNode)* n) => Node(n))
29             (node.mNumChildren, node.mChildren);
30     }
31 
32     auto meshes() {
33         return toArray!((uint i) => i)
34             (node.mNumMeshes, node.mMeshes);
35     }
36 
37     Nullable!MetaData metaData() {
38         if (node.mMetaData is null) return typeof(return).init;
39         return nullable(MetaData(node.mMetaData));
40     }
41 
42     string toString(Scene scene) {
43         import std.algorithm : map;
44         import std.array : array;
45         import std.format : format;
46         import sbylib.wrapper.assimp.functions : tree;
47 
48         string[] strs;
49         strs ~= children.map!(c => c.toString(scene)).array;
50         strs ~= meshes.map!(i => scene.meshes[i].toString(scene)).array;
51         strs ~= "matrix: " ~ this.transformation.toString();
52         if (metaData.isNull is false) strs ~= metaData.get().toString();
53 
54         return tree(format!"Node[%s]"(name), strs);
55     }
56 }