Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 30x 30x 30x 30x 30x 30x 17x 13x 1x 3x 14x 62x 59x 56x 3x 2x 4x 32x 42x 6x 36x 6x 30x 22x 8x 6x 2x 16x 1x 15x 16x 15x 15x 15x 14x 14x 14x 13x 1x 17x 1x 16x 3x 16x 1x 55x 5x 5x 50x 2x 2x 48x 2x 2x 46x 2x 2x 44x 19x 19x 25x 2x 2x 23x 2x 2x 21x 2x 2x 2x 2x 19x 2x 2x 2x 1x 1x 2x 17x 2x 2x 15x 2x 2x 13x 13x 13x 55x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 13x 13x 13x 16x 16x 16x 16x 15x 15x 28x 25x 25x 25x 3x 3x 3x 13x 13x 10x 13x 3x 1x 16x 16x 12x 4x 2x 16x 12x 4x 1x 16x 16x 16x 16x 16x 16x 2x 1x 18x 2x 16x 16x 17x 17x 17x 17x 16x 13x 2x 13x 13x 3x 1x 2x 2x 3x 3x 3x 1x 14x 1x | import defined from "../Core/defined.js";
import RuntimeError from "../Core/RuntimeError.js";
/**
* This class implements an I3S Field which is custom data attached
* to nodes
* @alias I3SField
* @internalConstructor
* @privateParam {I3SNode} parent The parent of that geometry
* @privateParam {object} storageInfo The structure containing the storage info of the field
*/
function I3SField(parent, storageInfo) {
this._storageInfo = storageInfo;
this._parent = parent;
this._dataProvider = parent._dataProvider;
this._loadPromise = undefined;
const uri = `attributes/${storageInfo.key}/0`;
if (defined(this._parent._nodeIndex)) {
this._resource = this._parent._layer.resource.getDerivedResource({
url: `nodes/${this._parent._data.mesh.attribute.resource}/${uri}`,
});
} else {
this._resource = this._parent.resource.getDerivedResource({ url: uri });
}
}
Object.defineProperties(I3SField.prototype, {
/**
* Gets the resource for the fields
* @memberof I3SField.prototype
* @type {Resource}
* @readonly
*/
resource: {
get: function () {
return this._resource;
},
},
/**
* Gets the header for this field.
* @memberof I3SField.prototype
* @type {object}
* @readonly
*/
header: {
get: function () {
return this._header;
},
},
/**
* Gets the values for this field.
* @memberof I3SField.prototype
* @type {object}
* @readonly
*/
values: {
get: function () {
if (defined(this._values)) {
// attribute data can be stored either as values or as object identifiers
if (defined(this._values.attributeValues)) {
return this._values.attributeValues;
}
if (defined(this._values.objectIds)) {
return this._values.objectIds;
}
}
return [];
},
},
/**
* Gets the name for the field.
* @memberof I3SField.prototype
* @type {string}
* @readonly
*/
name: {
get: function () {
return this._storageInfo.name;
},
},
});
function getNumericTypeSize(type) {
if (type === "UInt8" || type === "Int8") {
return 1;
} else if (type === "UInt16" || type === "Int16") {
return 2;
} else if (
type === "UInt32" ||
type === "Int32" ||
type === "Oid32" ||
type === "Float32"
) {
return 4;
} else if (type === "UInt64" || type === "Int64" || type === "Float64") {
return 8;
}
// Not a numeric type
return 0;
}
function getValueTypeSize(type) {
if (type === "String") {
return 1;
}
return getNumericTypeSize(type);
}
async function load(field) {
const data = await field._dataProvider._loadBinary(field._resource);
const dataView = new DataView(data);
field._data = data;
field._validateHeader(dataView);
const headerSize = field._parseHeader(dataView);
const offset = field._getBodyOffset(headerSize);
field._validateBody(dataView, offset);
field._parseBody(dataView, offset);
}
/**
* Loads the content.
* @returns {Promise<void>} A promise that is resolved when the field data is loaded
*/
I3SField.prototype.load = function () {
if (defined(this._loadPromise)) {
return this._loadPromise;
}
this._loadPromise = load(this).catch(function (error) {
console.error(error);
});
return this._loadPromise;
};
/**
* @private
*/
I3SField.prototype._parseValue = function (dataView, type, offset) {
let value;
if (type === "UInt8") {
value = dataView.getUint8(offset);
offset += 1;
} else if (type === "Int8") {
value = dataView.getInt8(offset);
offset += 1;
} else if (type === "UInt16") {
value = dataView.getUint16(offset, true);
offset += 2;
} else if (type === "Int16") {
value = dataView.getInt16(offset, true);
offset += 2;
} else if (type === "UInt32") {
value = dataView.getUint32(offset, true);
offset += 4;
} else if (type === "Oid32") {
value = dataView.getUint32(offset, true);
offset += 4;
} else if (type === "Int32") {
value = dataView.getInt32(offset, true);
offset += 4;
} else if (type === "UInt64") {
const left = dataView.getUint32(offset, true);
const right = dataView.getUint32(offset + 4, true);
value = left + Math.pow(2, 32) * right;
offset += 8;
} else if (type === "Int64") {
const left = dataView.getUint32(offset, true);
const right = dataView.getUint32(offset + 4, true);
if (right < Math.pow(2, 31)) {
// Positive number
value = left + Math.pow(2, 32) * right;
} else {
// Negative
value = left + Math.pow(2, 32) * (right - Math.pow(2, 32));
}
offset += 8;
} else if (type === "Float32") {
value = dataView.getFloat32(offset, true);
offset += 4;
} else if (type === "Float64") {
value = dataView.getFloat64(offset, true);
offset += 8;
} else Eif (type === "String") {
value = String.fromCharCode(dataView.getUint8(offset));
offset += 1;
}
return {
value: value,
offset: offset,
};
};
/**
* @private
*/
I3SField.prototype._parseHeader = function (dataView) {
let offset = 0;
this._header = {};
for (
let itemIndex = 0;
itemIndex < this._storageInfo.header.length;
itemIndex++
) {
const item = this._storageInfo.header[itemIndex];
const parsedValue = this._parseValue(dataView, item.valueType, offset);
this._header[item.property] = parsedValue.value;
offset = parsedValue.offset;
}
return offset;
};
/**
* @private
*/
I3SField.prototype._parseBody = function (dataView, offset) {
this._values = {};
for (
let itemIndex = 0;
itemIndex < this._storageInfo.ordering.length;
itemIndex++
) {
const orderingValue = this._storageInfo.ordering[itemIndex];
// all strings in the ordering array correspond to the property name, except ObjectIds
const item = orderingValue === "ObjectIds" ? "objectIds" : orderingValue;
const desc = this._storageInfo[item];
if (defined(desc)) {
this._values[item] = [];
for (let index = 0; index < this._header.count; ++index) {
if (desc.valueType !== "String") {
const parsedValue = this._parseValue(
dataView,
desc.valueType,
offset,
);
this._values[item].push(parsedValue.value);
offset = parsedValue.offset;
} else {
const stringLen = this._values.attributeByteCounts[index];
let stringContent = "";
for (let cIndex = 0; cIndex < stringLen; ++cIndex) {
const curParsedValue = this._parseValue(
dataView,
desc.valueType,
offset,
);
if (curParsedValue.value.charCodeAt(0) !== 0) {
stringContent += curParsedValue.value;
}
offset = curParsedValue.offset;
}
// We skip the last character of the string since it's a null terminator
this._values[item].push(stringContent);
}
}
}
}
};
/**
* @private
*/
I3SField.prototype._getBodyOffset = function (headerSize) {
let valueSize = 0;
if (defined(this._storageInfo.attributeValues)) {
valueSize = getNumericTypeSize(this._storageInfo.attributeValues.valueType);
} else if (defined(this._storageInfo.objectIds)) {
valueSize = getNumericTypeSize(this._storageInfo.objectIds.valueType);
}
if (valueSize > 0) {
// Values will be padded to align the addresses with the data size
return Math.ceil(headerSize / valueSize) * valueSize;
}
return headerSize;
};
/**
* @private
*/
I3SField.prototype._validateHeader = function (dataView) {
let headerSize = 0;
for (
let itemIndex = 0;
itemIndex < this._storageInfo.header.length;
itemIndex++
) {
const item = this._storageInfo.header[itemIndex];
headerSize += getValueTypeSize(item.valueType);
}
if (dataView.byteLength < headerSize) {
throw new RuntimeError(
`Invalid attribute buffer size (field: ${this.name}, header: ${headerSize}, actual: ${dataView.byteLength})`,
);
}
};
/**
* @private
*/
I3SField.prototype._validateBody = function (dataView, offset) {
if (!defined(this._header.count)) {
throw new RuntimeError(
`Invalid attribute buffer (field: ${this.name}, count is missing)`,
);
}
let attributeByteCountsOffset;
for (
let itemIndex = 0;
itemIndex < this._storageInfo.ordering.length &&
offset < dataView.byteLength;
itemIndex++
) {
const orderingValue = this._storageInfo.ordering[itemIndex];
// all strings in the ordering array correspond to the property name, except ObjectIds
const item = orderingValue === "ObjectIds" ? "objectIds" : orderingValue;
const desc = this._storageInfo[item];
if (defined(desc)) {
if (desc.valueType !== "String") {
if (item === "attributeByteCounts") {
attributeByteCountsOffset = offset;
}
const valueSize = getNumericTypeSize(desc.valueType);
offset += valueSize * this._header.count;
} else {
if (!defined(attributeByteCountsOffset)) {
throw new RuntimeError(
`Invalid attribute buffer (field: ${this.name}, attributeByteCounts is missing)`,
);
}
for (
let index = 0;
index < this._header.count && offset < dataView.byteLength;
++index
) {
const parsedValue = this._parseValue(
dataView,
this._storageInfo.attributeByteCounts.valueType,
attributeByteCountsOffset,
);
offset += parsedValue.value;
attributeByteCountsOffset = parsedValue.offset;
}
}
} else {
throw new RuntimeError(
`Invalid attribute buffer (field: ${this.name}, ${item} is missing)`,
);
}
}
if (dataView.byteLength < offset) {
throw new RuntimeError(
`Invalid attribute buffer size (field: ${this.name}, expected: ${offset}, actual: ${dataView.byteLength})`,
);
}
};
export default I3SField;
|