modeling/src/geometries/geom2/fromCompactBinary.js

  1. const mat4 = require('../../maths/mat4')
  2. const vec2 = require('../../maths/vec2')
  3. const create = require('./create')
  4. /**
  5. * Create a new 2D geometry from the given compact binary data.
  6. * @param {Array} data - compact binary data
  7. * @returns {geom2} a new geometry
  8. * @alias module:modeling/geometries/geom2.fromCompactBinary
  9. */
  10. const fromCompactBinary = (data) => {
  11. if (data[0] !== 0) throw new Error('invalid compact binary data')
  12. const created = create()
  13. created.transforms = mat4.clone(data.slice(1, 17))
  14. for (let i = 21; i < data.length; i += 4) {
  15. const point0 = vec2.fromValues(data[i + 0], data[i + 1])
  16. const point1 = vec2.fromValues(data[i + 2], data[i + 3])
  17. created.sides.push([point0, point1])
  18. }
  19. // transfer known properties, i.e. color
  20. if (data[17] >= 0) {
  21. created.color = [data[17], data[18], data[19], data[20]]
  22. }
  23. // TODO: how about custom properties or fields ?
  24. return created
  25. }
  26. module.exports = fromCompactBinary