blob: 57cebf74c8ad7959aa08c97eb93c5faf97b0825b [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:
12 Intersections()
13 : fUsed(0)
caryclark@google.coma7e483d2012-08-28 20:44:43 +000014 , fCoincidentUsed(0)
caryclark@google.com639df892012-01-10 21:46:10 +000015 , fSwap(0)
16 {
caryclark@google.coma7e483d2012-08-28 20:44:43 +000017 // OPTIMIZE: don't need to be initialized in release
caryclark@google.com639df892012-01-10 21:46:10 +000018 bzero(fT, sizeof(fT));
caryclark@google.coma7e483d2012-08-28 20:44:43 +000019 bzero(fCoincidentT, sizeof(fCoincidentT));
caryclark@google.com639df892012-01-10 21:46:10 +000020 }
21
22 void add(double one, double two) {
caryclark@google.com32546db2012-08-31 20:55:07 +000023 for (int index = 0; index < fUsed; ++index) {
24 if (approximately_equal(fT[fSwap][index], one)
25 && approximately_equal(fT[fSwap ^ 1][index], two)) {
26 return;
27 }
caryclark@google.com639df892012-01-10 21:46:10 +000028 }
29 fT[fSwap][fUsed] = one;
30 fT[fSwap ^ 1][fUsed] = two;
31 ++fUsed;
32 }
33
caryclark@google.coma7e483d2012-08-28 20:44:43 +000034 // start if index == 0 : end if index == 1
caryclark@google.com32546db2012-08-31 20:55:07 +000035 void addCoincident(double one, double two, bool cancel) {
36 for (int index = 0; index < fCoincidentUsed; ++index) {
37 if (approximately_equal(fCoincidentT[fSwap][index], one)
38 && approximately_equal(fCoincidentT[fSwap ^ 1][index], two)) {
39 if (cancel) {
40 --fCoincidentUsed;
41 }
42 return;
43 }
caryclark@google.coma7e483d2012-08-28 20:44:43 +000044 }
45 fCoincidentT[fSwap][fCoincidentUsed] = one;
46 fCoincidentT[fSwap ^ 1][fCoincidentUsed] = two;
47 ++fCoincidentUsed;
48 }
49
caryclark@google.com32546db2012-08-31 20:55:07 +000050 int coincidentUsed() {
51 return fCoincidentUsed;
52 }
53
caryclark@google.com639df892012-01-10 21:46:10 +000054 void offset(int base, double start, double end) {
55 for (int index = base; index < fUsed; ++index) {
56 double val = fT[fSwap][index];
57 val *= end - start;
58 val += start;
59 fT[fSwap][index] = val;
60 }
61 }
62
63 bool intersected() {
64 return fUsed > 0;
65 }
66
67 void swap() {
68 fSwap ^= 1;
69 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
caryclark@google.com639df892012-01-10 21:46:10 +000071 bool swapped() {
72 return fSwap;
73 }
74
75 int used() {
76 return fUsed;
77 }
78
79 double fT[2][9];
caryclark@google.coma7e483d2012-08-28 20:44:43 +000080 double fCoincidentT[2][9];
caryclark@google.com639df892012-01-10 21:46:10 +000081 int fUsed;
caryclark@google.coma7e483d2012-08-28 20:44:43 +000082 int fCoincidentUsed;
caryclark@google.coma5764232012-03-28 16:20:21 +000083private:
caryclark@google.com639df892012-01-10 21:46:10 +000084 int fSwap;
85};
caryclark@google.coma5764232012-03-28 16:20:21 +000086
87#endif
88