forked from gTile/gTile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilespec.ts
428 lines (363 loc) · 12.2 KB
/
tilespec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
const MAX_TUPLE_MEMBER_VALUE = Number.MAX_SAFE_INTEGER;
/**
* TileSpec represents a rectangular area on display by means of specifying a
* number of evenly spaced tiles and two corners.
*/
export class TileSpec {
readonly gridWidth: number;
readonly gridHeight: number;
readonly luc: XY;
readonly rdc: XY;
constructor(gridWidth: number, gridHeight: number, luc: XY, rdc: XY) {
this.gridWidth = gridWidth;
this.gridHeight = gridHeight;
this.luc = luc;
this.rdc = rdc;
}
toString() {
return [[this.gridWidth, this.gridHeight].join('x'),
[this.luc.x, this.luc.y].join(':'),
[this.rdc.x, this.rdc.y].join(':')].join(' ');
}
toFrameRect(workArea: Rect) {
const elemSize = new Size(
Math.floor(workArea.size.width / this.gridWidth),
Math.floor(workArea.size.height / this.gridHeight));
return new Rect(
new XY(
workArea.origin.x + this.luc.x * elemSize.width,
workArea.origin.y + this.luc.y * elemSize.height),
new Size((this.rdc.x + 1 - this.luc.x) * elemSize.width,
(this.rdc.y + 1 - this.luc.y) * elemSize.height));
}
get gridSize(): GridSize {
return new GridSize(this.gridWidth, this.gridHeight);
}
viewSize(): GridSize {
const sizeXY = this.rdc.minus(this.luc);
return new GridSize(sizeXY.x + 1, sizeXY.y + 1);
}
isFullscreen(): boolean {
return this.viewSize().equals(this.gridSize);
}
}
export class GridSize {
constructor(
// Number of columns.
readonly width: number,
// Number of rows.
readonly height: number) {}
toString(): string {
return `${this.width}x${this.height}`;
}
equals(other: GridSize): boolean {
return this.width === other.width && this.height == other.height;
}
}
export function parseGridSizesIgnoringErrors(s: string): GridSize[] {
return s.split(',').flatMap(
(part: string) => {
const size = parseGridSizeIgnoringErrors(part.trim());
return size ? [size] : [];
});
}
function parseGridSizeIgnoringErrors(s: string): GridSize|null {
const parts = s.split("x").map(Number);
if (parts.length !== 2 || !parts.every(x => !isNaN(x))) {
return null;
}
return new GridSize(parts[0], parts[1]);
}
export class XY {
constructor(readonly x: number, readonly y: number) {}
clone() {
return new XY(this.x, this.y);
}
toString() {
return 'XY(' + [this.x, this.y].join(', ') + ')';
}
dot(b: XY) {
return this.x * b.x + this.y * b.y;
}
unit() {
const norm = this.l2norm()
return new XY(this.x / norm, this.y / norm);
}
l2norm() {
return Math.sqrt(this.l2normSquared());
}
l2normSquared() {
return this.dot(this);
}
scale(s: number) {
return new XY(this.x * s, this.y * s)
}
project(b: XY): XY {
return b.scale(
this.dot(b) / b.l2normSquared());
}
scalarProjection(b: XY): number {
return this.dot(b.unit())
}
minus(b: XY) {
return new XY(this.x - b.x, this.y - b.y)
}
plus(b: XY) {
return new XY(this.x + b.x, this.y + b.y)
}
}
const ADJOIN_DOT_PRODUCT_TOL = .02;
export class LineSegment {
private constructor(readonly a: XY, readonly b: XY) { }
static fromTwoPoints(a: XY, b: XY): LineSegment {
return new LineSegment(a, b);
}
direction() {
return this.b.minus(this.a).unit()
}
adjoins(other: LineSegment, distTol: number) {
return this.parallels(other) && this.lineDistance(other) < distTol
}
parallels(other: LineSegment) {
const unitDot = this.direction().dot(other.direction());
return withinTol(Math.abs(unitDot), 1, ADJOIN_DOT_PRODUCT_TOL);
}
// The distance between the lines of two line segments. If lines are not
// (close to) parallel, 0 is returned
lineDistance(other: LineSegment) {
return this.perpVectorBetweenLines(other).l2norm();
}
// The perpendicular vector between the lines of two line segments. If lines
// are not (close to) parallel, [0, 0] is returned
perpVectorBetweenLines(other: LineSegment) {
const otherDir = other.direction();
const unitDot = this.direction().dot(otherDir);
if (!withinTol(Math.abs(unitDot), 1, ADJOIN_DOT_PRODUCT_TOL)) {
return new XY(0, 0);
}
// Basically parallel. Now measure the perpendicular distance between
// this.a->other.a and other.a->other.b.
const d = other.a.minus(this.a)
return d.minus(d.project(otherDir));
}
}
export class Size {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
clone() {
return new Size(this.width, this.height);
}
toString() {
return [this.width, this.height].join('x')
}
area() {
return this.width * this.height;
}
}
/**
* A screen rectangle. A (0, 0) origin represents the top left of a display
* area. Units are typically pixels.
*/
export class Rect {
origin: XY;
size: Size;
constructor(origin: XY, size: Size) {
this.origin = origin;
this.size = size;
}
clone() {
return new Rect(this.origin.clone(), this.size.clone());
}
toString() {
return [this.origin, this.size].join(' ');
}
equal(r: Rect, tol: number) {
const close = (a: number, b: number) => Math.abs(a - b) <= tol;
return (close(this.origin.x, r.origin.x) &&
close(this.origin.y, r.origin.y) &&
close(this.size.width, r.size.width) &&
close(this.size.height, r.size.height));
}
inset(s: Size) {
return new Rect(
new XY(this.origin.x + s.width, this.origin.y + s.height),
new Size(this.size.width - 2 * s.width,
this.size.height - 2 * s.height));
}
edges() {
const down = new XY(0, this.size.height);
const right = new XY(this.size.width, 0);
const seg = (a: XY, b: XY) => LineSegment.fromTwoPoints(a, b);
// a---b
// c---d
const a = this.origin;
const b = a.plus(right)
const c = a.plus(down)
const d = c.plus(right)
const rv = new Edges({
top: seg(a, b),
right: seg(b, d),
bottom: seg(c, d),
left: seg(a, c)
});
return rv;
}
translate(vec: XY) {
return new Rect(this.origin.plus(vec), this.size);
}
// Increases or decreases the size of the rectangle by moving one of its
// edges d units along the postive x or y axis, where positive x is right
// and positive y is down.
translateEdge(side: Side, d: number): Rect {
const [w, h] = [this.size.width, this.size.height];
switch (side) {
case Side.Top:
return new Rect(this.origin.plus(new XY(0, d)), new Size(w, h - d));
case Side.Bottom:
return new Rect(this.origin, new Size(w, h + d));
case Side.Right:
return new Rect(this.origin, new Size(w + d, h));
case Side.Left:
return new Rect(this.origin.plus(new XY(d, 0)), new Size(w - d, h));
default:
throw TypeError('bad side type ' + side);
}
}
topLeft() {
return this.origin
}
topRight() {
return this.origin.plus(new XY(this.size.width, 0))
}
bottomRight() {
return this.origin.plus(new XY(this.size.width, this.size.height))
}
bottomLeft() {
return this.origin.plus(new XY(0, this.size.height))
}
intersection(other: Rect): Rect {
// Not optimized, but that's not necessary.
const origin = new XY(Math.max(this.topLeft().x, other.topLeft().x),
Math.max(this.topLeft().y, other.topLeft().y));
const br = new XY(Math.min(this.bottomRight().x, other.bottomRight().x),
Math.min(this.bottomRight().y, other.bottomRight().y));
const sizeXY = br.minus(origin);
const size = new Size(sizeXY.x, sizeXY.y);
if (size.width < 0 || size.height < 0) {
return new Rect(new XY(0, 0), new Size(0, 0));
}
return new Rect(origin, size);
}
valid() {
return this.size.width >= 0 && this.size.height >= 0
}
}
export enum Side {
Top = "TOP",
Right = "RIGHT",
Bottom = "BOTTOM",
Left = "LEFT",
}
export class Edges {
readonly top: LineSegment;
readonly right: LineSegment;
readonly bottom: LineSegment;
readonly left: LineSegment;
constructor(obj: {
top: LineSegment,
left: LineSegment,
bottom: LineSegment,
right: LineSegment
}) {
this.top = obj.top;
this.left = obj.left;
this.bottom = obj.bottom;
this.right = obj.right;
}
getSide(s: Side): LineSegment {
switch (s) {
case Side.Top: return this.top;
case Side.Right: return this.right;
case Side.Bottom: return this.bottom;
case Side.Left: return this.left;
}
}
}
export function adjoiningSides(a: Edges, b: Edges, distTol: number) {
const sides = [Side.Top, Side.Bottom, Side.Left, Side.Right];
const result = [];
for (let sa of sides) {
for (let sb of sides) {
const sega = a.getSide(sa);
const segb = b.getSide(sb);
if (sega.adjoins(segb, distTol)) {
result.push([sa, sb]);
}
}
}
return result;
}
/**
* parsePreset parses a sequence of TileSpec objects from a string like "8x8 0:0
* 0:7, 8x10 0:0 2:7" or "8x8 0:0 0:7, 0:0 2:7"
*
* The 8x8 and 8x10 values above are the "grid size." The grid size may be
* omitted in all but the first component of the preset.
*/
export function parsePreset(preset: string): Array<TileSpec> {
const parts = preset.split(',').map(x => x.trim());
let mostRecentSpec: TileSpec|null = null;
return parts.map((part: string, index: number): TileSpec => {
if (hasImpliedGridSize(part)) {
if (mostRecentSpec === null) {
throw new Error(`preset component[${index}] ${part} of ${preset} is missing grid size (e.g., '3x3')`);
}
part = `${mostRecentSpec.gridWidth}x${mostRecentSpec.gridHeight} ${part}`;
}
const parsed = parseSinglePreset(part);
mostRecentSpec = parsed;
return parsed;
});
}
function parseSinglePreset(preset: string) {
const ps = preset.trim().split(" ");
if (ps.length != 3) {
throw new Error(`Bad preset: ${JSON.stringify(preset)}`);
}
const gridFormat = parseTuple(ps[0], "x");
const luc = parseTuple(ps[1], ":");
const rdc = parseTuple(ps[2], ":");
if (gridFormat.x < 1 || luc.x < 0 || rdc.x < 0
|| gridFormat.y < 1 || luc.y < 0 || rdc.y < 0
|| gridFormat.x <= luc.x || gridFormat.x <= rdc.x
|| gridFormat.y <= luc.y || gridFormat.y <= rdc.y
|| luc.x > rdc.x || luc.y > rdc.y) {
throw new Error(`Bad preset: ${JSON.stringify(preset)}`);
}
return new TileSpec(gridFormat.x, gridFormat.y, luc, rdc);
}
function hasImpliedGridSize(singlePreset: string): boolean {
return singlePreset.trim().split(" ").length === 2;
}
/**
* Parses a value like like 6x4 or 1:2 into {X: 6, Y: 4} or {X: 1, Y: 2}.
*/
function parseTuple(unparsed: string, delim: string) {
// parsing grid size in unparsed XdelimY, like 6x4 or 1:2
const gssk = unparsed.split(delim);
if (gssk.length !== 2) {
throw new Error("Failed to split " + unparsed + " by delimiter " + delim + " into two numbers");
}
const numbers = gssk.map(Number);
if (numbers.some(n => isNaN(n) || n < 0 || n > MAX_TUPLE_MEMBER_VALUE)) {
throw new Error(`All elements of tuple must be intgers in [0, ${MAX_TUPLE_MEMBER_VALUE}]: ${JSON.stringify(unparsed)}`)
}
return new XY(numbers[0], numbers[1]);
}
function withinTol(a: number, b: number, tol: number) {
return Math.abs(a - b) <= tol;
}