Skip to content

Commit

Permalink
PLYLoader: Ignore lines in the body
Browse files Browse the repository at this point in the history
Instead of assuming that each line describes one object,
simply split the body at whitespaces and consume the
resulting token stream with no regard to line boundaries.

This is needed because, for example, some ply files exported
by librealsense use multiple lines to describe a single object[0].

[0]: https://github.com/IntelRealSense/librealsense/blob/e9f05c55f88f6876633bd59fd1cb3848da64b699/include/librealsense2/hpp/rs_export.hpp#L240
  • Loading branch information
pobrn committed Apr 10, 2023
1 parent 9b542fa commit 388534f
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions examples/jsm/loaders/PLYLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,34 @@ class PLYLoader extends Loader {

}

function parseASCIIElement( properties, line ) {

const values = line.split( /\s+/ );
function parseASCIIElement( properties, tokens ) {

const element = {};

for ( let i = 0; i < properties.length; i ++ ) {

if ( tokens.empty() )
return null;

if ( properties[ i ].type === 'list' ) {

const list = [];
const n = parseASCIINumber( values.shift(), properties[ i ].countType );
const n = parseASCIINumber( tokens.next(), properties[ i ].countType );

for ( let j = 0; j < n; j ++ ) {

list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
if ( tokens.empty() )
return null;

list.push( parseASCIINumber( tokens.next(), properties[ i ].itemType ) );

}

element[ properties[ i ].name ] = list;

} else {

element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
element[ properties[ i ].name ] = parseASCIINumber( tokens.next(), properties[ i ].type );

}

Expand Down Expand Up @@ -343,46 +347,31 @@ class PLYLoader extends Loader {

let result;

const patternBody = /end_header\s([\s\S]*)$/;
const patternBody = /end_header\s+([\s\S]*)$/;
let body = '';
if ( ( result = patternBody.exec( data ) ) !== null ) {

body = result[ 1 ];

}

const lines = body.split( /\r\n|\r|\n/ );
let currentElement = 0;
let currentElementCount = 0;
let elementDesc = header.elements[ currentElement ];
let attributeMap = mapElementAttributes( elementDesc.properties );

for ( let i = 0; i < lines.length; i ++ ) {

let line = lines[ i ];
line = line.trim();
if ( line === '' ) {
const tokens = new ArrayStream( body.split( /\s+/ ) );

continue;
loop: for ( let i = 0; i < header.elements.length; i ++ ) {

}
const elementDesc = header.elements[ i ];
const attributeMap = mapElementAttributes( elementDesc.properties );

if ( currentElementCount >= elementDesc.count ) {
for ( let j = 0; j < elementDesc.count; j ++ ) {

currentElement ++;
currentElementCount = 0;
elementDesc = header.elements[ currentElement ];
const element = parseASCIIElement( elementDesc.properties, tokens );
if ( ! element )
break loop;

attributeMap = mapElementAttributes( elementDesc.properties );
handleElement( buffer, elementDesc.name, element, attributeMap );

}

const element = parseASCIIElement( elementDesc.properties, line );

handleElement( buffer, elementDesc.name, element, attributeMap );

currentElementCount ++;

}

return postProcess( buffer );
Expand Down Expand Up @@ -733,4 +722,27 @@ class PLYLoader extends Loader {

}

class ArrayStream {

constructor( arr ) {

this.arr = arr;
this.i = 0;

}

empty() {

return this.i >= this.arr.length;

}

next() {

return this.arr[ this.i ++ ];

}

}

export { PLYLoader };

0 comments on commit 388534f

Please sign in to comment.