Subdivision.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. #include <assimp/Subdivision.h>
  34. #include <assimp/SceneCombiner.h>
  35. #include <assimp/SpatialSort.h>
  36. #include <assimp/Vertex.h>
  37. #include <assimp/ai_assert.h>
  38. #include "PostProcessing/ProcessHelper.h"
  39. #include <stdio.h>
  40. #include <unordered_map>
  41. using namespace Assimp;
  42. void mydummy() {}
  43. #ifdef _MSC_VER
  44. #pragma warning(disable : 4709)
  45. #endif // _MSC_VER
  46. // ------------------------------------------------------------------------------------------------
  47. /** Subdivider stub class to implement the Catmull-Clarke subdivision algorithm. The
  48. * implementation is basing on recursive refinement. Directly evaluating the result is also
  49. * possible and much quicker, but it depends on lengthy matrix lookup tables. */
  50. // ------------------------------------------------------------------------------------------------
  51. class CatmullClarkSubdivider : public Subdivider {
  52. public:
  53. void Subdivide(aiMesh *mesh, aiMesh *&out, unsigned int num, bool discard_input);
  54. void Subdivide(aiMesh **smesh, size_t nmesh,
  55. aiMesh **out, unsigned int num, bool discard_input);
  56. // ---------------------------------------------------------------------------
  57. /** Intermediate description of an edge between two corners of a polygon*/
  58. // ---------------------------------------------------------------------------
  59. struct Edge {
  60. Edge() :
  61. ref(0) {}
  62. Vertex edge_point, midpoint;
  63. unsigned int ref;
  64. };
  65. typedef std::vector<unsigned int> UIntVector;
  66. typedef std::unordered_map<uint64_t, Edge> EdgeMap;
  67. // ---------------------------------------------------------------------------
  68. // Hashing function to derive an index into an #EdgeMap from two given
  69. // 'unsigned int' vertex coordinates (!!distinct coordinates - same
  70. // vertex position == same index!!).
  71. // NOTE - this leads to rare hash collisions if a) sizeof(unsigned int)>4
  72. // and (id[0]>2^32-1 or id[0]>2^32-1).
  73. // MAKE_EDGE_HASH() uses temporaries, so INIT_EDGE_HASH() needs to be put
  74. // at the head of every function which is about to use MAKE_EDGE_HASH().
  75. // Reason is that the hash is that hash construction needs to hold the
  76. // invariant id0<id1 to identify an edge - else two hashes would refer
  77. // to the same edge.
  78. // ---------------------------------------------------------------------------
  79. #define MAKE_EDGE_HASH(id0, id1) (eh_tmp0__ = id0, eh_tmp1__ = id1, \
  80. (eh_tmp0__ < eh_tmp1__ ? std::swap(eh_tmp0__, eh_tmp1__) : mydummy()), (uint64_t)eh_tmp0__ ^ ((uint64_t)eh_tmp1__ << 32u))
  81. #define INIT_EDGE_HASH_TEMPORARIES() \
  82. unsigned int eh_tmp0__, eh_tmp1__;
  83. private:
  84. void InternSubdivide(const aiMesh *const *smesh,
  85. size_t nmesh, aiMesh **out, unsigned int num);
  86. };
  87. // ------------------------------------------------------------------------------------------------
  88. // Construct a subdivider of a specific type
  89. Subdivider *Subdivider::Create(Algorithm algo) {
  90. switch (algo) {
  91. case CATMULL_CLARKE:
  92. return new CatmullClarkSubdivider();
  93. };
  94. ai_assert(false);
  95. return nullptr; // shouldn't happen
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // Call the Catmull Clark subdivision algorithm for one mesh
  99. void CatmullClarkSubdivider::Subdivide(
  100. aiMesh *mesh,
  101. aiMesh *&out,
  102. unsigned int num,
  103. bool discard_input) {
  104. ai_assert(mesh != out);
  105. Subdivide(&mesh, 1, &out, num, discard_input);
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. // Call the Catmull Clark subdivision algorithm for multiple meshes
  109. void CatmullClarkSubdivider::Subdivide(
  110. aiMesh **smesh,
  111. size_t nmesh,
  112. aiMesh **out,
  113. unsigned int num,
  114. bool discard_input) {
  115. ai_assert(nullptr != smesh);
  116. ai_assert(nullptr != out);
  117. // course, both regions may not overlap
  118. ai_assert(smesh < out || smesh + nmesh > out + nmesh);
  119. if (!num) {
  120. // No subdivision at all. Need to copy all the meshes .. argh.
  121. if (discard_input) {
  122. for (size_t s = 0; s < nmesh; ++s) {
  123. out[s] = smesh[s];
  124. smesh[s] = nullptr;
  125. }
  126. } else {
  127. for (size_t s = 0; s < nmesh; ++s) {
  128. SceneCombiner::Copy(out + s, smesh[s]);
  129. }
  130. }
  131. return;
  132. }
  133. std::vector<aiMesh *> inmeshes;
  134. std::vector<aiMesh *> outmeshes;
  135. std::vector<unsigned int> maptbl;
  136. inmeshes.reserve(nmesh);
  137. outmeshes.reserve(nmesh);
  138. maptbl.reserve(nmesh);
  139. // Remove pure line and point meshes from the working set to reduce the
  140. // number of edge cases the subdivider is forced to deal with. Line and
  141. // point meshes are simply passed through.
  142. for (size_t s = 0; s < nmesh; ++s) {
  143. aiMesh *i = smesh[s];
  144. // FIX - mPrimitiveTypes might not yet be initialized
  145. if (i->mPrimitiveTypes && (i->mPrimitiveTypes & (aiPrimitiveType_LINE | aiPrimitiveType_POINT)) == i->mPrimitiveTypes) {
  146. ASSIMP_LOG_VERBOSE_DEBUG("Catmull-Clark Subdivider: Skipping pure line/point mesh");
  147. if (discard_input) {
  148. out[s] = i;
  149. smesh[s] = nullptr;
  150. } else {
  151. SceneCombiner::Copy(out + s, i);
  152. }
  153. continue;
  154. }
  155. outmeshes.push_back(nullptr);
  156. inmeshes.push_back(i);
  157. maptbl.push_back(static_cast<unsigned int>(s));
  158. }
  159. // Do the actual subdivision on the preallocated storage. InternSubdivide
  160. // *always* assumes that enough storage is available, it does not bother
  161. // checking any ranges.
  162. ai_assert(inmeshes.size() == outmeshes.size());
  163. ai_assert(inmeshes.size() == maptbl.size());
  164. if (inmeshes.empty()) {
  165. ASSIMP_LOG_WARN("Catmull-Clark Subdivider: Pure point/line scene, I can't do anything");
  166. return;
  167. }
  168. InternSubdivide(&inmeshes.front(), inmeshes.size(), &outmeshes.front(), num);
  169. for (unsigned int i = 0; i < maptbl.size(); ++i) {
  170. ai_assert(nullptr != outmeshes[i]);
  171. out[maptbl[i]] = outmeshes[i];
  172. }
  173. if (discard_input) {
  174. for (size_t s = 0; s < nmesh; ++s) {
  175. delete smesh[s];
  176. }
  177. }
  178. }
  179. // ------------------------------------------------------------------------------------------------
  180. // Note - this is an implementation of the standard (recursive) Cm-Cl algorithm without further
  181. // optimizations (except we're using some nice LUTs). A description of the algorithm can be found
  182. // here: http://en.wikipedia.org/wiki/Catmull-Clark_subdivision_surface
  183. //
  184. // The code is mostly O(n), however parts are O(nlogn) which is therefore the algorithm's
  185. // expected total runtime complexity. The implementation is able to work in-place on the same
  186. // mesh arrays. Calling #InternSubdivide() directly is not encouraged. The code can operate
  187. // in-place unless 'smesh' and 'out' are equal (no strange overlaps or reorderings).
  188. // Previous data is replaced/deleted then.
  189. // ------------------------------------------------------------------------------------------------
  190. void CatmullClarkSubdivider::InternSubdivide(
  191. const aiMesh *const *smesh,
  192. size_t nmesh,
  193. aiMesh **out,
  194. unsigned int num) {
  195. ai_assert(nullptr != smesh);
  196. ai_assert(nullptr != out);
  197. INIT_EDGE_HASH_TEMPORARIES();
  198. // no subdivision requested or end of recursive refinement
  199. if (!num) {
  200. return;
  201. }
  202. UIntVector maptbl;
  203. SpatialSort spatial;
  204. // ---------------------------------------------------------------------
  205. // 0. Offset table to index all meshes continuously, generate a spatially
  206. // sorted representation of all vertices in all meshes.
  207. // ---------------------------------------------------------------------
  208. typedef std::pair<unsigned int, unsigned int> IntPair;
  209. std::vector<IntPair> moffsets(nmesh);
  210. unsigned int totfaces = 0, totvert = 0;
  211. for (size_t t = 0; t < nmesh; ++t) {
  212. const aiMesh *mesh = smesh[t];
  213. spatial.Append(mesh->mVertices, mesh->mNumVertices, sizeof(aiVector3D), false);
  214. moffsets[t] = IntPair(totfaces, totvert);
  215. totfaces += mesh->mNumFaces;
  216. totvert += mesh->mNumVertices;
  217. }
  218. spatial.Finalize();
  219. const unsigned int num_unique = spatial.GenerateMappingTable(maptbl, ComputePositionEpsilon(smesh, nmesh));
  220. #define FLATTEN_VERTEX_IDX(mesh_idx, vert_idx) (moffsets[mesh_idx].second + vert_idx)
  221. #define FLATTEN_FACE_IDX(mesh_idx, face_idx) (moffsets[mesh_idx].first + face_idx)
  222. // ---------------------------------------------------------------------
  223. // 1. Compute the centroid point for all faces
  224. // ---------------------------------------------------------------------
  225. std::vector<Vertex> centroids(totfaces);
  226. unsigned int nfacesout = 0;
  227. for (size_t t = 0, n = 0; t < nmesh; ++t) {
  228. const aiMesh *mesh = smesh[t];
  229. for (unsigned int i = 0; i < mesh->mNumFaces; ++i, ++n) {
  230. const aiFace &face = mesh->mFaces[i];
  231. Vertex &c = centroids[n];
  232. for (unsigned int a = 0; a < face.mNumIndices; ++a) {
  233. c += Vertex(mesh, face.mIndices[a]);
  234. }
  235. c /= static_cast<float>(face.mNumIndices);
  236. nfacesout += face.mNumIndices;
  237. }
  238. }
  239. {
  240. // we want edges to go away before the recursive calls so begin a new scope
  241. EdgeMap edges;
  242. // ---------------------------------------------------------------------
  243. // 2. Set each edge point to be the average of all neighbouring
  244. // face points and original points. Every edge exists twice
  245. // if there is a neighboring face.
  246. // ---------------------------------------------------------------------
  247. for (size_t t = 0; t < nmesh; ++t) {
  248. const aiMesh *mesh = smesh[t];
  249. for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
  250. const aiFace &face = mesh->mFaces[i];
  251. for (unsigned int p = 0; p < face.mNumIndices; ++p) {
  252. const unsigned int id[] = {
  253. face.mIndices[p],
  254. face.mIndices[p == face.mNumIndices - 1 ? 0 : p + 1]
  255. };
  256. const unsigned int mp[] = {
  257. maptbl[FLATTEN_VERTEX_IDX(t, id[0])],
  258. maptbl[FLATTEN_VERTEX_IDX(t, id[1])]
  259. };
  260. Edge &e = edges[MAKE_EDGE_HASH(mp[0], mp[1])];
  261. e.ref++;
  262. if (e.ref <= 2) {
  263. if (e.ref == 1) { // original points (end points) - add only once
  264. e.edge_point = e.midpoint = Vertex(mesh, id[0]) + Vertex(mesh, id[1]);
  265. e.midpoint *= 0.5f;
  266. }
  267. e.edge_point += centroids[FLATTEN_FACE_IDX(t, i)];
  268. }
  269. }
  270. }
  271. }
  272. // ---------------------------------------------------------------------
  273. // 3. Normalize edge points
  274. // ---------------------------------------------------------------------
  275. {
  276. unsigned int bad_cnt = 0;
  277. for (EdgeMap::iterator it = edges.begin(); it != edges.end(); ++it) {
  278. if ((*it).second.ref < 2) {
  279. ai_assert((*it).second.ref);
  280. ++bad_cnt;
  281. }
  282. (*it).second.edge_point *= 1.f / ((*it).second.ref + 2.f);
  283. }
  284. if (bad_cnt) {
  285. // Report the number of bad edges. bad edges are referenced by less than two
  286. // faces in the mesh. They occur at outer model boundaries in non-closed
  287. // shapes.
  288. ASSIMP_LOG_VERBOSE_DEBUG("Catmull-Clark Subdivider: got ", bad_cnt, " bad edges touching only one face (totally ",
  289. static_cast<unsigned int>(edges.size()), " edges). ");
  290. }
  291. }
  292. // ---------------------------------------------------------------------
  293. // 4. Compute a vertex-face adjacency table. We can't reuse the code
  294. // from VertexTriangleAdjacency because we need the table for multiple
  295. // meshes and out vertex indices need to be mapped to distinct values
  296. // first.
  297. // ---------------------------------------------------------------------
  298. UIntVector faceadjac(nfacesout), cntadjfac(maptbl.size(), 0), ofsadjvec(maptbl.size() + 1, 0);
  299. {
  300. for (size_t t = 0; t < nmesh; ++t) {
  301. const aiMesh *const minp = smesh[t];
  302. for (unsigned int i = 0; i < minp->mNumFaces; ++i) {
  303. const aiFace &f = minp->mFaces[i];
  304. for (unsigned int n = 0; n < f.mNumIndices; ++n) {
  305. ++cntadjfac[maptbl[FLATTEN_VERTEX_IDX(t, f.mIndices[n])]];
  306. }
  307. }
  308. }
  309. unsigned int cur = 0;
  310. for (size_t i = 0; i < cntadjfac.size(); ++i) {
  311. ofsadjvec[i + 1] = cur;
  312. cur += cntadjfac[i];
  313. }
  314. for (size_t t = 0; t < nmesh; ++t) {
  315. const aiMesh *const minp = smesh[t];
  316. for (unsigned int i = 0; i < minp->mNumFaces; ++i) {
  317. const aiFace &f = minp->mFaces[i];
  318. for (unsigned int n = 0; n < f.mNumIndices; ++n) {
  319. faceadjac[ofsadjvec[1 + maptbl[FLATTEN_VERTEX_IDX(t, f.mIndices[n])]]++] = FLATTEN_FACE_IDX(t, i);
  320. }
  321. }
  322. }
  323. // check the other way round for consistency
  324. #ifdef ASSIMP_BUILD_DEBUG
  325. for (size_t t = 0; t < ofsadjvec.size() - 1; ++t) {
  326. for (unsigned int m = 0; m < cntadjfac[t]; ++m) {
  327. const unsigned int fidx = faceadjac[ofsadjvec[t] + m];
  328. ai_assert(fidx < totfaces);
  329. for (size_t n = 1; n < nmesh; ++n) {
  330. if (moffsets[n].first > fidx) {
  331. const aiMesh *msh = smesh[--n];
  332. const aiFace &f = msh->mFaces[fidx - moffsets[n].first];
  333. bool haveit = false;
  334. for (unsigned int i = 0; i < f.mNumIndices; ++i) {
  335. if (maptbl[FLATTEN_VERTEX_IDX(n, f.mIndices[i])] == (unsigned int)t) {
  336. haveit = true;
  337. break;
  338. }
  339. }
  340. ai_assert(haveit);
  341. if (!haveit) {
  342. ASSIMP_LOG_VERBOSE_DEBUG("Catmull-Clark Subdivider: Index not used");
  343. }
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. #endif
  350. }
  351. #define GET_ADJACENT_FACES_AND_CNT(vidx, fstartout, numout) \
  352. fstartout = &faceadjac[ofsadjvec[vidx]], numout = cntadjfac[vidx]
  353. typedef std::pair<bool, Vertex> TouchedOVertex;
  354. std::vector<TouchedOVertex> new_points(num_unique, TouchedOVertex(false, Vertex()));
  355. // ---------------------------------------------------------------------
  356. // 5. Spawn a quad from each face point to the corresponding edge points
  357. // the original points being the fourth quad points.
  358. // ---------------------------------------------------------------------
  359. for (size_t t = 0; t < nmesh; ++t) {
  360. const aiMesh *const minp = smesh[t];
  361. aiMesh *const mout = out[t] = new aiMesh();
  362. for (unsigned int a = 0; a < minp->mNumFaces; ++a) {
  363. mout->mNumFaces += minp->mFaces[a].mNumIndices;
  364. }
  365. // We need random access to the old face buffer, so reuse is not possible.
  366. mout->mFaces = new aiFace[mout->mNumFaces];
  367. mout->mNumVertices = mout->mNumFaces * 4;
  368. mout->mVertices = new aiVector3D[mout->mNumVertices];
  369. // quads only, keep material index
  370. mout->mPrimitiveTypes = aiPrimitiveType_POLYGON;
  371. mout->mMaterialIndex = minp->mMaterialIndex;
  372. if (minp->HasNormals()) {
  373. mout->mNormals = new aiVector3D[mout->mNumVertices];
  374. }
  375. if (minp->HasTangentsAndBitangents()) {
  376. mout->mTangents = new aiVector3D[mout->mNumVertices];
  377. mout->mBitangents = new aiVector3D[mout->mNumVertices];
  378. }
  379. for (unsigned int i = 0; minp->HasTextureCoords(i); ++i) {
  380. mout->mTextureCoords[i] = new aiVector3D[mout->mNumVertices];
  381. mout->mNumUVComponents[i] = minp->mNumUVComponents[i];
  382. }
  383. for (unsigned int i = 0; minp->HasVertexColors(i); ++i) {
  384. mout->mColors[i] = new aiColor4D[mout->mNumVertices];
  385. }
  386. mout->mNumVertices = mout->mNumFaces << 2u;
  387. for (unsigned int i = 0, v = 0, n = 0; i < minp->mNumFaces; ++i) {
  388. const aiFace &face = minp->mFaces[i];
  389. for (unsigned int a = 0; a < face.mNumIndices; ++a) {
  390. // Get a clean new face.
  391. aiFace &faceOut = mout->mFaces[n++];
  392. faceOut.mIndices = new unsigned int[faceOut.mNumIndices = 4];
  393. // Spawn a new quadrilateral (ccw winding) for this original point between:
  394. // a) face centroid
  395. centroids[FLATTEN_FACE_IDX(t, i)].SortBack(mout, faceOut.mIndices[0] = v++);
  396. // b) adjacent edge on the left, seen from the centroid
  397. const Edge &e0 = edges[MAKE_EDGE_HASH(maptbl[FLATTEN_VERTEX_IDX(t, face.mIndices[a])],
  398. maptbl[FLATTEN_VERTEX_IDX(t, face.mIndices[a == face.mNumIndices - 1 ? 0 : a + 1])])]; // fixme: replace with mod face.mNumIndices?
  399. // c) adjacent edge on the right, seen from the centroid
  400. const Edge &e1 = edges[MAKE_EDGE_HASH(maptbl[FLATTEN_VERTEX_IDX(t, face.mIndices[a])],
  401. maptbl[FLATTEN_VERTEX_IDX(t, face.mIndices[!a ? face.mNumIndices - 1 : a - 1])])]; // fixme: replace with mod face.mNumIndices?
  402. e0.edge_point.SortBack(mout, faceOut.mIndices[3] = v++);
  403. e1.edge_point.SortBack(mout, faceOut.mIndices[1] = v++);
  404. // d= original point P with distinct index i
  405. // F := 0
  406. // R := 0
  407. // n := 0
  408. // for each face f containing i
  409. // F := F+ centroid of f
  410. // R := R+ midpoint of edge of f from i to i+1
  411. // n := n+1
  412. //
  413. // (F+2R+(n-3)P)/n
  414. const unsigned int org = maptbl[FLATTEN_VERTEX_IDX(t, face.mIndices[a])];
  415. TouchedOVertex &ov = new_points[org];
  416. if (!ov.first) {
  417. ov.first = true;
  418. const unsigned int *adj;
  419. unsigned int cnt;
  420. GET_ADJACENT_FACES_AND_CNT(org, adj, cnt);
  421. if (cnt < 3) {
  422. ov.second = Vertex(minp, face.mIndices[a]);
  423. } else {
  424. Vertex F, R;
  425. for (unsigned int o = 0; o < cnt; ++o) {
  426. ai_assert(adj[o] < totfaces);
  427. F += centroids[adj[o]];
  428. // adj[0] is a global face index - search the face in the mesh list
  429. const aiMesh *mp = nullptr;
  430. size_t nidx;
  431. if (adj[o] < moffsets[0].first) {
  432. mp = smesh[nidx = 0];
  433. } else {
  434. for (nidx = 1; nidx <= nmesh; ++nidx) {
  435. if (nidx == nmesh || moffsets[nidx].first > adj[o]) {
  436. mp = smesh[--nidx];
  437. break;
  438. }
  439. }
  440. }
  441. if (mp == nullptr) {
  442. continue;
  443. }
  444. ai_assert(adj[o] - moffsets[nidx].first < mp->mNumFaces);
  445. const aiFace &f = mp->mFaces[adj[o] - moffsets[nidx].first];
  446. bool haveit = false;
  447. // find our original point in the face
  448. for (unsigned int m = 0; m < f.mNumIndices; ++m) {
  449. if (maptbl[FLATTEN_VERTEX_IDX(nidx, f.mIndices[m])] == org) {
  450. // add *both* edges. this way, we can be sure that we add
  451. // *all* adjacent edges to R. In a closed shape, every
  452. // edge is added twice - so we simply leave out the
  453. // factor 2.f in the amove formula and get the right
  454. // result.
  455. const Edge &c0 = edges[MAKE_EDGE_HASH(org, maptbl[FLATTEN_VERTEX_IDX(
  456. nidx, f.mIndices[!m ? f.mNumIndices - 1 : m - 1])])];
  457. // fixme: replace with mod face.mNumIndices?
  458. const Edge &c1 = edges[MAKE_EDGE_HASH(org, maptbl[FLATTEN_VERTEX_IDX(
  459. nidx, f.mIndices[m == f.mNumIndices - 1 ? 0 : m + 1])])];
  460. // fixme: replace with mod face.mNumIndices?
  461. R += c0.midpoint + c1.midpoint;
  462. haveit = true;
  463. break;
  464. }
  465. }
  466. // this invariant *must* hold if the vertex-to-face adjacency table is valid
  467. ai_assert(haveit);
  468. if (!haveit) {
  469. ASSIMP_LOG_WARN("OBJ: no name for material library specified.");
  470. }
  471. }
  472. const float div = static_cast<float>(cnt), divsq = 1.f / (div * div);
  473. ov.second = Vertex(minp, face.mIndices[a]) * ((div - 3.f) / div) + R * divsq + F * divsq;
  474. }
  475. }
  476. ov.second.SortBack(mout, faceOut.mIndices[2] = v++);
  477. }
  478. }
  479. }
  480. } // end of scope for edges, freeing its memory
  481. // ---------------------------------------------------------------------
  482. // 7. Apply the next subdivision step.
  483. // ---------------------------------------------------------------------
  484. if (num != 1) {
  485. std::vector<aiMesh *> tmp(nmesh);
  486. InternSubdivide(out, nmesh, &tmp.front(), num - 1);
  487. for (size_t i = 0; i < nmesh; ++i) {
  488. delete out[i];
  489. out[i] = tmp[i];
  490. }
  491. }
  492. }