RSS

(root)/iphone/common : 23 : Jigs/source/Triangulation.m

To get this branch, use:
bzr branch /browse/iphone/common

« back to all changes in this revision

Viewing changes to Jigs/source/Triangulation.m

Dömötör Gulyás
2009-08-09 18:33:08
Revision ID: dognotdog@gmail.com-20090809163308-k92rpn48plb13cqe
adds ability to create textures from PDF; adds color value to shaders; jigs now displays movement mode

Show diffs side-by-side

added added

removed removed

604
604
        return var;
605
605
}
606
606
 
 
607
#define SQUISH_FORWARD_INNER            1
 
608
#define SQUISH_FORWARD_OUTER            2
 
609
#define SQUISH_BACKWARD_INNER           4
 
610
#define SQUISH_BACKWARD_OUTER           8
 
611
#define SQUISH_IGNORE_POINT_INNER       16
 
612
#define SQUISH_IGNORE_POINT_OUTER       32
 
613
 
 
614
void unsquishyOutline(CGPoint* points, CGPoint* innerPoints, CGPoint* outerPoints, size_t numPoints, float thickness)
 
615
{
 
616
        CGPoint* squishedInnerPoints = calloc(sizeof(*squishedInnerPoints), 2*numPoints);
 
617
        CGPoint* squishedOuterPoints = squishedInnerPoints + numPoints;
 
618
        uint8_t* squishedCorners = calloc(sizeof(*squishedCorners), numPoints);
 
619
 
 
620
        
 
621
        for (size_t i = 0; i < numPoints; ++i)
 
622
        {
 
623
                size_t i0 = (i) % numPoints;
 
624
                size_t i1 = (i+1) % numPoints;
 
625
                size_t i2 = (i+2) % numPoints;
 
626
                
 
627
                CGPoint p0 = points[i0];
 
628
                CGPoint p1 = points[i1];
 
629
                CGPoint p2 = points[i2];
 
630
 
 
631
                CGPoint pi0 = innerPoints[i0];
 
632
                CGPoint pi1 = innerPoints[i1];
 
633
                CGPoint pi2 = innerPoints[i2];
 
634
 
 
635
                CGPoint po0 = outerPoints[i0];
 
636
                CGPoint po1 = outerPoints[i1];
 
637
                CGPoint po2 = outerPoints[i2];
 
638
 
 
639
                squishedInnerPoints[i1] = p1;
 
640
                squishedOuterPoints[i1] = p1;
 
641
                
 
642
                squishedCorners[i1] = squishedCorners[i1]
 
643
                        | SQUISH_BACKWARD_OUTER*CGLineSegmentsIntersect(p0, po0, p1, po1)
 
644
                        | SQUISH_BACKWARD_INNER*CGLineSegmentsIntersect(p0, pi0, p1, pi1)
 
645
                        | SQUISH_FORWARD_OUTER*CGLineSegmentsIntersect(p2, po2, p1, po1)
 
646
                        | SQUISH_FORWARD_INNER*CGLineSegmentsIntersect(p2, pi2, p1, pi1);
 
647
        }
 
648
 
 
649
        size_t sbegin = 0;
 
650
        for (sbegin = 0; sbegin < numPoints; ++sbegin)
 
651
        {
 
652
 
 
653
                if (!(squishedCorners[sbegin] & (SQUISH_FORWARD_OUTER | SQUISH_FORWARD_INNER)))
 
654
                        break;
 
655
        }
 
656
        
 
657
        long didChange = 1;
 
658
        //while(didChange)
 
659
        {
 
660
                didChange = 0;
 
661
                // now that corners have been marked, walk em
 
662
                for (size_t ii = 0; ii < numPoints; ++ii)
 
663
                {
 
664
                        size_t i = (sbegin + ii) % numPoints;
 
665
                        if (!(squishedCorners[i] & (SQUISH_FORWARD_OUTER | SQUISH_FORWARD_INNER)))
 
666
                                continue;
 
667
                        
 
668
                        if ((squishedCorners[i] & SQUISH_FORWARD_INNER) && !(squishedCorners[i] & SQUISH_BACKWARD_INNER) && !(squishedCorners[i] & SQUISH_IGNORE_POINT_INNER))
 
669
                        {
 
670
                                size_t innerEnd = (i+1) % numPoints;
 
671
                                while ((innerEnd != i) && ((squishedCorners[innerEnd] & SQUISH_FORWARD_INNER) || (squishedCorners[innerEnd] & SQUISH_IGNORE_POINT_INNER)))
 
672
                                        innerEnd = (innerEnd+1) % numPoints;
 
673
                                        
 
674
                                if (innerEnd != i) // that'd mean we went all the way around
 
675
                                {
 
676
                                        size_t i0 = (i-1) % numPoints;
 
677
                                        size_t i1 = i;
 
678
                                        size_t i2 = innerEnd;
 
679
                                        size_t i3 = (innerEnd+1) % numPoints;
 
680
                                        CGPoint pp0 = squishedInnerPoints[i0];
 
681
                                        CGPoint pp1 = squishedInnerPoints[i1];
 
682
                                        CGPoint pp2 = squishedInnerPoints[i2];
 
683
                                        CGPoint pp3 = squishedInnerPoints[i3];
 
684
                                        
 
685
                                        CGPoint pp12 = CGLineIntersectionPoint(pp0, pp1, pp2, pp3);
 
686
                                        
 
687
                                        CGPoint e01 = CGPointSub(pp12, pp0);
 
688
                                        CGPoint e12 = CGPointSub(pp3, pp12);
 
689
 
 
690
                                        CGPoint n01 = CGPointScale(CGPointNormalize(CGPointMake(-e01.y, e01.x)), thickness*0.5f);
 
691
                                        CGPoint n12 = CGPointScale(CGPointNormalize(CGPointMake(-e12.y, e12.x)), thickness*0.5f);
 
692
                                        
 
693
                                        assert(!isnan(n01.x) && !isnan(n01.y));
 
694
                                        assert(!isnan(n12.x) && !isnan(n12.y));
 
695
                                        
 
696
                                        CGPoint hn = CGPointReverseProject(n01, CGPointAdd(n01,n12));
 
697
                                        
 
698
                                        CGPoint ip = CGPointAdd(pp12, hn);
 
699
 
 
700
                                        for (size_t k = i; k != (innerEnd+1) % numPoints; k = (k+1) % numPoints)
 
701
                                        {
 
702
                                                squishedInnerPoints[k] = pp12;
 
703
                                                innerPoints[k] = ip;
 
704
                                                //squishedCorners[k] &= ~SQUISH_FORWARD_INNER;
 
705
                                                if ((k != i) && (k != innerEnd))
 
706
                                                        squishedCorners[k] |= SQUISH_IGNORE_POINT_INNER;
 
707
                                        }
 
708
                                        didChange = 1;
 
709
                                }
 
710
                        }
 
711
                        if ((squishedCorners[i] & SQUISH_FORWARD_OUTER) && !(squishedCorners[i] & SQUISH_BACKWARD_OUTER) && !(squishedCorners[i] & SQUISH_IGNORE_POINT_OUTER))
 
712
                        {
 
713
                                size_t outerEnd = (i+1) % numPoints;
 
714
                                while ((outerEnd != i) && ((squishedCorners[outerEnd] & SQUISH_FORWARD_OUTER) || (squishedCorners[outerEnd] & SQUISH_IGNORE_POINT_OUTER)))
 
715
                                        outerEnd = (outerEnd+1) % numPoints;
 
716
                                        
 
717
                                if (outerEnd != i) // that'd mean we went all the way around
 
718
                                {
 
719
                                        size_t i0 = (i-1) % numPoints;
 
720
                                        size_t i1 = i;
 
721
                                        size_t i2 = outerEnd;
 
722
                                        size_t i3 = (outerEnd+1) % numPoints;
 
723
                                        CGPoint pp0 = squishedOuterPoints[i0];
 
724
                                        CGPoint pp1 = squishedOuterPoints[i1];
 
725
                                        CGPoint pp2 = squishedOuterPoints[i2];
 
726
                                        CGPoint pp3 = squishedOuterPoints[i3];
 
727
                                        
 
728
                                        CGPoint pp12 = CGLineIntersectionPoint(pp0, pp1, pp2, pp3);
 
729
                                        
 
730
                                        CGPoint e01 = CGPointSub(pp12, pp0);
 
731
                                        CGPoint e12 = CGPointSub(pp3, pp12);
 
732
 
 
733
                                        CGPoint n01 = CGPointScale(CGPointNormalize(CGPointMake(-e01.y, e01.x)), thickness*0.5f);
 
734
                                        CGPoint n12 = CGPointScale(CGPointNormalize(CGPointMake(-e12.y, e12.x)), thickness*0.5f);
 
735
                                        
 
736
                                        assert(!isnan(n01.x) && !isnan(n01.y));
 
737
                                        assert(!isnan(n12.x) && !isnan(n12.y));
 
738
                                        
 
739
                                        CGPoint hn = CGPointReverseProject(n01, CGPointAdd(n01,n12));
 
740
                                        
 
741
                                        CGPoint ip = CGPointSub(pp12, hn);
 
742
 
 
743
                                        for (size_t k = i; k != (outerEnd+1) % numPoints; k = (k+1) % numPoints)
 
744
                                        {
 
745
                                                squishedOuterPoints[k] = pp12;
 
746
                                                outerPoints[k] = ip;
 
747
                                                //squishedCorners[k] &= ~SQUISH_FORWARD_OUTER;
 
748
                                                if ((k != i) && (k != outerEnd))
 
749
                                                        squishedCorners[k] |= SQUISH_IGNORE_POINT_OUTER;
 
750
                                        }
 
751
                                        didChange = 1;
 
752
                                }
 
753
                        }
 
754
 
 
755
                }
 
756
 
 
757
                for (size_t i = 0; i < numPoints; ++i)
 
758
                {
 
759
                        squishedCorners[i] &= ~(SQUISH_FORWARD_INNER|SQUISH_BACKWARD_INNER|SQUISH_FORWARD_OUTER|SQUISH_BACKWARD_OUTER);
 
760
                }
 
761
                
 
762
                for (size_t i = 0; i < numPoints; ++i)
 
763
                {
 
764
                        size_t i0 = (i) % numPoints;
 
765
                        size_t i1 = (i+1) % numPoints;
 
766
                        size_t i2 = (i+2) % numPoints;
 
767
                        
 
768
                        while (squishedCorners[i0] & SQUISH_IGNORE_POINT_INNER)
 
769
                                i0 = (i0-1) % numPoints;
 
770
                        while (squishedCorners[i2] & SQUISH_IGNORE_POINT_INNER)
 
771
                                i2 = (i2+1) % numPoints;
 
772
 
 
773
                        CGPoint pii0 = squishedInnerPoints[i0];
 
774
                        CGPoint pii1 = squishedInnerPoints[i1];
 
775
                        CGPoint pii2 = squishedInnerPoints[i2];
 
776
 
 
777
 
 
778
                        CGPoint pi0 = innerPoints[i0];
 
779
                        CGPoint pi1 = innerPoints[i1];
 
780
                        CGPoint pi2 = innerPoints[i2];
 
781
 
 
782
                        
 
783
                        if (!(squishedCorners[i1] & SQUISH_IGNORE_POINT_INNER))
 
784
                        {
 
785
                                squishedCorners[i1] &= ~(SQUISH_FORWARD_INNER|SQUISH_BACKWARD_INNER);
 
786
                                squishedCorners[i1] |=
 
787
                                        SQUISH_BACKWARD_INNER*CGLineSegmentsIntersect(pii0, pi0, pii1, pi1)
 
788
                                        | SQUISH_FORWARD_INNER*CGLineSegmentsIntersect(pii2, pi2, pii1, pi1);
 
789
                        }                       
 
790
                }
 
791
 
 
792
                for (size_t i = 0; i < numPoints; ++i)
 
793
                {
 
794
                        size_t i0 = (i) % numPoints;
 
795
                        size_t i1 = (i+1) % numPoints;
 
796
                        size_t i2 = (i+2) % numPoints;
 
797
                        
 
798
                        while (squishedCorners[i0] & SQUISH_IGNORE_POINT_OUTER)
 
799
                                i0 = (i0-1) % numPoints;
 
800
                        while (squishedCorners[i2] & SQUISH_IGNORE_POINT_OUTER)
 
801
                                i2 = (i2+1) % numPoints;
 
802
 
 
803
                        CGPoint pii0 = squishedOuterPoints[i0];
 
804
                        CGPoint pii1 = squishedOuterPoints[i1];
 
805
                        CGPoint pii2 = squishedOuterPoints[i2];
 
806
 
 
807
 
 
808
                        CGPoint pi0 = outerPoints[i0];
 
809
                        CGPoint pi1 = outerPoints[i1];
 
810
                        CGPoint pi2 = outerPoints[i2];
 
811
 
 
812
                        
 
813
                        if (!(squishedCorners[i1] & SQUISH_IGNORE_POINT_OUTER))
 
814
                        {
 
815
                                squishedCorners[i1] &= ~(SQUISH_FORWARD_OUTER|SQUISH_BACKWARD_OUTER);
 
816
                                squishedCorners[i1] |=
 
817
                                        SQUISH_BACKWARD_OUTER*CGLineSegmentsIntersect(pii0, pi0, pii1, pi1)
 
818
                                        | SQUISH_FORWARD_OUTER*CGLineSegmentsIntersect(pii2, pi2, pii1, pi1);
 
819
                        }                       
 
820
                }
 
821
 
 
822
        }
 
823
        free(squishedInnerPoints);
 
824
        
 
825
        free(squishedCorners);
 
826
 
 
827
}
 
828
 
607
829
CGPoint* createOutline(CGPoint* points, size_t numPoints, float thickness)
608
830
{
609
831
        CGPoint* innerPoints = calloc(sizeof(*innerPoints), 2*numPoints);
610
832
        CGPoint* outerPoints = innerPoints + numPoints;
611
 
//      uint8_t* squishedEdges = calloc(sizeof(*squishedEdges), numPoints);
612
 
 
 
833
        
613
834
        for (size_t i = 0; i < numPoints; ++i)
614
835
        {
615
836
                size_t i0 = (i) % numPoints;
631
852
                
632
853
                CGPoint hn = CGPointReverseProject(n01, CGPointAdd(n01,n12));
633
854
                
 
855
                //innerNormals[i1] = hn;
 
856
                //outerNormals[i1] = CGPointScale(hn, -1.0);
 
857
                
634
858
                innerPoints[i1] = CGPointAdd(p1, hn);
635
859
                outerPoints[i1] = CGPointSub(p1, hn);
636
860
        }
637
 
        
638
 
        
639
 
        
640
 
        
641
 
//      free(squishedEdges);
 
861
 
 
862
//      unsquishyOutline(points, innerPoints, outerPoints, numPoints, thickness);
642
863
        
643
864
        return innerPoints;
644
865
}

Loggerhead 1.17 is a web-based interface for Bazaar branches