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