Visual Computing Library  devel
Loading...
Searching...
No Matches
info.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IO_MESH_GLTF_DETAIL_INFO_H
24#define VCL_IO_MESH_GLTF_DETAIL_INFO_H
25
26#include <vclib/space/core.h>
27
28#include <tiny_gltf.h>
29
30namespace vcl::detail {
31
44inline uint gltfNodeNumberMeshes(const tinygltf::Model& model, uint node)
45{
46 uint nMeshes = 0;
47 if (model.nodes[node].mesh >= 0) {
48 nMeshes = 1;
49 }
50 for (int c : model.nodes[node].children) {
51 if (c >= 0) {
52 nMeshes += gltfNodeNumberMeshes(model, c);
53 }
54 }
55 return nMeshes;
56}
57
71inline uint gltfNumberMeshes(const tinygltf::Model& model)
72{
73 uint nMeshes = 0;
74 for (uint s = 0; s < model.scenes.size(); ++s) {
75 const tinygltf::Scene& scene = model.scenes[s];
76 for (uint n = 0; n < scene.nodes.size(); ++n) {
77 nMeshes += gltfNodeNumberMeshes(model, scene.nodes[n]);
78 }
79 }
80 return nMeshes;
81}
82
91template<Matrix44Concept MatrixType>
92MatrixType gltfCurrentNodeMatrix(const tinygltf::Model& model, uint currentNode)
93{
94 using QuatType = Quaternion<typename MatrixType::Scalar>;
95
96 MatrixType currentMatrix = MatrixType::Identity();
97 // if the current node contains a 4x4 matrix
98 if (model.nodes[currentNode].matrix.size() == 16) {
99 for (uint i = 0; i < 4; ++i) {
100 for (uint j = 0; j < 4; ++j) {
101 // set the current matrix element
102 currentMatrix(i, j) =
103 model.nodes[currentNode].matrix[j * 4 + i];
104 }
105 }
106 }
107 // if the current node contains rotation quaternion, scale vector or
108 // translation vector
109 else {
110 // note: if one or more of these are missing, identity is used.
111 // note: final matrix is computed as M = T * R * S, as specified by
112 // gltf docs:
113 // https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_004_ScenesNodes.md
114 // 4x4 matrices associated to rotation, translation and scale
115 MatrixType rot = MatrixType::Identity();
116 MatrixType scale = MatrixType::Identity();
117 MatrixType trans = MatrixType::Identity();
118
119 // if node contains rotation quaternion
120 if (model.nodes[currentNode].rotation.size() == 4) {
121 QuatType qr(
122 model.nodes[currentNode].rotation[3],
123 model.nodes[currentNode].rotation[0],
124 model.nodes[currentNode].rotation[1],
125 model.nodes[currentNode].rotation[2]);
126 // Convert quaternion to 3x3 rotation matrix and insert into
127 // top-left of 4x4 matrix
128 rot.template block<3, 3>(0, 0) = qr.normalized().toRotationMatrix();
129 }
130 // if node contains scale
131 if (model.nodes[currentNode].scale.size() == 3) {
132 // set 4x4 matrix scale
133 scale(0, 0) = model.nodes[currentNode].scale[0];
134 scale(1, 1) = model.nodes[currentNode].scale[1];
135 scale(2, 2) = model.nodes[currentNode].scale[2];
136 }
137 // if node contains translation
138 if (model.nodes[currentNode].translation.size() == 3) {
139 // set 4x4 matrix translation
140 trans(0, 3) = model.nodes[currentNode].translation[0];
141 trans(1, 3) = model.nodes[currentNode].translation[1];
142 trans(2, 3) = model.nodes[currentNode].translation[2];
143 }
144
145 // M = T * R * S
146 currentMatrix = trans * rot * scale;
147 }
148 return currentMatrix;
149}
150
151} // namespace vcl::detail
152
153#endif // VCL_IO_MESH_GLTF_DETAIL_INFO_H