feat: render states - separate pole detection from layer render

This commit is contained in:
Azgaar 2024-09-02 03:23:05 +02:00
parent 093014088c
commit c66e6e4645
5 changed files with 45 additions and 24 deletions

View file

@ -13,6 +13,8 @@ window.BurgsAndStates = (() => {
placeTowns();
expandStates();
normalizeStates();
getPoles();
specifyBurgs();
collectStatistics();
@ -468,8 +470,7 @@ window.BurgsAndStates = (() => {
const normalizeStates = () => {
TIME && console.time("normalizeStates");
const cells = pack.cells,
burgs = pack.burgs;
const {cells, burgs} = pack;
for (const i of cells.i) {
if (cells.h[i] < 20 || cells.burg[i]) continue; // do not overwrite burgs
@ -486,6 +487,17 @@ window.BurgsAndStates = (() => {
TIME && console.timeEnd("normalizeStates");
};
// calculate pole of inaccessibility for each state
const getPoles = () => {
const getType = cellId => pack.cells.state[cellId];
const poles = getPolesOfInaccessibility(getType);
pack.states.forEach(s => {
if (!s.i || s.removed) return;
s.pole = poles[s.i] || [0, 0];
});
};
// Resets the cultures of all burgs and states to their
// cell or center cell's (respectively) culture.
const updateCultures = () => {
@ -1194,6 +1206,7 @@ window.BurgsAndStates = (() => {
generate,
expandStates,
normalizeStates,
getPoles,
assignColors,
drawBurgs,
specifyBurgs,

View file

@ -845,6 +845,7 @@ function recalculateStates(must) {
if (!layerIsOn("toggleBorders")) toggleBorders();
else drawBorders();
if (layerIsOn("toggleProvinces")) drawProvinces();
BurgsAndStates.getPoles();
if (adjustLabels.checked) drawStateLabels();
refreshStatesEditor();
}
@ -981,6 +982,7 @@ function applyStatesManualAssignent() {
if (affectedStates.length) {
refreshStatesEditor();
BurgsAndStates.getPoles();
layerIsOn("toggleStates") ? drawStates() : toggleStates();
if (adjustLabels.checked) drawStateLabels([...new Set(affectedStates)]);
adjustProvinces([...new Set(affectedProvinces)]);
@ -1415,6 +1417,7 @@ function openStateMergeDialog() {
unfog();
debug.selectAll(".highlight").remove();
BurgsAndStates.getPoles();
layerIsOn("toggleStates") ? drawStates() : toggleStates();
layerIsOn("toggleBorders") ? drawBorders() : toggleBorders();
layerIsOn("toggleProvinces") && drawProvinces();

View file

@ -991,18 +991,14 @@ function drawStates() {
TIME && console.time("drawStates");
const {cells, states} = pack;
const renderHalo = shapeRendering.value === "geometricPrecision";
const paths = getVertexPaths({
getType: cellId => cells.state[cellId],
options: {fill: true, waterGap: true, halo: renderHalo}
});
const maxLength = states.length - 1;
const bodyPaths = new Array(maxLength);
const clipPaths = new Array(maxLength);
const haloPaths = new Array(maxLength);
for (const [index, {fill, waterGap, halo}] of paths) {
const renderHalo = shapeRendering.value === "geometricPrecision";
const isolines = getIsolines(cellId => cells.state[cellId], {fill: true, waterGap: true, halo: renderHalo});
for (const [index, {fill, waterGap, halo}] of isolines) {
const color = states[index].color;
bodyPaths.push(
@ -1022,11 +1018,6 @@ function drawStates() {
byId("statePaths").innerHTML = renderHalo ? clipPaths.join("") : "";
byId("statesHalo").innerHTML = renderHalo ? haloPaths.join("") : "";
// vArray.forEach((ar, i) => {
// const sorted = ar.sort((a, b) => b.length - a.length); // sort by points number
// states[i].pole = polylabel(sorted, 1.0); // pole of inaccessibility
// });
TIME && console.timeEnd("drawStates");
}

View file

@ -152,6 +152,7 @@ function regenerateStates() {
pack.states = newStates;
BurgsAndStates.expandStates();
BurgsAndStates.normalizeStates();
BurgsAndStates.getPoles();
BurgsAndStates.collectStatistics();
BurgsAndStates.assignColors();
BurgsAndStates.generateCampaigns();