選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Subdivision.cpp 20 KiB

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