-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
I3SField.js
255 lines (240 loc) · 6.61 KB
/
I3SField.js
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
import defined from "../Core/defined.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;
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 () {
return defined(this._values) && defined(this._values.attributeValues)
? this._values.attributeValues
: [];
},
},
/**
* 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;
}
/**
* Loads the content.
* @returns {Promise.<void>} A promise that is resolved when the field data is loaded
*/
I3SField.prototype.load = function () {
const that = this;
return this._dataProvider._loadBinary(this._resource).then(function (data) {
// Check if we have a 404
const dataView = new DataView(data);
let success = true;
if (dataView.getUint8(0) === "{".charCodeAt(0)) {
const textContent = new TextDecoder();
const str = textContent.decode(data);
if (str.includes("404")) {
success = false;
console.error(`Failed to load: ${that.resource.url}`);
}
}
if (success) {
that._data = data;
let offset = that._parseHeader(dataView);
const valueSize = getNumericTypeSize(
that._storageInfo.attributeValues.valueType
);
if (valueSize > 0) {
// Values will be padded to align the addresses with the data size
offset = Math.ceil(offset / valueSize) * valueSize;
}
that._parseBody(dataView, offset);
}
});
};
/**
* @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 if (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 item = this._storageInfo.ordering[itemIndex];
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);
}
}
}
}
};
export default I3SField;