/src/assimp/code/Common/VertexTriangleAdjacency.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2025, 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 | 25 | bool bComputeNumTriangles /*= false*/) { |
56 | | // compute the number of referenced vertices if it wasn't specified by the caller |
57 | 25 | const aiFace *const pcFaceEnd = pcFaces + iNumFaces; |
58 | 25 | 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 | 25 | mNumVertices = iNumVertices + 1; |
69 | | |
70 | 25 | unsigned int *pi; |
71 | | |
72 | | // allocate storage |
73 | 25 | if (bComputeNumTriangles) { |
74 | 25 | pi = mLiveTriangles = new unsigned int[iNumVertices + 1]; |
75 | 25 | ::memset(mLiveTriangles, 0, sizeof(unsigned int) * (iNumVertices + 1)); |
76 | 25 | mOffsetTable = new unsigned int[iNumVertices + 2] + 1; |
77 | 25 | } 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 | 25 | unsigned int *piEnd = pi + iNumVertices; |
85 | 25 | *piEnd++ = 0u; |
86 | | |
87 | | // first pass: compute the number of faces referencing each vertex |
88 | 268k | for (aiFace *pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace) { |
89 | 268k | unsigned nind = pcFace->mNumIndices; |
90 | 268k | unsigned *ind = pcFace->mIndices; |
91 | 268k | if (nind > 0) pi[ind[0]]++; |
92 | 268k | if (nind > 1) pi[ind[1]]++; |
93 | 268k | if (nind > 2) pi[ind[2]]++; |
94 | 268k | } |
95 | | |
96 | | // second pass: compute the final offset table |
97 | 25 | unsigned int iSum = 0; |
98 | 25 | unsigned int *piCurOut = this->mOffsetTable; |
99 | 567 | for (unsigned int *piCur = pi; piCur != piEnd; ++piCur, ++piCurOut) { |
100 | | |
101 | 542 | unsigned int iLastSum = iSum; |
102 | 542 | iSum += *piCur; |
103 | 542 | *piCurOut = iLastSum; |
104 | 542 | } |
105 | 25 | pi = this->mOffsetTable; |
106 | | |
107 | | // third pass: compute the final table |
108 | 25 | this->mAdjacencyTable = new unsigned int[iSum]; |
109 | 25 | iSum = 0; |
110 | 268k | for (aiFace *pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace, ++iSum) { |
111 | 268k | unsigned nind = pcFace->mNumIndices; |
112 | 268k | unsigned *ind = pcFace->mIndices; |
113 | | |
114 | 268k | if (nind > 0) mAdjacencyTable[pi[ind[0]]++] = iSum; |
115 | 268k | if (nind > 1) mAdjacencyTable[pi[ind[1]]++] = iSum; |
116 | 268k | if (nind > 2) mAdjacencyTable[pi[ind[2]]++] = iSum; |
117 | 268k | } |
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 | 25 | --mOffsetTable; |
121 | 25 | *mOffsetTable = 0u; |
122 | 25 | } |
123 | | // ------------------------------------------------------------------------------------------------ |
124 | 25 | VertexTriangleAdjacency::~VertexTriangleAdjacency() { |
125 | | // delete allocated storage |
126 | 25 | delete[] mOffsetTable; |
127 | 25 | delete[] mAdjacencyTable; |
128 | 25 | delete[] mLiveTriangles; |
129 | 25 | } |