Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IndexDatatype.fromTypedArray #10350

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
##### Additions :tada:

- Added `Cesium3DTileStyle.fromUrl` for loading a style from a url. [#10348](https://github.com/CesiumGS/cesium/pull/10348)
- Added `IndexDatatype.fromTypedArray`. [#10350](https://github.com/CesiumGS/cesium/pull/10350)

##### Fixes :wrench:

Expand Down
6 changes: 6 additions & 0 deletions Source/Core/ComponentDatatype.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ ComponentDatatype.fromTypedArray = function (array) {
if (array instanceof Float64Array) {
return ComponentDatatype.DOUBLE;
}

//>>includeStart('debug', pragmas.debug);
throw new DeveloperError(
"array must be an Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, or Float64Array."
);
//>>includeEnd('debug');
};

/**
Expand Down
25 changes: 25 additions & 0 deletions Source/Core/IndexDatatype.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,29 @@ IndexDatatype.createTypedArrayFromArrayBuffer = function (

return new Uint16Array(sourceArray, byteOffset, length);
};

/**
* Gets the {@link IndexDatatype} for the provided TypedArray instance.
*
* @param {Uint8Array|Uint16Array|Uint32Array} array The typed array.
* @returns {IndexDatatype} The IndexDatatype for the provided array, or undefined if the array is not a Uint8Array, Uint16Array, or Uint32Array.
*/
IndexDatatype.fromTypedArray = function (array) {
if (array instanceof Uint8Array) {
return IndexDatatype.UNSIGNED_BYTE;
}
if (array instanceof Uint16Array) {
return IndexDatatype.UNSIGNED_SHORT;
}
if (array instanceof Uint32Array) {
return IndexDatatype.UNSIGNED_INT;
}

//>>includeStart('debug', pragmas.debug);
throw new DeveloperError(
"array must be a Uint8Array, Uint16Array, or Uint32Array."
);
//>>includeEnd('debug');
};

export default Object.freeze(IndexDatatype);
7 changes: 5 additions & 2 deletions Source/Scene/PointCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ function createResources(pointCloud, frameState) {
const positions = parsedContent.positions;
const colors = parsedContent.colors;
const normals = parsedContent.normals;
let batchIds = parsedContent.batchIds;
const batchIds = parsedContent.batchIds;
const styleableProperties = parsedContent.styleableProperties;
const hasStyleableProperties = defined(styleableProperties);
const isQuantized = pointCloud._isQuantized;
Expand Down Expand Up @@ -456,7 +456,10 @@ function createResources(pointCloud, frameState) {

let batchIdsVertexBuffer;
if (hasBatchIds) {
batchIds = prepareVertexAttribute(batchIds, "batchIds");
batchIds.typedArray = prepareVertexAttribute(
batchIds.typedArray,
"batchIds"
);
Comment on lines +459 to +462
Copy link
Contributor Author

@lilleyse lilleyse May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caught this bug in the process of adding a DeveloperError to ComponentDatatype.fromTypedArray.

batchIdsVertexBuffer = Buffer.createVertexBuffer({
context: context,
typedArray: batchIds.typedArray,
Expand Down
6 changes: 6 additions & 0 deletions Specs/Core/ComponentDatatypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe("Core/ComponentDatatype", function () {
);
});

it("fromTypedArray throws if array is not a valid typed array", function () {
expect(function () {
ComponentDatatype.fromTypedArray([]);
}).toThrowDeveloperError();
});

it("validate works", function () {
expect(ComponentDatatype.validate(ComponentDatatype.BYTE)).toBe(true);
expect(ComponentDatatype.validate(ComponentDatatype.UNSIGNED_BYTE)).toBe(
Expand Down
46 changes: 34 additions & 12 deletions Specs/Core/IndexDatatypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { IndexDatatype } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";

describe("Core/IndexDatatype", function () {
it("IndexDatatype.validate validates input", function () {
it("validate validates input", function () {
expect(IndexDatatype.validate(IndexDatatype.UNSIGNED_SHORT)).toEqual(true);
expect(IndexDatatype.validate("invalid")).toEqual(false);
expect(IndexDatatype.validate(undefined)).toEqual(false);
});

it("IndexDatatype.createTypedArray creates array", function () {
it("createTypedArray creates array", function () {
expect(IndexDatatype.createTypedArray(3, 3).BYTES_PER_ELEMENT).toEqual(
Uint16Array.BYTES_PER_ELEMENT
);
Expand All @@ -18,13 +18,13 @@ describe("Core/IndexDatatype", function () {
).toEqual(Uint32Array.BYTES_PER_ELEMENT);
});

it("IndexDatatype.createTypedArray throws without numberOfVertices", function () {
it("createTypedArray throws without numberOfVertices", function () {
expect(function () {
IndexDatatype.createTypedArray(undefined);
}).toThrowDeveloperError();
});

it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint16Array", function () {
it("createTypedArrayFromArrayBuffer creates Uint16Array", function () {
const sourceArray = new Uint16Array(10);
sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer(
Expand All @@ -40,7 +40,7 @@ describe("Core/IndexDatatype", function () {
expect(indexBuffer[0]).toEqual(0);
});

it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint16Array with offset", function () {
it("createTypedArrayFromArrayBuffer creates Uint16Array with offset", function () {
const sourceArray = new Uint16Array(10);
sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer(
Expand All @@ -56,7 +56,7 @@ describe("Core/IndexDatatype", function () {
expect(indexBuffer[0]).toEqual(5);
});

it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint32Array", function () {
it("createTypedArrayFromArrayBuffer creates Uint32Array", function () {
const sourceArray = new Uint32Array(10);
sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer(
Expand All @@ -72,7 +72,7 @@ describe("Core/IndexDatatype", function () {
expect(indexBuffer[0]).toEqual(0);
});

it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint32Array with offset", function () {
it("createTypedArrayFromArrayBuffer creates Uint32Array with offset", function () {
const sourceArray = new Uint32Array(10);
sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer(
Expand All @@ -88,19 +88,19 @@ describe("Core/IndexDatatype", function () {
expect(indexBuffer[0]).toEqual(5);
});

it("IndexDatatype.createTypedArrayFromArrayBuffer throws without numberOfVertices", function () {
it("createTypedArrayFromArrayBuffer throws without numberOfVertices", function () {
expect(function () {
IndexDatatype.createTypedArrayFromArrayBuffer(undefined);
}).toThrowDeveloperError();
});

it("IndexDatatype.createTypedArrayFromArrayBuffer throws without sourceArray", function () {
it("createTypedArrayFromArrayBuffer throws without sourceArray", function () {
expect(function () {
IndexDatatype.createTypedArrayFromArrayBuffer(3, undefined);
}).toThrowDeveloperError();
});

it("IndexDatatype.createTypedArrayFromArrayBuffer throws without byteOffset", function () {
it("createTypedArrayFromArrayBuffer throws without byteOffset", function () {
const sourceArray = new Uint16Array(5);
expect(function () {
IndexDatatype.createTypedArrayFromArrayBuffer(
Expand All @@ -111,7 +111,7 @@ describe("Core/IndexDatatype", function () {
}).toThrowDeveloperError();
});

it("IndexDatatype.getSizeInBytes returns size", function () {
it("getSizeInBytes returns size", function () {
expect(IndexDatatype.getSizeInBytes(IndexDatatype.UNSIGNED_BYTE)).toEqual(
Uint8Array.BYTES_PER_ELEMENT
);
Expand All @@ -123,9 +123,31 @@ describe("Core/IndexDatatype", function () {
);
});

it("IndexDatatype.getSizeInBytes throws without indexDatatype", function () {
it("getSizeInBytes throws without indexDatatype", function () {
expect(function () {
IndexDatatype.getSizeInBytes(undefined);
}).toThrowDeveloperError();
});

it("fromTypedArray works", function () {
expect(IndexDatatype.fromTypedArray(new Uint8Array())).toBe(
IndexDatatype.UNSIGNED_BYTE
);
expect(IndexDatatype.fromTypedArray(new Uint16Array())).toBe(
IndexDatatype.UNSIGNED_SHORT
);
expect(IndexDatatype.fromTypedArray(new Uint32Array())).toBe(
IndexDatatype.UNSIGNED_INT
);
});

it("fromTypedArray throws if array is not a valid typed array", function () {
expect(function () {
IndexDatatype.fromTypedArray(new Int8Array());
}).toThrowDeveloperError();

expect(function () {
IndexDatatype.fromTypedArray([]);
}).toThrowDeveloperError();
});
});