PLYLoader.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. * @author Wei Meng / http://about.me/menway
  3. *
  4. * Description: A THREE loader for PLY ASCII files (known as the Polygon File Format or the Stanford Triangle Format).
  5. *
  6. * Currently only supports ASCII encoded files.
  7. *
  8. * Limitations: ASCII decoding assumes file is UTF-8.
  9. *
  10. * Usage:
  11. * var loader = new THREE.PLYLoader();
  12. * loader.addEventListener( 'load', function ( event ) {
  13. *
  14. * var geometry = event.content;
  15. * scene.add( new THREE.Mesh( geometry ) );
  16. *
  17. * } );
  18. * loader.load( './models/ply/ascii/dolphins.ply' );
  19. */
  20. THREE.PLYLoader = function () {
  21. this.propertyNameMapping = {};
  22. };
  23. THREE.PLYLoader.prototype = {
  24. constructor: THREE.PLYLoader,
  25. setPropertyNameMapping: function ( mapping ) {
  26. this.propertyNameMapping = mapping;
  27. },
  28. load: function ( url, callback ) {
  29. var scope = this;
  30. var request = new XMLHttpRequest();
  31. request.addEventListener( 'load', function ( event ) {
  32. var geometry = scope.parse( event.target.response );
  33. scope.dispatchEvent( { type: 'load', content: geometry } );
  34. if ( callback ) callback( geometry );
  35. }, false );
  36. request.addEventListener( 'progress', function ( event ) {
  37. scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );
  38. }, false );
  39. request.addEventListener( 'error', function () {
  40. scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
  41. }, false );
  42. request.open( 'GET', url, true );
  43. request.responseType = "arraybuffer";
  44. request.send( null );
  45. },
  46. bin2str: function (buf) {
  47. var array_buffer = new Uint8Array(buf);
  48. var str = '';
  49. for(var i = 0; i < buf.byteLength; i++) {
  50. str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
  51. }
  52. return str;
  53. },
  54. isASCII: function( data ){
  55. var header = this.parseHeader( this.bin2str( data ) );
  56. return header.format === "ascii";
  57. },
  58. parse: function ( data ) {
  59. if ( data instanceof ArrayBuffer ) {
  60. return this.isASCII( data )
  61. ? this.parseASCII( this.bin2str( data ) )
  62. : this.parseBinary( data );
  63. } else {
  64. return this.parseASCII( data );
  65. }
  66. },
  67. parseHeader: function ( data ) {
  68. var patternHeader = /ply([\s\S]*)end_header\n/;
  69. var headerText = "";
  70. if ( ( result = patternHeader.exec( data ) ) != null ) {
  71. headerText = result [ 1 ];
  72. }
  73. var header = new Object();
  74. header.comments = [];
  75. header.elements = [];
  76. header.headerLength = result[0].length;
  77. var lines = headerText.split( '\n' );
  78. var currentElement = undefined;
  79. var lineType, lineValues;
  80. function make_ply_element_property(propertValues, propertyNameMapping) {
  81. var property = Object();
  82. property.type = propertValues[0]
  83. if ( property.type === "list" ) {
  84. property.name = propertValues[3]
  85. property.countType = propertValues[1]
  86. property.itemType = propertValues[2]
  87. } else {
  88. property.name = propertValues[1]
  89. }
  90. if ( property.name in propertyNameMapping ) {
  91. property.name = propertyNameMapping[property.name];
  92. }
  93. return property
  94. }
  95. for ( var i = 0; i < lines.length; i ++ ) {
  96. var line = lines[ i ];
  97. line = line.trim()
  98. if ( line === "" ) { continue; }
  99. lineValues = line.split( /\s+/ );
  100. lineType = lineValues.shift()
  101. line = lineValues.join(" ")
  102. switch( lineType ) {
  103. case "format":
  104. header.format = lineValues[0];
  105. header.version = lineValues[1];
  106. break;
  107. case "comment":
  108. header.comments.push(line);
  109. break;
  110. case "element":
  111. if ( !(currentElement === undefined) ) {
  112. header.elements.push(currentElement);
  113. }
  114. currentElement = Object();
  115. currentElement.name = lineValues[0];
  116. currentElement.count = parseInt( lineValues[1] );
  117. currentElement.properties = [];
  118. break;
  119. case "property":
  120. currentElement.properties.push( make_ply_element_property( lineValues, this.propertyNameMapping ) );
  121. break;
  122. default:
  123. console.log("unhandled", lineType, lineValues);
  124. }
  125. }
  126. if ( !(currentElement === undefined) ) {
  127. header.elements.push(currentElement);
  128. }
  129. return header;
  130. },
  131. parseASCIINumber: function ( n, type ) {
  132. switch( type ) {
  133. case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
  134. case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
  135. return parseInt( n );
  136. case 'float': case 'double': case 'float32': case 'float64':
  137. return parseFloat( n );
  138. }
  139. },
  140. parseASCIIElement: function ( properties, line ) {
  141. values = line.split( /\s+/ );
  142. var element = Object();
  143. for ( var i = 0; i < properties.length; i ++ ) {
  144. if ( properties[i].type === "list" ) {
  145. var list = [];
  146. var n = this.parseASCIINumber( values.shift(), properties[i].countType );
  147. for ( j = 0; j < n; j ++ ) {
  148. list.push( this.parseASCIINumber( values.shift(), properties[i].itemType ) );
  149. }
  150. element[ properties[i].name ] = list;
  151. } else {
  152. element[ properties[i].name ] = this.parseASCIINumber( values.shift(), properties[i].type );
  153. }
  154. }
  155. return element;
  156. },
  157. parseASCII: function ( data ) {
  158. // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
  159. var geometry = new THREE.Geometry();
  160. var result;
  161. var header = this.parseHeader( data );
  162. var patternBody = /end_header\n([\s\S]*)$/;
  163. var body = "";
  164. if ( ( result = patternBody.exec( data ) ) != null ) {
  165. body = result [ 1 ];
  166. }
  167. var lines = body.split( '\n' );
  168. var currentElement = 0;
  169. var currentElementCount = 0;
  170. geometry.useColor = false;
  171. for ( var i = 0; i < lines.length; i ++ ) {
  172. var line = lines[ i ];
  173. line = line.trim()
  174. if ( line === "" ) { continue; }
  175. if ( currentElementCount >= header.elements[currentElement].count ) {
  176. currentElement++;
  177. currentElementCount = 0;
  178. }
  179. var element = this.parseASCIIElement( header.elements[currentElement].properties, line );
  180. this.handleElement( geometry, header.elements[currentElement].name, element );
  181. currentElementCount++;
  182. }
  183. return this.postProcess( geometry );
  184. },
  185. postProcess: function ( geometry ) {
  186. if ( geometry.useColor ) {
  187. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  188. geometry.faces[i].vertexColors = [
  189. geometry.colors[geometry.faces[i].a],
  190. geometry.colors[geometry.faces[i].b],
  191. geometry.colors[geometry.faces[i].c]
  192. ];
  193. }
  194. geometry.elementsNeedUpdate = true;
  195. }
  196. geometry.computeBoundingSphere();
  197. return geometry;
  198. },
  199. handleElement: function ( geometry, elementName, element ) {
  200. if ( elementName === "vertex" ) {
  201. geometry.vertices.push(
  202. new THREE.Vector3( element.x, element.y, element.z )
  203. );
  204. if ( 'red' in element && 'green' in element && 'blue' in element ) {
  205. geometry.useColor = true;
  206. color = new THREE.Color();
  207. color.setRGB( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
  208. geometry.colors.push( color );
  209. }
  210. } else if ( elementName === "face" ) {
  211. geometry.faces.push(
  212. new THREE.Face3( element.vertex_indices[0], element.vertex_indices[1], element.vertex_indices[2] )
  213. );
  214. }
  215. },
  216. binaryRead: function ( dataview, at, type, little_endian ) {
  217. switch( type ) {
  218. // corespondences for non-specific length types here match rply:
  219. case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
  220. case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
  221. case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
  222. case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
  223. case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
  224. case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
  225. case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
  226. case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
  227. }
  228. },
  229. binaryReadElement: function ( dataview, at, properties, little_endian ) {
  230. var element = Object();
  231. var result, read = 0;
  232. for ( var i = 0; i < properties.length; i ++ ) {
  233. if ( properties[i].type === "list" ) {
  234. var list = [];
  235. result = this.binaryRead( dataview, at+read, properties[i].countType, little_endian );
  236. var n = result[0];
  237. read += result[1];
  238. for ( j = 0; j < n; j ++ ) {
  239. result = this.binaryRead( dataview, at+read, properties[i].itemType, little_endian );
  240. list.push( result[0] );
  241. read += result[1];
  242. }
  243. element[ properties[i].name ] = list;
  244. } else {
  245. result = this.binaryRead( dataview, at+read, properties[i].type, little_endian );
  246. element[ properties[i].name ] = result[0];
  247. read += result[1];
  248. }
  249. }
  250. return [ element, read ];
  251. },
  252. parseBinary: function ( data ) {
  253. var geometry = new THREE.Geometry();
  254. var header = this.parseHeader( this.bin2str( data ) );
  255. var little_endian = (header.format === "binary_little_endian");
  256. var body = new DataView( data, header.headerLength );
  257. var result, loc = 0;
  258. for ( var currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
  259. for ( var currentElementCount = 0; currentElementCount < header.elements[currentElement].count; currentElementCount ++ ) {
  260. result = this.binaryReadElement( body, loc, header.elements[currentElement].properties, little_endian );
  261. loc += result[1];
  262. var element = result[0];
  263. this.handleElement( geometry, header.elements[currentElement].name, element );
  264. }
  265. }
  266. return this.postProcess( geometry );
  267. }
  268. };
  269. THREE.EventDispatcher.prototype.apply( THREE.PLYLoader.prototype );