blob: c1e1ce904192b577e14b1a4cdc9b1e8cdd75d1df [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) {
23 if (fUsed > 0 && approximately_equal(fT[fSwap][fUsed - 1], one)
24 && approximately_equal(fT[fSwap ^ 1][fUsed - 1], two)) {
25 return;
26 }
27 fT[fSwap][fUsed] = one;
28 fT[fSwap ^ 1][fUsed] = two;
29 ++fUsed;
30 }
31
caryclark@google.coma7e483d2012-08-28 20:44:43 +000032 // start if index == 0 : end if index == 1
33 void addCoincident(double one, double two) {
34 if (fCoincidentUsed > 0
35 && approximately_equal(fCoincidentT[fSwap][fCoincidentUsed - 1], one)
36 && approximately_equal(fCoincidentT[fSwap ^ 1][fCoincidentUsed - 1], two)) {
37 --fCoincidentUsed;
38 return;
39 }
40 fCoincidentT[fSwap][fCoincidentUsed] = one;
41 fCoincidentT[fSwap ^ 1][fCoincidentUsed] = two;
42 ++fCoincidentUsed;
43 }
44
caryclark@google.com639df892012-01-10 21:46:10 +000045 void offset(int base, double start, double end) {
46 for (int index = base; index < fUsed; ++index) {
47 double val = fT[fSwap][index];
48 val *= end - start;
49 val += start;
50 fT[fSwap][index] = val;
51 }
52 }
53
54 bool intersected() {
55 return fUsed > 0;
56 }
57
58 void swap() {
59 fSwap ^= 1;
60 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000061
caryclark@google.com639df892012-01-10 21:46:10 +000062 bool swapped() {
63 return fSwap;
64 }
65
66 int used() {
67 return fUsed;
68 }
69
70 double fT[2][9];
caryclark@google.coma7e483d2012-08-28 20:44:43 +000071 double fCoincidentT[2][9];
caryclark@google.com639df892012-01-10 21:46:10 +000072 int fUsed;
caryclark@google.coma7e483d2012-08-28 20:44:43 +000073 int fCoincidentUsed;
caryclark@google.coma5764232012-03-28 16:20:21 +000074private:
caryclark@google.com639df892012-01-10 21:46:10 +000075 int fSwap;
76};
caryclark@google.coma5764232012-03-28 16:20:21 +000077
78#endif
79