| 594 | } |
| 595 | |
| 596 | bool insertConstraint(Polygon* poly, const Edge* constraint, Triangle* tri, s32 startIndex) |
| 597 | { |
| 598 | // *If* the constraint intersects an edge of *this* triangle, it must |
| 599 | // be the opposite edge. |
| 600 | s32 e0 = (startIndex + 1) % 3; |
| 601 | s32 e1 = (e0 + 1) % 3; |
| 602 | s32 i0 = tri->idx[e0]; |
| 603 | s32 i1 = tri->idx[e1]; |
| 604 | const Vec2f* c0 = &s_vertices[constraint->i0]; |
| 605 | const Vec2f* c1 = &s_vertices[constraint->i1]; |
| 606 | |
| 607 | const Vec2f* v0 = &s_vertices[i0]; |
| 608 | const Vec2f* v1 = &s_vertices[i1]; |
| 609 | |
| 610 | // Compute the intersection between line segment c0->c1 and v0->v1 |
| 611 | f32 constInter, triEdgeInter; |
| 612 | if (!TFE_Math::lineSegmentIntersect(c0, c1, v0, v1, &constInter, &triEdgeInter)) |
| 613 | { |
| 614 | return false; |
| 615 | } |
| 616 | // Make sure it is worth the hassle... |
| 617 | if (constInter <= FLT_EPSILON || constInter >= 1.0f - FLT_EPSILON) |
| 618 | { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | // *If* the intersection exists, *AND* it is before the end of the segment, recurse into the adjoining triangle. |
| 623 | // Store the adjacency and edge indices for later. |
| 624 | const s32 adj = tri->adj[e0]; |
| 625 | const s32 S = tri->idx[startIndex]; |
| 626 | |
| 627 | // Delete triangle. |
| 628 | deleteTriangle(tri); |
| 629 | |
| 630 | // Add two new triangles: |
| 631 | // startIndex -> iEdge -> newVtx |
| 632 | // startIndex -> newVtx -> iEdge + 1 |
| 633 | s32 N = (s32)s_vertices.size(); |
| 634 | Vec2f it = { v0->x + triEdgeInter * (v1->x - v0->x), v0->z + triEdgeInter * (v1->z - v0->z) }; |
| 635 | s_vertices.push_back(it); |
| 636 | addTriangle(S, i0, N); |
| 637 | addTriangle(S, N, i1); |
| 638 | |
| 639 | // Move on to the next triangle. |
| 640 | if (adj >= 0 && s_triangles[adj].allocated && constInter <= 1.0f - eps) |
| 641 | { |
| 642 | constraintSplit(i0, i1, N, &s_triangles[adj], it, c0, c1, constInter); |
| 643 | } |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | // Compute a valid triangulation for the polygon. |
| 648 | // Polygons may be complex, self-intersecting, and even be incomplete. |
no test coverage detected