Skip to content

Commit

Permalink
Geting rid of some vars in favour of const / let and defining variabl…
Browse files Browse the repository at this point in the history
…es closer to where they are used.
  • Loading branch information
smallsaucepan committed Nov 5, 2024
1 parent 67b1821 commit c7ab894
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 112 deletions.
8 changes: 4 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export {
SuccessCallback,
Path,
Ring,
LineEntry,
LineEdge,
LineCell,
BandCell,
BandCellGrid,
LineCellGrid,
BandEntry,
LineEntry,
LineEdge,
BandEdge,
BandCell,
BandCellGrid,
};
6 changes: 1 addition & 5 deletions src/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ function linear(a: number, b: number, v: number) {
* Note, that we assume that 'a' and 'b' have unit distance (i.e. 1)
*/
function linear_ab(a: number, b: number, v0: number, v1: number) {
var tmp;

if (v0 > v1) {
tmp = v0;
v0 = v1;
v1 = tmp;
[v0, v1] = [v1, v0]; // swap
}

if (a < b) {
Expand Down
22 changes: 10 additions & 12 deletions src/isobands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,7 @@ function computeCenterAverage(
minV: number,
maxV: number
) {
var average = (tl + tr + br + bl) / 4;
const average = (tl + tr + br + bl) / 4;

if (average > maxV) return 2; /* above isoband limits */

Expand All @@ -2086,16 +2086,14 @@ function prepareCell(
y: number,
opt: IsoBandOptions
) {
var cell, center_avg;

/* compose the 4-trit corner representation */
var cval = 0;
var x3 = grid[y + 1][x];
var x2 = grid[y + 1][x + 1];
var x1 = grid[y][x + 1];
var x0 = grid[y][x];
const minV = opt.minV!; // assume minV defined
const maxV = opt.maxV!; // assume maxV defined
let cval = 0;
const x3 = grid[y + 1][x],
x2 = grid[y + 1][x + 1],
x1 = grid[y][x + 1],
x0 = grid[y][x],
minV = opt.minV!, // assume minV defined
maxV = opt.maxV!; // assume maxV defined

/*
* Note that missing data within the grid will result
Expand Down Expand Up @@ -2152,9 +2150,9 @@ function prepareCell(
* 1 ... within iso band
* 2 ... above isoband
*/
center_avg = 0;
let center_avg = 0;

cell = {
let cell = {
cval: cval,
polygons: [],
edges: {},
Expand Down
16 changes: 8 additions & 8 deletions src/isolines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ function prepareCell(
y: number,
settings: IsoLineOptions
): LineCell | undefined {
var left, right, top, bottom, average, cell: LineCell;

let cval = 0;
const x3 = grid[y + 1][x];
const x2 = grid[y + 1][x + 1];
const x1 = grid[y][x + 1];
const x0 = grid[y][x];
const threshold = settings.threshold!; // assume threshold defined
const x3 = grid[y + 1][x],
x2 = grid[y + 1][x + 1],
x1 = grid[y][x + 1],
x0 = grid[y][x],
threshold = settings.threshold!; // assume threshold defined

/*
* Note that missing data within the grid will result
Expand Down Expand Up @@ -254,7 +252,7 @@ function prepareCell(
cval = +cval;

/* compose the cell object */
cell = {
const cell: LineCell = {
cval: cval,
polygons: [],
edges: {},
Expand All @@ -269,6 +267,8 @@ function prepareCell(
* with the cell borders and (i) add edges for polygon
* trace-back, or (ii) a list of small closed polygons
*/
let left, right, top, bottom, average;

switch (cval) {
case 0:
if (settings.polygons)
Expand Down
122 changes: 44 additions & 78 deletions src/polygons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
type Path,
type Ring,
type LineEntry,
type LineEdge,
type LineCell,
type LineCellGrid,
type BandEntry,
type BandEdge,
type BandCell,
type BandCellGrid,
} from "./common.js";
Expand Down Expand Up @@ -88,13 +86,11 @@ function requireFrame(
lowerBound: number,
upperBound: number
) {
var frameRequired, cols, rows, i, j;
let frameRequired = true;
const cols = data[0].length;
const rows = data.length;

frameRequired = true;
cols = data[0].length;
rows = data.length;

for (j = 0; j < rows; j++) {
for (let j = 0; j < rows; j++) {
if (
data[j][0] < lowerBound ||
data[j][0] > upperBound ||
Expand All @@ -117,7 +113,7 @@ function requireFrame(
}

if (frameRequired)
for (i = 0; i < cols - 1; i++) {
for (let i = 0; i < cols - 1; i++) {
if (
data[0][i] < lowerBound ||
data[0][i] > upperBound ||
Expand All @@ -133,13 +129,11 @@ function requireFrame(
}

function requireLineFrame(data: number[][], threshold: number) {
var frameRequired, cols, rows, i, j;

frameRequired = true;
cols = data[0].length;
rows = data.length;
let frameRequired = true;
const cols = data[0].length;
const rows = data.length;

for (j = 0; j < rows; j++) {
for (let j = 0; j < rows; j++) {
if (data[j][0] >= threshold || data[j][cols - 1] >= threshold) {
frameRequired = false;
break;
Expand All @@ -154,7 +148,7 @@ function requireLineFrame(data: number[][], threshold: number) {
}

if (frameRequired)
for (i = 0; i < cols - 1; i++) {
for (let i = 0; i < cols - 1; i++) {
if (data[0][i] >= threshold || data[rows - 1][i] > threshold) {
frameRequired = false;
break;
Expand All @@ -169,21 +163,6 @@ function traceBandPaths(
cellGrid: BandCellGrid,
settings: IsoBandOptions
) {
var nextedge,
e,
ee: BandEdge,
s,
ve: BandEntry,
enter: BandEntry,
x,
y,
finalized,
cc,
dir: 0 | 1 | 2 | 3,
count,
point,
found_entry;

const polygons: Ring[] = [];
const rows = data.length - 1;
const cols = data[0].length - 1;
Expand Down Expand Up @@ -246,29 +225,28 @@ function traceBandPaths(
/* finally, start tracing back first polygon(s) */
cellGrid.forEach(function (a, i) {
a.forEach(function (cell, j) {
nextedge = null;

/* trace paths for all available edges that go through this cell */
for (e = 0; e < 8; e++) {
nextedge = available_starts[e];
for (let e = 0; e < 8; e++) {
const nextedge = available_starts[e];

if (typeof cell?.edges[nextedge] !== "object") continue;

/* start a new, full path */
const path: Path = [];
ee = cell.edges[nextedge]!; // assume edge is defined
enter = nextedge;
x = i;
y = j;
finalized = false;
const origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]];
let ee = cell.edges[nextedge]!, // assume edge is defined
enter = nextedge,
x = i,
y = j,
finalized = false;

const path: Path = [],
origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]];

/* add start coordinate */
path.push(origin);

/* start traceback */
while (!finalized) {
cc = cellGrid[x][y];
let cc = cellGrid[x][y];

if (typeof cc?.edges[enter] !== "object") break;

Expand All @@ -278,7 +256,7 @@ function traceBandPaths(
delete cc.edges[enter];

/* add last point of edge to path arra, since we extend a polygon */
point = ee.path[1];
const point = ee.path[1];
point[0] += x;
point[1] += y;
path.push(point);
Expand All @@ -292,8 +270,8 @@ function traceBandPaths(
typeof cellGrid[x] === "undefined" ||
typeof cellGrid[x][y] === "undefined"
) {
dir = 0;
count = 0;
let dir: 0 | 1 | 2 | 3 = 0,
count = 0;

if (x === cols) {
x--;
Expand All @@ -317,8 +295,8 @@ function traceBandPaths(
break;
}

while (1) {
found_entry = false;
while (true) {
let found_entry = false;

if (count > 4)
throw new Error(
Expand All @@ -334,8 +312,8 @@ function traceBandPaths(
cc = cellGrid[x][y];

/* check for re-entry */
for (s = 0; s < valid_entries[dir].length; s++) {
ve = valid_entries[dir][s];
for (let s = 0; s < valid_entries[dir].length; s++) {
const ve = valid_entries[dir][s];
if (typeof cc?.edges[ve] === "object") {
/* found re-entry */
ee = cc.edges[ve]!; // assume edge is defined
Expand Down Expand Up @@ -405,19 +383,6 @@ function traceLinePaths(
cellGrid: LineCellGrid,
settings: IsoLineOptions
) {
var e,
ee: LineEdge,
cc,
enter,
x,
y,
finalized,
point,
dir: 0 | 1 | 2 | 3,
count,
found_entry,
ve;

const polygons: Ring[] = [];
const rows = data.length - 1;
const cols = data[0].length - 1;
Expand Down Expand Up @@ -472,26 +437,27 @@ function traceLinePaths(
cellGrid.forEach(function (a, i) {
a.forEach(function (cell, j) {
/* trace paths for all available edges that go through this cell */
for (e = 0; e < 4; e++) {
for (let e = 0; e < 4; e++) {
const nextedge = valid_entries[e];

if (typeof cell?.edges[nextedge] !== "object") continue;

/* start a new, full path */
const path: Path = [];
ee = cell.edges[nextedge];
enter = nextedge;
x = i;
y = j;
finalized = false;
const origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]];
let ee = cell.edges[nextedge],
enter = nextedge,
x = i,
y = j,
finalized = false;

const path: Path = [],
origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]];

/* add start coordinate */
path.push(origin);

/* start traceback */
while (!finalized) {
cc = cellGrid[x][y];
let cc = cellGrid[x][y];

if (typeof cc?.edges[enter] !== "object") break;

Expand All @@ -501,7 +467,7 @@ function traceLinePaths(
delete cc.edges[enter];

/* add last point of edge to path arra, since we extend a polygon */
point = ee.path[1];
const point = ee.path[1];
point[0] += x;
point[1] += y;
path.push(point);
Expand All @@ -517,8 +483,8 @@ function traceLinePaths(
) {
if (!settings.linearRing) break;

dir = 0;
count = 0;
let dir: 0 | 1 | 2 | 3 = 0,
count = 0;

if (x === cols) {
x--;
Expand All @@ -540,8 +506,8 @@ function traceLinePaths(
break;
}

while (1) {
found_entry = false;
while (true) {
let found_entry = false;

if (count > 4)
throw new Error(
Expand All @@ -557,7 +523,7 @@ function traceLinePaths(
cc = cellGrid[x][y];

/* check for re-entry */
ve = valid_entries[dir];
const ve = valid_entries[dir];
if (typeof cc?.edges[ve] === "object") {
/* found re-entry */
ee = cc.edges[ve]!; // assume edge is defined
Expand Down
Loading

0 comments on commit c7ab894

Please sign in to comment.