@dra2020/baseclient 1.0.18 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,3 +13,4 @@ export * from './polyround';
13
13
  export * from './topo';
14
14
  export * from './selfintersect';
15
15
  export * from './shamos';
16
+ export * from './pointinpoly';
@@ -0,0 +1 @@
1
+ export declare function polyContainsPoint(poly: any, x: number, y: number): boolean;
@@ -70,7 +70,12 @@ export declare class BinTrie {
70
70
  static fromBuffer(coder: Coder, ab: ArrayBuffer): BinTrie;
71
71
  get(key: string): string;
72
72
  }
73
+ export interface BinTrieOptions {
74
+ dedup?: boolean;
75
+ verbose?: boolean;
76
+ }
73
77
  export declare class BinTrieBuilder {
78
+ options: BinTrieOptions;
74
79
  coder: Coder;
75
80
  root: UnpackedNode;
76
81
  vtb: ValueTableBuilder;
@@ -78,7 +83,7 @@ export declare class BinTrieBuilder {
78
83
  ab: ArrayBuffer;
79
84
  u8: Uint8Array;
80
85
  i32: Int32Array;
81
- constructor(coder: Coder);
86
+ constructor(coder: Coder, options?: BinTrieOptions);
82
87
  nodeCount(node: UnpackedNode): number;
83
88
  nodeBranching(node: UnpackedNode, counts: number[]): number;
84
89
  nodeValueCount(node: UnpackedNode): number;
@@ -88,6 +93,6 @@ export declare class BinTrieBuilder {
88
93
  getUnpacked(key: string): string;
89
94
  packNode(node: UnpackedNode, byteOffset: number): number;
90
95
  toBinary(): void;
91
- static fromMap(coder: Coder, o: StringMap): BinTrie;
96
+ static fromMap(coder: Coder, o: StringMap, options?: BinTrieOptions): BinTrie;
92
97
  }
93
98
  export {};
package/lib/poly/all.ts CHANGED
@@ -13,3 +13,4 @@ export * from './polyround';
13
13
  export * from './topo';
14
14
  export * from './selfintersect';
15
15
  export * from './shamos';
16
+ export * from './pointinpoly';
@@ -0,0 +1,29 @@
1
+ import * as PP from './polypack';
2
+
3
+ export function polyContainsPoint(poly: any, x: number, y: number): boolean
4
+ {
5
+ let pp = PP.polyPack(poly);
6
+ if (pp == null) return false;
7
+ let bFound = false;
8
+ let bInside = false;
9
+
10
+ PP.polyPackEachRing(pp, (b: Float64Array, iPoly: number, iRing: number, iOffset: number, nPoints: number) => {
11
+ if (bFound) return;
12
+ let inside = false;
13
+ let iEnd = iOffset + (nPoints - 1) * 2;
14
+ for (let i = iOffset, j = iEnd; i < iEnd; j = i, i += 2)
15
+ {
16
+ let xi = b[i], yi = b[i+1];
17
+ let xj = b[j], yj = b[j+1];
18
+ let intersect = ((yi > y) !== (yj > y))
19
+ && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
20
+ if (intersect) inside = !inside;
21
+ }
22
+ if (inside)
23
+ {
24
+ bFound = iRing > 0; // if inside a hole, don't need to process further
25
+ bInside = iRing == 0; // not inside if inside a hole
26
+ }
27
+ });
28
+ return bInside;
29
+ }
@@ -1,3 +1,5 @@
1
+ import * as U from './util';
2
+
1
3
  //
2
4
  // Packed format Trie for mapping string to string
3
5
  // Assumptions:
@@ -343,8 +345,16 @@ export class BinTrie
343
345
  }
344
346
  }
345
347
 
348
+ export interface BinTrieOptions
349
+ {
350
+ dedup?: boolean,
351
+ verbose?: boolean,
352
+ }
353
+ let DefaultOptions: BinTrieOptions = { dedup: true, verbose: false };
354
+
346
355
  export class BinTrieBuilder
347
356
  {
357
+ options: BinTrieOptions;
348
358
  coder: Coder;
349
359
  root: UnpackedNode;
350
360
  vtb: ValueTableBuilder;
@@ -353,9 +363,10 @@ export class BinTrieBuilder
353
363
  u8: Uint8Array;
354
364
  i32: Int32Array;
355
365
 
356
- constructor(coder: Coder)
366
+ constructor(coder: Coder, options?: BinTrieOptions)
357
367
  {
358
368
  this.coder = coder;
369
+ this.options = U.shallowAssignImmutable(DefaultOptions, options);
359
370
  }
360
371
 
361
372
  // Building
@@ -542,9 +553,9 @@ export class BinTrieBuilder
542
553
  }
543
554
 
544
555
  // Building
545
- static fromMap(coder: Coder, o: StringMap): BinTrie
556
+ static fromMap(coder: Coder, o: StringMap, options?: BinTrieOptions): BinTrie
546
557
  {
547
- let btb = new BinTrieBuilder(coder);
558
+ let btb = new BinTrieBuilder(coder, options);
548
559
  btb.vtb = ValueTableBuilder.fromStrings(coder, Object.values(o));
549
560
  btb.vt = ValueTable.fromBuffer(coder, btb.vtb.ab, 0, btb.vtb.ab.byteLength);
550
561
  let keys = Object.keys(o);
@@ -563,7 +574,8 @@ export class BinTrieBuilder
563
574
  });
564
575
 
565
576
  // dedup (collapse branches pointing to same value)
566
- btb.dedup(btb.root);
577
+ if (btb.options.dedup)
578
+ btb.dedup(btb.root);
567
579
 
568
580
  // validate
569
581
  keys.forEach(k => {
@@ -596,7 +608,8 @@ export class BinTrieBuilder
596
608
  }
597
609
  });
598
610
 
599
- console.log(`bintrie: total size: ${btb.u8.length}, value table size: ${btb.vtb.u8.length}`);
611
+ if (btb.options.verbose)
612
+ console.log(`bintrie: total size: ${btb.u8.length}, value table size: ${btb.vtb.u8.length}`);
600
613
 
601
614
  return bt;
602
615
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.18",
3
+ "version": "1.0.21",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",