modeling/src/geometries/geom2/toCompactBinary.js

  1. /**
  2. * Produces a compact binary representation from the given geometry.
  3. * @param {geom2} geometry - the geometry
  4. * @returns {TypedArray} compact binary representation
  5. * @alias module:modeling/geometries/geom2.toCompactBinary
  6. */
  7. const toCompactBinary = (geometry) => {
  8. const sides = geometry.sides
  9. const transforms = geometry.transforms
  10. let color = [-1, -1, -1, -1]
  11. if (geometry.color) color = geometry.color
  12. // FIXME why Float32Array?
  13. const compacted = new Float32Array(1 + 16 + 4 + (sides.length * 4)) // type + transforms + color + sides data
  14. compacted[0] = 0 // type code: 0 => geom2, 1 => geom3 , 2 => path2
  15. compacted[1] = transforms[0]
  16. compacted[2] = transforms[1]
  17. compacted[3] = transforms[2]
  18. compacted[4] = transforms[3]
  19. compacted[5] = transforms[4]
  20. compacted[6] = transforms[5]
  21. compacted[7] = transforms[6]
  22. compacted[8] = transforms[7]
  23. compacted[9] = transforms[8]
  24. compacted[10] = transforms[9]
  25. compacted[11] = transforms[10]
  26. compacted[12] = transforms[11]
  27. compacted[13] = transforms[12]
  28. compacted[14] = transforms[13]
  29. compacted[15] = transforms[14]
  30. compacted[16] = transforms[15]
  31. compacted[17] = color[0]
  32. compacted[18] = color[1]
  33. compacted[19] = color[2]
  34. compacted[20] = color[3]
  35. for (let i = 0; i < sides.length; i++) {
  36. const ci = i * 4 + 21
  37. const point0 = sides[i][0]
  38. const point1 = sides[i][1]
  39. compacted[ci + 0] = point0[0]
  40. compacted[ci + 1] = point0[1]
  41. compacted[ci + 2] = point1[0]
  42. compacted[ci + 3] = point1[1]
  43. }
  44. // TODO: how about custom properties or fields ?
  45. return compacted
  46. }
  47. module.exports = toCompactBinary