Skip to content

Commit a306a37

Browse files
committed
Firefly-2122: improve HiPS accuracy around poles
1 parent 4d8064c commit a306a37

3 files changed

Lines changed: 91 additions & 20 deletions

File tree

‎src/firefly/js/visualize/iv/HiPSCellFinder.js‎

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,39 @@ import {makeDevicePt} from '../Point';
88
import {CysConverter} from '../CsysConverter';
99

1010
const imageOffsets= [[0,0],[0,1],[1,0],[1,1]]; // which half of the parent tile image, in units of the child tile size
11+
const POLE_DEGREES= 10; // tiles with any corner within this many degrees of a healpix pole are subdivided more
12+
const POLE_SUBDIVIDE_DEPTH= 3; // number of levels to subdivide a tile near the pole
13+
const INNER_POLE_DEGREES= 2; // when zoomed beyond the tile norder, tiles this close to a pole are subdivided even more
14+
const INNER_POLE_SUBDIVIDE_DEPTH= 4; // number of levels to subdivide a tile in the inner pole area when zoomed
15+
const DEEP_ZOOM_OVER= 3; // when zoomed more than this many orders beyond the tile norder, use DEEP_INNER_POLE_SUBDIVIDE_DEPTH
16+
const DEEP_INNER_POLE_SUBDIVIDE_DEPTH= 5; // number of levels to subdivide a tile in the inner pole area when deeply zoomed
17+
const MIN_POLE_SPLIT_SIZE= 32; // device pixels, a tile smaller than this on the screen is not subdivided because of a pole
18+
19+
/**
20+
* @param {Array.<WorldPt>} wpCorners - corners in the HiPS data coordinate system
21+
* @param {number} zoomOver - how many orders the desired norder is beyond the tile norder, 0 if not zoomed beyond
22+
* @return {number} how many levels below the base tile a tile with these corners should be subdivided because of pole distortion
23+
*/
24+
function getPoleDepth(wpCorners, zoomOver) {
25+
const maxLat= Math.max(...wpCorners.map( (wp) => wp ? Math.abs(wp.y) : 0));
26+
if (zoomOver>0 && maxLat >= 90-INNER_POLE_DEGREES) {
27+
return zoomOver>DEEP_ZOOM_OVER ? DEEP_INNER_POLE_SUBDIVIDE_DEPTH : INNER_POLE_SUBDIVIDE_DEPTH;
28+
}
29+
if (maxLat >= 90-POLE_DEGREES) return POLE_SUBDIVIDE_DEPTH;
30+
return 0;
31+
}
32+
33+
/**
34+
* @param {Array.<DevicePt>} devPtCorners
35+
* @return {number} the larger of the width or height of the bounding box of the corners
36+
*/
37+
function getDevSize(devPtCorners) {
38+
const pts= devPtCorners.filter(Boolean);
39+
if (!pts.length) return 0;
40+
const xs= pts.map( (pt) => pt.x);
41+
const ys= pts.map( (pt) => pt.y);
42+
return Math.max(Math.max(...xs)-Math.min(...xs), Math.max(...ys)-Math.min(...ys));
43+
}
1144

1245
/**
1346
* @global
@@ -19,6 +52,7 @@ const imageOffsets= [[0,0],[0,1],[1,0],[1,1]]; // which half of the parent tile
1952
* @prop {Array.<DevicePt>} devPtCorners - the target corners of the tile in device coordinates
2053
* @prop {number} dx - x offset into image, for a subCell of a normal tile: 0 or 1, the half of the parent tile
2154
* @prop {number} dy - y offset into image, for a subCell of a normal tile: 0 or 1, the half of the parent tile
55+
* @prop {Array.<WorldPt>} [wpCorners] - the corners in the data coordinate system, only set on subCells
2256
* @prop {boolean} [incompleteCell] - some corners cannot be projected or the cell wraps, only the subCells are drawn
2357
* @prop {Array.<HiPSDeviceTileData>} [subCells]
2458
*/
@@ -63,7 +97,7 @@ export function findCellOnScreen(plot, viewDim, norder, fov,centerWp, desiredNor
6397
if (!devPtCorners[j]) break;
6498
}
6599
if (aitoffAll || isQuadTileOnScreen(devPtCorners, viewDim)) {
66-
const subCells= computeDeeperTiles(cc,dataCoordSys,norder,desiredNorder,cell.ipix);
100+
const subCells= computeDeeperTiles(cc,dataCoordSys,norder,desiredNorder,cell.ipix,cell.wpCorners,devPtCorners);
67101
retCells.push({devPtCorners, tileNumber:cell.ipix, dx:0, dy:0, norder, incompleteCell:false, subCells});
68102
}
69103
}
@@ -88,7 +122,7 @@ export function findCellOnScreen(plot, viewDim, norder, fov,centerWp, desiredNor
88122
if (subCells) retCells.push({devPtCorners, tileNumber:cells[i].ipix, dx:0, dy:0, norder,subCells, incompleteCell:true});
89123
}
90124
else if (isQuadTileOnScreen(devPtCorners, viewDim)) {
91-
const subCells= computeDeeperTiles(cc,dataCoordSys,norder,desiredNorder,cells[i].ipix);
125+
const subCells= computeDeeperTiles(cc,dataCoordSys,norder,desiredNorder,cells[i].ipix,cells[i].wpCorners,devPtCorners);
92126
retCells.push({devPtCorners, tileNumber:cells[i].ipix, dx:0, dy:0, norder,subCells});
93127
}
94128
}
@@ -101,6 +135,8 @@ export function findCellOnScreen(plot, viewDim, norder, fov,centerWp, desiredNor
101135
* or, with checkWrap, that wraps around the edge of an aitoff projection.
102136
* Children that fully project are kept, incomplete children are replaced by their fully projected grandchildren.
103137
* Only parts that are on the screen are kept, unless allOnScreen is true.
138+
* Drawable parts near a pole are subdivided further, so they reach the same depth (POLE_SUBDIVIDE_DEPTH) below the
139+
* parent as a normal cell near the pole.
104140
* @param {CysConverter} cc
105141
* @param {CoordinateSys} dataCoordSys
106142
* @param {number} norder - norder of the parent tile
@@ -114,39 +150,71 @@ export function findCellOnScreen(plot, viewDim, norder, fov,centerWp, desiredNor
114150
*/
115151
function computeIncompleteTiles(cc, dataCoordSys, norder, npix, viewDim, {checkWrap=false, allOnScreen=false, centerDevPt}={}) {
116152
const onScreen= (t) => !t.incompleteCell && (allOnScreen || isQuadTileOnScreen(t.devPtCorners, viewDim));
153+
const addPoleSubCells= (t, level) => {
154+
t.subCells= makeSubTilesToDepth(cc,dataCoordSys,t,{level});
155+
};
117156
const subTiles= getProgradeTiles(cc,dataCoordSys,norder,npix, {allowPartial:true, checkWrap});
118157
if (!subTiles) return;
119158
subTiles.forEach( (t) => {
120159
if (t.incompleteCell) {
121160
const grandChildCells= (getProgradeTiles(cc,dataCoordSys,norder+1,t.tileNumber,
122161
{allowPartial:true, checkWrap, shim:Boolean(centerDevPt), centerDevPt, viewDim}) ?? [])
123162
.filter(onScreen);
163+
grandChildCells.forEach( (gc) => addPoleSubCells(gc, 2));
124164
t.subCells= grandChildCells.length>0 ? grandChildCells : undefined;
125165
}
126166
});
127167
const retSubTiles= subTiles.filter( (t) => t.incompleteCell ? t.subCells : onScreen(t));
168+
retSubTiles.forEach( (t) => {
169+
if (!t.incompleteCell) addPoleSubCells(t, 1);
170+
});
128171
return retSubTiles.length>0 ? retSubTiles : undefined;
129172
}
130173

131174
/**
132-
* Compute the child cells of a tile so it can be drawn as smaller quads when zoomed in beyond the tile norder.
133-
* Goes at most two levels deep: one level when desiredNorder is norder+1, otherwise two (the second in each child's subCells).
175+
* Compute the child cells of a tile so it can be drawn as smaller quads when zoomed in beyond the tile norder
176+
* or when the tile is near a pole, where the healpix cells are too distorted to draw accurately as 2 triangles.
177+
* For zoom, goes at most two levels deep: one level when desiredNorder is norder+1, otherwise two.
178+
* Near a pole, goes POLE_SUBDIVIDE_DEPTH levels deep, or INNER_POLE_SUBDIVIDE_DEPTH when zoomed and very close to the pole,
179+
* each child is evaluated by its own distance to the pole.
134180
* The dx/dy of each child is 0 or 1, the half of the parent tile image it comes from.
135181
* @param {CysConverter} cc
136182
* @param {CoordinateSys} dataCoordSys
137183
* @param {number} norder - norder of the parent tile
138184
* @param {number} desiredNorder
139185
* @param {number} npix - healpix pixel number of the parent tile
186+
* @param {Array.<WorldPt>} wpCorners - corners of the parent tile in the data coordinate system
187+
* @param {Array.<DevicePt>} devPtCorners - corners of the parent tile in device coordinates
140188
* @return {Array.<HiPSDeviceTileData>|undefined} the 4 child cells, or undefined if not needed or any child cannot be projected
141189
*/
142-
function computeDeeperTiles(cc, dataCoordSys, norder, desiredNorder, npix) {
143-
if (!desiredNorder) return;
144-
if (desiredNorder<=norder) return;
145-
const subTiles= getProgradeTiles(cc,dataCoordSys,norder,npix);
190+
function computeDeeperTiles(cc, dataCoordSys, norder, desiredNorder, npix, wpCorners, devPtCorners) {
191+
const zoomOver= desiredNorder>norder ? desiredNorder-norder : 0;
192+
const zoomDepth= Math.min(zoomOver, 2);
193+
return makeSubTilesToDepth(cc, dataCoordSys, {norder, tileNumber:npix, wpCorners, devPtCorners}, {zoomDepth, zoomOver});
194+
}
195+
196+
/**
197+
* Recursively make child cells until both the zoom depth and the pole depth of each cell are reached.
198+
* The pole depth is computed for each cell from its own corners, and a cell smaller than MIN_POLE_SPLIT_SIZE
199+
* on the screen is not subdivided because of a pole.
200+
* @param {CysConverter} cc
201+
* @param {CoordinateSys} dataCoordSys
202+
* @param {{norder:number, tileNumber:number, wpCorners:Array.<WorldPt>, devPtCorners:Array.<DevicePt>}} tile - the tile to subdivide
203+
* @param {Object} [options]
204+
* @param {number} [options.zoomDepth] - levels below the base tile to subdivide because of zoom
205+
* @param {number} [options.zoomOver] - how many orders the desired norder is beyond the base tile norder
206+
* @param {number} [options.level] - how many levels this tile is below the base tile
207+
* @return {Array.<HiPSDeviceTileData>|undefined}
208+
*/
209+
function makeSubTilesToDepth(cc, dataCoordSys, tile, {zoomDepth=0, zoomOver=0, level=0}={}) {
210+
const {norder, tileNumber, wpCorners, devPtCorners}= tile;
211+
const zoomSplit= level<zoomDepth;
212+
const poleSplit= level<getPoleDepth(wpCorners,zoomOver) && getDevSize(devPtCorners)>MIN_POLE_SPLIT_SIZE;
213+
if (!zoomSplit && !poleSplit) return;
214+
const subTiles= getProgradeTiles(cc,dataCoordSys,norder,tileNumber);
146215
if (!subTiles) return;
147-
if (desiredNorder===norder+1) return subTiles;
148216
subTiles.forEach( (t) => {
149-
t.subCells= getProgradeTiles(cc,dataCoordSys,norder+1,t.tileNumber);
217+
t.subCells= makeSubTilesToDepth(cc,dataCoordSys,t,{zoomDepth,zoomOver,level:level+1});
150218
});
151219
return subTiles;
152220
}
@@ -189,6 +257,7 @@ function getProgradeTiles(cc, dataCoordSys, norder, npix,
189257
tileNumber,
190258
norder: proNorder,
191259
devPtCorners,
260+
wpCorners,
192261
incompleteCell: devPtCorners.length!==4,
193262
dx: imageOffsets[idx][0],
194263
dy: imageOffsets[idx][1],

‎src/firefly/js/visualize/iv/HiPSRenderer.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function makeHipsRenderer(screenRenderParams, totalCnt, isBaseImage, scre
4747
// ------------------------------------------------------------
4848

4949
const normalCaseDrawOneCell= (tile, image, tileSize= tile.tileSize || image.width, x= tile.dx, y= tile.dy) => {
50-
if (tile.subCells && (tile.incompleteCell || desiredNorder>norder)) {
50+
if (tile.subCells) { // subCells are only set when needed: incomplete cell, zoomed beyond norder, or near a pole
5151
const half= tileSize/2;
5252
tile.subCells.forEach( (subTile) => {
5353
normalCaseDrawOneCell(subTile,image, half, x+subTile.dx*half, y+subTile.dy*half);

‎src/firefly/js/visualize/saga/MouseReadoutWatch.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,19 @@ export function* watchReadout() {
5959

6060
let getNextWithWithAsync= false;
6161
const lockByClick= isLockByClick(readoutRoot());
62-
let {worldPt,screenPt,imagePt}= mouseCtx;
63-
const {plotId,mouseState, shiftDown,eventType, hipsLocation}= mouseCtx;
64-
const useEv= !igoreEvTypes.includes(eventType);
65-
66-
if (!useEv) {
67-
if (!lockByClick) savedWP= undefined;
68-
if (lockByClick && worldPt && !shiftDown && mouseState===MouseState.CLICK) savedWP= worldPt;
69-
}
70-
62+
let {worldPt,screenPt,imagePt}= mouseCtx ?? {};
63+
const {plotId,mouseState, shiftDown,eventType, hipsLocation}= mouseCtx ?? {};
64+
let useEv= false;
7165
const {plot,pv:plotView}= currentP(plotId);
7266
const threeColor= plot?.plotState?.threeColor;
67+
if (mouseCtx) {
68+
useEv= !igoreEvTypes.includes(eventType);
69+
70+
if (!useEv) {
71+
if (!lockByClick) savedWP= undefined;
72+
if (lockByClick && worldPt && !shiftDown && mouseState===MouseState.CLICK) savedWP= worldPt;
73+
}
74+
}
7375

7476
if (useEv) {
7577
if (plot && lockByClick && savedWP && shiftDown && mouseState===MouseState.CLICK) {

0 commit comments

Comments
 (0)