vrshoot

view libs/assimp/VertexTriangleAdjacency.cpp @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
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:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
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.
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.
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 */
42 /** @file Implementation of the VertexTriangleAdjacency helper class
43 */
45 #include "AssimpPCH.h"
47 // internal headers
48 #include "VertexTriangleAdjacency.h"
50 using namespace Assimp;
52 // ------------------------------------------------------------------------------------------------
53 VertexTriangleAdjacency::VertexTriangleAdjacency(aiFace *pcFaces,
54 unsigned int iNumFaces,
55 unsigned int iNumVertices /*= 0*/,
56 bool bComputeNumTriangles /*= false*/)
57 {
58 // compute the number of referenced vertices if it wasn't specified by the caller
59 const aiFace* const pcFaceEnd = pcFaces + iNumFaces;
60 if (!iNumVertices) {
62 for (aiFace* pcFace = pcFaces; pcFace != pcFaceEnd; ++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 }
70 this->iNumVertices = iNumVertices;
72 unsigned int* pi;
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 }
80 else {
81 pi = mOffsetTable = new unsigned int[iNumVertices+2]+1;
82 memset(mOffsetTable,0,sizeof(unsigned int)*(iNumVertices+1));
83 mLiveTriangles = NULL; // important, otherwise the d'tor would crash
84 }
86 // get a pointer to the end of the buffer
87 unsigned int* piEnd = pi+iNumVertices;
88 *piEnd++ = 0u;
90 // first pass: compute the number of faces referencing each vertex
91 for (aiFace* pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace)
92 {
93 pi[pcFace->mIndices[0]]++;
94 pi[pcFace->mIndices[1]]++;
95 pi[pcFace->mIndices[2]]++;
96 }
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) {
103 unsigned int iLastSum = iSum;
104 iSum += *piCur;
105 *piCurOut = iLastSum;
106 }
107 pi = this->mOffsetTable;
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) {
114 unsigned int idx = pcFace->mIndices[0];
115 mAdjacencyTable[pi[idx]++] = iSum;
117 idx = pcFace->mIndices[1];
118 mAdjacencyTable[pi[idx]++] = iSum;
120 idx = pcFace->mIndices[2];
121 mAdjacencyTable[pi[idx]++] = iSum;
122 }
123 // fourth pass: undo the offset computations made during the third pass
124 // We could do this in a separate buffer, but this would be TIMES slower.
125 --mOffsetTable;
126 *mOffsetTable = 0u;
127 }
128 // ------------------------------------------------------------------------------------------------
129 VertexTriangleAdjacency::~VertexTriangleAdjacency()
130 {
131 // delete allocated storage
132 delete[] mOffsetTable;
133 delete[] mAdjacencyTable;
134 delete[] mLiveTriangles;
135 }