Coverage Report

Created: 2026-09-14 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/Common/SkeletonMeshBuilder.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
copyright notice, this list of conditions and the
15
following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
copyright notice, this list of conditions and the
19
following disclaimer in the documentation and/or other
20
materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
contributors may be used to endorse or promote products
24
derived from this software without specific prior
25
written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
42
/** @file  SkeletonMeshBuilder.cpp
43
 *  @brief Implementation of a little class to construct a dummy mesh for a skeleton
44
 */
45
46
#include <assimp/SkeletonMeshBuilder.h>
47
#include <assimp/scene.h>
48
49
using namespace Assimp;
50
51
// ------------------------------------------------------------------------------------------------
52
// The constructor processes the given scene and adds a mesh there.
53
13.0k
SkeletonMeshBuilder::SkeletonMeshBuilder(aiScene *pScene, aiNode *root, bool bKnobsOnly) {
54
    // nothing to do if there's mesh data already present at the scene
55
13.0k
    if (pScene->mNumMeshes > 0 || pScene->mRootNode == nullptr) {
56
0
        return;
57
0
    }
58
59
13.0k
    if (!root) {
60
12.6k
        root = pScene->mRootNode;
61
12.6k
    }
62
63
13.0k
    mKnobsOnly = bKnobsOnly;
64
65
    // build some faces around each node
66
13.0k
    CreateGeometry(root);
67
68
    // create a mesh to hold all the generated faces
69
13.0k
    pScene->mNumMeshes = 1;
70
13.0k
    pScene->mMeshes = new aiMesh *[1];
71
13.0k
    pScene->mMeshes[0] = CreateMesh();
72
    // and install it at the root node
73
13.0k
    root->mNumMeshes = 1;
74
13.0k
    root->mMeshes = new unsigned int[1];
75
13.0k
    root->mMeshes[0] = 0;
76
77
    // create a dummy material for the mesh
78
13.0k
    if (pScene->mNumMaterials == 0) {
79
9.10k
        pScene->mNumMaterials = 1;
80
9.10k
        pScene->mMaterials = new aiMaterial *[1];
81
9.10k
        pScene->mMaterials[0] = CreateMaterial();
82
9.10k
    }
83
13.0k
}
84
85
// ------------------------------------------------------------------------------------------------
86
// Recursively builds a simple mesh representation for the given node
87
1.44M
void SkeletonMeshBuilder::CreateGeometry(const aiNode *pNode) {
88
    // add a joint entry for the node.
89
1.44M
    const unsigned int vertexStartIndex = static_cast<unsigned int>(mVertices.size());
90
91
    // now build the geometry.
92
1.44M
    if (pNode->mNumChildren > 0 && !mKnobsOnly) {
93
        // If the node has children, we build little pointers to each of them
94
1.47M
        for (unsigned int a = 0; a < pNode->mNumChildren; a++) {
95
            // find a suitable coordinate system
96
1.41M
            const aiMatrix4x4 &childTransform = pNode->mChildren[a]->mTransformation;
97
1.41M
            aiVector3D childpos(childTransform.a4, childTransform.b4, childTransform.c4);
98
1.41M
            ai_real distanceToChild = childpos.Length();
99
1.41M
            if (distanceToChild < ai_epsilon) {
100
1.38M
                continue;
101
1.38M
            }
102
31.9k
            aiVector3D up = aiVector3D(childpos).Normalize();
103
31.9k
            aiVector3D orth(1.0, 0.0, 0.0);
104
31.9k
            if (std::fabs(orth * up) > 0.99) {
105
9.96k
                orth.Set(0.0, 1.0, 0.0);
106
9.96k
            }
107
108
31.9k
            aiVector3D front = (up ^ orth).Normalize();
109
31.9k
            aiVector3D side = (front ^ up).Normalize();
110
111
31.9k
            unsigned int localVertexStart = static_cast<unsigned int>(mVertices.size());
112
31.9k
            mVertices.push_back(-front * distanceToChild * (ai_real)0.1);
113
31.9k
            mVertices.push_back(childpos);
114
31.9k
            mVertices.push_back(-side * distanceToChild * (ai_real)0.1);
115
31.9k
            mVertices.push_back(-side * distanceToChild * (ai_real)0.1);
116
31.9k
            mVertices.push_back(childpos);
117
31.9k
            mVertices.push_back(front * distanceToChild * (ai_real)0.1);
118
31.9k
            mVertices.push_back(front * distanceToChild * (ai_real)0.1);
119
31.9k
            mVertices.push_back(childpos);
120
31.9k
            mVertices.push_back(side * distanceToChild * (ai_real)0.1);
121
31.9k
            mVertices.push_back(side * distanceToChild * (ai_real)0.1);
122
31.9k
            mVertices.push_back(childpos);
123
31.9k
            mVertices.push_back(-front * distanceToChild * (ai_real)0.1);
124
125
31.9k
            mFaces.emplace_back(localVertexStart + 0, localVertexStart + 1, localVertexStart + 2);
126
31.9k
            mFaces.emplace_back(localVertexStart + 3, localVertexStart + 4, localVertexStart + 5);
127
31.9k
            mFaces.emplace_back(localVertexStart + 6, localVertexStart + 7, localVertexStart + 8);
128
31.9k
            mFaces.emplace_back(localVertexStart + 9, localVertexStart + 10, localVertexStart + 11);
129
31.9k
        }
130
1.38M
    } else {
131
        // if the node has no children, it's an end node. Put a little knob there instead
132
1.38M
        aiVector3D ownpos(pNode->mTransformation.a4, pNode->mTransformation.b4, pNode->mTransformation.c4);
133
1.38M
        ai_real sizeEstimate = ownpos.Length() * ai_real(0.18);
134
1.38M
        const ai_real zero(0.0);
135
136
1.38M
        mVertices.emplace_back(-sizeEstimate, zero, zero);
137
1.38M
        mVertices.emplace_back(zero, sizeEstimate, zero);
138
1.38M
        mVertices.emplace_back(zero, zero, -sizeEstimate);
139
1.38M
        mVertices.emplace_back(zero, sizeEstimate, zero);
140
1.38M
        mVertices.emplace_back(sizeEstimate, zero, zero);
141
1.38M
        mVertices.emplace_back(zero, zero, -sizeEstimate);
142
1.38M
        mVertices.emplace_back(sizeEstimate, zero, zero);
143
1.38M
        mVertices.emplace_back(zero, -sizeEstimate, zero);
144
1.38M
        mVertices.emplace_back(zero, zero, -sizeEstimate);
145
1.38M
        mVertices.emplace_back(zero, -sizeEstimate, zero);
146
1.38M
        mVertices.emplace_back(-sizeEstimate, zero, zero);
147
1.38M
        mVertices.emplace_back(zero, zero, -sizeEstimate);
148
149
1.38M
        mVertices.emplace_back(-sizeEstimate, zero, zero);
150
1.38M
        mVertices.emplace_back(zero, zero, sizeEstimate);
151
1.38M
        mVertices.emplace_back(zero, sizeEstimate, zero);
152
1.38M
        mVertices.emplace_back(zero, sizeEstimate, zero);
153
1.38M
        mVertices.emplace_back(zero, zero, sizeEstimate);
154
1.38M
        mVertices.emplace_back(sizeEstimate, zero, zero);
155
1.38M
        mVertices.emplace_back(sizeEstimate, zero, zero);
156
1.38M
        mVertices.emplace_back(zero, zero, sizeEstimate);
157
1.38M
        mVertices.emplace_back(zero, -sizeEstimate, zero);
158
1.38M
        mVertices.emplace_back(zero, -sizeEstimate, zero);
159
1.38M
        mVertices.emplace_back(zero, zero, sizeEstimate);
160
1.38M
        mVertices.emplace_back(-sizeEstimate, zero, zero);
161
162
1.38M
        mFaces.emplace_back(vertexStartIndex + 0, vertexStartIndex + 1, vertexStartIndex + 2);
163
1.38M
        mFaces.emplace_back(vertexStartIndex + 3, vertexStartIndex + 4, vertexStartIndex + 5);
164
1.38M
        mFaces.emplace_back(vertexStartIndex + 6, vertexStartIndex + 7, vertexStartIndex + 8);
165
1.38M
        mFaces.emplace_back(vertexStartIndex + 9, vertexStartIndex + 10, vertexStartIndex + 11);
166
1.38M
        mFaces.emplace_back(vertexStartIndex + 12, vertexStartIndex + 13, vertexStartIndex + 14);
167
1.38M
        mFaces.emplace_back(vertexStartIndex + 15, vertexStartIndex + 16, vertexStartIndex + 17);
168
1.38M
        mFaces.emplace_back(vertexStartIndex + 18, vertexStartIndex + 19, vertexStartIndex + 20);
169
1.38M
        mFaces.emplace_back(vertexStartIndex + 21, vertexStartIndex + 22, vertexStartIndex + 23);
170
1.38M
    }
171
172
1.44M
    unsigned int numVertices = static_cast<unsigned int>(mVertices.size() - vertexStartIndex);
173
1.44M
    if (numVertices > 0) {
174
        // create a bone affecting all the newly created vertices
175
1.39M
        aiBone *bone = new aiBone;
176
1.39M
        mBones.push_back(bone);
177
1.39M
        bone->mName = pNode->mName;
178
179
        // calculate the bone offset matrix by concatenating the inverse transformations of all parents
180
1.39M
        bone->mOffsetMatrix = aiMatrix4x4(pNode->mTransformation).Inverse();
181
5.66M
        for (aiNode *parent = pNode->mParent; parent != nullptr; parent = parent->mParent)
182
4.27M
            bone->mOffsetMatrix = aiMatrix4x4(parent->mTransformation).Inverse() * bone->mOffsetMatrix;
183
184
        // add all the vertices to the bone's influences
185
1.39M
        bone->mNumWeights = numVertices;
186
1.39M
        bone->mWeights = new aiVertexWeight[numVertices];
187
35.1M
        for (unsigned int a = 0; a < numVertices; ++a) {
188
33.7M
            bone->mWeights[a] = aiVertexWeight(vertexStartIndex + a, 1.0);
189
33.7M
        }
190
191
        // HACK: (thom) transform all vertices to the bone's local space. Should be done before adding
192
        // them to the array, but I'm tired now and I'm annoyed.
193
1.39M
        aiMatrix4x4 boneToMeshTransform = aiMatrix4x4(bone->mOffsetMatrix).Inverse();
194
35.1M
        for (unsigned int a = vertexStartIndex; a < mVertices.size(); a++)
195
33.7M
            mVertices[a] = boneToMeshTransform * mVertices[a];
196
1.39M
    }
197
198
    // and finally recurse into the children list
199
2.88M
    for (unsigned int a = 0; a < pNode->mNumChildren; ++a) {
200
1.43M
        CreateGeometry(pNode->mChildren[a]);
201
1.43M
    }
202
1.44M
}
203
204
// ------------------------------------------------------------------------------------------------
205
// Creates the mesh from the internally accumulated stuff and returns it.
206
13.0k
aiMesh *SkeletonMeshBuilder::CreateMesh() {
207
13.0k
    aiMesh *mesh = new aiMesh();
208
209
    // add points
210
13.0k
    mesh->mNumVertices = static_cast<unsigned int>(mVertices.size());
211
13.0k
    mesh->mVertices = new aiVector3D[mesh->mNumVertices];
212
13.0k
    std::copy(mVertices.begin(), mVertices.end(), mesh->mVertices);
213
214
13.0k
    mesh->mNormals = new aiVector3D[mesh->mNumVertices];
215
216
    // add faces
217
13.0k
    mesh->mNumFaces = static_cast<unsigned int>(mFaces.size());
218
13.0k
    mesh->mFaces = new aiFace[mesh->mNumFaces];
219
11.2M
    for (unsigned int a = 0; a < mesh->mNumFaces; a++) {
220
11.2M
        const Face &inface = mFaces[a];
221
11.2M
        aiFace &outface = mesh->mFaces[a];
222
11.2M
        outface.mNumIndices = 3;
223
11.2M
        outface.mIndices = new unsigned int[3];
224
11.2M
        outface.mIndices[0] = inface.mIndices[0];
225
11.2M
        outface.mIndices[1] = inface.mIndices[1];
226
11.2M
        outface.mIndices[2] = inface.mIndices[2];
227
228
        // Compute per-face normals ... we don't want the bones to be smoothed ... they're built to visualize
229
        // the skeleton, so it's good if there's a visual difference to the rest of the geometry
230
11.2M
        aiVector3D nor = ((mVertices[inface.mIndices[2]] - mVertices[inface.mIndices[0]]) ^
231
11.2M
                          (mVertices[inface.mIndices[1]] - mVertices[inface.mIndices[0]]));
232
233
11.2M
        if (nor.Length() < 1e-5) /* ensure that FindInvalidData won't remove us ...*/
234
10.9M
            nor = aiVector3D(1.0, 0.0, 0.0);
235
236
44.9M
        for (unsigned int n = 0; n < 3; ++n)
237
33.7M
            mesh->mNormals[inface.mIndices[n]] = nor;
238
11.2M
    }
239
240
    // add the bones
241
13.0k
    mesh->mNumBones = static_cast<unsigned int>(mBones.size());
242
13.0k
    mesh->mBones = new aiBone *[mesh->mNumBones];
243
13.0k
    std::copy(mBones.begin(), mBones.end(), mesh->mBones);
244
245
    // default
246
13.0k
    mesh->mMaterialIndex = 0;
247
248
13.0k
    return mesh;
249
13.0k
}
250
251
// ------------------------------------------------------------------------------------------------
252
// Creates a dummy material and returns it.
253
9.10k
aiMaterial *SkeletonMeshBuilder::CreateMaterial() {
254
9.10k
    aiMaterial *matHelper = new aiMaterial;
255
256
    // Name
257
9.10k
    aiString matName(std::string("SkeletonMaterial"));
258
9.10k
    matHelper->AddProperty(&matName, AI_MATKEY_NAME);
259
260
    // Prevent backface culling
261
9.10k
    const int no_cull = 1;
262
9.10k
    matHelper->AddProperty(&no_cull, 1, AI_MATKEY_TWOSIDED);
263
264
9.10k
    return matHelper;
265
9.10k
}