TriangulateProcess.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. Found polygons to triangulate");
  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. std::vector<aiFace> newFaces;
  97. newFaces.reserve( pMesh->mNumFaces*2);
  98. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  99. {
  100. aiFace& face = pMesh->mFaces[a];
  101. // if it's a simple primitive, just copy it
  102. if( face.mNumIndices <= 3)
  103. {
  104. newFaces.push_back( aiFace());
  105. aiFace& nface = newFaces.back();
  106. nface.mNumIndices = face.mNumIndices;
  107. nface.mIndices = face.mIndices;
  108. face.mIndices = NULL;
  109. }
  110. else
  111. {
  112. assert( face.mNumIndices > 3);
  113. for( unsigned int b = 0; b < face.mNumIndices - 2; b++)
  114. {
  115. newFaces.push_back( aiFace());
  116. aiFace& nface = newFaces.back();
  117. nface.mNumIndices = 3;
  118. nface.mIndices = new unsigned int[3];
  119. nface.mIndices[0] = face.mIndices[0];
  120. nface.mIndices[1] = face.mIndices[b+1];
  121. nface.mIndices[2] = face.mIndices[b+2];
  122. }
  123. }
  124. }
  125. // kill the old faces
  126. delete [] pMesh->mFaces;
  127. // and insert our newly generated faces
  128. pMesh->mNumFaces = (unsigned int)newFaces.size();
  129. pMesh->mFaces = new aiFace[pMesh->mNumFaces];
  130. for( unsigned int a = 0; a < newFaces.size(); a++)
  131. {
  132. // operator= would copy the whole array which is definitely not necessary
  133. pMesh->mFaces[a].mNumIndices = newFaces[a].mNumIndices;
  134. pMesh->mFaces[a].mIndices = newFaces[a].mIndices;
  135. newFaces[a].mIndices = NULL;
  136. }
  137. return true;
  138. }