ProcessHelper.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, 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 "ProcessHelper.h"
  36. #include <limits>
  37. namespace Assimp {
  38. // -------------------------------------------------------------------------------
  39. void ConvertListToStrings(const std::string &in, std::list<std::string> &out) {
  40. const char *s = in.c_str();
  41. while (*s) {
  42. SkipSpacesAndLineEnd(&s);
  43. if (*s == '\'') {
  44. const char *base = ++s;
  45. while (*s != '\'') {
  46. ++s;
  47. if (*s == '\0') {
  48. ASSIMP_LOG_ERROR("ConvertListToString: String list is ill-formatted");
  49. return;
  50. }
  51. }
  52. out.push_back(std::string(base, (size_t)(s - base)));
  53. ++s;
  54. } else {
  55. out.push_back(GetNextToken(s));
  56. }
  57. }
  58. }
  59. // -------------------------------------------------------------------------------
  60. void FindAABBTransformed(const aiMesh *mesh, aiVector3D &min, aiVector3D &max,
  61. const aiMatrix4x4 &m) {
  62. min = aiVector3D(ai_real(10e10), ai_real(10e10), ai_real(10e10));
  63. max = aiVector3D(ai_real(-10e10), ai_real(-10e10), ai_real(-10e10));
  64. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  65. const aiVector3D v = m * mesh->mVertices[i];
  66. min = std::min(v, min);
  67. max = std::max(v, max);
  68. }
  69. }
  70. // -------------------------------------------------------------------------------
  71. void FindMeshCenter(aiMesh *mesh, aiVector3D &out, aiVector3D &min, aiVector3D &max) {
  72. ArrayBounds(mesh->mVertices, mesh->mNumVertices, min, max);
  73. out = min + (max - min) * (ai_real)0.5;
  74. }
  75. // -------------------------------------------------------------------------------
  76. void FindSceneCenter(aiScene *scene, aiVector3D &out, aiVector3D &min, aiVector3D &max) {
  77. if (nullptr == scene) {
  78. return;
  79. }
  80. if (0 == scene->mNumMeshes) {
  81. return;
  82. }
  83. FindMeshCenter(scene->mMeshes[0], out, min, max);
  84. for (unsigned int i = 1; i < scene->mNumMeshes; ++i) {
  85. aiVector3D tout, tmin, tmax;
  86. FindMeshCenter(scene->mMeshes[i], tout, tmin, tmax);
  87. if (min[0] > tmin[0]) min[0] = tmin[0];
  88. if (min[1] > tmin[1]) min[1] = tmin[1];
  89. if (min[2] > tmin[2]) min[2] = tmin[2];
  90. if (max[0] < tmax[0]) max[0] = tmax[0];
  91. if (max[1] < tmax[1]) max[1] = tmax[1];
  92. if (max[2] < tmax[2]) max[2] = tmax[2];
  93. }
  94. out = min + (max - min) * (ai_real)0.5;
  95. }
  96. // -------------------------------------------------------------------------------
  97. void FindMeshCenterTransformed(aiMesh *mesh, aiVector3D &out, aiVector3D &min,
  98. aiVector3D &max, const aiMatrix4x4 &m) {
  99. FindAABBTransformed(mesh, min, max, m);
  100. out = min + (max - min) * (ai_real)0.5;
  101. }
  102. // -------------------------------------------------------------------------------
  103. void FindMeshCenter(aiMesh *mesh, aiVector3D &out) {
  104. aiVector3D min, max;
  105. FindMeshCenter(mesh, out, min, max);
  106. }
  107. // -------------------------------------------------------------------------------
  108. void FindMeshCenterTransformed(aiMesh *mesh, aiVector3D &out,
  109. const aiMatrix4x4 &m) {
  110. aiVector3D min, max;
  111. FindMeshCenterTransformed(mesh, out, min, max, m);
  112. }
  113. // -------------------------------------------------------------------------------
  114. ai_real ComputePositionEpsilon(const aiMesh *pMesh) {
  115. const ai_real epsilon = ai_real(1e-4);
  116. // calculate the position bounds so we have a reliable epsilon to check position differences against
  117. aiVector3D minVec, maxVec;
  118. ArrayBounds(pMesh->mVertices, pMesh->mNumVertices, minVec, maxVec);
  119. return (maxVec - minVec).Length() * epsilon;
  120. }
  121. // -------------------------------------------------------------------------------
  122. ai_real ComputePositionEpsilon(const aiMesh *const *pMeshes, size_t num) {
  123. ai_assert(nullptr != pMeshes);
  124. const ai_real epsilon = ai_real(1e-4);
  125. // calculate the position bounds so we have a reliable epsilon to check position differences against
  126. aiVector3D minVec, maxVec, mi, ma;
  127. MinMaxChooser<aiVector3D>()(minVec, maxVec);
  128. for (size_t a = 0; a < num; ++a) {
  129. const aiMesh *pMesh = pMeshes[a];
  130. ArrayBounds(pMesh->mVertices, pMesh->mNumVertices, mi, ma);
  131. minVec = std::min(minVec, mi);
  132. maxVec = std::max(maxVec, ma);
  133. }
  134. return (maxVec - minVec).Length() * epsilon;
  135. }
  136. // -------------------------------------------------------------------------------
  137. unsigned int GetMeshVFormatUnique(const aiMesh *pcMesh) {
  138. ai_assert(nullptr != pcMesh);
  139. // FIX: the hash may never be 0. Otherwise a comparison against
  140. // nullptr could be successful
  141. unsigned int iRet = 1;
  142. // normals
  143. if (pcMesh->HasNormals()) iRet |= 0x2;
  144. // tangents and bitangents
  145. if (pcMesh->HasTangentsAndBitangents()) iRet |= 0x4;
  146. #ifdef BOOST_STATIC_ASSERT
  147. BOOST_STATIC_ASSERT(8 >= AI_MAX_NUMBER_OF_COLOR_SETS);
  148. BOOST_STATIC_ASSERT(8 >= AI_MAX_NUMBER_OF_TEXTURECOORDS);
  149. #endif
  150. // texture coordinates
  151. unsigned int p = 0;
  152. while (pcMesh->HasTextureCoords(p)) {
  153. iRet |= (0x100 << p);
  154. if (3 == pcMesh->mNumUVComponents[p])
  155. iRet |= (0x10000 << p);
  156. ++p;
  157. }
  158. // vertex colors
  159. p = 0;
  160. while (pcMesh->HasVertexColors(p))
  161. iRet |= (0x1000000 << p++);
  162. return iRet;
  163. }
  164. // -------------------------------------------------------------------------------
  165. VertexWeightTable *ComputeVertexBoneWeightTable(const aiMesh *pMesh) {
  166. if (!pMesh || !pMesh->mNumVertices || !pMesh->mNumBones) {
  167. return nullptr;
  168. }
  169. VertexWeightTable *avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices];
  170. for (unsigned int i = 0; i < pMesh->mNumBones; ++i) {
  171. aiBone *bone = pMesh->mBones[i];
  172. for (unsigned int a = 0; a < bone->mNumWeights; ++a) {
  173. const aiVertexWeight &weight = bone->mWeights[a];
  174. avPerVertexWeights[weight.mVertexId].push_back(std::pair<unsigned int, float>(i, weight.mWeight));
  175. }
  176. }
  177. return avPerVertexWeights;
  178. }
  179. // -------------------------------------------------------------------------------
  180. const char *MappingTypeToString(aiTextureMapping in) {
  181. switch (in) {
  182. case aiTextureMapping_UV:
  183. return "UV";
  184. case aiTextureMapping_BOX:
  185. return "Box";
  186. case aiTextureMapping_SPHERE:
  187. return "Sphere";
  188. case aiTextureMapping_CYLINDER:
  189. return "Cylinder";
  190. case aiTextureMapping_PLANE:
  191. return "Plane";
  192. case aiTextureMapping_OTHER:
  193. return "Other";
  194. default:
  195. break;
  196. }
  197. ai_assert(false);
  198. return "BUG";
  199. }
  200. // -------------------------------------------------------------------------------
  201. aiMesh *MakeSubmesh(const aiMesh *pMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags) {
  202. aiMesh *oMesh = new aiMesh();
  203. std::vector<unsigned int> vMap(pMesh->mNumVertices, UINT_MAX);
  204. size_t numSubVerts = 0;
  205. size_t numSubFaces = subMeshFaces.size();
  206. for (unsigned int i = 0; i < numSubFaces; i++) {
  207. const aiFace &f = pMesh->mFaces[subMeshFaces[i]];
  208. for (unsigned int j = 0; j < f.mNumIndices; j++) {
  209. if (vMap[f.mIndices[j]] == UINT_MAX) {
  210. vMap[f.mIndices[j]] = static_cast<unsigned int>(numSubVerts++);
  211. }
  212. }
  213. }
  214. oMesh->mName = pMesh->mName;
  215. oMesh->mMaterialIndex = pMesh->mMaterialIndex;
  216. oMesh->mPrimitiveTypes = pMesh->mPrimitiveTypes;
  217. // create all the arrays for this mesh if the old mesh contained them
  218. oMesh->mNumFaces = static_cast<unsigned int>(subMeshFaces.size());
  219. oMesh->mNumVertices = static_cast<unsigned int>(numSubVerts);
  220. oMesh->mVertices = new aiVector3D[numSubVerts];
  221. if (pMesh->HasNormals()) {
  222. oMesh->mNormals = new aiVector3D[numSubVerts];
  223. }
  224. if (pMesh->HasTangentsAndBitangents()) {
  225. oMesh->mTangents = new aiVector3D[numSubVerts];
  226. oMesh->mBitangents = new aiVector3D[numSubVerts];
  227. }
  228. for (size_t a = 0; pMesh->HasTextureCoords(static_cast<unsigned int>(a)); ++a) {
  229. oMesh->mTextureCoords[a] = new aiVector3D[numSubVerts];
  230. oMesh->mNumUVComponents[a] = pMesh->mNumUVComponents[a];
  231. }
  232. for (size_t a = 0; pMesh->HasVertexColors(static_cast<unsigned int>(a)); ++a) {
  233. oMesh->mColors[a] = new aiColor4D[numSubVerts];
  234. }
  235. // and copy over the data, generating faces with linear indices along the way
  236. oMesh->mFaces = new aiFace[numSubFaces];
  237. for (unsigned int a = 0; a < numSubFaces; ++a) {
  238. const aiFace &srcFace = pMesh->mFaces[subMeshFaces[a]];
  239. aiFace &dstFace = oMesh->mFaces[a];
  240. dstFace.mNumIndices = srcFace.mNumIndices;
  241. dstFace.mIndices = new unsigned int[dstFace.mNumIndices];
  242. // accumulate linearly all the vertices of the source face
  243. for (size_t b = 0; b < dstFace.mNumIndices; ++b) {
  244. dstFace.mIndices[b] = vMap[srcFace.mIndices[b]];
  245. }
  246. }
  247. for (unsigned int srcIndex = 0; srcIndex < pMesh->mNumVertices; ++srcIndex) {
  248. unsigned int nvi = vMap[srcIndex];
  249. if (nvi == UINT_MAX) {
  250. continue;
  251. }
  252. oMesh->mVertices[nvi] = pMesh->mVertices[srcIndex];
  253. if (pMesh->HasNormals()) {
  254. oMesh->mNormals[nvi] = pMesh->mNormals[srcIndex];
  255. }
  256. if (pMesh->HasTangentsAndBitangents()) {
  257. oMesh->mTangents[nvi] = pMesh->mTangents[srcIndex];
  258. oMesh->mBitangents[nvi] = pMesh->mBitangents[srcIndex];
  259. }
  260. for (size_t c = 0, cc = pMesh->GetNumUVChannels(); c < cc; ++c) {
  261. oMesh->mTextureCoords[c][nvi] = pMesh->mTextureCoords[c][srcIndex];
  262. }
  263. for (size_t c = 0, cc = pMesh->GetNumColorChannels(); c < cc; ++c) {
  264. oMesh->mColors[c][nvi] = pMesh->mColors[c][srcIndex];
  265. }
  266. }
  267. if (~subFlags & AI_SUBMESH_FLAGS_SANS_BONES) {
  268. std::vector<unsigned int> subBones(pMesh->mNumBones, 0);
  269. for (unsigned int a = 0; a < pMesh->mNumBones; ++a) {
  270. const aiBone *bone = pMesh->mBones[a];
  271. for (unsigned int b = 0; b < bone->mNumWeights; b++) {
  272. unsigned int v = vMap[bone->mWeights[b].mVertexId];
  273. if (v != UINT_MAX) {
  274. subBones[a]++;
  275. }
  276. }
  277. }
  278. for (unsigned int a = 0; a < pMesh->mNumBones; ++a) {
  279. if (subBones[a] > 0) {
  280. oMesh->mNumBones++;
  281. }
  282. }
  283. if (oMesh->mNumBones) {
  284. oMesh->mBones = new aiBone *[oMesh->mNumBones]();
  285. unsigned int nbParanoia = oMesh->mNumBones;
  286. oMesh->mNumBones = 0; //rewind
  287. for (unsigned int a = 0; a < pMesh->mNumBones; ++a) {
  288. if (subBones[a] == 0) {
  289. continue;
  290. }
  291. aiBone *newBone = new aiBone;
  292. oMesh->mBones[oMesh->mNumBones++] = newBone;
  293. const aiBone *bone = pMesh->mBones[a];
  294. newBone->mName = bone->mName;
  295. newBone->mOffsetMatrix = bone->mOffsetMatrix;
  296. newBone->mWeights = new aiVertexWeight[subBones[a]];
  297. for (unsigned int b = 0; b < bone->mNumWeights; b++) {
  298. const unsigned int v = vMap[bone->mWeights[b].mVertexId];
  299. if (v != UINT_MAX) {
  300. aiVertexWeight w(v, bone->mWeights[b].mWeight);
  301. newBone->mWeights[newBone->mNumWeights++] = w;
  302. }
  303. }
  304. }
  305. ai_assert(nbParanoia == oMesh->mNumBones);
  306. (void)nbParanoia; // remove compiler warning on release build
  307. }
  308. }
  309. return oMesh;
  310. }
  311. } // namespace Assimp