| 1 | import clip from "./index.js";
|
|---|
| 2 | import {abs, atan, cos, epsilon, halfPi, pi, sin} from "../math.js";
|
|---|
| 3 |
|
|---|
| 4 | export default clip(
|
|---|
| 5 | function() { return true; },
|
|---|
| 6 | clipAntimeridianLine,
|
|---|
| 7 | clipAntimeridianInterpolate,
|
|---|
| 8 | [-pi, -halfPi]
|
|---|
| 9 | );
|
|---|
| 10 |
|
|---|
| 11 | // Takes a line and cuts into visible segments. Return values: 0 - there were
|
|---|
| 12 | // intersections or the line was empty; 1 - no intersections; 2 - there were
|
|---|
| 13 | // intersections, and the first and last segments should be rejoined.
|
|---|
| 14 | function clipAntimeridianLine(stream) {
|
|---|
| 15 | var lambda0 = NaN,
|
|---|
| 16 | phi0 = NaN,
|
|---|
| 17 | sign0 = NaN,
|
|---|
| 18 | clean; // no intersections
|
|---|
| 19 |
|
|---|
| 20 | return {
|
|---|
| 21 | lineStart: function() {
|
|---|
| 22 | stream.lineStart();
|
|---|
| 23 | clean = 1;
|
|---|
| 24 | },
|
|---|
| 25 | point: function(lambda1, phi1) {
|
|---|
| 26 | var sign1 = lambda1 > 0 ? pi : -pi,
|
|---|
| 27 | delta = abs(lambda1 - lambda0);
|
|---|
| 28 | if (abs(delta - pi) < epsilon) { // line crosses a pole
|
|---|
| 29 | stream.point(lambda0, phi0 = (phi0 + phi1) / 2 > 0 ? halfPi : -halfPi);
|
|---|
| 30 | stream.point(sign0, phi0);
|
|---|
| 31 | stream.lineEnd();
|
|---|
| 32 | stream.lineStart();
|
|---|
| 33 | stream.point(sign1, phi0);
|
|---|
| 34 | stream.point(lambda1, phi0);
|
|---|
| 35 | clean = 0;
|
|---|
| 36 | } else if (sign0 !== sign1 && delta >= pi) { // line crosses antimeridian
|
|---|
| 37 | if (abs(lambda0 - sign0) < epsilon) lambda0 -= sign0 * epsilon; // handle degeneracies
|
|---|
| 38 | if (abs(lambda1 - sign1) < epsilon) lambda1 -= sign1 * epsilon;
|
|---|
| 39 | phi0 = clipAntimeridianIntersect(lambda0, phi0, lambda1, phi1);
|
|---|
| 40 | stream.point(sign0, phi0);
|
|---|
| 41 | stream.lineEnd();
|
|---|
| 42 | stream.lineStart();
|
|---|
| 43 | stream.point(sign1, phi0);
|
|---|
| 44 | clean = 0;
|
|---|
| 45 | }
|
|---|
| 46 | stream.point(lambda0 = lambda1, phi0 = phi1);
|
|---|
| 47 | sign0 = sign1;
|
|---|
| 48 | },
|
|---|
| 49 | lineEnd: function() {
|
|---|
| 50 | stream.lineEnd();
|
|---|
| 51 | lambda0 = phi0 = NaN;
|
|---|
| 52 | },
|
|---|
| 53 | clean: function() {
|
|---|
| 54 | return 2 - clean; // if intersections, rejoin first and last segments
|
|---|
| 55 | }
|
|---|
| 56 | };
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | function clipAntimeridianIntersect(lambda0, phi0, lambda1, phi1) {
|
|---|
| 60 | var cosPhi0,
|
|---|
| 61 | cosPhi1,
|
|---|
| 62 | sinLambda0Lambda1 = sin(lambda0 - lambda1);
|
|---|
| 63 | return abs(sinLambda0Lambda1) > epsilon
|
|---|
| 64 | ? atan((sin(phi0) * (cosPhi1 = cos(phi1)) * sin(lambda1)
|
|---|
| 65 | - sin(phi1) * (cosPhi0 = cos(phi0)) * sin(lambda0))
|
|---|
| 66 | / (cosPhi0 * cosPhi1 * sinLambda0Lambda1))
|
|---|
| 67 | : (phi0 + phi1) / 2;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | function clipAntimeridianInterpolate(from, to, direction, stream) {
|
|---|
| 71 | var phi;
|
|---|
| 72 | if (from == null) {
|
|---|
| 73 | phi = direction * halfPi;
|
|---|
| 74 | stream.point(-pi, phi);
|
|---|
| 75 | stream.point(0, phi);
|
|---|
| 76 | stream.point(pi, phi);
|
|---|
| 77 | stream.point(pi, 0);
|
|---|
| 78 | stream.point(pi, -phi);
|
|---|
| 79 | stream.point(0, -phi);
|
|---|
| 80 | stream.point(-pi, -phi);
|
|---|
| 81 | stream.point(-pi, 0);
|
|---|
| 82 | stream.point(-pi, phi);
|
|---|
| 83 | } else if (abs(from[0] - to[0]) > epsilon) {
|
|---|
| 84 | var lambda = from[0] < to[0] ? pi : -pi;
|
|---|
| 85 | phi = direction * lambda / 2;
|
|---|
| 86 | stream.point(-lambda, phi);
|
|---|
| 87 | stream.point(0, phi);
|
|---|
| 88 | stream.point(lambda, phi);
|
|---|
| 89 | } else {
|
|---|
| 90 | stream.point(to[0], to[1]);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|