diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/App.vue | 8 | ||||
| -rw-r--r-- | src/components/AncestryBar.vue | 2 | ||||
| -rw-r--r-- | src/components/SearchModal.vue | 2 | ||||
| -rw-r--r-- | src/components/SettingsPane.vue | 2 | ||||
| -rw-r--r-- | src/components/Tile.vue | 2 | ||||
| -rw-r--r-- | src/components/TileImg.vue | 2 | ||||
| -rwxr-xr-x | src/genTestImgs.sh | 6 | ||||
| -rw-r--r-- | src/layout.ts (renamed from src/lib.ts) | 89 | ||||
| -rw-r--r-- | src/tolData.txt (renamed from src/tol.txt) | 0 | ||||
| -rw-r--r-- | src/util.ts | 88 |
10 files changed, 102 insertions, 99 deletions
diff --git a/src/App.vue b/src/App.vue index 8789653..541e630 100644 --- a/src/App.vue +++ b/src/App.vue @@ -7,13 +7,13 @@ import SearchModal from './components/SearchModal.vue'; import HelpModal from './components/HelpModal.vue'; import SettingsPane from './components/SettingsPane.vue'; import {TolNode, TolNodeRaw, tolFromRaw, getTolMap} from './tol'; -import {LayoutNode, initLayoutTree, initLayoutMap, tryLayout} from './lib'; -import type {LayoutOptions} from './lib'; -import {arraySum, randWeightedChoice} from './lib'; +import {LayoutNode, initLayoutTree, initLayoutMap, tryLayout} from './layout'; +import type {LayoutOptions} from './layout'; +import {arraySum, randWeightedChoice} from './util'; // Import paths lack a .ts or .js extension because .ts makes vue-tsc complain, and .js makes vite complain // Obtain tree-of-life data -import tolRaw from './tol.json'; +import tolRaw from './tolData.json'; const tol: TolNode = tolFromRaw(tolRaw); const tolMap = getTolMap(tol); diff --git a/src/components/AncestryBar.vue b/src/components/AncestryBar.vue index 2e0d5d0..46f1edf 100644 --- a/src/components/AncestryBar.vue +++ b/src/components/AncestryBar.vue @@ -1,6 +1,6 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; -import {LayoutNode} from '../lib'; +import {LayoutNode} from '../layout'; import TileImg from './TileImg.vue' export default defineComponent({ diff --git a/src/components/SearchModal.vue b/src/components/SearchModal.vue index 647181b..1d9b181 100644 --- a/src/components/SearchModal.vue +++ b/src/components/SearchModal.vue @@ -1,7 +1,7 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; import {TolNode} from '../tol'; -import {LayoutNode} from '../lib'; +import {LayoutNode} from '../layout'; export default defineComponent({ props: { diff --git a/src/components/SettingsPane.vue b/src/components/SettingsPane.vue index 4fa3e03..d49dd68 100644 --- a/src/components/SettingsPane.vue +++ b/src/components/SettingsPane.vue @@ -1,6 +1,6 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; -import type {LayoutOptions} from '../lib'; +import type {LayoutOptions} from '../layout'; export default defineComponent({ props: { diff --git a/src/components/Tile.vue b/src/components/Tile.vue index df124e9..986ca77 100644 --- a/src/components/Tile.vue +++ b/src/components/Tile.vue @@ -1,6 +1,6 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; -import {LayoutNode} from '../lib'; +import {LayoutNode} from '../layout'; import TileImg from './TileImg.vue'; // Component holds a tree-node structure representing a tile or tile-group to be rendered diff --git a/src/components/TileImg.vue b/src/components/TileImg.vue index 00cc2e6..ebd350d 100644 --- a/src/components/TileImg.vue +++ b/src/components/TileImg.vue @@ -1,6 +1,6 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; -import {LayoutNode} from '../lib'; +import {LayoutNode} from '../layout'; export default defineComponent({ props: { diff --git a/src/genTestImgs.sh b/src/genTestImgs.sh index 1198a60..21b001b 100755 --- a/src/genTestImgs.sh +++ b/src/genTestImgs.sh @@ -2,10 +2,10 @@ set -e #generate tol.json from tol.txt -cat tol.txt | ./txtTreeToJSON.py > tol.json +cat tolData.txt | ./txtTreeToJSON.py > tolData.json -#reads through tol.json, gets names, and generates image for each name -cat tol.json | \ +#reads through tolData.json, gets names, and generates image for each name +cat tolData.json | \ gawk 'match ($0, /"name"\s*:\s*"([^"]*)"/, arr) {print arr[1]}' | \ while read; do convert -size 200x200 xc:khaki +repage \ diff --git a/src/lib.ts b/src/layout.ts index 765a82e..711f20a 100644 --- a/src/lib.ts +++ b/src/layout.ts @@ -1,13 +1,13 @@ /* * Contains classes used for representing tile-based layouts of tree-of-life data. * - * Generally, given a TolNode with child TolNodes representing tree-of-life T, - * initLayoutTree() produces a tree structure representing a subtree of T, + * Generally, given a TolNode tree T,initLayoutTree() produces a tree structure representing a subtree of T, * which is passed to tryLayout(), which alters data fields to represent a tile-based layout. * The tree structure consists of LayoutNode objects, each of which holds placement info for a linked TolNode. */ import {TolNode} from './tol'; +import {range, arraySum, limitVals, updateAscSeq} from './util'; // Represents a node/tree, and holds layout data for a TolNode node/tree export class LayoutNode { @@ -776,88 +776,3 @@ let sweepLayout: LayoutFn = function (node, pos, dims, showHeader, allowCollapse node.assignLayoutData(pos, usedDims, {showHeader, empSpc, sepSweptArea: usingParentArea ? parentArea! : null}); return true; } - -// Returns [0 ... len] -export function range(len: number){ - return [...Array(len).keys()]; -} -// Returns sum of array values -export function arraySum(array: number[]){ - return array.reduce((x,y) => x+y); -} -// Returns array copy with vals clipped to within [min,max], redistributing to compensate (returns null on failure) -export function limitVals(arr: number[], min: number, max: number){ - let vals = [...arr]; - let clipped = new Array(vals.length).fill(false); - let owedChg = 0; // Stores total change made after clipping values - while (true){ - // Clip values - for (let i = 0; i < vals.length; i++){ - if (clipped[i]){ - continue; - } - if (vals[i] < min){ - owedChg += vals[i] - min; - vals[i] = min; - clipped[i] = true; - } else if (vals[i] > max){ - owedChg += vals[i] - max; - vals[i] = max; - clipped[i] = true; - } - } - if (Math.abs(owedChg) < Number.EPSILON){ - return vals; - } - // Compensate for changes made - let indicesToUpdate = (owedChg > 0) ? - range(vals.length).filter(idx => vals[idx] < max) : - range(vals.length).filter(idx => vals[idx] > min); - if (indicesToUpdate.length == 0){ - return null; - } - for (let i of indicesToUpdate){ - vals[i] += owedChg / indicesToUpdate.length; - } - owedChg = 0; - } -} -// Usable to iterate through possible int arrays with ascending values in the range 0 to maxLen-1, starting with [0] - // eg: With maxLen 3, updates [0] to [0,1], then to [0,2], then [0,1,2], then null -export function updateAscSeq(seq: number[], maxLen: number){ - // Try increasing last element, then preceding elements, then extending the array - let i = seq.length - 1; - while (true){ - if (i > 0 && seq[i] < (maxLen - 1) - (seq.length - 1 - i)){ - seq[i]++; - return true; - } else if (i > 0){ - i--; - } else { - if (seq.length < maxLen){ - seq.push(0); - seq.splice(0, seq.length, ...range(seq.length)); - return true; - } else { - return false; - } - } - } -} -// Given a non-empty array of non-negative weights, returns an array index chosen with weighted pseudorandomness -// Returns null if array contains all zeros -export function randWeightedChoice(weights: number[]): number | null { - let thresholds = Array(weights.length); - let sum = 0; - for (let i = 0; i < weights.length; i++){ - sum += weights[i]; - thresholds[i] = sum; - } - let rand = Math.random(); - for (let i = 0; i < weights.length; i++){ - if (rand <= thresholds[i] / sum){ - return i; - } - } - return null; -} diff --git a/src/tol.txt b/src/tolData.txt index f73a064..f73a064 100644 --- a/src/tol.txt +++ b/src/tolData.txt diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..79c2b5c --- /dev/null +++ b/src/util.ts @@ -0,0 +1,88 @@ +/* + * Contains commonly-used utility functions. + */ + +// Returns [0 ... len] +export function range(len: number){ + return [...Array(len).keys()]; +} +// Returns sum of array values +export function arraySum(array: number[]){ + return array.reduce((x,y) => x+y); +} +// Returns array copy with vals clipped to within [min,max], redistributing to compensate (returns null on failure) +export function limitVals(arr: number[], min: number, max: number){ + let vals = [...arr]; + let clipped = new Array(vals.length).fill(false); + let owedChg = 0; // Stores total change made after clipping values + while (true){ + // Clip values + for (let i = 0; i < vals.length; i++){ + if (clipped[i]){ + continue; + } + if (vals[i] < min){ + owedChg += vals[i] - min; + vals[i] = min; + clipped[i] = true; + } else if (vals[i] > max){ + owedChg += vals[i] - max; + vals[i] = max; + clipped[i] = true; + } + } + if (Math.abs(owedChg) < Number.EPSILON){ + return vals; + } + // Compensate for changes made + let indicesToUpdate = (owedChg > 0) ? + range(vals.length).filter(idx => vals[idx] < max) : + range(vals.length).filter(idx => vals[idx] > min); + if (indicesToUpdate.length == 0){ + return null; + } + for (let i of indicesToUpdate){ + vals[i] += owedChg / indicesToUpdate.length; + } + owedChg = 0; + } +} +// Usable to iterate through possible int arrays with ascending values in the range 0 to maxLen-1, starting with [0] + // eg: With maxLen 3, updates [0] to [0,1], then to [0,2], then [0,1,2], then null +export function updateAscSeq(seq: number[], maxLen: number){ + // Try increasing last element, then preceding elements, then extending the array + let i = seq.length - 1; + while (true){ + if (i > 0 && seq[i] < (maxLen - 1) - (seq.length - 1 - i)){ + seq[i]++; + return true; + } else if (i > 0){ + i--; + } else { + if (seq.length < maxLen){ + seq.push(0); + seq.splice(0, seq.length, ...range(seq.length)); + return true; + } else { + return false; + } + } + } +} +// Given a non-empty array of non-negative weights, returns an array index chosen with weighted pseudorandomness +// Returns null if array contains all zeros +export function randWeightedChoice(weights: number[]): number | null { + let thresholds = Array(weights.length); + let sum = 0; + for (let i = 0; i < weights.length; i++){ + sum += weights[i]; + thresholds[i] = sum; + } + let rand = Math.random(); + for (let i = 0; i < weights.length; i++){ + if (rand <= thresholds[i] / sum){ + return i; + } + } + return null; +} |
