JoinVerticesProcess.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 join identical vertices
  35. * for all imported meshes
  36. */
  37. #include <vector>
  38. #include <assert.h>
  39. #include "JoinVerticesProcess.h"
  40. #include "SpatialSort.h"
  41. #include "../include/DefaultLogger.h"
  42. #include "../include/aiPostProcess.h"
  43. #include "../include/aiMesh.h"
  44. #include "../include/aiScene.h"
  45. using namespace Assimp;
  46. // ------------------------------------------------------------------------------------------------
  47. // Constructor to be privately used by Importer
  48. JoinVerticesProcess::JoinVerticesProcess()
  49. {
  50. // nothing to do here
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. // Destructor, private as well
  54. JoinVerticesProcess::~JoinVerticesProcess()
  55. {
  56. // nothing to do here
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Returns whether the processing step is present in the given flag field.
  60. bool JoinVerticesProcess::IsActive( unsigned int pFlags) const
  61. {
  62. return (pFlags & aiProcess_JoinIdenticalVertices) != 0;
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Executes the post processing step on the given imported data.
  66. void JoinVerticesProcess::Execute( aiScene* pScene)
  67. {
  68. DefaultLogger::get()->debug("JoinVerticesProcess begin");
  69. bool bHas = false;
  70. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  71. {
  72. if( this->ProcessMesh( pScene->mMeshes[a]))
  73. bHas = true;
  74. }
  75. if (bHas)DefaultLogger::get()->info("JoinVerticesProcess finished. Found vertices to join");
  76. else DefaultLogger::get()->debug("JoinVerticesProcess finished. There was nothing to do.");
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Unites identical vertices in the given mesh
  80. bool JoinVerticesProcess::ProcessMesh( aiMesh* pMesh)
  81. {
  82. // helper structure to hold all the data a single vertex can possibly have
  83. typedef struct Vertex vertex;
  84. struct Vertex
  85. {
  86. aiVector3D mPosition;
  87. aiVector3D mNormal;
  88. aiVector3D mTangent, mBitangent;
  89. aiColor4D mColors[AI_MAX_NUMBER_OF_COLOR_SETS];
  90. aiVector3D mTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  91. };
  92. std::vector<Vertex> uniqueVertices;
  93. uniqueVertices.reserve( pMesh->mNumVertices);
  94. unsigned int iOldVerts = pMesh->mNumVertices;
  95. // For each vertex the index of the vertex it was replaced by.
  96. std::vector<unsigned int> replaceIndex( pMesh->mNumVertices, 0xffffffff);
  97. // for each vertex whether it was replaced by an existing unique vertex (true) or a new vertex was created for it (false)
  98. std::vector<bool> isVertexUnique( pMesh->mNumVertices, false);
  99. // calculate the position bounds so we have a reliable epsilon to check position differences against
  100. aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f);
  101. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  102. {
  103. minVec.x = std::min( minVec.x, pMesh->mVertices[a].x);
  104. minVec.y = std::min( minVec.y, pMesh->mVertices[a].y);
  105. minVec.z = std::min( minVec.z, pMesh->mVertices[a].z);
  106. maxVec.x = std::max( maxVec.x, pMesh->mVertices[a].x);
  107. maxVec.y = std::max( maxVec.y, pMesh->mVertices[a].y);
  108. maxVec.z = std::max( maxVec.z, pMesh->mVertices[a].z);
  109. }
  110. // squared because we check against squared length of the vector difference
  111. const float epsilon = 1e-5f;
  112. const float posEpsilon = (maxVec - minVec).Length() * epsilon;
  113. const float squareEpsilon = epsilon * epsilon;
  114. // a little helper to find locally close vertices faster
  115. SpatialSort vertexFinder( pMesh->mVertices, pMesh->mNumVertices, sizeof( aiVector3D));
  116. std::vector<unsigned int> verticesFound;
  117. // now check each vertex if it brings something new to the table
  118. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  119. {
  120. // collect the vertex data
  121. Vertex v;
  122. v.mPosition = pMesh->mVertices[a];
  123. v.mNormal = (pMesh->mNormals != NULL) ? pMesh->mNormals[a] : aiVector3D( 0, 0, 0);
  124. v.mTangent = (pMesh->mTangents != NULL) ? pMesh->mTangents[a] : aiVector3D( 0, 0, 0);
  125. v.mBitangent = (pMesh->mBitangents != NULL) ? pMesh->mBitangents[a] : aiVector3D( 0, 0, 0);
  126. for( unsigned int b = 0; b < AI_MAX_NUMBER_OF_COLOR_SETS; b++)
  127. v.mColors[b] = (pMesh->mColors[b] != NULL) ? pMesh->mColors[b][a] : aiColor4D( 0, 0, 0, 0);
  128. for( unsigned int b = 0; b < AI_MAX_NUMBER_OF_TEXTURECOORDS; b++)
  129. v.mTexCoords[b] = (pMesh->mTextureCoords[b] != NULL) ? pMesh->mTextureCoords[b][a] : aiVector3D( 0, 0, 0);
  130. // collect all vertices that are close enough to the given position
  131. vertexFinder.FindPositions( v.mPosition, posEpsilon, verticesFound);
  132. unsigned int matchIndex = 0xffffffff;
  133. // check all unique vertices close to the position if this vertex is already present among them
  134. for( unsigned int b = 0; b < verticesFound.size(); b++)
  135. {
  136. unsigned int vidx = verticesFound[b];
  137. unsigned int uidx = replaceIndex[ vidx];
  138. if( uidx == 0xffffffff || !isVertexUnique[ vidx])
  139. continue;
  140. const Vertex& uv = uniqueVertices[ uidx];
  141. // Position mismatch is impossible - the vertex finder already discarded all non-matching positions
  142. // We just test the other attributes even if they're not present in the mesh.
  143. // In this case they're initialized to 0 so the comparision succeeds.
  144. // By this method the non-present attributes are effectively ignored in the comparision.
  145. if( (uv.mNormal - v.mNormal).SquareLength() > squareEpsilon)
  146. continue;
  147. if( (uv.mTangent - v.mTangent).SquareLength() > squareEpsilon)
  148. continue;
  149. if( (uv.mBitangent - v.mBitangent).SquareLength() > squareEpsilon)
  150. continue;
  151. // manually unrolled because continue wouldn't work as desired in an inner loop
  152. assert( AI_MAX_NUMBER_OF_COLOR_SETS == 4);
  153. if( GetColorDifference( uv.mColors[0], v.mColors[0]) > squareEpsilon)
  154. continue;
  155. if( GetColorDifference( uv.mColors[1], v.mColors[1]) > squareEpsilon)
  156. continue;
  157. if( GetColorDifference( uv.mColors[2], v.mColors[2]) > squareEpsilon)
  158. continue;
  159. if( GetColorDifference( uv.mColors[3], v.mColors[3]) > squareEpsilon)
  160. continue;
  161. // texture coord matching manually unrolled as well
  162. assert( AI_MAX_NUMBER_OF_TEXTURECOORDS == 4);
  163. if( (uv.mTexCoords[0] - v.mTexCoords[0]).SquareLength() > squareEpsilon)
  164. continue;
  165. if( (uv.mTexCoords[1] - v.mTexCoords[1]).SquareLength() > squareEpsilon)
  166. continue;
  167. if( (uv.mTexCoords[2] - v.mTexCoords[2]).SquareLength() > squareEpsilon)
  168. continue;
  169. if( (uv.mTexCoords[3] - v.mTexCoords[3]).SquareLength() > squareEpsilon)
  170. continue;
  171. // we're still here -> this vertex perfectly matches our given vertex
  172. matchIndex = uidx;
  173. break;
  174. }
  175. // found a replacement vertex among the uniques?
  176. if( matchIndex != 0xffffffff)
  177. {
  178. // store where to found the matching unique vertex
  179. replaceIndex[a] = matchIndex;
  180. isVertexUnique[a] = false;
  181. } else
  182. {
  183. // no unique vertex matches it upto now -> so add it
  184. replaceIndex[a] = uniqueVertices.size();
  185. uniqueVertices.push_back( v);
  186. isVertexUnique[a] = true;
  187. }
  188. }
  189. // replace vertex data with the unique data sets
  190. pMesh->mNumVertices = uniqueVertices.size();
  191. // Position
  192. delete [] pMesh->mVertices;
  193. pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
  194. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  195. pMesh->mVertices[a] = uniqueVertices[a].mPosition;
  196. // Normals, if present
  197. if( pMesh->mNormals)
  198. {
  199. delete [] pMesh->mNormals;
  200. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  201. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  202. pMesh->mNormals[a] = uniqueVertices[a].mNormal;
  203. }
  204. // Tangents, if present
  205. if( pMesh->mTangents)
  206. {
  207. delete [] pMesh->mTangents;
  208. pMesh->mTangents = new aiVector3D[pMesh->mNumVertices];
  209. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  210. pMesh->mTangents[a] = uniqueVertices[a].mTangent;
  211. }
  212. // Bitangents as well
  213. if( pMesh->mBitangents)
  214. {
  215. delete [] pMesh->mBitangents;
  216. pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices];
  217. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  218. pMesh->mBitangents[a] = uniqueVertices[a].mBitangent;
  219. }
  220. // Vertex colors
  221. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++)
  222. {
  223. if( !pMesh->mColors[a])
  224. continue;
  225. delete [] pMesh->mColors[a];
  226. pMesh->mColors[a] = new aiColor4D[pMesh->mNumVertices];
  227. for( unsigned int b = 0; b < pMesh->mNumVertices; b++)
  228. pMesh->mColors[a][b] = uniqueVertices[b].mColors[a];
  229. }
  230. // Texture coords
  231. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  232. {
  233. if( !pMesh->mTextureCoords[a])
  234. continue;
  235. delete [] pMesh->mTextureCoords[a];
  236. pMesh->mTextureCoords[a] = new aiVector3D[pMesh->mNumVertices];
  237. for( unsigned int b = 0; b < pMesh->mNumVertices; b++)
  238. pMesh->mTextureCoords[a][b] = uniqueVertices[b].mTexCoords[a];
  239. }
  240. // adjust the indices in all faces
  241. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  242. {
  243. aiFace& face = pMesh->mFaces[a];
  244. for( unsigned int b = 0; b < face.mNumIndices; b++)
  245. {
  246. const size_t index = face.mIndices[b];
  247. face.mIndices[b] = replaceIndex[face.mIndices[b]];
  248. }
  249. }
  250. // adjust bone vertex weights.
  251. for( unsigned int a = 0; a < pMesh->mNumBones; a++)
  252. {
  253. aiBone* bone = pMesh->mBones[a];
  254. std::vector<aiVertexWeight> newWeights;
  255. newWeights.reserve( bone->mNumWeights);
  256. for( unsigned int b = 0; b < bone->mNumWeights; b++)
  257. {
  258. const aiVertexWeight& ow = bone->mWeights[b];
  259. // if the vertex is a unique one, translate it
  260. if( isVertexUnique[ow.mVertexId])
  261. {
  262. aiVertexWeight nw;
  263. nw.mVertexId = replaceIndex[ow.mVertexId];
  264. nw.mWeight = ow.mWeight;
  265. newWeights.push_back( nw);
  266. }
  267. }
  268. // there should be some. At least I think there should be some
  269. assert( newWeights.size() > 0);
  270. // kill the old and replace them with the translated weights
  271. delete [] bone->mWeights;
  272. bone->mNumWeights = newWeights.size();
  273. bone->mWeights = new aiVertexWeight[bone->mNumWeights];
  274. memcpy( bone->mWeights, &newWeights[0], bone->mNumWeights * sizeof( aiVertexWeight));
  275. }
  276. return (iOldVerts != pMesh->mNumVertices);
  277. }