CSVExporter.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. export class CSVExporter {
  2. static toString (points) {
  3. let string = '';
  4. let attributes = Object.keys(points.data)
  5. .filter(a => a !== 'normal')
  6. .sort((a, b) => {
  7. if (a === 'position') return -1;
  8. if (b === 'position') return 1;
  9. if (a === 'rgba') return -1;
  10. if (b === 'rgba') return 1;
  11. });
  12. let headerValues = [];
  13. for (let attribute of attributes) {
  14. let itemSize = points.data[attribute].length / points.numPoints;
  15. if (attribute === 'position') {
  16. headerValues = headerValues.concat(['x', 'y', 'z']);
  17. } else if (attribute === 'rgba') {
  18. headerValues = headerValues.concat(['r', 'g', 'b', 'a']);
  19. } else if (itemSize > 1) {
  20. for (let i = 0; i < itemSize; i++) {
  21. headerValues.push(`${attribute}_${i}`);
  22. }
  23. } else {
  24. headerValues.push(attribute);
  25. }
  26. }
  27. string = headerValues.join(', ') + '\n';
  28. for (let i = 0; i < points.numPoints; i++) {
  29. let values = [];
  30. for (let attribute of attributes) {
  31. let itemSize = points.data[attribute].length / points.numPoints;
  32. let value = points.data[attribute]
  33. .subarray(itemSize * i, itemSize * i + itemSize)
  34. .join(', ');
  35. values.push(value);
  36. }
  37. string += values.join(', ') + '\n';
  38. }
  39. return string;
  40. }
  41. };