JoinVerticesProcess.cpp 13 KB

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