blob: fc4c26c6e7b7dca9aef5a7a88dcb094a0760adba [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.coma5764232012-03-28 16:20:21 +00007#ifndef Intersections_DEFINE
8#define Intersections_DEFINE
9
caryclark@google.com639df892012-01-10 21:46:10 +000010class Intersections {
11public:
skia.committer@gmail.com9f876ed2013-01-20 07:05:51 +000012 Intersections()
caryclark@google.com05c4bad2013-01-19 13:22:39 +000013 : fFlip(0)
caryclark@google.comf9502d72013-02-04 14:06:49 +000014#if SK_DEBUG
15 , fDepth(0)
16#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +000017 , fSwap(0)
caryclark@google.com639df892012-01-10 21:46:10 +000018 {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000019#if SK_DEBUG
20 bzero(fPt, sizeof(fPt));
caryclark@google.com639df892012-01-10 21:46:10 +000021 bzero(fT, sizeof(fT));
caryclark@google.com45a8fc62013-02-14 15:29:11 +000022 bzero(fIsCoincident, sizeof(fIsCoincident));
23#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +000024 reset();
caryclark@google.com639df892012-01-10 21:46:10 +000025 }
26
caryclark@google.comf9502d72013-02-04 14:06:49 +000027 int coincidentUsed() const {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000028 if (!fIsCoincident[0]) {
29 SkASSERT(!fIsCoincident[0]);
30 return 0;
31 }
32 int count = 0;
33 SkDEBUGCODE(int count2 = 0;)
34 for (int index = 0; index < fUsed; ++index) {
35 if (fIsCoincident[0] & (1 << index)) {
36 ++count;
37 }
38 #if SK_DEBUG
39 if (fIsCoincident[1] & (1 << index)) {
40 ++count2;
41 }
42 #endif
43 }
44 SkASSERT(count == count2);
45 return count;
caryclark@google.com32546db2012-08-31 20:55:07 +000046 }
skia.committer@gmail.com0c38ed32013-02-05 07:02:01 +000047
caryclark@google.com639df892012-01-10 21:46:10 +000048 void offset(int base, double start, double end) {
49 for (int index = base; index < fUsed; ++index) {
50 double val = fT[fSwap][index];
51 val *= end - start;
52 val += start;
53 fT[fSwap][index] = val;
54 }
55 }
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000056
caryclark@google.com7ff5c842013-02-26 15:56:05 +000057 // FIXME : does not respect swap
caryclark@google.com45a8fc62013-02-14 15:29:11 +000058 int insert(double one, double two, const _Point& pt);
caryclark@google.com639df892012-01-10 21:46:10 +000059
caryclark@google.com45a8fc62013-02-14 15:29:11 +000060 // start if index == 0 : end if index == 1
61 void insertCoincident(double one, double two, const _Point& pt) {
62 int index = insertSwap(one, two, pt);
63 int bit = 1 << index;
64 fIsCoincident[0] |= bit;
65 fIsCoincident[1] |= bit;
66 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000067
caryclark@google.com45a8fc62013-02-14 15:29:11 +000068 void insertCoincidentPair(double s1, double e1, double s2, double e2,
69 const _Point& startPt, const _Point& endPt);
70
71 int insertSwap(double one, double two, const _Point& pt) {
72 if (fSwap) {
73 return insert(two, one, pt);
74 } else {
75 return insert(one, two, pt);
76 }
77 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000078
caryclark@google.com235f56a2012-09-14 14:19:30 +000079 bool intersected() const {
caryclark@google.com639df892012-01-10 21:46:10 +000080 return fUsed > 0;
81 }
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000082
caryclark@google.com47d73da2013-02-17 01:41:25 +000083 void removeOne(int index);
skia.committer@gmail.come7707c22013-02-17 07:02:20 +000084
caryclark@google.com05c4bad2013-01-19 13:22:39 +000085 // leaves flip, swap alone
86 void reset() {
caryclark@google.com47d73da2013-02-17 01:41:25 +000087 fUsed = 0;
caryclark@google.com05c4bad2013-01-19 13:22:39 +000088 fUnsortable = false;
89 }
90
caryclark@google.com639df892012-01-10 21:46:10 +000091 void swap() {
caryclark@google.com73ca6242013-01-17 21:02:47 +000092 fSwap ^= true;
93 }
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000094
caryclark@google.com73ca6242013-01-17 21:02:47 +000095 void swapPts() {
96 int index;
97 for (index = 0; index < fUsed; ++index) {
98 SkTSwap(fT[0][index], fT[1][index]);
99 }
caryclark@google.com639df892012-01-10 21:46:10 +0000100 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
caryclark@google.com73ca6242013-01-17 21:02:47 +0000102 bool swapped() const {
caryclark@google.com639df892012-01-10 21:46:10 +0000103 return fSwap;
104 }
105
caryclark@google.com73ca6242013-01-17 21:02:47 +0000106 bool unsortable() const {
107 return fUnsortable;
108 }
109
110 int used() const {
caryclark@google.com639df892012-01-10 21:46:10 +0000111 return fUsed;
112 }
113
caryclark@google.comf9502d72013-02-04 14:06:49 +0000114 void downDepth() {
115 SkASSERT(--fDepth >= 0);
116 }
117
118 void upDepth() {
119 SkASSERT(++fDepth < 16);
120 }
121
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000122#if SK_DEBUG
123 int depth() const {
124 return fDepth;
125 }
126#endif
127
128 _Point fPt[9];
caryclark@google.com639df892012-01-10 21:46:10 +0000129 double fT[2][9];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000130 unsigned short fIsCoincident[2]; // bit arrays, one bit set for each coincident T
131 unsigned char fUsed;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000132 bool fFlip;
133 bool fUnsortable;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000134#if SK_DEBUG
135 int fDepth;
136#endif
caryclark@google.combeda3892013-02-07 13:13:41 +0000137protected:
138 // used by addCoincident to remove ordinary intersections in range
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000139 void remove(double one, double two, const _Point& startPt, const _Point& endPt);
caryclark@google.coma5764232012-03-28 16:20:21 +0000140private:
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000141 bool fSwap;
caryclark@google.com639df892012-01-10 21:46:10 +0000142};
caryclark@google.coma5764232012-03-28 16:20:21 +0000143
144#endif