All files / Sources/IO/Geometry/PLYWriter index.js

8.97% Statements 14/156
0% Branches 0/75
10% Functions 2/20
9.15% Lines 14/153

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472              1x         1x                                                                                                                                               1x                                                                                                                                                                                                                               1x                                                                                                                                                                                                                                                                                                                                                                                         1x                   1x   1x                                                   1x                               1x     1x     1x   1x                           1x         1x          
import macro from 'vtk.js/Sources/macros';
 
import {
  FormatTypes,
  TextureCoordinatesName,
} from 'vtk.js/Sources/IO/Geometry/PLYWriter/Constants';
 
const { vtkErrorMacro, vtkWarningMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkPLYWriter methods
// ----------------------------------------------------------------------------
const writeHeader = (
  polyData,
  fileFormat,
  fileType,
  headerComments,
  textureFileName,
  textureCoordinatesName,
  vertexCount,
  faceListLength,
  withNormals,
  withUVs,
  withColors,
  withIndices
) => {
  const isBinary = fileFormat !== FormatTypes.ASCII;
  let format;
  if (isBinary) {
    format = fileType ? 'binary_little_endian' : 'binary_big_endian';
  } else format = 'ascii';
 
  headerComments.unshift('VTK.js generated PLY File');
  if (textureFileName) {
    headerComments.push(`TextureFile ${textureFileName}`);
  }
  const commentElements = headerComments
    .map((comment) => `comment ${comment}`)
    .join('\n');
 
  const header = [
    'ply',
    `format ${format} 1.0`,
    `${commentElements}`,
    `element vertex ${vertexCount}`,
    'property float x',
    'property float y',
    'property float z',
  ];
 
  // normals
  if (withNormals) {
    header.push('property float nx', 'property float ny', 'property float nz');
  }
 
  // uvs
  if (withUVs) {
    header.push(
      `property float ${textureCoordinatesName[0]}`,
      `property float ${textureCoordinatesName[1]}`
    );
  }
 
  // colors
  if (withColors) {
    header.push(
      'property uchar red',
      'property uchar green',
      'property uchar blue'
    );
  }
 
  // faces
  if (withIndices) {
    header.push(
      `element face ${faceListLength}`,
      'property list uchar int vertex_indices'
    );
  }
 
  header.push('end_header\n');
  return header.join('\n');
};
 
const binaryWriter = () => {
  let output;
  let vOffset;
  let fOffset;
  const indexByteCount = 4;
  let ft;
  return {
    init: (polyData) => {},
    writeHeader: (
      polyData,
      fileFormat,
      fileType,
      headerComments,
      textureFileName,
      textureCoordinatesName,
      numPts,
      numPolys,
      withNormals,
      withUVs,
      withColors,
      withIndices
    ) => {
      const vertexCount = polyData.getPoints().getNumberOfPoints();
      ft = fileType;
      // 1 byte shape descriptor
      // 3 vertex indices at ${indexByteCount} bytes
      const faceListLength = withIndices
        ? numPolys * (indexByteCount * 3 + 1)
        : 0;
 
      // 3 position values at 4 bytes
      // 3 normal values at 4 bytes
      // 3 color channels with 1 byte
      // 2 uv values at 4 bytes
      const vertexListLength =
        vertexCount *
        (4 * 3 +
          (withNormals ? 4 * 3 : 0) +
          (withUVs ? 4 * 2 : 0) +
          (withColors ? 3 : 0));
 
      const header = writeHeader(
        polyData,
        fileFormat,
        fileType,
        headerComments,
        textureFileName,
        textureCoordinatesName,
        numPts,
        numPolys,
        withNormals,
        withUVs,
        withColors,
        withIndices
      );
      const headerBin = new TextEncoder().encode(header);
      output = new DataView(
        new ArrayBuffer(headerBin.length + vertexListLength + faceListLength)
      );
      new Uint8Array(output.buffer).set(headerBin, 0);
      vOffset = headerBin.length;
      fOffset = vOffset + vertexListLength;
    },
    writeVertice: (x, y, z, nx, ny, nz, u, v, r, g, b) => {
      // xyz
      output.setFloat32(vOffset, x, ft);
      vOffset += 4;
      output.setFloat32(vOffset, y, ft);
      vOffset += 4;
      output.setFloat32(vOffset, z, ft);
      vOffset += 4;
      // nxnynz
      if (nx !== null && ny !== null && nz !== null) {
        output.setFloat32(vOffset, nx, ft);
        vOffset += 4;
        output.setFloat32(vOffset, ny, ft);
        vOffset += 4;
        output.setFloat32(vOffset, nz, ft);
        vOffset += 4;
      }
      // uv
      if (u !== null && v !== null) {
        output.setFloat32(vOffset, u, ft);
        vOffset += 4;
        output.setFloat32(vOffset, v, ft);
        vOffset += 4;
      }
      // rgb
      if (r !== null && g !== null && b !== null) {
        output.setUint8(vOffset, r);
        vOffset += 1;
        output.setUint8(vOffset, g);
        vOffset += 1;
        output.setUint8(vOffset, b);
        vOffset += 1;
      }
    },
    writeFace: (n, x, y, z) => {
      output.setUint8(fOffset, n);
      fOffset += 1;
      output.setUint32(fOffset, x, ft);
      fOffset += indexByteCount;
      output.setUint32(fOffset, y, ft);
      fOffset += indexByteCount;
      output.setUint32(fOffset, z, ft);
      fOffset += indexByteCount;
    },
    writeFooter: (polyData) => {},
    getOutputData: () => output,
  };
};
 
const asciiWriter = () => {
  let fileContent = '';
  return {
    init: (polyData) => {},
    writeHeader: (
      polyData,
      fileFormat,
      fileType,
      headerComments,
      textureFileName,
      textureCoordinatesName,
      numPts,
      numPolys,
      withNormals,
      withUVs,
      withColors,
      withIndices
    ) => {
      fileContent += writeHeader(
        polyData,
        fileFormat,
        fileType,
        headerComments,
        textureFileName,
        textureCoordinatesName,
        numPts,
        numPolys,
        withNormals,
        withUVs,
        withColors,
        withIndices
      );
    },
    writeVertice: (x, y, z, nx, ny, nz, u, v, r, g, b) => {
      fileContent += `${x} ${y} ${z}`;
      if (nx !== null && ny !== null && nz !== null) {
        fileContent += ` ${nx} ${ny} ${nz}`;
      }
      if (u !== null && v !== null) {
        fileContent += ` ${u} ${v}`;
      }
      if (r !== null && g !== null && b !== null) {
        fileContent += ` ${r} ${g} ${b}`;
      }
      fileContent += '\n';
    },
    writeFace: (n, x, y, z) => {
      fileContent += `${n} ${x} ${y} ${z}\n`;
    },
    writeFooter: (polyData) => {},
    getOutputData: () => fileContent,
  };
};
 
function writePLY(
  polyData,
  format,
  dataByteOrder,
  headerComments,
  textureFileName,
  textureCoordinatesName,
  transform,
  withNormals,
  withUVs,
  withColors,
  withIndices
) {
  const inPts = polyData.getPoints();
  const polys = polyData.getPolys();
 
  if (inPts === null || polys === null) {
    vtkErrorMacro('No data to write!');
  }
 
  let writer = null;
  if (format === FormatTypes.BINARY) {
    writer = binaryWriter();
  } else if (format === FormatTypes.ASCII) {
    writer = asciiWriter();
  } else {
    vtkErrorMacro('Invalid type format');
  }
 
  let tCoordsName = textureCoordinatesName;
  if (typeof textureCoordinatesName === 'undefined') {
    vtkWarningMacro(
      'Invalid TextureCoordinatesName value, fallback to default uv values'
    );
    tCoordsName = TextureCoordinatesName.UV;
  }
 
  writer.init(polyData);
 
  const numPts = inPts.getNumberOfPoints();
  const numPolys = polys.getNumberOfCells();
 
  // textureCoords / uvs
  const textureCoords = polyData.getPointData().getTCoords();
  // eslint-disable-next-line no-param-reassign
  withUVs = !(textureCoords === null);
 
  // scalars / colors
  const scalars = polyData.getPointData().getScalars();
  // eslint-disable-next-line no-param-reassign
  withColors = !(scalars === null);
 
  const fileType = dataByteOrder ? 0 : 1;
 
  writer.writeHeader(
    polyData,
    format,
    fileType,
    headerComments,
    textureFileName,
    tCoordsName,
    numPts,
    numPolys,
    withNormals,
    withUVs,
    withColors,
    withIndices
  );
 
  const normals = polyData.getPointData().getNormals();
 
  // points / vertices
  for (let i = 0; i < numPts; i++) {
    // eslint-disable-next-line prefer-const
    let p = inPts.getPoint(i);
 
    // TODO: apply transform matrix
    if (transform) {
      // vec3.transformMat4(p, p, transform);
    }
 
    // coords
    // divide by 1 to remove trailing zeros
    const x = p[0].toPrecision(6) / 1;
    const y = p[1].toPrecision(6) / 1;
    const z = p[2].toPrecision(6) / 1;
 
    // normals
    let nx = null;
    let ny = null;
    let nz = null;
 
    // uvs
    let u = null;
    let v = null;
 
    // colors
    let r = null;
    let g = null;
    let b = null;
 
    if (textureCoords) {
      u = textureCoords.getData()[i * 2];
      v = textureCoords.getData()[i * 2 + 1];
    }
 
    if (scalars) {
      r = scalars.getData()[i * 3];
      g = scalars.getData()[i * 3 + 1];
      b = scalars.getData()[i * 3 + 2];
    }
 
    if (normals) {
      nx = normals.getData()[i * 3];
      ny = normals.getData()[i * 3 + 1];
      nz = normals.getData()[i * 3 + 2];
    }
 
    writer.writeVertice(x, y, z, nx, ny, nz, u, v, r, g, b);
  }
 
  // polys / indices
  const pd = polys.getData();
  for (let i = 0, l = pd.length; i < l; i += 4) {
    writer.writeFace(pd[i + 0], pd[i + 1], pd[i + 2], pd[i + 3]);
  }
 
  writer.writeFooter(polyData);
  return writer.getOutputData();
}
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
export const STATIC = {
  writePLY,
};
 
// ----------------------------------------------------------------------------
// vtkPLYWriter methods
// ----------------------------------------------------------------------------
 
function vtkPLYWriter(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPLYWriter');
 
  publicAPI.requestData = (inData, outData) => {
    const input = inData[0];
    if (!input || input.getClassName() !== 'vtkPolyData') {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
    outData[0] = writePLY(
      input,
      model.format,
      model.dataByteOrder,
      model.headerComments,
      model.textureFileName,
      model.textureCoordinatesName,
      model.transform,
      model.withNormals,
      model.withUVs,
      model.withColors,
      model.withIndices
    );
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  format: FormatTypes.ASCII,
  dataByteOrder: 0,
  headerComments: [],
  textureFileName: null,
  textureCoordinatesName: TextureCoordinatesName.UV,
  transform: null,
  withNormals: true,
  withUVs: true,
  withColors: true,
  withIndices: true,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  macro.setGet(publicAPI, model, [
    'format',
    'dataByteOrder', // binary_little_endian 0 binary_big_endian 1
    'headerComments',
    'textureFileName',
    'textureCoordinatesName',
    'transform',
    'withNormals',
    'withUVs',
    'withColors',
    'withIndices',
  ]);
 
  // Object specific methods
  vtkPLYWriter(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPLYWriter');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...STATIC };