2012-08-27 14:11:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-03-28 16:20:21 +00:00
|
|
|
#ifndef Intersections_DEFINE
|
|
|
|
#define Intersections_DEFINE
|
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
#include <algorithm> // for std::min -- Skia doesn't have a SkMinDouble
|
|
|
|
#include "SkTypes.h"
|
2012-09-14 14:19:30 +00:00
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
class Intersections {
|
|
|
|
public:
|
2013-01-20 07:05:51 +00:00
|
|
|
Intersections()
|
2013-01-19 13:22:39 +00:00
|
|
|
: fFlip(0)
|
2012-01-10 21:46:10 +00:00
|
|
|
, fSwap(0)
|
|
|
|
{
|
2012-08-28 20:44:43 +00:00
|
|
|
// OPTIMIZE: don't need to be initialized in release
|
2012-01-10 21:46:10 +00:00
|
|
|
bzero(fT, sizeof(fT));
|
2012-08-28 20:44:43 +00:00
|
|
|
bzero(fCoincidentT, sizeof(fCoincidentT));
|
2013-01-19 13:22:39 +00:00
|
|
|
reset();
|
2012-01-10 21:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add(double one, double two) {
|
2012-08-31 20:55:07 +00:00
|
|
|
for (int index = 0; index < fUsed; ++index) {
|
|
|
|
if (approximately_equal(fT[fSwap][index], one)
|
|
|
|
&& approximately_equal(fT[fSwap ^ 1][index], two)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-01-10 21:46:10 +00:00
|
|
|
}
|
2012-09-14 14:19:30 +00:00
|
|
|
assert(fUsed < 9);
|
2012-01-10 21:46:10 +00:00
|
|
|
fT[fSwap][fUsed] = one;
|
|
|
|
fT[fSwap ^ 1][fUsed] = two;
|
|
|
|
++fUsed;
|
|
|
|
}
|
|
|
|
|
2012-08-28 20:44:43 +00:00
|
|
|
// start if index == 0 : end if index == 1
|
2012-09-14 14:19:30 +00:00
|
|
|
void addCoincident(double one, double two) {
|
2012-08-31 20:55:07 +00:00
|
|
|
for (int index = 0; index < fCoincidentUsed; ++index) {
|
|
|
|
if (approximately_equal(fCoincidentT[fSwap][index], one)
|
|
|
|
&& approximately_equal(fCoincidentT[fSwap ^ 1][index], two)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-28 20:44:43 +00:00
|
|
|
}
|
2012-09-14 14:19:30 +00:00
|
|
|
assert(fCoincidentUsed < 9);
|
2012-08-28 20:44:43 +00:00
|
|
|
fCoincidentT[fSwap][fCoincidentUsed] = one;
|
|
|
|
fCoincidentT[fSwap ^ 1][fCoincidentUsed] = two;
|
|
|
|
++fCoincidentUsed;
|
|
|
|
}
|
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
void addCoincident(double s1, double e1, double s2, double e2);
|
2012-09-14 14:19:30 +00:00
|
|
|
|
|
|
|
// FIXME: this is necessary because curve/curve intersections are noisy
|
|
|
|
// remove once curve/curve intersections are improved
|
|
|
|
void cleanUp();
|
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
int coincidentUsed() const{
|
2012-08-31 20:55:07 +00:00
|
|
|
return fCoincidentUsed;
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
void offset(int base, double start, double end) {
|
|
|
|
for (int index = base; index < fUsed; ++index) {
|
|
|
|
double val = fT[fSwap][index];
|
|
|
|
val *= end - start;
|
|
|
|
val += start;
|
|
|
|
fT[fSwap][index] = val;
|
|
|
|
}
|
|
|
|
}
|
2012-09-15 02:01:41 +00:00
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
void insert(double one, double two);
|
|
|
|
void insertOne(double t, int side);
|
2012-01-10 21:46:10 +00:00
|
|
|
|
2012-09-14 14:19:30 +00:00
|
|
|
bool intersected() const {
|
2012-01-10 21:46:10 +00:00
|
|
|
return fUsed > 0;
|
|
|
|
}
|
2012-09-15 02:01:41 +00:00
|
|
|
|
2012-09-14 14:19:30 +00:00
|
|
|
bool insertBalanced() const {
|
|
|
|
return fUsed == fUsed2;
|
|
|
|
}
|
2012-01-10 21:46:10 +00:00
|
|
|
|
2013-01-19 13:22:39 +00:00
|
|
|
// leaves flip, swap alone
|
|
|
|
void reset() {
|
|
|
|
fUsed = fUsed2 = fCoincidentUsed = 0;
|
|
|
|
fUnsortable = false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
void swap() {
|
2013-01-17 21:02:47 +00:00
|
|
|
fSwap ^= true;
|
|
|
|
}
|
2013-01-18 07:07:28 +00:00
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
void swapPts() {
|
|
|
|
int index;
|
|
|
|
for (index = 0; index < fUsed; ++index) {
|
|
|
|
SkTSwap(fT[0][index], fT[1][index]);
|
|
|
|
}
|
2012-01-10 21:46:10 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
bool swapped() const {
|
2012-01-10 21:46:10 +00:00
|
|
|
return fSwap;
|
|
|
|
}
|
|
|
|
|
2013-01-17 21:02:47 +00:00
|
|
|
bool unsortable() const {
|
|
|
|
return fUnsortable;
|
|
|
|
}
|
|
|
|
|
|
|
|
int used() const {
|
2012-01-10 21:46:10 +00:00
|
|
|
return fUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
double fT[2][9];
|
2012-08-28 20:44:43 +00:00
|
|
|
double fCoincidentT[2][9];
|
2012-01-10 21:46:10 +00:00
|
|
|
int fUsed;
|
2012-09-14 14:19:30 +00:00
|
|
|
int fUsed2;
|
2012-08-28 20:44:43 +00:00
|
|
|
int fCoincidentUsed;
|
2013-01-17 21:02:47 +00:00
|
|
|
bool fFlip;
|
|
|
|
bool fUnsortable;
|
2012-03-28 16:20:21 +00:00
|
|
|
private:
|
2012-01-10 21:46:10 +00:00
|
|
|
int fSwap;
|
|
|
|
};
|
2012-03-28 16:20:21 +00:00
|
|
|
|
|
|
|
#endif
|