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

Co-authored-by: Michael Herzog <[email protected]>
  • Loading branch information
pobrn and Mugen87 committed Apr 12, 2023
1 parent 9b542fa commit ceb3a0d
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions examples/jsm/loaders/PLYLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,32 @@ 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 +345,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 );
const tokens = new ArrayStream( body.split( /\s+/ ) );

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

let line = lines[ i ];
line = line.trim();
if ( line === '' ) {
loop: for ( let i = 0; i < header.elements.length; i ++ ) {

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

}
for ( let j = 0; j < elementDesc.count; j ++ ) {

if ( currentElementCount >= elementDesc.count ) {
const element = parseASCIIElement( elementDesc.properties, tokens );

currentElement ++;
currentElementCount = 0;
elementDesc = header.elements[ currentElement ];
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 +720,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 ceb3a0d

Please sign in to comment.