TriangulateProcess.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file TriangulateProcess.cpp
  35. * @brief Implementation of the post processing step to split up
  36. * all faces with more than three indices into triangles.
  37. *
  38. *
  39. * The triangulation algorithm will handle concave or convex polygons.
  40. * Self-intersecting or non-planar polygons are not rejected, but
  41. * they're probably not triangulated correctly.
  42. *
  43. * DEBUG SWITCHES - do not enable any of them in release builds:
  44. *
  45. * AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
  46. * - generates vertex colors to represent the face winding order.
  47. * the first vertex of a polygon becomes red, the last blue.
  48. * AI_BUILD_TRIANGULATE_DEBUG_POLYS
  49. * - dump all polygons and their triangulation sequences to
  50. * a file
  51. */
  52. #ifndef ASSIMP_BUILD_NO_TRIANGULATE_PROCESS
  53. #include "PostProcessing/TriangulateProcess.h"
  54. #include "PostProcessing/ProcessHelper.h"
  55. #include "Common/PolyTools.h"
  56. #include <memory>
  57. #include <cstdint>
  58. //#define AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
  59. //#define AI_BUILD_TRIANGULATE_DEBUG_POLYS
  60. #define POLY_GRID_Y 40
  61. #define POLY_GRID_X 70
  62. #define POLY_GRID_XPAD 20
  63. #define POLY_OUTPUT_FILE "assimp_polygons_debug.txt"
  64. using namespace Assimp;
  65. namespace {
  66. /**
  67. * @brief Helper struct used to simplify NGON encoding functions.
  68. */
  69. struct NGONEncoder {
  70. NGONEncoder() : mLastNGONFirstIndex((unsigned int)-1) {}
  71. /**
  72. * @brief Encode the current triangle, and make sure it is recognized as a triangle.
  73. *
  74. * This method will rotate indices in tri if needed in order to avoid tri to be considered
  75. * part of the previous ngon. This method is to be used whenever you want to emit a real triangle,
  76. * and make sure it is seen as a triangle.
  77. *
  78. * @param tri Triangle to encode.
  79. */
  80. void ngonEncodeTriangle(aiFace * tri) {
  81. ai_assert(tri->mNumIndices == 3);
  82. // Rotate indices in new triangle to avoid ngon encoding false ngons
  83. // Otherwise, the new triangle would be considered part of the previous NGON.
  84. if (isConsideredSameAsLastNgon(tri)) {
  85. std::swap(tri->mIndices[0], tri->mIndices[2]);
  86. std::swap(tri->mIndices[1], tri->mIndices[2]);
  87. }
  88. mLastNGONFirstIndex = tri->mIndices[0];
  89. }
  90. /**
  91. * @brief Encode a quad (2 triangles) in ngon encoding, and make sure they are seen as a single ngon.
  92. *
  93. * @param tri1 First quad triangle
  94. * @param tri2 Second quad triangle
  95. *
  96. * @pre Triangles must be properly fanned from the most appropriate vertex.
  97. */
  98. void ngonEncodeQuad(aiFace *tri1, aiFace *tri2) {
  99. ai_assert(tri1->mNumIndices == 3);
  100. ai_assert(tri2->mNumIndices == 3);
  101. ai_assert(tri1->mIndices[0] == tri2->mIndices[0]);
  102. // If the selected fanning vertex is the same as the previously
  103. // emitted ngon, we use the opposite vertex which also happens to work
  104. // for tri-fanning a concave quad.
  105. // ref: https://github.com/assimp/assimp/pull/3695#issuecomment-805999760
  106. if (isConsideredSameAsLastNgon(tri1)) {
  107. // Right-rotate indices for tri1 (index 2 becomes the new fanning vertex)
  108. std::swap(tri1->mIndices[0], tri1->mIndices[2]);
  109. std::swap(tri1->mIndices[1], tri1->mIndices[2]);
  110. // Left-rotate indices for tri2 (index 2 becomes the new fanning vertex)
  111. std::swap(tri2->mIndices[1], tri2->mIndices[2]);
  112. std::swap(tri2->mIndices[0], tri2->mIndices[2]);
  113. ai_assert(tri1->mIndices[0] == tri2->mIndices[0]);
  114. }
  115. mLastNGONFirstIndex = tri1->mIndices[0];
  116. }
  117. /**
  118. * @brief Check whether this triangle would be considered part of the lastly emitted ngon or not.
  119. *
  120. * @param tri Current triangle.
  121. * @return true If used as is, this triangle will be part of last ngon.
  122. * @return false If used as is, this triangle is not considered part of the last ngon.
  123. */
  124. bool isConsideredSameAsLastNgon(const aiFace * tri) const {
  125. ai_assert(tri->mNumIndices == 3);
  126. return tri->mIndices[0] == mLastNGONFirstIndex;
  127. }
  128. private:
  129. unsigned int mLastNGONFirstIndex;
  130. };
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. // Returns whether the processing step is present in the given flag field.
  134. bool TriangulateProcess::IsActive( unsigned int pFlags) const {
  135. return (pFlags & aiProcess_Triangulate) != 0;
  136. }
  137. // ------------------------------------------------------------------------------------------------
  138. // Executes the post processing step on the given imported data.
  139. void TriangulateProcess::Execute( aiScene* pScene) {
  140. ASSIMP_LOG_DEBUG("TriangulateProcess begin");
  141. bool bHas = false;
  142. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  143. {
  144. if (pScene->mMeshes[ a ]) {
  145. if ( TriangulateMesh( pScene->mMeshes[ a ] ) ) {
  146. bHas = true;
  147. }
  148. }
  149. }
  150. if ( bHas ) {
  151. ASSIMP_LOG_INFO( "TriangulateProcess finished. All polygons have been triangulated." );
  152. } else {
  153. ASSIMP_LOG_DEBUG( "TriangulateProcess finished. There was nothing to be done." );
  154. }
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. // Triangulates the given mesh.
  158. bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh) {
  159. // Now we have aiMesh::mPrimitiveTypes, so this is only here for test cases
  160. if (!pMesh->mPrimitiveTypes) {
  161. bool bNeed = false;
  162. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  163. const aiFace& face = pMesh->mFaces[a];
  164. if( face.mNumIndices != 3) {
  165. bNeed = true;
  166. }
  167. }
  168. if (!bNeed)
  169. return false;
  170. }
  171. else if (!(pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON)) {
  172. return false;
  173. }
  174. // Find out how many output faces we'll get
  175. uint32_t numOut = 0, max_out = 0;
  176. bool get_normals = true;
  177. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  178. aiFace& face = pMesh->mFaces[a];
  179. if (face.mNumIndices <= 4) {
  180. get_normals = false;
  181. }
  182. if( face.mNumIndices <= 3) {
  183. numOut++;
  184. } else {
  185. numOut += face.mNumIndices-2;
  186. max_out = std::max(max_out,face.mNumIndices);
  187. }
  188. }
  189. // Just another check whether aiMesh::mPrimitiveTypes is correct
  190. ai_assert(numOut != pMesh->mNumFaces);
  191. aiVector3D *nor_out = nullptr;
  192. // if we don't have normals yet, but expect them to be a cheap side
  193. // product of triangulation anyway, allocate storage for them.
  194. if (!pMesh->mNormals && get_normals) {
  195. // XXX need a mechanism to inform the GenVertexNormals process to treat these normals as preprocessed per-face normals
  196. // nor_out = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  197. }
  198. // the output mesh will contain triangles, but no polys anymore
  199. pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  200. pMesh->mPrimitiveTypes &= ~aiPrimitiveType_POLYGON;
  201. // The mesh becomes NGON encoded now, during the triangulation process.
  202. pMesh->mPrimitiveTypes |= aiPrimitiveType_NGONEncodingFlag;
  203. aiFace* out = new aiFace[numOut](), *curOut = out;
  204. std::vector<aiVector3D> temp_verts3d(max_out+2); /* temporary storage for vertices */
  205. std::vector<aiVector2D> temp_verts(max_out+2);
  206. NGONEncoder ngonEncoder;
  207. // Apply vertex colors to represent the face winding?
  208. #ifdef AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
  209. if (!pMesh->mColors[0])
  210. pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
  211. else
  212. new(pMesh->mColors[0]) aiColor4D[pMesh->mNumVertices];
  213. aiColor4D* clr = pMesh->mColors[0];
  214. #endif
  215. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  216. FILE* fout = fopen(POLY_OUTPUT_FILE,"a");
  217. #endif
  218. const aiVector3D* verts = pMesh->mVertices;
  219. // use std::unique_ptr to avoid slow std::vector<bool> specialiations
  220. std::unique_ptr<bool[]> done(new bool[max_out]);
  221. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  222. aiFace& face = pMesh->mFaces[a];
  223. unsigned int* idx = face.mIndices;
  224. int num = (int)face.mNumIndices, ear = 0, tmp, prev = num-1, next = 0, max = num;
  225. // Apply vertex colors to represent the face winding?
  226. #ifdef AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
  227. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  228. aiColor4D& c = clr[idx[i]];
  229. c.r = (i+1) / (float)max;
  230. c.b = 1.f - c.r;
  231. }
  232. #endif
  233. aiFace* const last_face = curOut;
  234. // if it's a simple point,line or triangle: just copy it
  235. if( face.mNumIndices <= 3)
  236. {
  237. aiFace& nface = *curOut++;
  238. nface.mNumIndices = face.mNumIndices;
  239. nface.mIndices = face.mIndices;
  240. face.mIndices = nullptr;
  241. // points and lines don't require ngon encoding (and are not supported either!)
  242. if (nface.mNumIndices == 3) ngonEncoder.ngonEncodeTriangle(&nface);
  243. continue;
  244. }
  245. // optimized code for quadrilaterals
  246. else if ( face.mNumIndices == 4) {
  247. // quads can have at maximum one concave vertex. Determine
  248. // this vertex (if it exists) and start tri-fanning from
  249. // it.
  250. unsigned int start_vertex = 0;
  251. for (unsigned int i = 0; i < 4; ++i) {
  252. const aiVector3D& v0 = verts[face.mIndices[(i+3) % 4]];
  253. const aiVector3D& v1 = verts[face.mIndices[(i+2) % 4]];
  254. const aiVector3D& v2 = verts[face.mIndices[(i+1) % 4]];
  255. const aiVector3D& v = verts[face.mIndices[i]];
  256. aiVector3D left = (v0-v);
  257. aiVector3D diag = (v1-v);
  258. aiVector3D right = (v2-v);
  259. left.Normalize();
  260. diag.Normalize();
  261. right.Normalize();
  262. const float angle = std::acos(left*diag) + std::acos(right*diag);
  263. if (angle > AI_MATH_PI_F) {
  264. // this is the concave point
  265. start_vertex = i;
  266. break;
  267. }
  268. }
  269. const unsigned int temp[] = {face.mIndices[0], face.mIndices[1], face.mIndices[2], face.mIndices[3]};
  270. aiFace& nface = *curOut++;
  271. nface.mNumIndices = 3;
  272. nface.mIndices = face.mIndices;
  273. nface.mIndices[0] = temp[start_vertex];
  274. nface.mIndices[1] = temp[(start_vertex + 1) % 4];
  275. nface.mIndices[2] = temp[(start_vertex + 2) % 4];
  276. aiFace& sface = *curOut++;
  277. sface.mNumIndices = 3;
  278. sface.mIndices = new unsigned int[3];
  279. sface.mIndices[0] = temp[start_vertex];
  280. sface.mIndices[1] = temp[(start_vertex + 2) % 4];
  281. sface.mIndices[2] = temp[(start_vertex + 3) % 4];
  282. // prevent double deletion of the indices field
  283. face.mIndices = nullptr;
  284. ngonEncoder.ngonEncodeQuad(&nface, &sface);
  285. continue;
  286. }
  287. else
  288. {
  289. // A polygon with more than 3 vertices can be either concave or convex.
  290. // Usually everything we're getting is convex and we could easily
  291. // triangulate by tri-fanning. However, LightWave is probably the only
  292. // modeling suite to make extensive use of highly concave, monster polygons ...
  293. // so we need to apply the full 'ear cutting' algorithm to get it right.
  294. // REQUIREMENT: polygon is expected to be simple and *nearly* planar.
  295. // We project it onto a plane to get a 2d triangle.
  296. // Collect all vertices of of the polygon.
  297. for (tmp = 0; tmp < max; ++tmp) {
  298. temp_verts3d[tmp] = verts[idx[tmp]];
  299. }
  300. // Get newell normal of the polygon. Store it for future use if it's a polygon-only mesh
  301. aiVector3D n;
  302. NewellNormal<3,3,3>(n,max,&temp_verts3d.front().x,&temp_verts3d.front().y,&temp_verts3d.front().z);
  303. if (nor_out) {
  304. for (tmp = 0; tmp < max; ++tmp)
  305. nor_out[idx[tmp]] = n;
  306. }
  307. // Select largest normal coordinate to ignore for projection
  308. const float ax = (n.x>0 ? n.x : -n.x);
  309. const float ay = (n.y>0 ? n.y : -n.y);
  310. const float az = (n.z>0 ? n.z : -n.z);
  311. unsigned int ac = 0, bc = 1; /* no z coord. projection to xy */
  312. float inv = n.z;
  313. if (ax > ay) {
  314. if (ax > az) { /* no x coord. projection to yz */
  315. ac = 1; bc = 2;
  316. inv = n.x;
  317. }
  318. }
  319. else if (ay > az) { /* no y coord. projection to zy */
  320. ac = 2; bc = 0;
  321. inv = n.y;
  322. }
  323. // Swap projection axes to take the negated projection vector into account
  324. if (inv < 0.f) {
  325. std::swap(ac,bc);
  326. }
  327. for (tmp =0; tmp < max; ++tmp) {
  328. temp_verts[tmp].x = verts[idx[tmp]][ac];
  329. temp_verts[tmp].y = verts[idx[tmp]][bc];
  330. done[tmp] = false;
  331. }
  332. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  333. // plot the plane onto which we mapped the polygon to a 2D ASCII pic
  334. aiVector2D bmin,bmax;
  335. ArrayBounds(&temp_verts[0],max,bmin,bmax);
  336. char grid[POLY_GRID_Y][POLY_GRID_X+POLY_GRID_XPAD];
  337. std::fill_n((char*)grid,POLY_GRID_Y*(POLY_GRID_X+POLY_GRID_XPAD),' ');
  338. for (int i =0; i < max; ++i) {
  339. const aiVector2D& v = (temp_verts[i] - bmin) / (bmax-bmin);
  340. const size_t x = static_cast<size_t>(v.x*(POLY_GRID_X-1)), y = static_cast<size_t>(v.y*(POLY_GRID_Y-1));
  341. char* loc = grid[y]+x;
  342. if (grid[y][x] != ' ') {
  343. for(;*loc != ' '; ++loc);
  344. *loc++ = '_';
  345. }
  346. *(loc+::ai_snprintf(loc, POLY_GRID_XPAD,"%i",i)) = ' ';
  347. }
  348. for(size_t y = 0; y < POLY_GRID_Y; ++y) {
  349. grid[y][POLY_GRID_X+POLY_GRID_XPAD-1] = '\0';
  350. fprintf(fout,"%s\n",grid[y]);
  351. }
  352. fprintf(fout,"\ntriangulation sequence: ");
  353. #endif
  354. //
  355. // FIXME: currently this is the slow O(kn) variant with a worst case
  356. // complexity of O(n^2) (I think). Can be done in O(n).
  357. while (num > 3) {
  358. // Find the next ear of the polygon
  359. int num_found = 0;
  360. for (ear = next;;prev = ear,ear = next) {
  361. // break after we looped two times without a positive match
  362. for (next=ear+1;done[(next>=max?next=0:next)];++next);
  363. if (next < ear) {
  364. if (++num_found == 2) {
  365. break;
  366. }
  367. }
  368. const aiVector2D* pnt1 = &temp_verts[ear],
  369. *pnt0 = &temp_verts[prev],
  370. *pnt2 = &temp_verts[next];
  371. // Must be a convex point. Assuming ccw winding, it must be on the right of the line between p-1 and p+1.
  372. if (OnLeftSideOfLine2D(*pnt0,*pnt2,*pnt1) == 1) {
  373. continue;
  374. }
  375. // Skip when three point is in a line
  376. aiVector2D left = *pnt0 - *pnt1;
  377. aiVector2D right = *pnt2 - *pnt1;
  378. left.Normalize();
  379. right.Normalize();
  380. auto mul = left * right;
  381. // if the angle is 0 or 180
  382. if (std::abs(mul - 1.f) < ai_epsilon || std::abs(mul + 1.f) < ai_epsilon) {
  383. // skip this ear
  384. ASSIMP_LOG_WARN("Skip a ear, due to its angle is near 0 or 180.");
  385. continue;
  386. }
  387. // and no other point may be contained in this triangle
  388. for ( tmp = 0; tmp < max; ++tmp) {
  389. // We need to compare the actual values because it's possible that multiple indexes in
  390. // the polygon are referring to the same position. concave_polygon.obj is a sample
  391. //
  392. // FIXME: Use 'epsiloned' comparisons instead? Due to numeric inaccuracies in
  393. // PointInTriangle() I'm guessing that it's actually possible to construct
  394. // input data that would cause us to end up with no ears. The problem is,
  395. // which epsilon? If we chose a too large value, we'd get wrong results
  396. const aiVector2D& vtmp = temp_verts[tmp];
  397. if ( vtmp != *pnt1 && vtmp != *pnt2 && vtmp != *pnt0 && PointInTriangle2D(*pnt0,*pnt1,*pnt2,vtmp)) {
  398. break;
  399. }
  400. }
  401. if (tmp != max) {
  402. continue;
  403. }
  404. // this vertex is an ear
  405. break;
  406. }
  407. if (num_found == 2) {
  408. // Due to the 'two ear theorem', every simple polygon with more than three points must
  409. // have 2 'ears'. Here's definitely something wrong ... but we don't give up yet.
  410. //
  411. // Instead we're continuing with the standard tri-fanning algorithm which we'd
  412. // use if we had only convex polygons. That's life.
  413. ASSIMP_LOG_ERROR("Failed to triangulate polygon (no ear found). Probably not a simple polygon?");
  414. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  415. fprintf(fout,"critical error here, no ear found! ");
  416. #endif
  417. num = 0;
  418. break;
  419. }
  420. aiFace& nface = *curOut++;
  421. nface.mNumIndices = 3;
  422. if (!nface.mIndices) {
  423. nface.mIndices = new unsigned int[3];
  424. }
  425. // setup indices for the new triangle ...
  426. nface.mIndices[0] = prev;
  427. nface.mIndices[1] = ear;
  428. nface.mIndices[2] = next;
  429. // exclude the ear from most further processing
  430. done[ear] = true;
  431. --num;
  432. }
  433. if (num > 0) {
  434. // We have three indices forming the last 'ear' remaining. Collect them.
  435. aiFace& nface = *curOut++;
  436. nface.mNumIndices = 3;
  437. if (!nface.mIndices) {
  438. nface.mIndices = new unsigned int[3];
  439. }
  440. for (tmp = 0; done[tmp]; ++tmp);
  441. nface.mIndices[0] = tmp;
  442. for (++tmp; done[tmp]; ++tmp);
  443. nface.mIndices[1] = tmp;
  444. for (++tmp; done[tmp]; ++tmp);
  445. nface.mIndices[2] = tmp;
  446. }
  447. }
  448. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  449. for(aiFace* f = last_face; f != curOut; ++f) {
  450. unsigned int* i = f->mIndices;
  451. fprintf(fout," (%i %i %i)",i[0],i[1],i[2]);
  452. }
  453. fprintf(fout,"\n*********************************************************************\n");
  454. fflush(fout);
  455. #endif
  456. for(aiFace* f = last_face; f != curOut; ) {
  457. unsigned int* i = f->mIndices;
  458. i[0] = idx[i[0]];
  459. i[1] = idx[i[1]];
  460. i[2] = idx[i[2]];
  461. // IMPROVEMENT: Polygons are not supported yet by this ngon encoding + triangulation step.
  462. // So we encode polygons as regular triangles. No way to reconstruct the original
  463. // polygon in this case.
  464. ngonEncoder.ngonEncodeTriangle(f);
  465. ++f;
  466. }
  467. delete[] face.mIndices;
  468. face.mIndices = nullptr;
  469. }
  470. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  471. fclose(fout);
  472. #endif
  473. // kill the old faces
  474. delete [] pMesh->mFaces;
  475. // ... and store the new ones
  476. pMesh->mFaces = out;
  477. pMesh->mNumFaces = (unsigned int)(curOut-out); /* not necessarily equal to numOut */
  478. return true;
  479. }
  480. #endif // !! ASSIMP_BUILD_NO_TRIANGULATE_PROCESS