Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

416 рядки
12 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /// @file ProcessHelper.cpp
  34. /** Implement shared utility functions for postprocessing steps */
  35. #include "AssimpPCH.h"
  36. #include "ProcessHelper.h"
  37. #include <limits>
  38. namespace Assimp {
  39. // -------------------------------------------------------------------------------
  40. void ConvertListToStrings(const std::string& in, std::list<std::string>& out)
  41. {
  42. const char* s = in.c_str();
  43. while (*s) {
  44. SkipSpacesAndLineEnd(&s);
  45. if (*s == '\'') {
  46. const char* base = ++s;
  47. while (*s != '\'') {
  48. ++s;
  49. if (*s == '\0') {
  50. DefaultLogger::get()->error("ConvertListToString: String list is ill-formatted");
  51. return;
  52. }
  53. }
  54. out.push_back(std::string(base,(size_t)(s-base)));
  55. ++s;
  56. }
  57. else {
  58. out.push_back(GetNextToken(s));
  59. }
  60. }
  61. }
  62. // -------------------------------------------------------------------------------
  63. void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D& max,
  64. const aiMatrix4x4& m)
  65. {
  66. min = aiVector3D (10e10f, 10e10f, 10e10f);
  67. max = aiVector3D (-10e10f,-10e10f,-10e10f);
  68. for (unsigned int i = 0;i < mesh->mNumVertices;++i)
  69. {
  70. const aiVector3D v = m * mesh->mVertices[i];
  71. min = std::min(v,min);
  72. max = std::max(v,max);
  73. }
  74. }
  75. // -------------------------------------------------------------------------------
  76. void FindMeshCenter (aiMesh* mesh, aiVector3D& out, aiVector3D& min, aiVector3D& max)
  77. {
  78. ArrayBounds(mesh->mVertices,mesh->mNumVertices, min,max);
  79. out = min + (max-min)*0.5f;
  80. }
  81. // -------------------------------------------------------------------------------
  82. void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out, aiVector3D& min,
  83. aiVector3D& max, const aiMatrix4x4& m)
  84. {
  85. FindAABBTransformed(mesh,min,max,m);
  86. out = min + (max-min)*0.5f;
  87. }
  88. // -------------------------------------------------------------------------------
  89. void FindMeshCenter (aiMesh* mesh, aiVector3D& out)
  90. {
  91. aiVector3D min,max;
  92. FindMeshCenter(mesh,out,min,max);
  93. }
  94. // -------------------------------------------------------------------------------
  95. void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out,
  96. const aiMatrix4x4& m)
  97. {
  98. aiVector3D min,max;
  99. FindMeshCenterTransformed(mesh,out,min,max,m);
  100. }
  101. // -------------------------------------------------------------------------------
  102. float ComputePositionEpsilon(const aiMesh* pMesh)
  103. {
  104. const float epsilon = 1e-4f;
  105. // calculate the position bounds so we have a reliable epsilon to check position differences against
  106. aiVector3D minVec, maxVec;
  107. ArrayBounds(pMesh->mVertices,pMesh->mNumVertices,minVec,maxVec);
  108. return (maxVec - minVec).Length() * epsilon;
  109. }
  110. // -------------------------------------------------------------------------------
  111. float ComputePositionEpsilon(const aiMesh* const* pMeshes, size_t num)
  112. {
  113. const float epsilon = 1e-4f;
  114. // calculate the position bounds so we have a reliable epsilon to check position differences against
  115. aiVector3D minVec, maxVec, mi, ma;
  116. MinMaxChooser<aiVector3D>()(minVec,maxVec);
  117. for (size_t a = 0; a < num; ++a) {
  118. const aiMesh* pMesh = pMeshes[a];
  119. ArrayBounds(pMesh->mVertices,pMesh->mNumVertices,mi,ma);
  120. minVec = std::min(minVec,mi);
  121. maxVec = std::max(maxVec,ma);
  122. }
  123. return (maxVec - minVec).Length() * epsilon;
  124. }
  125. // -------------------------------------------------------------------------------
  126. unsigned int GetMeshVFormatUnique(const aiMesh* pcMesh)
  127. {
  128. ai_assert(NULL != pcMesh);
  129. // FIX: the hash may never be 0. Otherwise a comparison against
  130. // nullptr could be successful
  131. unsigned int iRet = 1;
  132. // normals
  133. if (pcMesh->HasNormals())iRet |= 0x2;
  134. // tangents and bitangents
  135. if (pcMesh->HasTangentsAndBitangents())iRet |= 0x4;
  136. #ifdef BOOST_STATIC_ASSERT
  137. BOOST_STATIC_ASSERT(8 >= AI_MAX_NUMBER_OF_COLOR_SETS);
  138. BOOST_STATIC_ASSERT(8 >= AI_MAX_NUMBER_OF_TEXTURECOORDS);
  139. #endif
  140. // texture coordinates
  141. unsigned int p = 0;
  142. while (pcMesh->HasTextureCoords(p))
  143. {
  144. iRet |= (0x100 << p);
  145. if (3 == pcMesh->mNumUVComponents[p])
  146. iRet |= (0x10000 << p);
  147. ++p;
  148. }
  149. // vertex colors
  150. p = 0;
  151. while (pcMesh->HasVertexColors(p))iRet |= (0x1000000 << p++);
  152. return iRet;
  153. }
  154. // -------------------------------------------------------------------------------
  155. VertexWeightTable* ComputeVertexBoneWeightTable(const aiMesh* pMesh)
  156. {
  157. if (!pMesh || !pMesh->mNumVertices || !pMesh->mNumBones) {
  158. return NULL;
  159. }
  160. VertexWeightTable* avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices];
  161. for (unsigned int i = 0; i < pMesh->mNumBones;++i) {
  162. aiBone* bone = pMesh->mBones[i];
  163. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  164. const aiVertexWeight& weight = bone->mWeights[a];
  165. avPerVertexWeights[weight.mVertexId].push_back( std::pair<unsigned int,float>(i,weight.mWeight) );
  166. }
  167. }
  168. return avPerVertexWeights;
  169. }
  170. // -------------------------------------------------------------------------------
  171. const char* TextureTypeToString(aiTextureType in)
  172. {
  173. switch (in)
  174. {
  175. case aiTextureType_NONE:
  176. return "n/a";
  177. case aiTextureType_DIFFUSE:
  178. return "Diffuse";
  179. case aiTextureType_SPECULAR:
  180. return "Specular";
  181. case aiTextureType_AMBIENT:
  182. return "Ambient";
  183. case aiTextureType_EMISSIVE:
  184. return "Emissive";
  185. case aiTextureType_OPACITY:
  186. return "Opacity";
  187. case aiTextureType_NORMALS:
  188. return "Normals";
  189. case aiTextureType_HEIGHT:
  190. return "Height";
  191. case aiTextureType_SHININESS:
  192. return "Shininess";
  193. case aiTextureType_DISPLACEMENT:
  194. return "Displacement";
  195. case aiTextureType_LIGHTMAP:
  196. return "Lightmap";
  197. case aiTextureType_REFLECTION:
  198. return "Reflection";
  199. case aiTextureType_UNKNOWN:
  200. return "Unknown";
  201. default:
  202. break;
  203. }
  204. ai_assert(false);
  205. return "BUG";
  206. }
  207. // -------------------------------------------------------------------------------
  208. const char* MappingTypeToString(aiTextureMapping in)
  209. {
  210. switch (in)
  211. {
  212. case aiTextureMapping_UV:
  213. return "UV";
  214. case aiTextureMapping_BOX:
  215. return "Box";
  216. case aiTextureMapping_SPHERE:
  217. return "Sphere";
  218. case aiTextureMapping_CYLINDER:
  219. return "Cylinder";
  220. case aiTextureMapping_PLANE:
  221. return "Plane";
  222. case aiTextureMapping_OTHER:
  223. return "Other";
  224. default:
  225. break;
  226. }
  227. ai_assert(false);
  228. return "BUG";
  229. }
  230. // -------------------------------------------------------------------------------
  231. aiMesh* MakeSubmesh(const aiMesh *pMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags)
  232. {
  233. aiMesh *oMesh = new aiMesh();
  234. std::vector<unsigned int> vMap(pMesh->mNumVertices,UINT_MAX);
  235. size_t numSubVerts = 0;
  236. size_t numSubFaces = subMeshFaces.size();
  237. for(unsigned int i=0;i<numSubFaces;i++) {
  238. const aiFace &f = pMesh->mFaces[subMeshFaces[i]];
  239. for(unsigned int j=0;j<f.mNumIndices;j++) {
  240. if(vMap[f.mIndices[j]]==UINT_MAX) {
  241. vMap[f.mIndices[j]] = numSubVerts++;
  242. }
  243. }
  244. }
  245. oMesh->mName = pMesh->mName;
  246. oMesh->mMaterialIndex = pMesh->mMaterialIndex;
  247. oMesh->mPrimitiveTypes = pMesh->mPrimitiveTypes;
  248. // create all the arrays for this mesh if the old mesh contained them
  249. oMesh->mNumFaces = subMeshFaces.size();
  250. oMesh->mNumVertices = numSubVerts;
  251. oMesh->mVertices = new aiVector3D[numSubVerts];
  252. if( pMesh->HasNormals() ) {
  253. oMesh->mNormals = new aiVector3D[numSubVerts];
  254. }
  255. if( pMesh->HasTangentsAndBitangents() ) {
  256. oMesh->mTangents = new aiVector3D[numSubVerts];
  257. oMesh->mBitangents = new aiVector3D[numSubVerts];
  258. }
  259. for( size_t a = 0; pMesh->HasTextureCoords( a) ; ++a ) {
  260. oMesh->mTextureCoords[a] = new aiVector3D[numSubVerts];
  261. oMesh->mNumUVComponents[a] = pMesh->mNumUVComponents[a];
  262. }
  263. for( size_t a = 0; pMesh->HasVertexColors( a); ++a ) {
  264. oMesh->mColors[a] = new aiColor4D[numSubVerts];
  265. }
  266. // and copy over the data, generating faces with linear indices along the way
  267. oMesh->mFaces = new aiFace[numSubFaces];
  268. for(unsigned int a = 0; a < numSubFaces; ++a ) {
  269. const aiFace& srcFace = pMesh->mFaces[subMeshFaces[a]];
  270. aiFace& dstFace = oMesh->mFaces[a];
  271. dstFace.mNumIndices = srcFace.mNumIndices;
  272. dstFace.mIndices = new unsigned int[dstFace.mNumIndices];
  273. // accumulate linearly all the vertices of the source face
  274. for( size_t b = 0; b < dstFace.mNumIndices; ++b ) {
  275. dstFace.mIndices[b] = vMap[srcFace.mIndices[b]];
  276. }
  277. }
  278. for(unsigned int srcIndex = 0; srcIndex < pMesh->mNumVertices; ++srcIndex ) {
  279. unsigned int nvi = vMap[srcIndex];
  280. if(nvi==UINT_MAX) {
  281. continue;
  282. }
  283. oMesh->mVertices[nvi] = pMesh->mVertices[srcIndex];
  284. if( pMesh->HasNormals() ) {
  285. oMesh->mNormals[nvi] = pMesh->mNormals[srcIndex];
  286. }
  287. if( pMesh->HasTangentsAndBitangents() ) {
  288. oMesh->mTangents[nvi] = pMesh->mTangents[srcIndex];
  289. oMesh->mBitangents[nvi] = pMesh->mBitangents[srcIndex];
  290. }
  291. for( size_t c = 0, cc = pMesh->GetNumUVChannels(); c < cc; ++c ) {
  292. oMesh->mTextureCoords[c][nvi] = pMesh->mTextureCoords[c][srcIndex];
  293. }
  294. for( size_t c = 0, cc = pMesh->GetNumColorChannels(); c < cc; ++c ) {
  295. oMesh->mColors[c][nvi] = pMesh->mColors[c][srcIndex];
  296. }
  297. }
  298. if(~subFlags&AI_SUBMESH_FLAGS_SANS_BONES) {
  299. std::vector<unsigned int> subBones(pMesh->mNumBones,0);
  300. for(unsigned int a=0;a<pMesh->mNumBones;++a) {
  301. const aiBone* bone = pMesh->mBones[a];
  302. for(unsigned int b=0;b<bone->mNumWeights;b++) {
  303. unsigned int v = vMap[bone->mWeights[b].mVertexId];
  304. if(v!=UINT_MAX) {
  305. subBones[a]++;
  306. }
  307. }
  308. }
  309. for(unsigned int a=0;a<pMesh->mNumBones;++a) {
  310. if(subBones[a]>0) {
  311. oMesh->mNumBones++;
  312. }
  313. }
  314. if(oMesh->mNumBones) {
  315. oMesh->mBones = new aiBone*[oMesh->mNumBones]();
  316. unsigned int nbParanoia = oMesh->mNumBones;
  317. oMesh->mNumBones = 0; //rewind
  318. for(unsigned int a=0;a<pMesh->mNumBones;++a) {
  319. if(subBones[a]==0) {
  320. continue;
  321. }
  322. aiBone *newBone = new aiBone;
  323. oMesh->mBones[oMesh->mNumBones++] = newBone;
  324. const aiBone* bone = pMesh->mBones[a];
  325. newBone->mName = bone->mName;
  326. newBone->mOffsetMatrix = bone->mOffsetMatrix;
  327. newBone->mWeights = new aiVertexWeight[subBones[a]];
  328. for(unsigned int b=0;b<bone->mNumWeights;b++) {
  329. const unsigned int v = vMap[bone->mWeights[b].mVertexId];
  330. if(v!=UINT_MAX) {
  331. aiVertexWeight w(v,bone->mWeights[b].mWeight);
  332. newBone->mWeights[newBone->mNumWeights++] = w;
  333. }
  334. }
  335. }
  336. ai_assert(nbParanoia==oMesh->mNumBones);
  337. (void)nbParanoia; // remove compiler warning on release build
  338. }
  339. }
  340. return oMesh;
  341. }
  342. } // namespace Assimp