blob: 9a35a7db89c190cdf76ff0c69bfb0640eb9b97a5 [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 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000066
caryclark@google.com45a8fc62013-02-14 15:29:11 +000067 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 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000077
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.com47d73da2013-02-17 01:41:25 +000082 void removeOne(int index);
skia.committer@gmail.come7707c22013-02-17 07:02:20 +000083
caryclark@google.com05c4bad2013-01-19 13:22:39 +000084 // leaves flip, swap alone
85 void reset() {
caryclark@google.com47d73da2013-02-17 01:41:25 +000086 fUsed = 0;
caryclark@google.com05c4bad2013-01-19 13:22:39 +000087 fUnsortable = false;
88 }
89
caryclark@google.com639df892012-01-10 21:46:10 +000090 void swap() {
caryclark@google.com73ca6242013-01-17 21:02:47 +000091 fSwap ^= true;
92 }
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000093
caryclark@google.com73ca6242013-01-17 21:02:47 +000094 void swapPts() {
95 int index;
96 for (index = 0; index < fUsed; ++index) {
97 SkTSwap(fT[0][index], fT[1][index]);
98 }
caryclark@google.com639df892012-01-10 21:46:10 +000099 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
caryclark@google.com73ca6242013-01-17 21:02:47 +0000101 bool swapped() const {
caryclark@google.com639df892012-01-10 21:46:10 +0000102 return fSwap;
103 }
104
caryclark@google.com73ca6242013-01-17 21:02:47 +0000105 bool unsortable() const {
106 return fUnsortable;
107 }
108
109 int used() const {
caryclark@google.com639df892012-01-10 21:46:10 +0000110 return fUsed;
111 }
112
caryclark@google.comf9502d72013-02-04 14:06:49 +0000113 void downDepth() {
114 SkASSERT(--fDepth >= 0);
115 }
116
117 void upDepth() {
118 SkASSERT(++fDepth < 16);
119 }
120
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000121#if SK_DEBUG
122 int depth() const {
123 return fDepth;
124 }
125#endif
126
127 _Point fPt[9];
caryclark@google.com639df892012-01-10 21:46:10 +0000128 double fT[2][9];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000129 unsigned short fIsCoincident[2]; // bit arrays, one bit set for each coincident T
130 unsigned char fUsed;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000131 bool fFlip;
132 bool fUnsortable;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000133#if SK_DEBUG
134 int fDepth;
135#endif
caryclark@google.combeda3892013-02-07 13:13:41 +0000136protected:
137 // used by addCoincident to remove ordinary intersections in range
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000138 void remove(double one, double two, const _Point& startPt, const _Point& endPt);
caryclark@google.coma5764232012-03-28 16:20:21 +0000139private:
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000140 bool fSwap;
caryclark@google.com639df892012-01-10 21:46:10 +0000141};
caryclark@google.coma5764232012-03-28 16:20:21 +0000142
143#endif