I3SField.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import defined from "../Core/defined.js";
  2. /**
  3. * This class implements an I3S Field which is custom data attached
  4. * to nodes
  5. * @alias I3SField
  6. * @internalConstructor
  7. * @privateParam {I3SNode} parent The parent of that geometry
  8. * @privateParam {object} storageInfo The structure containing the storage info of the field
  9. */
  10. function I3SField(parent, storageInfo) {
  11. this._storageInfo = storageInfo;
  12. this._parent = parent;
  13. this._dataProvider = parent._dataProvider;
  14. const uri = `attributes/${storageInfo.key}/0`;
  15. if (defined(this._parent._nodeIndex)) {
  16. this._resource = this._parent._layer.resource.getDerivedResource({
  17. url: `nodes/${this._parent._data.mesh.attribute.resource}/${uri}`,
  18. });
  19. } else {
  20. this._resource = this._parent.resource.getDerivedResource({ url: uri });
  21. }
  22. }
  23. Object.defineProperties(I3SField.prototype, {
  24. /**
  25. * Gets the resource for the fields
  26. * @memberof I3SField.prototype
  27. * @type {Resource}
  28. * @readonly
  29. */
  30. resource: {
  31. get: function () {
  32. return this._resource;
  33. },
  34. },
  35. /**
  36. * Gets the header for this field.
  37. * @memberof I3SField.prototype
  38. * @type {object}
  39. * @readonly
  40. */
  41. header: {
  42. get: function () {
  43. return this._header;
  44. },
  45. },
  46. /**
  47. * Gets the values for this field.
  48. * @memberof I3SField.prototype
  49. * @type {object}
  50. * @readonly
  51. */
  52. values: {
  53. get: function () {
  54. return defined(this._values) && defined(this._values.attributeValues)
  55. ? this._values.attributeValues
  56. : [];
  57. },
  58. },
  59. /**
  60. * Gets the name for the field.
  61. * @memberof I3SField.prototype
  62. * @type {string}
  63. * @readonly
  64. */
  65. name: {
  66. get: function () {
  67. return this._storageInfo.name;
  68. },
  69. },
  70. });
  71. function getNumericTypeSize(type) {
  72. if (type === "UInt8" || type === "Int8") {
  73. return 1;
  74. } else if (type === "UInt16" || type === "Int16") {
  75. return 2;
  76. } else if (
  77. type === "UInt32" ||
  78. type === "Int32" ||
  79. type === "Oid32" ||
  80. type === "Float32"
  81. ) {
  82. return 4;
  83. } else if (type === "UInt64" || type === "Int64" || type === "Float64") {
  84. return 8;
  85. }
  86. // Not a numeric type
  87. return 0;
  88. }
  89. /**
  90. * Loads the content.
  91. * @returns {Promise<void>} A promise that is resolved when the field data is loaded
  92. */
  93. I3SField.prototype.load = function () {
  94. const that = this;
  95. return this._dataProvider._loadBinary(this._resource).then(function (data) {
  96. // Check if we have a 404
  97. const dataView = new DataView(data);
  98. let success = true;
  99. if (dataView.getUint8(0) === "{".charCodeAt(0)) {
  100. const textContent = new TextDecoder();
  101. const str = textContent.decode(data);
  102. if (str.includes("404")) {
  103. success = false;
  104. console.error(`Failed to load: ${that.resource.url}`);
  105. }
  106. }
  107. if (success) {
  108. that._data = data;
  109. let offset = that._parseHeader(dataView);
  110. const valueSize = getNumericTypeSize(
  111. that._storageInfo.attributeValues.valueType
  112. );
  113. if (valueSize > 0) {
  114. // Values will be padded to align the addresses with the data size
  115. offset = Math.ceil(offset / valueSize) * valueSize;
  116. }
  117. that._parseBody(dataView, offset);
  118. }
  119. });
  120. };
  121. /**
  122. * @private
  123. */
  124. I3SField.prototype._parseValue = function (dataView, type, offset) {
  125. let value;
  126. if (type === "UInt8") {
  127. value = dataView.getUint8(offset);
  128. offset += 1;
  129. } else if (type === "Int8") {
  130. value = dataView.getInt8(offset);
  131. offset += 1;
  132. } else if (type === "UInt16") {
  133. value = dataView.getUint16(offset, true);
  134. offset += 2;
  135. } else if (type === "Int16") {
  136. value = dataView.getInt16(offset, true);
  137. offset += 2;
  138. } else if (type === "UInt32") {
  139. value = dataView.getUint32(offset, true);
  140. offset += 4;
  141. } else if (type === "Oid32") {
  142. value = dataView.getUint32(offset, true);
  143. offset += 4;
  144. } else if (type === "Int32") {
  145. value = dataView.getInt32(offset, true);
  146. offset += 4;
  147. } else if (type === "UInt64") {
  148. const left = dataView.getUint32(offset, true);
  149. const right = dataView.getUint32(offset + 4, true);
  150. value = left + Math.pow(2, 32) * right;
  151. offset += 8;
  152. } else if (type === "Int64") {
  153. const left = dataView.getUint32(offset, true);
  154. const right = dataView.getUint32(offset + 4, true);
  155. if (right < Math.pow(2, 31)) {
  156. // Positive number
  157. value = left + Math.pow(2, 32) * right;
  158. } else {
  159. // Negative
  160. value = left + Math.pow(2, 32) * (right - Math.pow(2, 32));
  161. }
  162. offset += 8;
  163. } else if (type === "Float32") {
  164. value = dataView.getFloat32(offset, true);
  165. offset += 4;
  166. } else if (type === "Float64") {
  167. value = dataView.getFloat64(offset, true);
  168. offset += 8;
  169. } else if (type === "String") {
  170. value = String.fromCharCode(dataView.getUint8(offset));
  171. offset += 1;
  172. }
  173. return {
  174. value: value,
  175. offset: offset,
  176. };
  177. };
  178. /**
  179. * @private
  180. */
  181. I3SField.prototype._parseHeader = function (dataView) {
  182. let offset = 0;
  183. this._header = {};
  184. for (
  185. let itemIndex = 0;
  186. itemIndex < this._storageInfo.header.length;
  187. itemIndex++
  188. ) {
  189. const item = this._storageInfo.header[itemIndex];
  190. const parsedValue = this._parseValue(dataView, item.valueType, offset);
  191. this._header[item.property] = parsedValue.value;
  192. offset = parsedValue.offset;
  193. }
  194. return offset;
  195. };
  196. /**
  197. * @private
  198. */
  199. I3SField.prototype._parseBody = function (dataView, offset) {
  200. this._values = {};
  201. for (
  202. let itemIndex = 0;
  203. itemIndex < this._storageInfo.ordering.length;
  204. itemIndex++
  205. ) {
  206. const item = this._storageInfo.ordering[itemIndex];
  207. const desc = this._storageInfo[item];
  208. if (defined(desc)) {
  209. this._values[item] = [];
  210. for (let index = 0; index < this._header.count; ++index) {
  211. if (desc.valueType !== "String") {
  212. const parsedValue = this._parseValue(
  213. dataView,
  214. desc.valueType,
  215. offset
  216. );
  217. this._values[item].push(parsedValue.value);
  218. offset = parsedValue.offset;
  219. } else {
  220. const stringLen = this._values.attributeByteCounts[index];
  221. let stringContent = "";
  222. for (let cIndex = 0; cIndex < stringLen; ++cIndex) {
  223. const curParsedValue = this._parseValue(
  224. dataView,
  225. desc.valueType,
  226. offset
  227. );
  228. if (curParsedValue.value.charCodeAt(0) !== 0) {
  229. stringContent += curParsedValue.value;
  230. }
  231. offset = curParsedValue.offset;
  232. }
  233. // We skip the last character of the string since it's a null terminator
  234. this._values[item].push(stringContent);
  235. }
  236. }
  237. }
  238. }
  239. };
  240. export default I3SField;