blob: d339116b798058b742f322ae60f5f301be7d4896 [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.com45a8fc62013-02-14 15:29:11 +000057 int insert(double one, double two, const _Point& pt);
caryclark@google.com639df892012-01-10 21:46:10 +000058
caryclark@google.com45a8fc62013-02-14 15:29:11 +000059 // start if index == 0 : end if index == 1
60 void insertCoincident(double one, double two, const _Point& pt) {
61 int index = insertSwap(one, two, pt);
62 int bit = 1 << index;
63 fIsCoincident[0] |= bit;
64 fIsCoincident[1] |= bit;
65 }
66
67 void insertCoincidentPair(double s1, double e1, double s2, double e2,
68 const _Point& startPt, const _Point& endPt);
69
70 int insertSwap(double one, double two, const _Point& pt) {
71 if (fSwap) {
72 return insert(two, one, pt);
73 } else {
74 return insert(one, two, pt);
75 }
76 }
77
caryclark@google.com235f56a2012-09-14 14:19:30 +000078 bool intersected() const {
caryclark@google.com639df892012-01-10 21:46:10 +000079 return fUsed > 0;
80 }
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000081
caryclark@google.com05c4bad2013-01-19 13:22:39 +000082 // leaves flip, swap alone
83 void reset() {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000084 fUsed = /* fUsed2 = fCoincidentUsed = */ 0;
caryclark@google.com05c4bad2013-01-19 13:22:39 +000085 fUnsortable = false;
86 }
87
caryclark@google.com639df892012-01-10 21:46:10 +000088 void swap() {
caryclark@google.com73ca6242013-01-17 21:02:47 +000089 fSwap ^= true;
90 }
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000091
caryclark@google.com73ca6242013-01-17 21:02:47 +000092 void swapPts() {
93 int index;
94 for (index = 0; index < fUsed; ++index) {
95 SkTSwap(fT[0][index], fT[1][index]);
96 }
caryclark@google.com639df892012-01-10 21:46:10 +000097 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
caryclark@google.com73ca6242013-01-17 21:02:47 +000099 bool swapped() const {
caryclark@google.com639df892012-01-10 21:46:10 +0000100 return fSwap;
101 }
102
caryclark@google.com73ca6242013-01-17 21:02:47 +0000103 bool unsortable() const {
104 return fUnsortable;
105 }
106
107 int used() const {
caryclark@google.com639df892012-01-10 21:46:10 +0000108 return fUsed;
109 }
110
caryclark@google.comf9502d72013-02-04 14:06:49 +0000111 void downDepth() {
112 SkASSERT(--fDepth >= 0);
113 }
114
115 void upDepth() {
116 SkASSERT(++fDepth < 16);
117 }
118
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000119#if SK_DEBUG
120 int depth() const {
121 return fDepth;
122 }
123#endif
124
125 _Point fPt[9];
caryclark@google.com639df892012-01-10 21:46:10 +0000126 double fT[2][9];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000127 unsigned short fIsCoincident[2]; // bit arrays, one bit set for each coincident T
128 unsigned char fUsed;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000129 bool fFlip;
130 bool fUnsortable;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000131#if SK_DEBUG
132 int fDepth;
133#endif
caryclark@google.combeda3892013-02-07 13:13:41 +0000134protected:
135 // used by addCoincident to remove ordinary intersections in range
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000136 void remove(double one, double two, const _Point& startPt, const _Point& endPt);
caryclark@google.coma5764232012-03-28 16:20:21 +0000137private:
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000138 bool fSwap;
caryclark@google.com639df892012-01-10 21:46:10 +0000139};
caryclark@google.coma5764232012-03-28 16:20:21 +0000140
141#endif