Subdivision.cpp 25 KB

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