LCOV - code coverage report
Current view: top level - packages/viewport3d/src/load3d/loader - ply.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 2.4 % 85 2
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Vector3 } from '@comfyorg/shared-frontend-utils/geometry'
       2              : /**
       3              :  * PLY (Polygon File Format) decoder
       4              :  * Parses ASCII PLY files and extracts vertex positions and colors
       5              :  */
       6              : interface PLYHeader {
       7              :   vertexCount: number
       8              :   hasColor: boolean
       9              :   propertyIndices: Vector3 & {
      10              :     red: number
      11              :     green: number
      12              :     blue: number
      13              :   }
      14              :   headerEndLine: number
      15              : }
      16              : 
      17              : interface PLYData {
      18              :   positions: Float32Array
      19              :   colors: Float32Array | null
      20              :   vertexCount: number
      21              : }
      22              : 
      23            0 : function parsePLYHeader(lines: string[]): PLYHeader | null {
      24            0 :   let vertexCount = 0
      25            0 :   let headerEndLine = 0
      26            0 :   let hasColor = false
      27            0 :   let xIndex = -1
      28            0 :   let yIndex = -1
      29            0 :   let zIndex = -1
      30            0 :   let redIndex = -1
      31            0 :   let greenIndex = -1
      32            0 :   let blueIndex = -1
      33            0 :   let propertyIndex = 0
      34              : 
      35            0 :   for (let i = 0; i < lines.length; i++) {
      36            0 :     const line = lines[i].trim()
      37              : 
      38            0 :     if (line.startsWith('element vertex')) {
      39            0 :       vertexCount = parseInt(line.split(/\s+/)[2])
      40            0 :     } else if (line.startsWith('property')) {
      41            0 :       const parts = line.split(/\s+/)
      42            0 :       const propName = parts[parts.length - 1]
      43              : 
      44            0 :       if (propName === 'x') xIndex = propertyIndex
      45            0 :       else if (propName === 'y') yIndex = propertyIndex
      46            0 :       else if (propName === 'z') zIndex = propertyIndex
      47            0 :       else if (propName === 'red') {
      48            0 :         hasColor = true
      49            0 :         redIndex = propertyIndex
      50            0 :       } else if (propName === 'green') greenIndex = propertyIndex
      51            0 :       else if (propName === 'blue') blueIndex = propertyIndex
      52              : 
      53            0 :       propertyIndex++
      54            0 :     } else if (line === 'end_header') {
      55            0 :       headerEndLine = i
      56            0 :       break
      57              :     }
      58              :   }
      59              : 
      60            0 :   if (vertexCount === 0 || xIndex < 0 || yIndex < 0 || zIndex < 0) {
      61            0 :     return null
      62              :   }
      63              : 
      64            0 :   return {
      65              :     vertexCount,
      66              :     hasColor,
      67              :     propertyIndices: {
      68              :       x: xIndex,
      69              :       y: yIndex,
      70              :       z: zIndex,
      71              :       red: redIndex,
      72              :       green: greenIndex,
      73              :       blue: blueIndex
      74              :     },
      75              :     headerEndLine
      76              :   }
      77              : }
      78              : 
      79            0 : function parsePLYVertices(lines: string[], header: PLYHeader): PLYData {
      80            0 :   const { vertexCount, hasColor, propertyIndices, headerEndLine } = header
      81            0 :   const { x: xIndex, y: yIndex, z: zIndex } = propertyIndices
      82            0 :   const { red: redIndex, green: greenIndex, blue: blueIndex } = propertyIndices
      83              : 
      84            0 :   const positions = new Float32Array(vertexCount * 3)
      85            0 :   const colors = hasColor ? new Float32Array(vertexCount * 3) : null
      86              : 
      87            0 :   let vertexIndex = 0
      88              : 
      89            0 :   for (
      90              :     let i = headerEndLine + 1;
      91              :     i < lines.length && vertexIndex < vertexCount;
      92              :     i++
      93              :   ) {
      94            0 :     const line = lines[i].trim()
      95            0 :     if (!line) continue
      96              : 
      97            0 :     const parts = line.split(/\s+/)
      98            0 :     if (parts.length < 3) continue
      99              : 
     100            0 :     const posIndex = vertexIndex * 3
     101              : 
     102            0 :     positions[posIndex] = parseFloat(parts[xIndex])
     103            0 :     positions[posIndex + 1] = parseFloat(parts[yIndex])
     104            0 :     positions[posIndex + 2] = parseFloat(parts[zIndex])
     105              : 
     106            0 :     if (
     107              :       hasColor &&
     108              :       colors &&
     109              :       redIndex >= 0 &&
     110              :       greenIndex >= 0 &&
     111              :       blueIndex >= 0
     112              :     ) {
     113            0 :       if (parts.length > Math.max(redIndex, greenIndex, blueIndex)) {
     114            0 :         colors[posIndex] = parseInt(parts[redIndex]) / 255
     115            0 :         colors[posIndex + 1] = parseInt(parts[greenIndex]) / 255
     116            0 :         colors[posIndex + 2] = parseInt(parts[blueIndex]) / 255
     117              :       }
     118              :     }
     119              : 
     120            0 :     vertexIndex++
     121              :   }
     122              : 
     123            0 :   return {
     124              :     positions,
     125              :     colors,
     126              :     vertexCount: vertexIndex
     127              :   }
     128              : }
     129              : 
     130              : /**
     131              :  * Parse ASCII PLY data from an ArrayBuffer
     132              :  * Returns positions and colors as typed arrays
     133              :  */
     134            0 : export function parseASCIIPLY(arrayBuffer: ArrayBuffer): PLYData | null {
     135              :   const text = new TextDecoder().decode(arrayBuffer)
     136            0 :   const lines = text.split('\n')
     137              : 
     138            0 :   const header = parsePLYHeader(lines)
     139            0 :   if (!header) return null
     140              : 
     141            0 :   return parsePLYVertices(lines, header)
     142              : }
     143              : 
     144              : /**
     145              :  * Check if PLY data is in ASCII format
     146              :  */
     147            0 : export function isPLYAsciiFormat(arrayBuffer: ArrayBuffer): boolean {
     148              :   const header = new TextDecoder().decode(arrayBuffer.slice(0, 500))
     149            0 :   return header.includes('format ascii')
     150              : }
     151              : 
     152           24 : const PLY_HEADER_SCAN_BYTES = 64 * 1024
     153              : 
     154           24 : const GAUSSIAN_SPLAT_PROPERTIES = [
     155              :   'scale_0',
     156              :   'scale_1',
     157              :   'scale_2',
     158              :   'rot_0',
     159              :   'rot_1',
     160              :   'rot_2',
     161              :   'rot_3'
     162              : ]
     163              : 
     164            0 : function readPLYHeaderLines(arrayBuffer: ArrayBuffer): string[] | null {
     165              :   const text = new TextDecoder('latin1').decode(
     166              :     arrayBuffer.slice(0, PLY_HEADER_SCAN_BYTES)
     167              :   )
     168            0 :   const lines = text.split('\n').map((line) => line.trim())
     169            0 :   const end = lines.indexOf('end_header')
     170            0 :   if (lines[0] !== 'ply' || end < 0) return null
     171            0 :   return lines.slice(0, end)
     172              : }
     173              : 
     174            0 : function vertexPropertyNames(headerLines: string[]): Set<string> {
     175            0 :   const names = new Set<string>()
     176            0 :   let inVertexElement = false
     177            0 :   for (const line of headerLines) {
     178            0 :     const parts = line.split(/\s+/)
     179            0 :     if (parts[0] === 'element') inVertexElement = parts[1] === 'vertex'
     180            0 :     else if (inVertexElement && parts[0] === 'property')
     181            0 :       names.add(parts[parts.length - 1])
     182              :   }
     183            0 :   return names
     184              : }
     185              : 
     186            0 : export function isGaussianSplatPLY(arrayBuffer: ArrayBuffer): boolean {
     187            0 :   const headerLines = readPLYHeaderLines(arrayBuffer)
     188            0 :   if (!headerLines) return false
     189            0 :   if (headerLines.some((line) => line.startsWith('format ascii'))) return false
     190            0 :   const properties = vertexPropertyNames(headerLines)
     191            0 :   return GAUSSIAN_SPLAT_PROPERTIES.every((name) => properties.has(name))
     192              : }
        

Generated by: LCOV version 2.0-1