Skip to content

Commit

Permalink
inline code from d3-geo-projection (closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Oct 28, 2018
1 parent ab7ff70 commit 9de8ff5
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-geo-polygon",
"version": "1.6.1",
"version": "1.6.2",
"description": "Clipping and geometric operations for spherical polygons.",
"keywords": [
"d3",
Expand Down
1 change: 0 additions & 1 deletion src/clip/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function(geometry) {
} else {
return clipNone;
}
polygons.forEach(polygon => (polygon.mark = geometry.mark));

var clips = polygons.map(function(polygon) {
polygon = polygon.map(ringRadians);
Expand Down
2 changes: 1 addition & 1 deletion src/cubic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/
import voronoi from "./polyhedral/voronoi";
import { default as cube } from "../node_modules/d3-geo-projection/src/polyhedral/cube";
import { default as cube } from "./polyhedral/cube";

export default function() {
var polygons = {
Expand Down
61 changes: 44 additions & 17 deletions src/math.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
export var epsilon = 1e-6;
export var epsilon2 = 1e-12;
export var pi = Math.PI;
export var halfPi = pi / 2;
export var quarterPi = pi / 4;
export var tau = pi * 2;

export var degrees = 180 / pi;
export var radians = pi / 180;

export var abs = Math.abs;
export var atan = Math.atan;
export var atan2 = Math.atan2;
export var cos = Math.cos;
export var ceil = Math.ceil;
export var cos = Math.cos;
export var exp = Math.exp;
export var floor = Math.floor;
export var log = Math.log;
export var max = Math.max;
export var min = Math.min;
export var pow = Math.pow;
export var sin = Math.sin;
export var round = Math.round;
export var sign = Math.sign || function(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; };
export var sqrt = Math.sqrt;
export var sin = Math.sin;
export var tan = Math.tan;

export function acos(x) {
return x > 1 ? 0 : x < -1 ? pi : Math.acos(x);
export var epsilon = 1e-6;
export var epsilon2 = 1e-12;
export var pi = Math.PI;
export var halfPi = pi / 2;
export var quarterPi = pi / 4;
export var sqrt1_2 = Math.SQRT1_2;
export var sqrt2 = sqrt(2);
export var sqrtPi = sqrt(pi);
export var tau = pi * 2;
export var degrees = 180 / pi;
export var radians = pi / 180;

export function sinci(x) {
return x ? x / Math.sin(x) : 1;
}

export function asin(x) {
return x > 1 ? halfPi : x < -1 ? -halfPi : Math.asin(x);
}

export function haversin(x) {
return (x = sin(x / 2)) * x;
export function acos(x) {
return x > 1 ? 0 : x < -1 ? pi : Math.acos(x);
}

export function sqrt(x) {
return x > 0 ? Math.sqrt(x) : 0;
}

export function tanh(x) {
x = exp(2 * x);
return (x - 1) / (x + 1);
}

export function sinh(x) {
return (exp(x) - exp(-x)) / 2;
}

export function cosh(x) {
return (exp(x) + exp(-x)) / 2;
}

export function arsinh(x) {
return log(x + sqrt(x * x + 1));
}

export function arcosh(x) {
return log(x + sqrt(x * x - 1));
}
4 changes: 2 additions & 2 deletions src/polyhedral/butterfly.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {geoCentroid as centroid, geoGnomonic as gnomonic} from "d3-geo";
import {pi} from "../../node_modules/d3-geo-projection/src/math";
import {pi} from "../math";
import polyhedral from "./index";
import octahedron from "../../node_modules/d3-geo-projection/src/polyhedral/octahedron";
import octahedron from "./octahedron";

export default function(faceProjection) {

Expand Down
4 changes: 2 additions & 2 deletions src/polyhedral/collignon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {geoCentroid as centroid, geoProjection as projection} from "d3-geo";
import {geoCollignonRaw as collignonRaw} from "d3-geo-projection";
import {pi, sqrt} from "../../node_modules/d3-geo-projection/src/math";
import {pi, sqrt} from "../math";
import polyhedral from "./index";
import octahedron from "../../node_modules/d3-geo-projection/src/polyhedral/octahedron";
import octahedron from "./octahedron";

var kx = 2 / sqrt(3);

Expand Down
21 changes: 21 additions & 0 deletions src/polyhedral/cube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {atan, degrees, sqrt1_2} from "../math";

var phi1 = atan(sqrt1_2) * degrees;

var cube = [
[0, phi1], [90, phi1], [180, phi1], [-90, phi1],
[0, -phi1], [90, -phi1], [180, -phi1], [-90, -phi1]
];

export default [
[0, 3, 2, 1], // N
[0, 1, 5, 4],
[1, 2, 6, 5],
[2, 3, 7, 6],
[3, 0, 4, 7],
[4, 5, 6, 7] // S
].map(function(face) {
return face.map(function(i) {
return cube[i];
});
});
4 changes: 2 additions & 2 deletions src/polyhedral/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {geoBounds as bounds, geoCentroid as centroid, geoInterpolate as interpolate, geoProjection as projection} from "d3-geo";
import {default as clipPolygon} from "../clip/polygon";
import {abs, degrees, epsilon, radians} from "../../node_modules/d3-geo-projection/src/math";
import {default as matrix, multiply, inverse} from "../../node_modules/d3-geo-projection/src/polyhedral/matrix";
import {abs, degrees, epsilon, radians} from "../math";
import {default as matrix, multiply, inverse} from "./matrix";

// Creates a polyhedral projection.
// * tree: a spanning tree of polygon faces. Nodes are automatically
Expand Down
64 changes: 64 additions & 0 deletions src/polyhedral/matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {atan2, cos, sin, sqrt} from "../math";

// Note: 6-element arrays are used to denote the 3x3 affine transform matrix:
// [a, b, c,
// d, e, f,
// 0, 0, 1] - this redundant row is left out.

// Transform matrix for [a0, a1] -> [b0, b1].
export default function(a, b) {
var u = subtract(a[1], a[0]),
v = subtract(b[1], b[0]),
phi = angle(u, v),
s = length(u) / length(v);

return multiply([
1, 0, a[0][0],
0, 1, a[0][1]
], multiply([
s, 0, 0,
0, s, 0
], multiply([
cos(phi), sin(phi), 0,
-sin(phi), cos(phi), 0
], [
1, 0, -b[0][0],
0, 1, -b[0][1]
])));
}

// Inverts a transform matrix.
export function inverse(m) {
var k = 1 / (m[0] * m[4] - m[1] * m[3]);
return [
k * m[4], -k * m[1], k * (m[1] * m[5] - m[2] * m[4]),
-k * m[3], k * m[0], k * (m[2] * m[3] - m[0] * m[5])
];
}

// Multiplies two 3x2 matrices.
export function multiply(a, b) {
return [
a[0] * b[0] + a[1] * b[3],
a[0] * b[1] + a[1] * b[4],
a[0] * b[2] + a[1] * b[5] + a[2],
a[3] * b[0] + a[4] * b[3],
a[3] * b[1] + a[4] * b[4],
a[3] * b[2] + a[4] * b[5] + a[5]
];
}

// Subtracts 2D vectors.
function subtract(a, b) {
return [a[0] - b[0], a[1] - b[1]];
}

// Magnitude of a 2D vector.
function length(v) {
return sqrt(v[0] * v[0] + v[1] * v[1]);
}

// Angle between two 2D vectors.
function angle(a, b) {
return atan2(a[0] * b[1] - a[1] * b[0], a[0] * b[0] + a[1] * b[1]);
}
21 changes: 21 additions & 0 deletions src/polyhedral/octahedron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// TODO generate on-the-fly to avoid external modification.
var octahedron = [
[0, 90],
[-90, 0], [0, 0], [90, 0], [180, 0],
[0, -90]
];

export default [
[0, 2, 1],
[0, 3, 2],
[5, 1, 2],
[5, 2, 3],
[0, 1, 4],
[0, 4, 3],
[5, 4, 1],
[5, 3, 4]
].map(function(face) {
return face.map(function(i) {
return octahedron[i];
});
});
4 changes: 2 additions & 2 deletions src/polyhedral/waterman.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {geoCentroid as centroid, geoGnomonic as gnomonic} from "d3-geo";
import {asin, atan2, cos, degrees, max, min, pi, radians, sin} from "../../node_modules/d3-geo-projection/src/math";
import {asin, atan2, cos, degrees, max, min, pi, radians, sin} from "../math";
import polyhedral from "./index";
import octahedron from "../../node_modules/d3-geo-projection/src/polyhedral/octahedron";
import octahedron from "./octahedron";

export default function(faceProjection) {

Expand Down

0 comments on commit 9de8ff5

Please sign in to comment.