TriangulateProcess.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 post processing step to split up
  35. * all faces with more than three indices into triangles.
  36. */
  37. #include "AssimpPCH.h"
  38. #include "TriangulateProcess.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor to be privately used by Importer
  42. TriangulateProcess::TriangulateProcess()
  43. {
  44. // nothing to do here
  45. }
  46. // ------------------------------------------------------------------------------------------------
  47. // Destructor, private as well
  48. TriangulateProcess::~TriangulateProcess()
  49. {
  50. // nothing to do here
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. // Returns whether the processing step is present in the given flag field.
  54. bool TriangulateProcess::IsActive( unsigned int pFlags) const
  55. {
  56. return (pFlags & aiProcess_Triangulate) != 0;
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Executes the post processing step on the given imported data.
  60. void TriangulateProcess::Execute( aiScene* pScene)
  61. {
  62. DefaultLogger::get()->debug("TriangulateProcess begin");
  63. bool bHas = false;
  64. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  65. {
  66. if( TriangulateMesh( pScene->mMeshes[a]))
  67. bHas = true;
  68. }
  69. if (bHas)DefaultLogger::get()->info ("TriangulateProcess finished. All polygons have been triangulated");
  70. else DefaultLogger::get()->debug("TriangulateProcess finished. There was nothing to do.");
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Triangulates the given mesh.
  74. bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
  75. {
  76. // check whether we will need to do something ...
  77. // FIX: now we have aiMesh::mPrimitiveTypes, so this is only here for test cases
  78. if (!pMesh->mPrimitiveTypes)
  79. {
  80. bool bNeed = false;
  81. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  82. {
  83. const aiFace& face = pMesh->mFaces[a];
  84. if( face.mNumIndices != 3)
  85. {
  86. bNeed = true;
  87. }
  88. }
  89. if (!bNeed)return false;
  90. }
  91. else if (!(pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON))
  92. return false;
  93. // the output mesh will contain triangles, but no polys anymore
  94. pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  95. pMesh->mPrimitiveTypes &= ~aiPrimitiveType_POLYGON;
  96. // Find out how many output faces we'll have
  97. unsigned int numOut = 0;
  98. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  99. {
  100. aiFace& face = pMesh->mFaces[a];
  101. if( face.mNumIndices <= 3)
  102. numOut++;
  103. else numOut += face.mNumIndices-2;
  104. }
  105. // Just another check whether aiMesh::mPrimitiveTypes is correct
  106. assert(numOut != pMesh->mNumFaces);
  107. aiFace* out = new aiFace[numOut], *curOut = out;
  108. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  109. {
  110. aiFace& face = pMesh->mFaces[a];
  111. // if it's a simple primitive, just copy it
  112. if( face.mNumIndices <= 3)
  113. {
  114. aiFace& nface = *curOut++;
  115. nface.mNumIndices = face.mNumIndices;
  116. nface.mIndices = face.mIndices;
  117. }
  118. else
  119. {
  120. for( unsigned int b = 0, end = face.mNumIndices - 2; b < end; b++)
  121. {
  122. aiFace& nface = *curOut++;
  123. nface.mNumIndices = 3;
  124. // Reuse the buffer for the very last element to save another allocation
  125. if (b == end-1)
  126. nface.mIndices = face.mIndices;
  127. else
  128. {
  129. nface.mIndices = new unsigned int[3];
  130. nface.mIndices[0] = face.mIndices[0];
  131. }
  132. nface.mIndices[1] = face.mIndices[b+1];
  133. nface.mIndices[2] = face.mIndices[b+2];
  134. }
  135. }
  136. face.mIndices = NULL;
  137. }
  138. // kill the old faces
  139. delete [] pMesh->mFaces;
  140. // ... and store the new ones
  141. pMesh->mFaces = out;
  142. pMesh->mNumFaces = numOut;
  143. return true;
  144. }