caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 7 | #ifndef Intersections_DEFINE |
| 8 | #define Intersections_DEFINE |
| 9 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 10 | class Intersections { |
| 11 | public: |
| 12 | Intersections() |
| 13 | : fUsed(0) |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 14 | , fCoincidentUsed(0) |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 15 | , fSwap(0) |
| 16 | { |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 17 | // OPTIMIZE: don't need to be initialized in release |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 18 | bzero(fT, sizeof(fT)); |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 19 | bzero(fCoincidentT, sizeof(fCoincidentT)); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void add(double one, double two) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 23 | 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.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 28 | } |
| 29 | fT[fSwap][fUsed] = one; |
| 30 | fT[fSwap ^ 1][fUsed] = two; |
| 31 | ++fUsed; |
| 32 | } |
| 33 | |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 34 | // start if index == 0 : end if index == 1 |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 35 | 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.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 44 | } |
| 45 | fCoincidentT[fSwap][fCoincidentUsed] = one; |
| 46 | fCoincidentT[fSwap ^ 1][fCoincidentUsed] = two; |
| 47 | ++fCoincidentUsed; |
| 48 | } |
| 49 | |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 50 | int coincidentUsed() { |
| 51 | return fCoincidentUsed; |
| 52 | } |
| 53 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 54 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 71 | bool swapped() { |
| 72 | return fSwap; |
| 73 | } |
| 74 | |
| 75 | int used() { |
| 76 | return fUsed; |
| 77 | } |
| 78 | |
| 79 | double fT[2][9]; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 80 | double fCoincidentT[2][9]; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 81 | int fUsed; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 82 | int fCoincidentUsed; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 83 | private: |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 84 | int fSwap; |
| 85 | }; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 86 | |
| 87 | #endif |
| 88 | |