yzt
2023-05-08 24e1c6a1c3d5331b5a4f1111dcbae3ef148eda1a
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import { BatchTableHierarchy, Cartesian2 } from "../../Source/Cesium.js";
 
describe("Scene/BatchTableHierarchy", function () {
  var hierarchyExtension = {
    classes: [
      {
        name: "Wall",
        length: 6,
        instances: {
          color: ["white", "red", "yellow", "gray", "brown", "black"],
        },
      },
      {
        name: "Building",
        length: 3,
        instances: {
          name: ["unit29", "unit20", "unit93"],
          address: ["100 Main St", "102 Main St", "104 Main St"],
        },
      },
      {
        name: "Owner",
        length: 3,
        instances: {
          type: ["city", "resident", "commercial"],
          id: [1120, 1250, 6445],
        },
      },
    ],
    instancesLength: 12,
    classIds: [0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2],
    parentCounts: [1, 3, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0],
    parentIds: [6, 6, 10, 11, 7, 11, 7, 8, 8, 10, 10, 9],
  };
 
  var binaryHierarchy = {
    classes: [
      {
        name: "Box",
        length: 3,
        instances: {
          items: {
            type: "SCALAR",
            componentType: "UNSIGNED_BYTE",
            byteOffset: 0,
          },
          coordinates: {
            type: "VEC2",
            componentType: "UNSIGNED_BYTE",
            byteOffset: 4,
          },
        },
      },
      {
        name: "Pallet",
        length: 1,
        instances: {
          boxCount: [1],
        },
      },
    ],
    instancesLength: 4,
    classIds: [0, 1, 0, 0],
    parentIds: [1, 1, 2, 3],
  };
  var binaryHierarchyBody = new Uint8Array([1, 2, 3, 0, 1, 0, 1, 2, 3, 2]);
 
  it("throws without extension", function () {
    expect(function () {
      return new BatchTableHierarchy({
        extension: undefined,
      });
    }).toThrowDeveloperError();
  });
 
  it("throws for invalid binary property", function () {
    var missingType = {
      classes: [
        {
          name: "Resources",
          length: 2,
          instances: {
            foodUnits: {
              byteOffset: 0,
              componentType: "UNSIGNED_SHORT",
            },
            waterUnits: [15, 13],
          },
        },
      ],
      instancesLength: 2,
      classIds: [0, 0],
      parentIds: [1, 1],
    };
 
    // Using 16 bits because this is the default size
    var foodUnits = new Uint16Array([10, 20]);
    var binaryBody = new Uint8Array(foodUnits.buffer);
 
    expect(function () {
      return new BatchTableHierarchy({
        extension: missingType,
        binaryBody: binaryBody,
      });
    }).toThrowRuntimeError();
 
    var missingComponentType = {
      classes: [
        {
          name: "Resources",
          length: 2,
          instances: {
            foodUnits: {
              byteOffset: 0,
              type: "SCALAR",
            },
            waterUnits: [15, 13],
          },
        },
      ],
      instancesLength: 2,
      classIds: [0, 0],
      parentIds: [1, 1],
    };
 
    expect(function () {
      return new BatchTableHierarchy({
        extension: missingComponentType,
        binaryBody: binaryBody,
      });
    }).toThrowRuntimeError();
  });
 
  it("throws if binaryBody is needed and not provided", function () {
    var hierarchyExtension = {
      classes: [
        {
          name: "Resources",
          length: 2,
          instances: {
            foodUnits: {
              byteOffset: 0,
              type: "SCALAR",
              componentType: "UNSIGNED_SHORT",
            },
            waterUnits: [15, 13],
          },
        },
      ],
      instancesLength: 2,
      classIds: [0, 0],
      parentIds: [1, 1],
    };
 
    expect(function () {
      return new BatchTableHierarchy({
        extension: hierarchyExtension,
      });
    }).toThrowRuntimeError();
  });
 
  it("hasProperty returns true if the feature has this property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.hasProperty(0, "color")).toBe(true);
  });
 
  it("hasProperty returns false if the feature does not have this property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.hasProperty(0, "height")).toBe(false);
  });
 
  it("hasProperty returns false if the feature does not inherit this property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.hasProperty(6, "color")).toBe(false);
  });
 
  it("propertyExists returns true if any feature has this property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.propertyExists("color")).toBe(true);
  });
 
  it("propertyExists returns false if no features have this property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.propertyExists("other")).toBe(false);
  });
 
  it("getProperty returns property value", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.getProperty(0, "color")).toBe("white");
    expect(hierarchy.getProperty(0, "name")).toBe("unit29");
    expect(hierarchy.getProperty(0, "address")).toBe("100 Main St");
    expect(hierarchy.getProperty(0, "type")).toBe("resident");
    expect(hierarchy.getProperty(0, "id")).toBe(1250);
  });
 
  it("getProperty returns undefined for unknown property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.getProperty(0, "occupancy")).not.toBeDefined();
  });
 
  it("getProperty works with binary properties", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: binaryHierarchy,
      binaryBody: binaryHierarchyBody,
    });
    expect(hierarchy.getProperty(0, "items")).toBe(1);
    expect(hierarchy.getProperty(0, "coordinates")).toEqual(
      new Cartesian2(1, 0)
    );
    expect(hierarchy.getProperty(0, "boxCount")).toBe(1);
 
    expect(hierarchy.getProperty(1, "items")).not.toBeDefined();
    expect(hierarchy.getProperty(1, "coordinates")).not.toBeDefined();
    expect(hierarchy.getProperty(1, "boxCount")).toBe(1);
 
    expect(hierarchy.getProperty(2, "items")).toBe(2);
    expect(hierarchy.getProperty(2, "coordinates")).toEqual(
      new Cartesian2(1, 2)
    );
    expect(hierarchy.getProperty(2, "boxCount")).not.toBeDefined();
 
    expect(hierarchy.getProperty(3, "items")).toBe(3);
    expect(hierarchy.getProperty(3, "coordinates")).toEqual(
      new Cartesian2(3, 2)
    );
    expect(hierarchy.getProperty(3, "boxCount")).not.toBeDefined();
  });
 
  it("setProperty throws when trying to set an inherited property", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(function () {
      hierarchy.setProperty(0, "type", "city");
    }).toThrowDeveloperError();
  });
 
  it("setProperty returns false when property does not exist", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.setProperty(0, "occupancy", 100)).toBe(false);
  });
 
  it("setProperty sets property value", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: hierarchyExtension,
    });
    expect(hierarchy.getProperty(0, "color")).toBe("white");
    expect(hierarchy.setProperty(0, "color", "brown")).toBe(true);
    expect(hierarchy.getProperty(0, "color")).toBe("brown");
  });
 
  it("setProperty works with binary values", function () {
    var hierarchy = new BatchTableHierarchy({
      extension: binaryHierarchy,
      binaryBody: binaryHierarchyBody,
    });
 
    expect(hierarchy.getProperty(0, "items")).toBe(1);
    expect(hierarchy.setProperty(0, "items", 5)).toBe(true);
    expect(hierarchy.getProperty(0, "items")).toBe(5);
 
    expect(hierarchy.getProperty(2, "coordinates")).toEqual(
      new Cartesian2(1, 2)
    );
    var position = new Cartesian2(5, 5);
    expect(hierarchy.setProperty(2, "coordinates", position)).toBe(true);
    expect(hierarchy.getProperty(2, "coordinates")).toEqual(position);
  });
 
  it("validates hierarchy with multiple parents", function () {
    //     building0
    //     /      \
    //  door0    door1
    //     \      /
    //      window0
    var extension = {
      instancesLength: 4,
      classIds: [0, 1, 1, 2],
      parentCounts: [2, 1, 1, 0],
      parentIds: [1, 2, 3, 3],
      classes: [
        {
          name: "window",
          length: 1,
          instances: {
            window_name: ["window0"],
          },
        },
        {
          name: "door",
          length: 2,
          instances: {
            door_name: ["door0", "door1"],
          },
        },
        {
          name: "building",
          length: 1,
          instances: {
            building_name: ["building0"],
          },
        },
      ],
    };
 
    var hierarchy = new BatchTableHierarchy({
      extension: extension,
    });
 
    expect(hierarchy.getPropertyIds(0).sort()).toEqual([
      "building_name",
      "door_name",
      "window_name",
    ]);
  });
 
  it("validates hierarchy with multiple parents (2)", function () {
    //             zone
    //             / |  \
    //   building0   |   \
    //     /      \  |    \
    //    door0  door1    /
    //        \    |     /
    //           window0
    var extension = {
      instancesLength: 4,
      classIds: [0, 1, 1, 2, 3],
      parentCounts: [3, 1, 2, 1, 0],
      parentIds: [1, 2, 4, 3, 3, 4, 4],
      classes: [
        {
          name: "window",
          length: 1,
          instances: {
            window_name: ["window0"],
          },
        },
        {
          name: "door",
          length: 2,
          instances: {
            door_name: ["door0", "door1"],
          },
        },
        {
          name: "building",
          length: 1,
          instances: {
            building_name: ["building0"],
          },
        },
        {
          name: "zone",
          length: 1,
          instances: {
            zone_name: ["zone0"],
          },
        },
      ],
    };
 
    var hierarchy = new BatchTableHierarchy({
      extension: extension,
    });
    expect(hierarchy.getPropertyIds(0).sort()).toEqual([
      "building_name",
      "door_name",
      "window_name",
      "zone_name",
    ]); // check window
    expect(hierarchy.hasProperty(1, "zone_name")).toEqual(true); // check doo0
    expect(hierarchy.hasProperty(2, "zone_name")).toEqual(true); // check door1
  });
 
  // Circular dependencies are only caught in debug builds.
  it("throws if hierarchy has a circular dependency", function () {
    // window0 -> door0 -> building0 -> window0
    var extension = {
      instancesLength: 3,
      classIds: [0, 1, 2],
      parentIds: [1, 2, 0],
      classes: [
        {
          name: "window",
          length: 1,
          instances: {
            window_name: ["window0"],
          },
        },
        {
          name: "door",
          length: 1,
          instances: {
            door_name: ["door0"],
          },
        },
        {
          name: "building",
          length: 1,
          instances: {
            building_name: ["building0"],
          },
        },
      ],
    };
 
    expect(function () {
      return new BatchTableHierarchy({
        extension: extension,
      });
    }).toThrowDeveloperError();
  });
 
  it("throws if hierarchy has a circular dependency (2)", function () {
    // window0 -> door0 -> building0 -> window1 -> door0
    var extension = {
      instancesLength: 4,
      classIds: [0, 1, 2, 0],
      parentIds: [1, 2, 3, 1],
      classes: [
        {
          name: "window",
          length: 2,
          instances: {
            window_name: ["window0", "window1"],
          },
        },
        {
          name: "door",
          length: 1,
          instances: {
            door_name: ["door0"],
          },
        },
        {
          name: "building",
          length: 1,
          instances: {
            building_name: ["building0"],
          },
        },
      ],
    };
 
    expect(function () {
      return new BatchTableHierarchy({
        extension: extension,
      });
    }).toThrowDeveloperError();
  });
 
  it("throws if an instance's parentId exceeds instancesLength", function () {
    var extension = {
      instancesLength: 2,
      classIds: [0, 1],
      parentIds: [1, 2],
      classes: [
        {
          name: "window",
          length: 1,
          instances: {
            window_name: ["window0"],
          },
        },
        {
          name: "door",
          length: 1,
          instances: {
            door_name: ["door0"],
          },
        },
      ],
    };
 
    expect(function () {
      return new BatchTableHierarchy({
        extension: extension,
      });
    }).toThrowDeveloperError();
  });
});