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