@@ -10,14 +10,20 @@ export default class Flatbush {
10
10
/**
11
11
* Recreate a Flatbush index from raw `ArrayBuffer` or `SharedArrayBuffer` data.
12
12
* @param {ArrayBuffer | SharedArrayBuffer } data
13
+ * @param {number } [byteOffset=0] byte offset to the start of the Flatbush buffer in the referenced ArrayBuffer.
13
14
* @returns {Flatbush } index
14
15
*/
15
- static from ( data ) {
16
+ static from ( data , byteOffset = 0 ) {
17
+ if ( byteOffset % 8 !== 0 ) {
18
+ throw new Error ( 'byteOffset must be 8-byte aligned.' ) ;
19
+ }
20
+
16
21
// @ts -expect-error duck typing array buffers
17
22
if ( ! data || data . byteLength === undefined || data . buffer ) {
18
23
throw new Error ( 'Data must be an instance of ArrayBuffer or SharedArrayBuffer.' ) ;
19
24
}
20
- const [ magic , versionAndType ] = new Uint8Array ( data , 0 , 2 ) ;
25
+
26
+ const [ magic , versionAndType ] = new Uint8Array ( data , byteOffset + 0 , 2 ) ;
21
27
if ( magic !== 0xfb ) {
22
28
throw new Error ( 'Data does not appear to be in a Flatbush format.' ) ;
23
29
}
@@ -29,10 +35,10 @@ export default class Flatbush {
29
35
if ( ! ArrayType ) {
30
36
throw new Error ( 'Unrecognized array type.' ) ;
31
37
}
32
- const [ nodeSize ] = new Uint16Array ( data , 2 , 1 ) ;
33
- const [ numItems ] = new Uint32Array ( data , 4 , 1 ) ;
38
+ const [ nodeSize ] = new Uint16Array ( data , byteOffset + 2 , 1 ) ;
39
+ const [ numItems ] = new Uint32Array ( data , byteOffset + 4 , 1 ) ;
34
40
35
- return new Flatbush ( numItems , nodeSize , ArrayType , undefined , data ) ;
41
+ return new Flatbush ( numItems , nodeSize , ArrayType , undefined , data , byteOffset ) ;
36
42
}
37
43
38
44
/**
@@ -42,13 +48,15 @@ export default class Flatbush {
42
48
* @param {TypedArrayConstructor } [ArrayType=Float64Array] The array type used for coordinates storage (`Float64Array` by default).
43
49
* @param {ArrayBufferConstructor | SharedArrayBufferConstructor } [ArrayBufferType=ArrayBuffer] The array buffer type used to store data (`ArrayBuffer` by default).
44
50
* @param {ArrayBuffer | SharedArrayBuffer } [data] (Only used internally)
51
+ * @param {number } [byteOffset=0] (Only used internally)
45
52
*/
46
- constructor ( numItems , nodeSize = 16 , ArrayType = Float64Array , ArrayBufferType = ArrayBuffer , data ) {
53
+ constructor ( numItems , nodeSize = 16 , ArrayType = Float64Array , ArrayBufferType = ArrayBuffer , data , byteOffset = 0 ) {
47
54
if ( numItems === undefined ) throw new Error ( 'Missing required argument: numItems.' ) ;
48
55
if ( isNaN ( numItems ) || numItems <= 0 ) throw new Error ( `Unexpected numItems value: ${ numItems } .` ) ;
49
56
50
57
this . numItems = + numItems ;
51
58
this . nodeSize = Math . min ( Math . max ( + nodeSize , 2 ) , 65535 ) ;
59
+ this . byteOffset = byteOffset ;
52
60
53
61
// calculate the total number of nodes in the R-tree to allocate space for
54
62
// and the index of each tree level (used in search later)
@@ -74,8 +82,8 @@ export default class Flatbush {
74
82
// @ts -expect-error duck typing array buffers
75
83
if ( data && data . byteLength !== undefined && ! data . buffer ) {
76
84
this . data = data ;
77
- this . _boxes = new this . ArrayType ( this . data , 8 , numNodes * 4 ) ;
78
- this . _indices = new this . IndexArrayType ( this . data , 8 + nodesByteSize , numNodes ) ;
85
+ this . _boxes = new this . ArrayType ( this . data , byteOffset + 8 , numNodes * 4 ) ;
86
+ this . _indices = new this . IndexArrayType ( this . data , byteOffset + 8 + nodesByteSize , numNodes ) ;
79
87
80
88
this . _pos = numNodes * 4 ;
81
89
this . minX = this . _boxes [ this . _pos - 4 ] ;
0 commit comments