Subdivision.cpp 23 KB

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