VertexTriangleAdjacency.cpp 5.7 KB

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