| 561 | } |
| 562 | |
| 563 | static bool canRemoveVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned short rem) |
| 564 | { |
| 565 | const int nvp = mesh.nvp; |
| 566 | |
| 567 | // Count number of polygons to remove. |
| 568 | int numTouchedVerts = 0; |
| 569 | int numRemainingEdges = 0; |
| 570 | for (int i = 0; i < mesh.npolys; ++i) |
| 571 | { |
| 572 | unsigned short* p = &mesh.polys[i*nvp*2]; |
| 573 | const int nv = countPolyVerts(p, nvp); |
| 574 | int numRemoved = 0; |
| 575 | int numVerts = 0; |
| 576 | for (int j = 0; j < nv; ++j) |
| 577 | { |
| 578 | if (p[j] == rem) |
| 579 | { |
| 580 | numTouchedVerts++; |
| 581 | numRemoved++; |
| 582 | } |
| 583 | numVerts++; |
| 584 | } |
| 585 | if (numRemoved) |
| 586 | { |
| 587 | numRemainingEdges += numVerts-(numRemoved+1); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // There would be too few edges remaining to create a polygon. |
| 592 | // This can happen for example when a tip of a triangle is marked |
| 593 | // as deletion, but there are no other polys that share the vertex. |
| 594 | // In this case, the vertex should not be removed. |
| 595 | if (numRemainingEdges <= 2) |
| 596 | return false; |
| 597 | |
| 598 | // Find edges which share the removed vertex. |
| 599 | const int maxEdges = numTouchedVerts*2; |
| 600 | int nedges = 0; |
| 601 | rcScopedDelete<int> edges((int*)rcAlloc(sizeof(int)*maxEdges*3, RC_ALLOC_TEMP)); |
| 602 | if (!edges) |
| 603 | { |
| 604 | ctx->log(RC_LOG_WARNING, "canRemoveVertex: Out of memory 'edges' (%d).", maxEdges*3); |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | for (int i = 0; i < mesh.npolys; ++i) |
| 609 | { |
| 610 | unsigned short* p = &mesh.polys[i*nvp*2]; |
| 611 | const int nv = countPolyVerts(p, nvp); |
| 612 | |
| 613 | // Collect edges which touches the removed vertex. |
| 614 | for (int j = 0, k = nv-1; j < nv; k = j++) |
| 615 | { |
| 616 | if (p[j] == rem || p[k] == rem) |
| 617 | { |
| 618 | // Arrange edge so that a=rem. |
| 619 | int a = p[j], b = p[k]; |
| 620 | if (b == rem) |
no test coverage detected