Skip to content
Snippets Groups Projects
triquad.py 37.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    # ##### BEGIN GPL LICENSE BLOCK #####
    #
    #  This program is free software; you can redistribute it and/or
    #  modify it under the terms of the GNU General Public License
    #  as published by the Free Software Foundation; either version 2
    #  of the License, or (at your option) any later version.
    #
    #  This program is distributed in the hope that it will be useful,
    #  but WITHOUT ANY WARRANTY; without even the implied warranty of
    #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    #  GNU General Public License for more details.
    #
    #  You should have received a copy of the GNU General Public License
    #  along with this program; if not, write to the Free Software Foundation,
    #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    #
    # ##### END GPL LICENSE BLOCK #####
    
    # <pep8 compliant>
    
    
    from . import geom
    import math
    import random
    from math import sqrt
    
    # Points are 3-tuples or 2-tuples of reals: (x,y,z) or (x,y)
    # Faces are lists of integers (vertex indices into coord lists)
    # After triangulation/quadrangulation, the tris and quads will
    # be tuples instead of lists.
    # Vmaps are lists taking vertex index -> Point
    
    TOL = 1e-7     # a tolerance for fuzzy equality
    GTHRESH = 75   # threshold above which use greedy to _Quandrangulate
    ANGFAC = 1.0   # weighting for angles in quad goodness measure
    DEGFAC = 10.0  # weighting for degree in quad goodness measure
    
    # Angle kind constants
    Ang0 = 1
    Angconvex = 2
    Angreflex = 3
    Angtangential = 4
    Ang360 = 5
    
    
    def TriangulateFace(face, points):
        """Triangulate the given face.
    
        Uses an easy triangulation first, followed by a constrained delauney
        triangulation to get better shaped triangles.
    
        Args:
          face: list of int - indices in points, assumed CCW-oriented
          points: geom.Points - holds coordinates for vertices
        Returns:
          list of (int, int, int) - 3-tuples are CCW-oriented vertices of
              triangles making up the triangulation
        """
    
        if len(face) <= 3:
            return [tuple(face)]
        tris = EarChopTriFace(face, points)
        bord = _BorderEdges([face])
        triscdt = _CDT(tris, bord, points)
        return triscdt
    
    
    def TriangulateFaceWithHoles(face, holes, points):
        """Like TriangulateFace, but with holes inside the face.
    
        Works by making one complex polygon that has segments to
        and from the holes ("islands"), and then using the same method
        as TriangulateFace.
    
        Args:
          face: list of int - indices in points, assumed CCW-oriented
          holes: list of list of int - each sublist is like face
              but CW-oriented and assumed to be inside face
          points: geom.Points - holds coordinates for vertices
        Returns:
          list of (int, int, int) - 3-tuples are CCW-oriented vertices of
              triangles making up the triangulation
        """
    
        if len(holes) == 0:
            return TriangulateFace(face, points)
        allfaces = [face] + holes
        sholes = [_SortFace(h, points) for h in holes]
        joinedface = _JoinIslands(face, sholes, points)
        tris = EarChopTriFace(joinedface, points)
        bord = _BorderEdges(allfaces)
        triscdt = _CDT(tris, bord, points)
        return triscdt
    
    
    def QuadrangulateFace(face, points):
        """Quadrangulate the face (subdivide into convex quads and tris).
    
        Like TriangulateFace, but after triangulating, join as many pairs
        of triangles as possible into convex quadrilaterals.
    
        Args:
          face: list of int - indices in points, assumed CCW-oriented
          points: geom.Points - holds coordinates for vertices
        Returns:
          list of 3-tuples or 4-tuples of ints - CCW-oriented vertices of
              quadrilaterals and triangles making up the quadrangulation.
        """
    
        if len(face) <= 3:
            return [tuple(face)]
        tris = EarChopTriFace(face, points)
        bord = _BorderEdges([face])
        triscdt = _CDT(tris, bord, points)
        qs = _Quandrangulate(triscdt, bord, points)
        return qs
    
    
    def QuadrangulateFaceWithHoles(face, holes, points):
        """Like QuadrangulateFace, but with holes inside the faces.
    
        Args:
          face: list of int - indices in points, assumed CCW-oriented
          holes: list of list of int - each sublist is like face
              but CW-oriented and assumed to be inside face
          points: geom.Points - holds coordinates for vertices
        Returns:
          list of 3-tuples or 4-tuples of ints - CCW-oriented vertices of
              quadrilaterals and triangles making up the quadrangulation.
        """
    
        if len(holes) == 0:
            return QuadrangulateFace(face, points)
        allfaces = [face] + holes
        sholes = [_SortFace(h, points) for h in holes]
        joinedface = _JoinIslands(face, sholes, points)
        tris = EarChopTriFace(joinedface, points)
        bord = _BorderEdges(allfaces)
        triscdt = _CDT(tris, bord, points)
        qs = _Quandrangulate(triscdt, bord, points)
        return qs
    
    
    def _SortFace(face, points):
        """Rotate face so leftmost vertex is first, where face is
        list of indices in points."""
    
        n = len(face)
        if n <= 1:
            return face
        lefti = 0
        leftv = face[0]
        for i in range(1, n):
            # following comparison is lexicographic on n-tuple
            # so sorts on x first, using lower y as tie breaker.
            if points.pos[face[i]] < points.pos[leftv]:
                lefti = i
                leftv = face[i]
        return face[lefti:] + face[0:lefti]
    
    
    def EarChopTriFace(face, points):
        """Triangulate given face, with coords given by indexing into points.
        Return list of faces, each of which will be a triangle.
        Use the ear-chopping method."""
    
        # start with lowest coord in 2d space to try
        # to get a pleasing uniform triangulation if starting with
        # a regular structure (like a grid)
        start = _GetLeastIndex(face, points)
        ans = []
        incr = 1
        n = len(face)
        while n > 3:
            i = _FindEar(face, n, start, incr, points)
            vm1 = face[(i - 1) % n]
            v0 = face[i]
            v1 = face[(i + 1) % n]
            face = _ChopEar(face, i)
            n = len(face)
            incr = - incr
            if incr == 1:
                start = i % n
            else:
                start = (i - 1) % n
            ans.append((vm1, v0, v1))
        ans.append(tuple(face))
        return ans
    
    
    def _GetLeastIndex(face, points):
        """Return index of coordinate that is leftmost, lowest in face."""
    
        bestindex = 0
        bestpos = points.pos[face[0]]
        for i in range(1, len(face)):
            pos = points.pos[face[i]]
            if pos[0] < bestpos[0] or \
                    (pos[0] == bestpos[0] and pos[1] < bestpos[1]):
                bestindex = i
                bestpos = pos
        return bestindex
    
    
    def _FindEar(face, n, start, incr, points):
        """An ear of a polygon consists of three consecutive vertices
        v(-1), v0, v1 such that v(-1) can connect to v1 without intersecting
        the polygon.
        Finds an ear, starting at index 'start' and moving
        in direction incr. (We attempt to alternate directions, to find
        'nice' triangulations for simple convex polygons.)
        Returns index into faces of v0 (will always find one, because
        uses a desperation mode if fails to find one with above rule)."""
    
        angk = _ClassifyAngles(face, n, points)
        for mode in range(0, 5):
            i = start
            while True:
                if _IsEar(face, i, n, angk, points, mode):
                    return i
                i = (i + incr) % n
                if i == start:
                    break  # try next higher desperation mode
    
    
    def _IsEar(face, i, n, angk, points, mode):
        """Return true, false depending on ear status of vertices
        with indices i-1, i, i+1.
        mode is amount of desperation: 0 is Normal mode,
        mode 1 allows degenerate triangles (with repeated vertices)
        mode 2 allows local self crossing (folded) ears
        mode 3 allows any convex vertex (should always be one)
        mode 4 allows anything (just to be sure loop terminates!)"""
    
        k = angk[i]
        vm2 = face[(i - 2) % n]
        vm1 = face[(i - 1) % n]
        v0 = face[i]
        v1 = face[(i + 1) % n]
        v2 = face[(i + 2) % n]
        if vm1 == v0 or v0 == v1:
            return (mode > 0)
        b = (k == Angconvex or k == Angtangential or k == Ang0)
        c = _InCone(vm1, v0, v1, v2, angk[(i + 1) % n], points) and \
            _InCone(v1, vm2, vm1, v0, angk[(i - 1) % n], points)
        if b and c:
            return _EarCheck(face, n, angk, vm1, v0, v1, points)
        if mode < 2:
            return False
        if mode == 3:
            return SegsIntersect(vm2, vm1, v0, v1, points)
        if mode == 4:
            return b
        return True
    
    
    def _EarCheck(face, n, angk, vm1, v0, v1, points):
        """Return True if the successive vertices vm1, v0, v1
        forms an ear.  We already know that it is not a reflex
        Angle, and that the local cone containment is ok.
        What remains to check is that the edge vm1-v1 doesn't
        intersect any other edge of the face (besides vm1-v0
        and v0-v1).  Equivalently, there can't be a reflex Angle
        inside the triangle vm1-v0-v1.  (Well, there are
        messy cases when other points of the face coincide with
        v0 or touch various lines involved in the ear.)"""
        for j in range(0, n):
            fv = face[j]
            k = angk[j]
            b = (k == Angreflex or k == Ang360) \
                and not(fv == vm1 or fv == v0 or fv == v1)
            if b:
                # Is fv inside closure of triangle (vm1,v0,v1)?
                c = not(Ccw(v0, vm1, fv, points) \
                              or Ccw(vm1, v1, fv, points) \
                              or Ccw(v1, v0, fv, points))
                fvm1 = face[(j - 1) % n]
                fv1 = face[(j + 1) % n]
                # To try to deal with some degenerate cases,
                # also check to see if either segment attached to fv
                # intersects either segment of potential ear.
                d = SegsIntersect(fvm1, fv, vm1, v0, points) or \
                          SegsIntersect(fvm1, fv, v0, v1, points) or \
                          SegsIntersect(fv, fv1, vm1, v0, points) or \
                          SegsIntersect(fv, fv1, v0, v1, points)
                if c or d:
                    return False
        return True
    
    
    def _ChopEar(face, i):
        """Return a copy of face (of length n), omitting element i."""
    
        return face[0:i] + face[i + 1:]
    
    
    def _InCone(vtest, a, b, c, bkind, points):
        """Return true if point with index vtest is in Cone of points with
        indices a, b, c, where Angle ABC has AngleKind Bkind.
        The Cone is the set of points inside the left face defined by
        segments ab and bc, disregarding all other segments of polygon for
        purposes of inside test."""
    
        if bkind == Angreflex or bkind == Ang360:
            if _InCone(vtest, c, b, a, Angconvex, points):
                return False
            return not((not(Ccw(b, a, vtest, points)) \
                                 and not(Ccw(b, vtest, a, points)) \
                                 and Ccw(b, a, vtest, points))
                                or
                                (not(Ccw(b, c, vtest, points)) \
                                 and not(Ccw(b, vtest, c, points)) \
                                 and Ccw(b, a, vtest, points)))
        else:
            return Ccw(a, b, vtest, points) and Ccw(b, c, vtest, points)
    
    
    def _JoinIslands(face, holes, points):
        """face is a CCW face containing the CW faces in the holes list,
        where each hole is sorted so the leftmost-lowest vertex is first.
        faces and holes are given as lists of indices into points.
        The holes should be sorted by softface.
        Add edges to make a new face that includes the holes (a Ccw traversal
        of the new face will have the inside always on the left),
        and return the new face."""
    
        while len(holes) > 0:
            (hole, holeindex) = _LeftMostFace(holes, points)
            holes = holes[0:holeindex] + holes[holeindex + 1:]
            face = _JoinIsland(face, hole, points)
        return face
    
    
    def _JoinIsland(face, hole, points):
        """Return a modified version of face that splices in the
        vertices of hole (which should be sorted)."""
    
        if len(hole) == 0:
            return face
        hv0 = hole[0]
        d = _FindDiag(face, hv0, points)
        newface = face[0:d + 1] + hole + [hv0] + face[d:]
        return newface
    
    
    def _LeftMostFace(holes, points):
        """Return (hole,index of hole in holes) where hole has
        the leftmost first vertex.  To be able to handle empty
        holes gracefully, call an empty hole 'leftmost'.
        Assumes holes are sorted by softface."""
    
        assert(len(holes) > 0)
        lefti = 0
        lefthole = holes[0]
        if len(lefthole) == 0:
            return (lefthole, lefti)
        leftv = lefthole[0]
        for i in range(1, len(holes)):
            ihole = holes[i]
            if len(ihole) == 0:
                return (ihole, i)
            iv = ihole[0]
            if points.pos[iv] < points.pos[leftv]:
                (lefti, lefthole, leftv) = (i, ihole, iv)
        return (lefthole, lefti)
    
    
    def _FindDiag(face, hv, points):
        """Find a vertex in face that can see vertex hv, if possible,
        and return the index into face of that vertex.
        Should be able to find a diagonal that connects a vertex of face
        left of v to hv without crossing face, but try two
        more desperation passes after that to get SOME diagonal, even if
        it might cross some edge somewhere.
        First desperation pass (mode == 1): allow points right of hv.
        Second desperation pass (mode == 2): allow crossing boundary poly"""
    
        besti = - 1
        bestdist = 1e30
        for mode in range(0, 3):
            for i in range(0, len(face)):
                v = face[i]
                if mode == 0 and points.pos[v] > points.pos[hv]:
                    continue  # in mode 0, only want points left of hv
                dist = _DistSq(v, hv, points)
                if dist < bestdist:
                    if _IsDiag(i, v, hv, face, points) or mode == 2:
                        (besti, bestdist) = (i, dist)
            if besti >= 0:
                break  # found one, so don't need other modes
        assert(besti >= 0)
        return besti
    
    
    def _IsDiag(i, v, hv, face, points):
        """Return True if vertex v (at index i in face) can see vertex hv.
        v and hv are indices into points.
        (v, hv) is a diagonal if hv is in the cone of the Angle at index i on face
        and no segment in face intersects (h, hv).
        """
    
        n = len(face)
        vm1 = face[(i - 1) % n]
        v1 = face[(i + 1) % n]
        k = _AngleKind(vm1, v, v1, points)
        if not _InCone(hv, vm1, v, v1, k, points):
            return False
        for j in range(0, n):
            vj = face[j]
            vj1 = face[(j + 1) % n]
            if SegsIntersect(v, hv, vj, vj1, points):
                return False
        return True
    
    
    def _DistSq(a, b, points):
        """Return distance squared between coords with indices a and b in points.
        """
    
        diff = Sub2(points.pos[a], points.pos[b])
        return Dot2(diff, diff)
    
    
    def _BorderEdges(facelist):
        """Return a set of (u,v) where u and v are successive vertex indices
        in some face in the list in facelist."""
    
        ans = set()
        for i in range(0, len(facelist)):
            f = facelist[i]
            for j in range(1, len(f)):
                ans.add((f[j - 1], f[j]))
            ans.add((f[-1], f[0]))
        return ans
    
    
    def _CDT(tris, bord, points):
        """Tris is a list of triangles ((a,b,c), CCW-oriented indices into points)
        Bord is a set of border edges (u,v), oriented so that tris
        is a triangulation of the left face of the border(s).
        Make the triangulation "Constrained Delaunay" by flipping "reversed"
        quadrangulaterals until can flip no more.
        Return list of triangles in new triangulation."""
    
        td = _TriDict(tris)
        re = _ReveresedEdges(tris, td, bord, points)
        ts = set(tris)
        # reverse the reversed edges until done.
        # reversing and edge adds new edges, which may or
        # may not be reversed or border edges, to re for
        # consideration, but the process will stop eventually.
        while len(re) > 0:
            (a, b) = e = re.pop()
            if e in bord or not _IsReversed(e, td, points):
                continue
            # rotate e in quad adbc to get other diagonal
            erev = (b, a)
            tl = td.get(e)
            tr = td.get(erev)
            if not tl or not tr:
                continue  # shouldn't happen
            c = _OtherVert(tl, a, b)
            d = _OtherVert(tr, a, b)
            if c is None or d is None:
                continue  # shouldn't happen
            newt1 = (c, d, b)
            newt2 = (c, a, d)
            del td[e]
            del td[erev]
            td[(c, d)] = newt1
            td[(d, b)] = newt1
            td[(b, c)] = newt1
            td[(d, c)] = newt2
            td[(c, a)] = newt2
            td[(a, d)] = newt2
            if tl in ts:
                ts.remove(tl)
            if tr in ts:
                ts.remove(tr)
            ts.add(newt1)
            ts.add(newt2)
            re.extend([(d, b), (b, c), (c, a), (a, d)])
        return list(ts)
    
    
    def _TriDict(tris):
        """tris is a list of triangles (a,b,c), CCW-oriented indices.
        Return dict mapping all edges in the triangles to the containing
        triangle list."""
    
        ans = dict()
        for i in range(0, len(tris)):
            (a, b, c) = t = tris[i]
            ans[(a, b)] = t
            ans[(b, c)] = t
            ans[(c, a)] = t
        return ans
    
    
    def _ReveresedEdges(tris, td, bord, points):
        """Return list of reversed edges in tris.
        Only want edges not in bord, and only need one representative
        of (u,v)/(v,u), so choose the one with u < v.
        td is dictionary from _TriDict, and is used to find left and right
        triangles of edges."""
    
        ans = []
        for i in range(0, len(tris)):
            (a, b, c) = tris[i]
            for e in [(a, b), (b, c), (c, a)]:
                if e in bord:
                    continue
                (u, v) = e
                if u < v:
                    if _IsReversed(e, td, points):
                        ans.append(e)
        return ans
    
    
    def _IsReversed(e, td, points):
        """If e=(a,b) is a non-border edge, with left-face triangle tl and
        right-face triangle tr, then it is 'reversed' if the circle through
        a, b, and (say) the other vertex of tl containts the other vertex of tr.
        td is a _TriDict, for finding triangles containing edges, and points
        gives the coordinates for vertex indices used in edges."""
    
        tl = td.get(e)
        if not tl:
            return False
        (a, b) = e
        tr = td.get((b, a))
        if not tr:
            return False
        c = _OtherVert(tl, a, b)
        d = _OtherVert(tr, a, b)
        if c is None or d is None:
            return False
        return InCircle(a, b, c, d, points)
    
    
    def _OtherVert(tri, a, b):
        """tri should be a tuple of 3 vertex indices, two of which are a and b.
        Return the third index, or None if all vertices are a or b"""
    
        for v in tri:
            if v != a and v != b:
                return v
        return None
    
    
    def _ClassifyAngles(face, n, points):
        """Return vector of anglekinds of the Angle around each point in face."""
    
        return [_AngleKind(face[(i - 1) % n], face[i], face[(i + 1) % n], points) \
            for i in list(range(0, n))]
    
    
    def _AngleKind(a, b, c, points):
        """Return one of the Ang... constants to classify Angle formed by ABC,
        in a counterclockwise traversal of a face,
        where a, b, c are indices into points."""
    
        if Ccw(a, b, c, points):
            return Angconvex
        elif Ccw(a, c, b, points):
            return Angreflex
        else:
            vb = points.pos[b]
            udotv = Dot2(Sub2(vb, points.pos[a]), Sub2(points.pos[c], vb))
            if udotv > 0.0:
                return Angtangential
            else:
                return Ang0   # to fix: return Ang360 if "inside" spur
    
    
    def _Quandrangulate(tris, bord, points):
        """Tris is list of triangles, forming a triangulation of region whose
        border edges are in set bord.
        Combine adjacent triangles to make quads, trying for "good" quads where
        possible. Some triangles will probably remain uncombined"""
    
        (er, td) = _ERGraph(tris, bord, points)
        if len(er) == 0:
            return tris
        if len(er) > GTHRESH:
            match = _GreedyMatch(er)
        else:
            match = _MaxMatch(er)
        return _RemoveEdges(tris, match)
    
    
    def _RemoveEdges(tris, match):
        """tris is list of triangles.
        er is as returned from _MaxMatch or _GreedyMatch.
    
        Return list of (A,D,B,C) resulting from deleting edge (A,B) causing a merge
        of two triangles; append to that list the remaining unmatched triangles."""
    
        ans = []
        triset = set(tris)
        while len(match) > 0:
            (_, e, tl, tr) = match.pop()
            (a, b) = e
            if tl in triset:
                triset.remove(tl)
            if tr in triset:
                triset.remove(tr)
            c = _OtherVert(tl, a, b)
            d = _OtherVert(tr, a, b)
            if c is None or d is None:
                continue
            ans.append((a, d, b, c))
        return ans + list(triset)
    
    
    def _ERGraph(tris, bord, points):
        """Make an 'Edge Removal Graph'.
    
        Given a list of triangles, the 'Edge Removal Graph' is a graph whose
        nodes are the triangles (think of a point in the center of them),
        and whose edges go between adjacent triangles (they share a non-border
        edge), such that it would be possible to remove the shared edge
        and form a convex quadrilateral.  Forming a quadrilateralization
        is then a matter of finding a matching (set of edges that don't
        share a vertex - remember, these are the 'face' vertices).
        For better quadrilaterlization, we'll make the Edge Removal Graph
        edges have weights, with higher weights going to the edges that
        are more desirable to remove.  Then we want a maximum weight matching
        in this graph.
    
        We'll return the graph in a kind of implicit form, using edges of
        the original triangles as a proxy for the edges between the faces
        (i.e., the edge of the triangle is the shared edge). We'll arbitrarily
        pick the triangle graph edge with lower-index start vertex.
        Also, to aid in traversing the implicit graph, we'll keep the left
        and right triangle triples with edge 'ER edge'.
        Finally, since we calculate it anyway, we'll return a dictionary
        mapping edges of the triangles to the triangle triples they're in.
    
        Args:
          tris: list of (int, int, int) giving a triple of vertex indices for
              triangles, assumed CCW oriented
          bord: set of (int, int) giving vertex indices for border edges
          points: geom.Points - for mapping vertex indices to coords
        Returns:
          (list of (weight,e,tl,tr), dict)
            where edge e=(a,b) is non-border edge
            with left face tl and right face tr (each a triple (i,j,k)),
            where removing the edge would form an "OK" quad (no concave angles),
            with weight representing the desirability of removing the edge
            The dict maps int pairs (a,b) to int triples (i,j,k), that is,
            mapping edges to their containing triangles.
        """
    
        td = _TriDict(tris)
        dd = _DegreeDict(tris)
        ans = []
        ctris = tris[:]  # copy, so argument not affected
        while len(ctris) > 0:
            (i, j, k) = tl = ctris.pop()
            for e in [(i, j), (j, k), (k, i)]:
                if e in bord:
                    continue
                (a, b) = e
                # just consider one of (a,b) and (b,a), to avoid dups
                if a > b:
                    continue
                erev = (b, a)
                tr = td.get(erev)
                if not tr:
                    continue
                c = _OtherVert(tl, a, b)
                d = _OtherVert(tr, a, b)
                if c is None or d is None:
                    continue
                # calculate amax, the max of the new angles that would
                # be formed at a and b if tl and tr were combined
                amax = max(Angle(c, a, b, points) + Angle(d, a, b, points),
                           Angle(c, b, a, points) + Angle(d, b, a, points))
                if amax > 180.0:
                    continue
                weight = ANGFAC * (180.0 - amax) + DEGFAC * (dd[a] + dd[b])
                ans.append((weight, e, tl, tr))
        return (ans, td)
    
    
    def _GreedyMatch(er):
        """er is list of (weight,e,tl,tr).
    
        Find maximal set so that each triangle appears in at most
        one member of set"""
    
        # sort in order of decreasing weight
        er.sort(key=lambda v: v[0], reverse=True)
        match = set()
        ans = []
        while len(er) > 0:
            (_, _, tl, tr) = q = er.pop()
            if tl not in match and tr not in match:
                match.add(tl)
                match.add(tr)
                ans.append(q)
        return ans
    
    
    def _MaxMatch(er):
        """Like _GreedyMatch, but use divide and conquer to find best possible set.
    
        Args:
          er: list of (weight,e,tl,tr)  - see _ERGraph
        Returns:
          list that is a subset of er giving a maximum weight match
        """
    
        (ans, _) = _DCMatch(er)
        return ans
    
    
    def _DCMatch(er):
        """Recursive helper for _MaxMatch.
    
        Divide and Conquer approach to finding max weight matching.
        If we're lucky, there's an edge in er that separates the edge removal
        graph into (at least) two separate components.  Then the max weight
        is either one that includes that edge or excludes it - and we can
        use a recursive call to _DCMatch to handle each component separately
        on what remains of the graph after including/excluding the separating edge.
        If we're not lucky, we fall back on _EMatch (see below).
    
        Args:
          er: list of (weight, e, tl, tr) (see _ERGraph)
        Returns:
          (list of (weight, e, tl, tr), float) - the subset forming a maximum
              matching, and the total weight of the match.
        """
    
        if not er:
            return ([], 0.0)
        if len(er) == 1:
            return (er, er[0][0])
        match = []
        matchw = 0.0
        for i in range(0, len(er)):
            (nc, comp) = _FindComponents(er, i)
            if nc == 1:
                # er[i] doesn't separate er
                continue
            (wi, _, tl, tr) = er[i]
            if comp[tl] != comp[tr]:
                # case 1: er separates graph
                # compare the matches that include er[i] versus
                # those that exclude it
                (a, b) = _PartitionComps(er, comp, i, comp[tl], comp[tr])
                ax = _CopyExcluding(a, tl, tr)
                bx = _CopyExcluding(b, tl, tr)
                (axmatch, wax) = _DCMatch(ax)
                (bxmatch, wbx) = _DCMatch(bx)
                if len(ax) == len(a):
                    wa = wax
                    amatch = axmatch
                else:
                    (amatch, wa) = _DCMatch(a)
                if len(bx) == len(b):
                    wb = wbx
                    bmatch = bxmatch
                else:
                    (bmatch, wb) = _DCMatch(b)
                w = wa + wb
                wx = wax + wbx + wi
                if w > wx:
                    match = amatch + bmatch
                    matchw = w
                else:
                    match = [er[i]] + axmatch + bxmatch
                    matchw = wx
            else:
                # case 2: er not needed to separate graph
                (a, b) = _PartitionComps(er, comp, -1, 0, 0)
                (amatch, wa) = _DCMatch(a)
                (bmatch, wb) = _DCMatch(b)
                match = amatch + bmatch
                matchw = wa + wb
            if match:
                break
        if not match:
            return _EMatch(er)
        return (match, matchw)
    
    
    def _EMatch(er):
        """Exhaustive match helper for _MaxMatch.
    
        This is the case when we were unable to find a single edge
        separating the edge removal graph into two components.
        So pick a single edge and try _DCMatch on the two cases of
        including or excluding that edge.  We may be lucky in these
        subcases (say, if the graph is currently a simple cycle, so
        only needs one more edge after the one we pick here to separate
        it into components).  Otherwise, we'll end up back in _EMatch
        again, and the worse case will be exponential.
    
        Pick a random edge rather than say, the first, to hopefully
        avoid some pathological cases.
    
        Args:
          er: list of (weight, el, tl, tr) (see _ERGraph)
        Returns:
           (list of (weight, e, tl, tr), float) - the subset forming a maximum
              matching, and the total weight of the match.
        """
    
        if not er:
            return ([], 0.0)
        if len(er) == 1:
            return (er, er[1][1])
        i = random.randint(0, len(er) - 1)
        eri = (wi, _, tl, tr) = er[i]
        # case a: include eri.  exlude other edges that touch tl or tr
        a = _CopyExcluding(er, tl, tr)
        a.append(eri)
        (amatch, wa) = _DCMatch(a)
        wa += wi
        if len(a) == len(er) - 1:
            # if a excludes only eri, then er didn't touch anything else
            # in the graph, and the best match will always include er
            # and we can skip the call for case b
            wb = -1.0
            bmatch = []
        else:
            b = er[:i] + er[i + 1:]
            (bmatch, wb) = _DCMatch(b)
        if wa > wb:
            match = amatch
            match.append(eri)
            matchw = wa
        else:
            match = bmatch
            matchw = wb
        return (match, matchw)
    
    
    def _FindComponents(er, excepti):
        """Find connected components induced by edges, excluding one edge.
    
        Args:
          er: list of (weight, el, tl, tr) (see _ERGraph)
          excepti: index in er of edge to be excluded
        Returns:
          (int, dict): int is number of connected components found,
              dict maps triangle triple ->
                  connected component index (starting at 1)
         """
    
        ncomps = 0
        comp = dict()
        for i in range(0, len(er)):
            (_, _, tl, tr) = er[i]
            for t in [tl, tr]:
                if t not in comp:
                    ncomps += 1
                    _FCVisit(er, excepti, comp, t, ncomps)
        return (ncomps, comp)
    
    
    def _FCVisit(er, excepti, comp, t, compnum):
        """Helper for _FindComponents depth-first-search."""
    
        comp[t] = compnum
        for i in range(0, len(er)):
            if i == excepti:
                continue
            (_, _, tl, tr) = er[i]
            if tl == t or tr == t:
                s = tl
                if s == t:
                    s = tr
                if s not in comp:
                    _FCVisit(er, excepti, comp, s, compnum)
    
    
    def _PartitionComps(er, comp, excepti, compa, compb):
        """Partition the edges of er by component number, into two lists.
    
        Generally, put odd components into first list and even into second,
        except that component compa goes in the first and compb goes in the second,
        and we ignore edge er[excepti].
    
        Args:
          er: list of (weight, el, tl, tr) (see _ERGraph)
          comp: dict - mapping triangle triple -> connected component index
          excepti: int - index in er to ignore (unless excepti==-1)
          compa: int - component to go in first list of answer (unless 0)
          compb: int - component to go in second list of answer (unless 0)
        Returns:
          (list, list) - a partition of er according to above rules
        """
    
        parta = []
        partb = []
        for i in range(0, len(er)):
    
            if i == excepti:
                continue
            tl = er[i][2]
            c = comp[tl]
            if c == compa or (c != compb and (c & 1) == 1):
                parta.append(er[i])
            else:
                partb.append(er[i])
        return (parta, partb)
    
    
    def _CopyExcluding(er, s, t):
        """Return a copy of er, excluding all those involving triangles s and t.
    
        Args:
          er: list of (weight, e, tl, tr) - see _ERGraph
          s: 3-tuple of int - a triangle
          t: 3-tuple of int - a triangle
        Returns:
          Copy of er excluding those with tl or tr == s or t
        """
    
        ans = []
        for e in er:
            (_, _, tl, tr) = e
            if tl == s or tr == s or tl == t or tr == t:
                continue
            ans.append(e)
        return ans
    
    
    def _DegreeDict(tris):
        """Return a dictionary mapping vertices in tris to the number of triangles
        that they are touch."""
    
        ans = dict()
        for t in tris:
            for v in t:
                if v in ans:
                    ans[v] = ans[v] + 1
                else:
                    ans[v] = 1
        return ans
    
    
    def PolygonPlane(face, points):
        """Return a Normal vector for the face with 3d coords given by indexing
        into points."""
    
        if len(face) < 3:
            return (0.0, 0.0, 1.0)    # arbitrary, we really have no idea
        else:
            coords = [points.pos[i] for i in face]
            return Normal(coords)
    
    
    # This Normal appears to be on the CCW-traversing side of a polygon
    def Normal(coords):
        """Return an average Normal vector for the point list, 3d coords."""
    
        if len(coords) < 3:
            return (0.0, 0.0, 1.0)    # arbitrary
    
        (ax, ay, az) = coords[0]
        (bx, by, bz) = coords[1]
        (cx, cy, cz) = coords[2]
    
        if len(coords) == 3:
            sx = (ay - by) * (az + bz) + \
                (by - cy) * (bz + cz) + \
                (cy - ay) * (cz + az)
            sy = (az - bz) * (ax + bx) + \
                (bz - cz) * (bx + cx) + \
                (cz - az) * (cx + ax)
            sz = (ax - bx) * (by + by) + \
                (bx - cx) * (by + cy) + \
                (cx - ax) * (cy + ay)
            return Norm3(sx, sy, sz)
        else:
            sx = (ay - by) * (az + bz) + (by - cy) * (bz + cz)
            sy = (az - bz) * (ax + bx) + (bz - cz) * (bx + cx)
            sz = (ax - bx) * (ay + by) + (bx - cx) * (by + cy)
            return _NormalAux(coords[3:], coords[0], sx, sy, sz)
    
    
    def _NormalAux(rest, first, sx, sy, sz):
        (ax, ay, az) = rest[0]
        if len(rest) == 1:
            (bx, by, bz) = first
        else:
            (bx, by, bz) = rest[1]
        nx = sx + (ay - by) * (az + bz)
        ny = sy + (az - bz) * (ax + bx)
        nz = sz + (ax - bx) * (ay + by)
        if len(rest) == 1:
            return Norm3(nx, ny, nz)
        else:
            return _NormalAux(rest[1:], first, nx, ny, nz)