Skip to content

Commit

Permalink
simplify (use reclip for HEALPix)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 16, 2024
1 parent 6801072 commit cffb782
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions src/reclip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {merge, range} from "d3-array";
import {merge} from "d3-array";
import {geoInterpolate} from "d3-geo";
import {
geoBerghaus as berghaus,
Expand All @@ -19,17 +19,17 @@ import geoClipPolygon from "./clip/polygon.js";
*/
export function geoBerghaus() { return reclip(berghaus.apply(this, arguments)); }
export function geoGingery() { return reclip(gingery.apply(this, arguments)); }
export function geoHealpix() {return clipInterrupted(healpix.apply(this, arguments)); }
export function geoHealpix() { return reclip(healpix.apply(this, arguments), true); }
export function geoInterrupt() { return clipInterrupted(interrupt.apply(this, arguments)); }
export function geoInterruptedBoggs() { return clipInterrupted(interruptedBoggs.apply(this, arguments)); }
export function geoInterruptedHomolosine() {return clipInterrupted(interruptedHomolosine.apply(this, arguments)); }
export function geoInterruptedMollweide() {return clipInterrupted(interruptedMollweide.apply(this, arguments)); }
export function geoInterruptedMollweideHemispheres() {return clipInterrupted(interruptedMollweideHemispheres.apply(this, arguments)); }
export function geoInterruptedSinuMollweide() {return clipInterrupted(interruptedSinuMollweide.apply(this, arguments)); }
export function geoInterruptedSinusoidal() {return clipInterrupted(interruptedSinusoidal.apply(this, arguments)); }
export function geoInterruptedHomolosine() { return clipInterrupted(interruptedHomolosine.apply(this, arguments)); }
export function geoInterruptedMollweide() { return clipInterrupted(interruptedMollweide.apply(this, arguments)); }
export function geoInterruptedMollweideHemispheres() { return clipInterrupted(interruptedMollweideHemispheres.apply(this, arguments)); }
export function geoInterruptedSinuMollweide() { return clipInterrupted(interruptedSinuMollweide.apply(this, arguments)); }
export function geoInterruptedSinusoidal() { return clipInterrupted(interruptedSinusoidal.apply(this, arguments)); }

function reclip(projection) {
const { lobes } = projection;
function reclip(projection, vertical = false) {
const {lobes} = projection;
function reset(projection) {
const rotate = projection.rotate();
const scale = projection.scale();
Expand All @@ -40,26 +40,29 @@ function reclip(projection) {
};

projection.preclip((stream) => stream); // clipNone
const R = 1 - 1e-7;
const Rx = vertical ? 1 : R;
let points = [];
projection
.stream({
point(x, y) {points.push(([x, y]));},
point(x, y) {
points.push([x * Rx, y * R]);
},
lineStart() {},
lineEnd() {},
polygonStart() {},
polygonEnd() {},
sphere() {}
sphere() {},
})
.sphere();

projection.scale(scale * 1.0001);
projection.scale(scale);
points = points.map(projection.invert);
points.push(points[0]);

return projection
.rotate(rotate)
.translate(translate)
.scale(scale)
.preclip(geoClipPolygon({ type: "Polygon", coordinates: [points] }));
}
return reset(projection);
Expand All @@ -68,27 +71,7 @@ function reclip(projection) {
function clipInterrupted(projection) {
const { lobes } = projection;
function reset(projection) {
let l = lobes?.();

// Special treatment for HEALpix
if (typeof l === "number") {
const hp = 41 + 48 / 36 + 37 / 3600; // healpixParallel, for K=3
const step = 360 / (l + 1e-10);
l = [
range(l).map((i) => [
[-180 + i * step, hp],
[-180 + ((2 * i + 1) / 2) * step, 90],
[-180 + (i + 1) * step, hp],
]),
range(l).map((i) => [
[-180 + i * step, -hp],
[-180 + ((2 * i + 1) / 2) * step, -90],
[-180 + (i + 1) * step, -hp],
]),
];
}
if (!Array.isArray(l)) return projection;

const l = lobes?.();
const polygon = merge(
Array.from(l, (d, i) => {
const hemisphere = d.flatMap(
Expand Down
Binary file modified test/snapshots/berghaus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/berghaus13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/berghaus7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/gingery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/gingery3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/gingery7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/healpix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/snapshots/healpix5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cffb782

Please sign in to comment.