Skip to content

Commit 0bc2cbe

Browse files
committed
chore(BufferPolygon): Unit tests on new polygon APIs
1 parent f530c58 commit 0bc2cbe

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

packages/engine/Source/Scene/BufferPolygon.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import BufferPrimitiveCollection from "./BufferPrimitiveCollection.js";
88

99
/** @import BufferPolygonCollection from "./BufferPolygonCollection.js"; */
1010

11-
const { ERR_CAPACITY, ERR_RESIZE } = BufferPrimitiveCollection.Error;
11+
const { ERR_CAPACITY, ERR_RESIZE, ERR_OUT_OF_RANGE } =
12+
BufferPrimitiveCollection.Error;
1213

1314
/**
1415
* View bound to the underlying buffer data of a {@link BufferPolygonCollection}.
@@ -151,7 +152,7 @@ class BufferPolygon extends BufferPrimitive {
151152
* return {Float64Array}
152153
*/
153154
getPositions(result) {
154-
return this._getPositionsRange(this.vertexOffset, this.vertexCount, result);
155+
return this._getPositionsRange(0, this.vertexCount, result);
155156
}
156157

157158
/** @param {Float64Array} positions */
@@ -216,11 +217,7 @@ class BufferPolygon extends BufferPrimitive {
216217
* @returns {Float64Array}
217218
*/
218219
getOuterPositions(result) {
219-
return this._getPositionsRange(
220-
this.outerVertexOffset,
221-
this.outerVertexCount,
222-
result,
223-
);
220+
return this._getPositionsRange(0, this.outerVertexCount, result);
224221
}
225222

226223
/**
@@ -350,9 +347,11 @@ class BufferPolygon extends BufferPrimitive {
350347
}
351348

352349
/**
353-
* Internal helper for accessing vertex positions. If 'result' argument is
354-
* given, the requested range of vertices are written to the result array
355-
* and returned. Otherwise, returns an ArrayView on collection memory.
350+
* Internal helper for accessing vertex positions. 'vertexOffset' argument
351+
* is relative to the start of the polygon's vertex block, with 0 being
352+
* the first vertex in the polygon. If 'result' argument is given, the
353+
* requested range of vertices are written to the result array and returned.
354+
* Otherwise, returns an ArrayView on collection memory.
356355
*
357356
* @param {number} vertexOffset
358357
* @param {number} vertexCount
@@ -361,19 +360,29 @@ class BufferPolygon extends BufferPrimitive {
361360
* @private
362361
*/
363362
_getPositionsRange(vertexOffset, vertexCount, result) {
363+
const collection = this._collection;
364364
const positionF64 = this._collection._positionF64;
365365

366+
const collectionVertexOffset = this.vertexOffset + vertexOffset;
367+
368+
//>>includeStart('debug', pragmas.debug);
369+
assert(collectionVertexOffset >= 0, ERR_OUT_OF_RANGE);
370+
assert(collectionVertexOffset < collection.vertexCount, ERR_OUT_OF_RANGE);
371+
assert(vertexCount > 0, ERR_OUT_OF_RANGE);
372+
assert(vertexCount <= this.vertexCount, ERR_OUT_OF_RANGE);
373+
//>>includeEnd('debug');
374+
366375
if (!defined(result)) {
367376
const byteOffset =
368377
positionF64.byteOffset +
369-
vertexOffset * 3 * Float64Array.BYTES_PER_ELEMENT;
378+
collectionVertexOffset * 3 * Float64Array.BYTES_PER_ELEMENT;
370379
return new Float64Array(positionF64.buffer, byteOffset, vertexCount * 3);
371380
}
372381

373382
for (let i = 0; i < vertexCount; i++) {
374-
result[i * 3] = positionF64[(vertexOffset + i) * 3];
375-
result[i * 3 + 1] = positionF64[(vertexOffset + i) * 3 + 1];
376-
result[i * 3 + 2] = positionF64[(vertexOffset + i) * 3 + 2];
383+
result[i * 3] = positionF64[(collectionVertexOffset + i) * 3];
384+
result[i * 3 + 1] = positionF64[(collectionVertexOffset + i) * 3 + 1];
385+
result[i * 3 + 2] = positionF64[(collectionVertexOffset + i) * 3 + 2];
377386
}
378387
return result;
379388
}

packages/engine/Source/Scene/BufferPrimitiveCollection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class BufferPrimitiveCollection {
5454
ERR_CAPACITY: "BufferPrimitiveCollection capacity exceeded.",
5555
ERR_MULTIPLE_OF_FOUR:
5656
"BufferPrimitive byte length must be a multiple of 4.",
57+
ERR_OUT_OF_RANGE: "BufferPrimitive buffer access out of range.",
5758
};
5859

5960
/**

packages/engine/Specs/Scene/BufferPolygonCollectionSpec.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,34 @@ describe("BufferPolygonCollection", () => {
3232

3333
collection.get(0, polygon);
3434
expect(polygon.vertexCount, 3);
35-
expect(polygon.getPositions(new Float64Array(9))).toEqual(positions1);
35+
expect(polygon.getPositions()).toEqual(positions1);
3636

3737
collection.get(1, polygon);
3838
expect(polygon.vertexCount, 3);
39-
expect(polygon.getPositions(new Float64Array(9))).toEqual(positions2);
39+
expect(polygon.getPositions()).toEqual(positions2);
4040

4141
collection.get(2, polygon);
4242
expect(polygon.vertexCount, 4);
43-
expect(polygon.getPositions(new Float64Array(12))).toEqual(positions3);
43+
expect(polygon.getPositions()).toEqual(positions3);
44+
});
45+
46+
it("outerPositions", () => {
47+
const collection = new BufferPolygonCollection();
48+
const polygon = new BufferPolygon();
49+
50+
const positions = new Float64Array([
51+
...createBoxPositions(2), // outer loop
52+
...createBoxPositions(1), // hole
53+
]);
54+
const holes = new Uint32Array([3]);
55+
56+
collection.add({ positions, holes }, polygon);
57+
58+
expect(polygon.vertexCount, 6);
59+
expect(polygon.holeCount, 1);
60+
expect(polygon.outerVertexCount, 3);
61+
expect(polygon.getPositions()).toEqual(positions);
62+
expect(polygon.getOuterPositions()).toEqual(createBoxPositions(2));
4463
});
4564

4665
it("holes", () => {
@@ -71,7 +90,8 @@ describe("BufferPolygonCollection", () => {
7190

7291
collection.get(2, polygon);
7392
expect(polygon.holeCount, 1);
74-
expect(polygon.getHoles(new Uint32Array(1))).toEqual(holes3);
93+
expect(polygon.getHoles()).toEqual(holes3);
94+
expect(polygon.getHolePositions(0)).toEqual(createBoxPositions(1));
7595
});
7696

7797
it("triangles", () => {
@@ -96,15 +116,15 @@ describe("BufferPolygonCollection", () => {
96116

97117
collection.get(0, polygon);
98118
expect(polygon.triangleCount, 2);
99-
expect(polygon.getTriangles(new Uint32Array(6))).toEqual(triangles1);
119+
expect(polygon.getTriangles()).toEqual(triangles1);
100120

101121
collection.get(1, polygon);
102122
expect(polygon.triangleCount, 1);
103-
expect(polygon.getTriangles(new Uint32Array(3))).toEqual(triangles2);
123+
expect(polygon.getTriangles()).toEqual(triangles2);
104124

105125
collection.get(2, polygon);
106126
expect(polygon.triangleCount, 1);
107-
expect(polygon.getTriangles(new Uint32Array(3))).toEqual(triangles3);
127+
expect(polygon.getTriangles()).toEqual(triangles3);
108128
});
109129

110130
it("show", () => {
@@ -219,25 +239,19 @@ describe("BufferPolygonCollection", () => {
219239

220240
dst.get(0, polygon);
221241
expect(polygon.getColor(color)).toEqual(Color.RED);
222-
expect(polygon.getPositions(new Float64Array(positions1.length))).toEqual(
223-
positions1,
224-
);
242+
expect(polygon.getPositions()).toEqual(positions1);
225243
expect(polygon.holeCount).toBe(0);
226244
expect(polygon.triangleCount).toBe(2);
227245

228246
dst.get(1, polygon);
229247
expect(polygon.getColor(color)).toEqual(Color.GREEN);
230-
expect(polygon.getPositions(new Float64Array(positions2.length))).toEqual(
231-
positions2,
232-
);
248+
expect(polygon.getPositions()).toEqual(positions2);
233249
expect(polygon.holeCount).toBe(0);
234250
expect(polygon.triangleCount).toBe(2);
235251

236252
dst.get(2, polygon);
237253
expect(polygon.getColor(color)).toEqual(Color.BLUE);
238-
expect(polygon.getPositions(new Float64Array(positions3.length))).toEqual(
239-
positions3,
240-
);
254+
expect(polygon.getPositions()).toEqual(positions3);
241255
expect(polygon.holeCount).toBe(1);
242256
expect(polygon.triangleCount).toBe(2);
243257
});

0 commit comments

Comments
 (0)