aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTerry Truong <terry06890@gmail.com>2022-04-26 15:33:15 +1000
committerTerry Truong <terry06890@gmail.com>2022-04-26 18:03:56 +1000
commit46891ca052e6049252a560895af55301f5e37b19 (patch)
tree5dcac087c16845f02a10af36ebdbb34871e55fb1
parentde55b59141a82c68b6a5b360d6f57a7e760e2fd6 (diff)
Add small sqrLayout optimisation
-rwxr-xr-xbackend/data/otolToSqlite.py1
-rw-r--r--src/layout.ts7
-rw-r--r--src/util.ts9
3 files changed, 14 insertions, 3 deletions
diff --git a/backend/data/otolToSqlite.py b/backend/data/otolToSqlite.py
index 187e224..c393474 100755
--- a/backend/data/otolToSqlite.py
+++ b/backend/data/otolToSqlite.py
@@ -223,6 +223,5 @@ cur = con.cursor()
cur.execute("CREATE TABLE nodes (name TEXT PRIMARY KEY, data TEXT)")
for name in nodeMap.keys():
cur.execute("INSERT INTO nodes VALUES (?, ?)", (name, json.dumps(nodeMap[name])))
-cur.execute("CREATE UNIQUE INDEX nodes_idx on nodes(name)")
con.commit()
con.close()
diff --git a/src/layout.ts b/src/layout.ts
index 6f1fd77..fd65b87 100644
--- a/src/layout.ts
+++ b/src/layout.ts
@@ -8,7 +8,7 @@
import {TolNode} from './tol';
import type {TolMap} from './tol';
-import {range, arraySum, limitVals, updateAscSeq} from './util';
+import {range, arraySum, linspace, limitVals, updateAscSeq} from './util';
// Represents a node/tree that holds layout data for a TolNode node/tree
export class LayoutNode {
@@ -276,7 +276,10 @@ let sqrLayout: LayoutFn = function (node, pos, dims, showHeader, allowCollapse,
let numChildren = node.children.length;
let areaAR = newDims[0] / newDims[1]; // Aspect ratio
let lowestEmpSpc = Number.POSITIVE_INFINITY, usedNumCols = 0, usedNumRows = 0, usedTileSz = 0;
- for (let numCols = 1; numCols <= numChildren; numCols++){
+ const MAX_TRIES = 10; // If there are many possibilities, skip some
+ let ptlNumCols = numChildren == 1 ? [1] :
+ linspace(1, numChildren, Math.min(numChildren, MAX_TRIES)).map(n => Math.floor(n));
+ for (let numCols of ptlNumCols){
let numRows = Math.ceil(numChildren / numCols);
let gridAR = numCols / numRows;
let usedFrac = // Fraction of area occupied by maximally-fitting grid
diff --git a/src/util.ts b/src/util.ts
index be31102..a698d23 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -10,6 +10,15 @@ export function range(len: number): number[] {
export function arraySum(array: number[]): number {
return array.reduce((x,y) => x+y);
}
+// Returns an array of increasing evenly-spaced numbers from 'start' to 'end' with size 'size'
+export function linspace(start: number, end: number, size: number): number[] {
+ let step = (end - start) / (size - 1);
+ let ar = [];
+ for (let i = 0; i < size; i++){
+ ar.push(start + step * i);
+ }
+ return ar;
+}
// 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): number[] | null {