Skip to content

Commit

Permalink
Merge pull request #9888 from CesiumGS/split-offset-type
Browse files Browse the repository at this point in the history
Handle stringOffsetType and arrayOffsetType from EXT_mesh_features
  • Loading branch information
sanjeetsuhag authored Oct 22, 2021
2 parents 251e279 + 9abd454 commit 33df071
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
34 changes: 26 additions & 8 deletions Source/Scene/MetadataTableProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import MetadataType from "./MetadataType.js";
/**
* A binary property in a {@MetadataTable}
* <p>
* For 3D Tiles Next details, see the {@link https://github.com/CesiumGS/3d-tiles/tree/3d-tiles-next/extensions/3DTILES_metadata|3DTILES_metadata Extension} for 3D Tiles, as well as the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension} for glTF.
* For 3D Tiles Next details, see the {@link https://github.com/CesiumGS/3d-tiles/tree/3d-tiles-next/extensions/3DTILES_metadata|3DTILES_metadata Extension}
* for 3D Tiles, as well as the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_mesh_features|EXT_mesh_features Extension}
* for glTF. For the legacy glTF extension, see {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension}
* </p>
*
* @param {Object} options Object with the following properties:
Expand Down Expand Up @@ -53,16 +55,20 @@ function MetadataTableProperty(options) {
var hasStrings = valueType === MetadataComponentType.STRING;
var hasBooleans = valueType === MetadataComponentType.BOOLEAN;

var offsetType = defaultValue(
MetadataComponentType[property.offsetType],
MetadataComponentType.UINT32
);

var arrayOffsets;
if (isVariableSizeArray) {
// EXT_mesh_features uses arrayOffsetType, EXT_feature_metadata uses offsetType for both arrays and strings
var arrayOffsetType = defaultValue(
property.arrayOffsetType,
property.offsetType
);
arrayOffsetType = defaultValue(
MetadataComponentType[arrayOffsetType],
MetadataComponentType.UINT32
);
arrayOffsets = new BufferView(
bufferViews[property.arrayOffsetBufferView],
offsetType,
arrayOffsetType,
count + 1
);
}
Expand All @@ -78,9 +84,18 @@ function MetadataTableProperty(options) {

var stringOffsets;
if (hasStrings) {
// EXT_mesh_features uses stringOffsetType, EXT_feature_metadata uses offsetType for both arrays and strings
var stringOffsetType = defaultValue(
property.stringOffsetType,
property.offsetType
);
stringOffsetType = defaultValue(
MetadataComponentType[stringOffsetType],
MetadataComponentType.UINT32
);
stringOffsets = new BufferView(
bufferViews[property.stringOffsetBufferView],
offsetType,
stringOffsetType,
componentCount + 1
);
}
Expand Down Expand Up @@ -670,6 +685,9 @@ function BufferView(bufferView, componentType, length) {
this.dataView = new DataView(typedArray.buffer, typedArray.byteOffset);
this.get = getFunction;
this.set = setFunction;

// for unit testing
this._componentType = componentType;
}

export default MetadataTableProperty;
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"count": 4,
"properties": {
"name": {
"offsetType": "UINT16",
"stringOffsetType": "UINT16",
"bufferView": 7,
"stringOffsetBufferView": 8
},
Expand All @@ -57,7 +57,7 @@
"count": 2,
"properties": {
"name": {
"offsetType": "UINT16",
"stringOffsetType": "UINT16",
"bufferView": 10,
"stringOffsetBufferView": 11
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"count": 4,
"properties": {
"name": {
"offsetType": "UINT16",
"stringOffsetType": "UINT16",
"bufferView": 4,
"stringOffsetBufferView": 5
},
Expand All @@ -55,7 +55,7 @@
"count": 2,
"properties": {
"name": {
"offsetType": "UINT16",
"stringOffsetType": "UINT16",
"bufferView": 7,
"stringOffsetBufferView": 8
},
Expand Down
21 changes: 19 additions & 2 deletions Specs/MetadataTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ MetadataTester.createProperty = function (options) {
var table = MetadataTester.createMetadataTable({
properties: properties,
propertyValues: propertyValues,
// offsetType is for legacy EXT_feature_metadata, arrayOffsetType and
// stringOffsetType are for EXT_mesh_features
offsetType: options.offsetType,
arrayOffsetType: options.arrayOffsetType,
stringOffsetType: options.stringOffsetType,
enums: options.enums,
disableBigIntSupport: options.disableBigIntSupport,
disableBigInt64ArraySupport: options.disableBigInt64ArraySupport,
Expand All @@ -47,6 +51,8 @@ function createProperties(options) {
var classId = options.classId;
var propertyValues = options.propertyValues;
var offsetType = options.offsetType;
var stringOffsetType = options.stringOffsetType;
var arrayOffsetType = options.arrayOffsetType;
var bufferViews = defined(options.bufferViews) ? options.bufferViews : {};

var enums = defined(schema.enums) ? schema.enums : {};
Expand Down Expand Up @@ -86,25 +92,36 @@ function createProperties(options) {

properties[propertyId] = property;

// for legacy EXT_feature_metadata
if (defined(offsetType)) {
property.offsetType = offsetType;
}

if (defined(stringOffsetType)) {
property.stringOffsetType = offsetType;
}

if (defined(arrayOffsetType)) {
property.arrayOffsetType = arrayOffsetType;
}

if (
classProperty.type === MetadataType.ARRAY &&
!defined(classProperty.componentCount)
) {
var arrayOffsetBufferType = defaultValue(arrayOffsetType, offsetType);
var arrayOffsetBuffer = addPadding(
createArrayOffsetBuffer(values, offsetType)
createArrayOffsetBuffer(values, arrayOffsetBufferType)
);
var arrayOffsetBufferView = bufferViewIndex++;
bufferViews[arrayOffsetBufferView] = arrayOffsetBuffer;
property.arrayOffsetBufferView = arrayOffsetBufferView;
}

if (classProperty.componentType === MetadataComponentType.STRING) {
var stringOffsetBufferType = defaultValue(stringOffsetType, offsetType);
var stringOffsetBuffer = addPadding(
createStringOffsetBuffer(values, offsetType)
createStringOffsetBuffer(values, stringOffsetBufferType)
);
var stringOffsetBufferView = bufferViewIndex++;
bufferViews[stringOffsetBufferView] = stringOffsetBuffer;
Expand Down
104 changes: 104 additions & 0 deletions Specs/Scene/MetadataTablePropertySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
defaultValue,
Cartesian3,
MetadataClassProperty,
MetadataComponentType,
MetadataTableProperty,
} from "../../Source/Cesium.js";
import MetadataTester from "../MetadataTester.js";
Expand Down Expand Up @@ -61,6 +62,109 @@ describe("Scene/MetadataTableProperty", function () {
expect(property.extensions).toBe(extensions);
});

it("constructs properties with stringOffset and arrayOffset", function () {
var extras = {
other: 0,
};

var extensions = {
EXT_other_extension: {},
};

var a = 97;
var b = 98;
var c = 99;
var d = 100;
var e = 101;

var property = new MetadataTableProperty({
count: 2,
property: {
bufferView: 0,
extras: extras,
extensions: extensions,
stringOffsetType: "UINT16",
stringOffsetBufferView: 1,
arrayOffsetType: "UINT8",
arrayOffsetBufferView: 2,
},
classProperty: new MetadataClassProperty({
id: "property",
property: {
type: "ARRAY",
componentType: "STRING",
},
}),
bufferViews: {
0: new Uint8Array([a, b, b, c, c, c, d, d, d, d, e, e, e, e, e]),
1: new Uint8Array([0, 0, 1, 0, 3, 0, 6, 0, 10, 0, 15, 0]),
2: new Uint8Array([0, 3, 5]),
},
});

expect(property.extras).toBe(extras);
expect(property.extensions).toBe(extensions);
expect(property._stringOffsets._componentType).toBe(
MetadataComponentType.UINT16
);
expect(property._arrayOffsets._componentType).toBe(
MetadataComponentType.UINT8
);
expect(property.get(0)).toEqual(["a", "bb", "ccc"]);
expect(property.get(1)).toEqual(["dddd", "eeeee"]);
});

it("constructs property with EXT_feature_metadata offsetType", function () {
var extras = {
other: 0,
};

var extensions = {
EXT_other_extension: {},
};

var a = 97;
var b = 98;
var c = 99;
var d = 100;
var e = 101;

var property = new MetadataTableProperty({
count: 2,
property: {
bufferView: 0,
extras: extras,
extensions: extensions,
offsetType: "UINT16",
stringOffsetBufferView: 1,
arrayOffsetBufferView: 2,
},
classProperty: new MetadataClassProperty({
id: "property",
property: {
type: "ARRAY",
componentType: "STRING",
},
}),
bufferViews: {
0: new Uint8Array([a, b, b, c, c, c, d, d, d, d, e, e, e, e, e]),
1: new Uint8Array([0, 0, 1, 0, 3, 0, 6, 0, 10, 0, 15, 0]),
2: new Uint8Array([0, 0, 3, 0, 5, 0]),
},
});

expect(property.extras).toBe(extras);
expect(property.extensions).toBe(extensions);
expect(property._stringOffsets._componentType).toBe(
MetadataComponentType.UINT16
);
expect(property._arrayOffsets._componentType).toBe(
MetadataComponentType.UINT16
);
expect(property.get(0)).toEqual(["a", "bb", "ccc"]);
expect(property.get(1)).toEqual(["dddd", "eeeee"]);
});

it("constructor throws without count", function () {
expect(function () {
return new MetadataTableProperty({
Expand Down

0 comments on commit 33df071

Please sign in to comment.