TriangulateProcess.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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. // Constructor to be privately used by Importer
  134. TriangulateProcess::TriangulateProcess()
  135. {
  136. // nothing to do here
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. // Destructor, private as well
  140. TriangulateProcess::~TriangulateProcess()
  141. {
  142. // nothing to do here
  143. }
  144. // ------------------------------------------------------------------------------------------------
  145. // Returns whether the processing step is present in the given flag field.
  146. bool TriangulateProcess::IsActive( unsigned int pFlags) const
  147. {
  148. return (pFlags & aiProcess_Triangulate) != 0;
  149. }
  150. // ------------------------------------------------------------------------------------------------
  151. // Executes the post processing step on the given imported data.
  152. void TriangulateProcess::Execute( aiScene* pScene)
  153. {
  154. ASSIMP_LOG_DEBUG("TriangulateProcess begin");
  155. bool bHas = false;
  156. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  157. {
  158. if (pScene->mMeshes[ a ]) {
  159. if ( TriangulateMesh( pScene->mMeshes[ a ] ) ) {
  160. bHas = true;
  161. }
  162. }
  163. }
  164. if ( bHas ) {
  165. ASSIMP_LOG_INFO( "TriangulateProcess finished. All polygons have been triangulated." );
  166. } else {
  167. ASSIMP_LOG_DEBUG( "TriangulateProcess finished. There was nothing to be done." );
  168. }
  169. }
  170. // ------------------------------------------------------------------------------------------------
  171. // Triangulates the given mesh.
  172. bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
  173. {
  174. // Now we have aiMesh::mPrimitiveTypes, so this is only here for test cases
  175. if (!pMesh->mPrimitiveTypes) {
  176. bool bNeed = false;
  177. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  178. const aiFace& face = pMesh->mFaces[a];
  179. if( face.mNumIndices != 3) {
  180. bNeed = true;
  181. }
  182. }
  183. if (!bNeed)
  184. return false;
  185. }
  186. else if (!(pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON)) {
  187. return false;
  188. }
  189. // Find out how many output faces we'll get
  190. uint32_t numOut = 0, max_out = 0;
  191. bool get_normals = true;
  192. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  193. aiFace& face = pMesh->mFaces[a];
  194. if (face.mNumIndices <= 4) {
  195. get_normals = false;
  196. }
  197. if( face.mNumIndices <= 3) {
  198. numOut++;
  199. }
  200. else {
  201. numOut += face.mNumIndices-2;
  202. max_out = std::max(max_out,face.mNumIndices);
  203. }
  204. }
  205. // Just another check whether aiMesh::mPrimitiveTypes is correct
  206. ai_assert(numOut != pMesh->mNumFaces);
  207. aiVector3D *nor_out = nullptr;
  208. // if we don't have normals yet, but expect them to be a cheap side
  209. // product of triangulation anyway, allocate storage for them.
  210. if (!pMesh->mNormals && get_normals) {
  211. // XXX need a mechanism to inform the GenVertexNormals process to treat these normals as preprocessed per-face normals
  212. // nor_out = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  213. }
  214. // the output mesh will contain triangles, but no polys anymore
  215. pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  216. pMesh->mPrimitiveTypes &= ~aiPrimitiveType_POLYGON;
  217. // The mesh becomes NGON encoded now, during the triangulation process.
  218. pMesh->mPrimitiveTypes |= aiPrimitiveType_NGONEncodingFlag;
  219. aiFace* out = new aiFace[numOut](), *curOut = out;
  220. std::vector<aiVector3D> temp_verts3d(max_out+2); /* temporary storage for vertices */
  221. std::vector<aiVector2D> temp_verts(max_out+2);
  222. NGONEncoder ngonEncoder;
  223. // Apply vertex colors to represent the face winding?
  224. #ifdef AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
  225. if (!pMesh->mColors[0])
  226. pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
  227. else
  228. new(pMesh->mColors[0]) aiColor4D[pMesh->mNumVertices];
  229. aiColor4D* clr = pMesh->mColors[0];
  230. #endif
  231. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  232. FILE* fout = fopen(POLY_OUTPUT_FILE,"a");
  233. #endif
  234. const aiVector3D* verts = pMesh->mVertices;
  235. // use std::unique_ptr to avoid slow std::vector<bool> specialiations
  236. std::unique_ptr<bool[]> done(new bool[max_out]);
  237. for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  238. aiFace& face = pMesh->mFaces[a];
  239. unsigned int* idx = face.mIndices;
  240. int num = (int)face.mNumIndices, ear = 0, tmp, prev = num-1, next = 0, max = num;
  241. // Apply vertex colors to represent the face winding?
  242. #ifdef AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
  243. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  244. aiColor4D& c = clr[idx[i]];
  245. c.r = (i+1) / (float)max;
  246. c.b = 1.f - c.r;
  247. }
  248. #endif
  249. aiFace* const last_face = curOut;
  250. // if it's a simple point,line or triangle: just copy it
  251. if( face.mNumIndices <= 3)
  252. {
  253. aiFace& nface = *curOut++;
  254. nface.mNumIndices = face.mNumIndices;
  255. nface.mIndices = face.mIndices;
  256. face.mIndices = nullptr;
  257. // points and lines don't require ngon encoding (and are not supported either!)
  258. if (nface.mNumIndices == 3) ngonEncoder.ngonEncodeTriangle(&nface);
  259. continue;
  260. }
  261. // optimized code for quadrilaterals
  262. else if ( face.mNumIndices == 4) {
  263. // quads can have at maximum one concave vertex. Determine
  264. // this vertex (if it exists) and start tri-fanning from
  265. // it.
  266. unsigned int start_vertex = 0;
  267. for (unsigned int i = 0; i < 4; ++i) {
  268. const aiVector3D& v0 = verts[face.mIndices[(i+3) % 4]];
  269. const aiVector3D& v1 = verts[face.mIndices[(i+2) % 4]];
  270. const aiVector3D& v2 = verts[face.mIndices[(i+1) % 4]];
  271. const aiVector3D& v = verts[face.mIndices[i]];
  272. aiVector3D left = (v0-v);
  273. aiVector3D diag = (v1-v);
  274. aiVector3D right = (v2-v);
  275. left.Normalize();
  276. diag.Normalize();
  277. right.Normalize();
  278. const float angle = std::acos(left*diag) + std::acos(right*diag);
  279. if (angle > AI_MATH_PI_F) {
  280. // this is the concave point
  281. start_vertex = i;
  282. break;
  283. }
  284. }
  285. const unsigned int temp[] = {face.mIndices[0], face.mIndices[1], face.mIndices[2], face.mIndices[3]};
  286. aiFace& nface = *curOut++;
  287. nface.mNumIndices = 3;
  288. nface.mIndices = face.mIndices;
  289. nface.mIndices[0] = temp[start_vertex];
  290. nface.mIndices[1] = temp[(start_vertex + 1) % 4];
  291. nface.mIndices[2] = temp[(start_vertex + 2) % 4];
  292. aiFace& sface = *curOut++;
  293. sface.mNumIndices = 3;
  294. sface.mIndices = new unsigned int[3];
  295. sface.mIndices[0] = temp[start_vertex];
  296. sface.mIndices[1] = temp[(start_vertex + 2) % 4];
  297. sface.mIndices[2] = temp[(start_vertex + 3) % 4];
  298. // prevent double deletion of the indices field
  299. face.mIndices = nullptr;
  300. ngonEncoder.ngonEncodeQuad(&nface, &sface);
  301. continue;
  302. }
  303. else
  304. {
  305. // A polygon with more than 3 vertices can be either concave or convex.
  306. // Usually everything we're getting is convex and we could easily
  307. // triangulate by tri-fanning. However, LightWave is probably the only
  308. // modeling suite to make extensive use of highly concave, monster polygons ...
  309. // so we need to apply the full 'ear cutting' algorithm to get it right.
  310. // REQUIREMENT: polygon is expected to be simple and *nearly* planar.
  311. // We project it onto a plane to get a 2d triangle.
  312. // Collect all vertices of of the polygon.
  313. for (tmp = 0; tmp < max; ++tmp) {
  314. temp_verts3d[tmp] = verts[idx[tmp]];
  315. }
  316. // Get newell normal of the polygon. Store it for future use if it's a polygon-only mesh
  317. aiVector3D n;
  318. NewellNormal<3,3,3>(n,max,&temp_verts3d.front().x,&temp_verts3d.front().y,&temp_verts3d.front().z);
  319. if (nor_out) {
  320. for (tmp = 0; tmp < max; ++tmp)
  321. nor_out[idx[tmp]] = n;
  322. }
  323. // Select largest normal coordinate to ignore for projection
  324. const float ax = (n.x>0 ? n.x : -n.x);
  325. const float ay = (n.y>0 ? n.y : -n.y);
  326. const float az = (n.z>0 ? n.z : -n.z);
  327. unsigned int ac = 0, bc = 1; /* no z coord. projection to xy */
  328. float inv = n.z;
  329. if (ax > ay) {
  330. if (ax > az) { /* no x coord. projection to yz */
  331. ac = 1; bc = 2;
  332. inv = n.x;
  333. }
  334. }
  335. else if (ay > az) { /* no y coord. projection to zy */
  336. ac = 2; bc = 0;
  337. inv = n.y;
  338. }
  339. // Swap projection axes to take the negated projection vector into account
  340. if (inv < 0.f) {
  341. std::swap(ac,bc);
  342. }
  343. for (tmp =0; tmp < max; ++tmp) {
  344. temp_verts[tmp].x = verts[idx[tmp]][ac];
  345. temp_verts[tmp].y = verts[idx[tmp]][bc];
  346. done[tmp] = false;
  347. }
  348. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  349. // plot the plane onto which we mapped the polygon to a 2D ASCII pic
  350. aiVector2D bmin,bmax;
  351. ArrayBounds(&temp_verts[0],max,bmin,bmax);
  352. char grid[POLY_GRID_Y][POLY_GRID_X+POLY_GRID_XPAD];
  353. std::fill_n((char*)grid,POLY_GRID_Y*(POLY_GRID_X+POLY_GRID_XPAD),' ');
  354. for (int i =0; i < max; ++i) {
  355. const aiVector2D& v = (temp_verts[i] - bmin) / (bmax-bmin);
  356. 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));
  357. char* loc = grid[y]+x;
  358. if (grid[y][x] != ' ') {
  359. for(;*loc != ' '; ++loc);
  360. *loc++ = '_';
  361. }
  362. *(loc+::ai_snprintf(loc, POLY_GRID_XPAD,"%i",i)) = ' ';
  363. }
  364. for(size_t y = 0; y < POLY_GRID_Y; ++y) {
  365. grid[y][POLY_GRID_X+POLY_GRID_XPAD-1] = '\0';
  366. fprintf(fout,"%s\n",grid[y]);
  367. }
  368. fprintf(fout,"\ntriangulation sequence: ");
  369. #endif
  370. //
  371. // FIXME: currently this is the slow O(kn) variant with a worst case
  372. // complexity of O(n^2) (I think). Can be done in O(n).
  373. while (num > 3) {
  374. // Find the next ear of the polygon
  375. int num_found = 0;
  376. for (ear = next;;prev = ear,ear = next) {
  377. // break after we looped two times without a positive match
  378. for (next=ear+1;done[(next>=max?next=0:next)];++next);
  379. if (next < ear) {
  380. if (++num_found == 2) {
  381. break;
  382. }
  383. }
  384. const aiVector2D* pnt1 = &temp_verts[ear],
  385. *pnt0 = &temp_verts[prev],
  386. *pnt2 = &temp_verts[next];
  387. // Must be a convex point. Assuming ccw winding, it must be on the right of the line between p-1 and p+1.
  388. if (OnLeftSideOfLine2D(*pnt0,*pnt2,*pnt1)) {
  389. continue;
  390. }
  391. // and no other point may be contained in this triangle
  392. for ( tmp = 0; tmp < max; ++tmp) {
  393. // We need to compare the actual values because it's possible that multiple indexes in
  394. // the polygon are referring to the same position. concave_polygon.obj is a sample
  395. //
  396. // FIXME: Use 'epsiloned' comparisons instead? Due to numeric inaccuracies in
  397. // PointInTriangle() I'm guessing that it's actually possible to construct
  398. // input data that would cause us to end up with no ears. The problem is,
  399. // which epsilon? If we chose a too large value, we'd get wrong results
  400. const aiVector2D& vtmp = temp_verts[tmp];
  401. if ( vtmp != *pnt1 && vtmp != *pnt2 && vtmp != *pnt0 && PointInTriangle2D(*pnt0,*pnt1,*pnt2,vtmp)) {
  402. break;
  403. }
  404. }
  405. if (tmp != max) {
  406. continue;
  407. }
  408. // this vertex is an ear
  409. break;
  410. }
  411. if (num_found == 2) {
  412. // Due to the 'two ear theorem', every simple polygon with more than three points must
  413. // have 2 'ears'. Here's definitely something wrong ... but we don't give up yet.
  414. //
  415. // Instead we're continuing with the standard tri-fanning algorithm which we'd
  416. // use if we had only convex polygons. That's life.
  417. ASSIMP_LOG_ERROR("Failed to triangulate polygon (no ear found). Probably not a simple polygon?");
  418. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  419. fprintf(fout,"critical error here, no ear found! ");
  420. #endif
  421. num = 0;
  422. break;
  423. /*curOut -= (max-num); // undo all previous work
  424. for (tmp = 0; tmp < max-2; ++tmp) {
  425. aiFace& nface = *curOut++;
  426. nface.mNumIndices = 3;
  427. if (!nface.mIndices)
  428. nface.mIndices = new unsigned int[3];
  429. nface.mIndices[0] = 0;
  430. nface.mIndices[1] = tmp+1;
  431. nface.mIndices[2] = tmp+2;
  432. }
  433. num = 0;
  434. break;*/
  435. }
  436. aiFace& nface = *curOut++;
  437. nface.mNumIndices = 3;
  438. if (!nface.mIndices) {
  439. nface.mIndices = new unsigned int[3];
  440. }
  441. // setup indices for the new triangle ...
  442. nface.mIndices[0] = prev;
  443. nface.mIndices[1] = ear;
  444. nface.mIndices[2] = next;
  445. // exclude the ear from most further processing
  446. done[ear] = true;
  447. --num;
  448. }
  449. if (num > 0) {
  450. // We have three indices forming the last 'ear' remaining. Collect them.
  451. aiFace& nface = *curOut++;
  452. nface.mNumIndices = 3;
  453. if (!nface.mIndices) {
  454. nface.mIndices = new unsigned int[3];
  455. }
  456. for (tmp = 0; done[tmp]; ++tmp);
  457. nface.mIndices[0] = tmp;
  458. for (++tmp; done[tmp]; ++tmp);
  459. nface.mIndices[1] = tmp;
  460. for (++tmp; done[tmp]; ++tmp);
  461. nface.mIndices[2] = tmp;
  462. }
  463. }
  464. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  465. for(aiFace* f = last_face; f != curOut; ++f) {
  466. unsigned int* i = f->mIndices;
  467. fprintf(fout," (%i %i %i)",i[0],i[1],i[2]);
  468. }
  469. fprintf(fout,"\n*********************************************************************\n");
  470. fflush(fout);
  471. #endif
  472. for(aiFace* f = last_face; f != curOut; ) {
  473. unsigned int* i = f->mIndices;
  474. // drop dumb 0-area triangles - deactivated for now:
  475. //FindDegenerates post processing step can do the same thing
  476. //if (std::fabs(GetArea2D(temp_verts[i[0]],temp_verts[i[1]],temp_verts[i[2]])) < 1e-5f) {
  477. // ASSIMP_LOG_VERBOSE_DEBUG("Dropping triangle with area 0");
  478. // --curOut;
  479. // delete[] f->mIndices;
  480. // f->mIndices = nullptr;
  481. // for(aiFace* ff = f; ff != curOut; ++ff) {
  482. // ff->mNumIndices = (ff+1)->mNumIndices;
  483. // ff->mIndices = (ff+1)->mIndices;
  484. // (ff+1)->mIndices = nullptr;
  485. // }
  486. // continue;
  487. //}
  488. i[0] = idx[i[0]];
  489. i[1] = idx[i[1]];
  490. i[2] = idx[i[2]];
  491. // IMPROVEMENT: Polygons are not supported yet by this ngon encoding + triangulation step.
  492. // So we encode polygons as regular triangles. No way to reconstruct the original
  493. // polygon in this case.
  494. ngonEncoder.ngonEncodeTriangle(f);
  495. ++f;
  496. }
  497. delete[] face.mIndices;
  498. face.mIndices = nullptr;
  499. }
  500. #ifdef AI_BUILD_TRIANGULATE_DEBUG_POLYS
  501. fclose(fout);
  502. #endif
  503. // kill the old faces
  504. delete [] pMesh->mFaces;
  505. // ... and store the new ones
  506. pMesh->mFaces = out;
  507. pMesh->mNumFaces = (unsigned int)(curOut-out); /* not necessarily equal to numOut */
  508. return true;
  509. }
  510. #endif // !! ASSIMP_BUILD_NO_TRIANGULATE_PROCESS