Coverage Report

Created: 2026-04-01 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/Common/VertexTriangleAdjacency.cpp
Line
Count
Source
1
/*
2
---------------------------------------------------------------------------
3
Open Asset Import Library (assimp)
4
---------------------------------------------------------------------------
5
6
Copyright (c) 2006-2026, assimp team
7
8
All rights reserved.
9
10
Redistribution and use of this software in source and binary forms,
11
with or without modification, are permitted provided that the following
12
conditions are met:
13
14
* Redistributions of source code must retain the above
15
  copyright notice, this list of conditions and the
16
  following disclaimer.
17
18
* Redistributions in binary form must reproduce the above
19
  copyright notice, this list of conditions and the
20
  following disclaimer in the documentation and/or other
21
  materials provided with the distribution.
22
23
* Neither the name of the assimp team, nor the names of its
24
  contributors may be used to endorse or promote products
25
  derived from this software without specific prior
26
  written permission of the assimp team.
27
28
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
---------------------------------------------------------------------------
40
*/
41
42
/** @file Implementation of the VertexTriangleAdjacency helper class
43
 */
44
45
// internal headers
46
#include "VertexTriangleAdjacency.h"
47
#include <assimp/mesh.h>
48
49
using namespace Assimp;
50
51
// ------------------------------------------------------------------------------------------------
52
VertexTriangleAdjacency::VertexTriangleAdjacency(aiFace *pcFaces,
53
        unsigned int iNumFaces,
54
        unsigned int iNumVertices /*= 0*/,
55
9.01k
        bool bComputeNumTriangles /*= false*/) {
56
    // compute the number of referenced vertices if it wasn't specified by the caller
57
9.01k
    const aiFace *const pcFaceEnd = pcFaces + iNumFaces;
58
9.01k
    if (0 == iNumVertices) {
59
0
        for (aiFace *pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace) {
60
0
            ai_assert(nullptr != pcFace);
61
0
            ai_assert(3 == pcFace->mNumIndices);
62
0
            iNumVertices = std::max(iNumVertices, pcFace->mIndices[0]);
63
0
            iNumVertices = std::max(iNumVertices, pcFace->mIndices[1]);
64
0
            iNumVertices = std::max(iNumVertices, pcFace->mIndices[2]);
65
0
        }
66
0
    }
67
68
9.01k
    mNumVertices = iNumVertices + 1;
69
70
9.01k
    unsigned int *pi;
71
72
    // allocate storage
73
9.01k
    if (bComputeNumTriangles) {
74
9.01k
        pi = mLiveTriangles = new unsigned int[iNumVertices + 1];
75
9.01k
        ::memset(mLiveTriangles, 0, sizeof(unsigned int) * (iNumVertices + 1));
76
9.01k
        mOffsetTable = new unsigned int[iNumVertices + 2] + 1;
77
9.01k
    } else {
78
0
        pi = mOffsetTable = new unsigned int[iNumVertices + 2] + 1;
79
0
        ::memset(mOffsetTable, 0, sizeof(unsigned int) * (iNumVertices + 1));
80
0
        mLiveTriangles = nullptr; // important, otherwise the d'tor would crash
81
0
    }
82
83
    // get a pointer to the end of the buffer
84
9.01k
    unsigned int *piEnd = pi + iNumVertices;
85
9.01k
    *piEnd++ = 0u;
86
87
    // first pass: compute the number of faces referencing each vertex
88
512k
    for (aiFace *pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace) {
89
503k
        unsigned nind = pcFace->mNumIndices;
90
503k
        unsigned *ind = pcFace->mIndices;
91
503k
        if (nind > 0) pi[ind[0]]++;
92
503k
        if (nind > 1) pi[ind[1]]++;
93
503k
        if (nind > 2) pi[ind[2]]++;
94
503k
    }
95
96
    // second pass: compute the final offset table
97
9.01k
    unsigned int iSum = 0;
98
9.01k
    unsigned int *piCurOut = this->mOffsetTable;
99
695k
    for (unsigned int *piCur = pi; piCur != piEnd; ++piCur, ++piCurOut) {
100
101
686k
        unsigned int iLastSum = iSum;
102
686k
        iSum += *piCur;
103
686k
        *piCurOut = iLastSum;
104
686k
    }
105
9.01k
    pi = this->mOffsetTable;
106
107
    // third pass: compute the final table
108
9.01k
    this->mAdjacencyTable = new unsigned int[iSum];
109
9.01k
    iSum = 0;
110
512k
    for (aiFace *pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace, ++iSum) {
111
503k
        unsigned nind = pcFace->mNumIndices;
112
503k
        unsigned *ind = pcFace->mIndices;
113
114
503k
        if (nind > 0) mAdjacencyTable[pi[ind[0]]++] = iSum;
115
503k
        if (nind > 1) mAdjacencyTable[pi[ind[1]]++] = iSum;
116
503k
        if (nind > 2) mAdjacencyTable[pi[ind[2]]++] = iSum;
117
503k
    }
118
    // fourth pass: undo the offset computations made during the third pass
119
    // We could do this in a separate buffer, but this would be TIMES slower.
120
9.01k
    --mOffsetTable;
121
9.01k
    *mOffsetTable = 0u;
122
9.01k
}
123
// ------------------------------------------------------------------------------------------------
124
9.01k
VertexTriangleAdjacency::~VertexTriangleAdjacency() {
125
    // delete allocated storage
126
9.01k
    delete[] mOffsetTable;
127
9.01k
    delete[] mAdjacencyTable;
128
9.01k
    delete[] mLiveTriangles;
129
9.01k
}