cannabinieri website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
11 KiB

3 years ago
  1. import {
  2. BufferGeometry,
  3. FileLoader,
  4. Float32BufferAttribute,
  5. Loader,
  6. LoaderUtils
  7. } from '../../../build/three.module.js';
  8. /**
  9. * Description: A THREE loader for PLY ASCII files (known as the Polygon
  10. * File Format or the Stanford Triangle Format).
  11. *
  12. * Limitations: ASCII decoding assumes file is UTF-8.
  13. *
  14. * Usage:
  15. * const loader = new PLYLoader();
  16. * loader.load('./models/ply/ascii/dolphins.ply', function (geometry) {
  17. *
  18. * scene.add( new THREE.Mesh( geometry ) );
  19. *
  20. * } );
  21. *
  22. * If the PLY file uses non standard property names, they can be mapped while
  23. * loading. For example, the following maps the properties
  24. * diffuse_(red|green|blue) in the file to standard color names.
  25. *
  26. * loader.setPropertyNameMapping( {
  27. * diffuse_red: 'red',
  28. * diffuse_green: 'green',
  29. * diffuse_blue: 'blue'
  30. * } );
  31. *
  32. */
  33. class PLYLoader extends Loader {
  34. constructor( manager ) {
  35. super( manager );
  36. this.propertyNameMapping = {};
  37. }
  38. load( url, onLoad, onProgress, onError ) {
  39. const scope = this;
  40. const loader = new FileLoader( this.manager );
  41. loader.setPath( this.path );
  42. loader.setResponseType( 'arraybuffer' );
  43. loader.setRequestHeader( this.requestHeader );
  44. loader.setWithCredentials( this.withCredentials );
  45. loader.load( url, function ( text ) {
  46. try {
  47. onLoad( scope.parse( text ) );
  48. } catch ( e ) {
  49. if ( onError ) {
  50. onError( e );
  51. } else {
  52. console.error( e );
  53. }
  54. scope.manager.itemError( url );
  55. }
  56. }, onProgress, onError );
  57. }
  58. setPropertyNameMapping( mapping ) {
  59. this.propertyNameMapping = mapping;
  60. }
  61. parse( data ) {
  62. function parseHeader( data ) {
  63. const patternHeader = /ply([\s\S]*)end_header\r?\n/;
  64. let headerText = '';
  65. let headerLength = 0;
  66. const result = patternHeader.exec( data );
  67. if ( result !== null ) {
  68. headerText = result[ 1 ];
  69. headerLength = new Blob( [ result[ 0 ] ] ).size;
  70. }
  71. const header = {
  72. comments: [],
  73. elements: [],
  74. headerLength: headerLength,
  75. objInfo: ''
  76. };
  77. const lines = headerText.split( '\n' );
  78. let currentElement;
  79. function make_ply_element_property( propertValues, propertyNameMapping ) {
  80. const property = { type: propertValues[ 0 ] };
  81. if ( property.type === 'list' ) {
  82. property.name = propertValues[ 3 ];
  83. property.countType = propertValues[ 1 ];
  84. property.itemType = propertValues[ 2 ];
  85. } else {
  86. property.name = propertValues[ 1 ];
  87. }
  88. if ( property.name in propertyNameMapping ) {
  89. property.name = propertyNameMapping[ property.name ];
  90. }
  91. return property;
  92. }
  93. for ( let i = 0; i < lines.length; i ++ ) {
  94. let line = lines[ i ];
  95. line = line.trim();
  96. if ( line === '' ) continue;
  97. const lineValues = line.split( /\s+/ );
  98. const lineType = lineValues.shift();
  99. line = lineValues.join( ' ' );
  100. switch ( lineType ) {
  101. case 'format':
  102. header.format = lineValues[ 0 ];
  103. header.version = lineValues[ 1 ];
  104. break;
  105. case 'comment':
  106. header.comments.push( line );
  107. break;
  108. case 'element':
  109. if ( currentElement !== undefined ) {
  110. header.elements.push( currentElement );
  111. }
  112. currentElement = {};
  113. currentElement.name = lineValues[ 0 ];
  114. currentElement.count = parseInt( lineValues[ 1 ] );
  115. currentElement.properties = [];
  116. break;
  117. case 'property':
  118. currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) );
  119. break;
  120. case 'obj_info':
  121. header.objInfo = line;
  122. break;
  123. default:
  124. console.log( 'unhandled', lineType, lineValues );
  125. }
  126. }
  127. if ( currentElement !== undefined ) {
  128. header.elements.push( currentElement );
  129. }
  130. return header;
  131. }
  132. function parseASCIINumber( n, type ) {
  133. switch ( type ) {
  134. case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
  135. case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
  136. return parseInt( n );
  137. case 'float': case 'double': case 'float32': case 'float64':
  138. return parseFloat( n );
  139. }
  140. }
  141. function parseASCIIElement( properties, line ) {
  142. const values = line.split( /\s+/ );
  143. const element = {};
  144. for ( let i = 0; i < properties.length; i ++ ) {
  145. if ( properties[ i ].type === 'list' ) {
  146. const list = [];
  147. const n = parseASCIINumber( values.shift(), properties[ i ].countType );
  148. for ( let j = 0; j < n; j ++ ) {
  149. list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
  150. }
  151. element[ properties[ i ].name ] = list;
  152. } else {
  153. element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
  154. }
  155. }
  156. return element;
  157. }
  158. function parseASCII( data, header ) {
  159. // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
  160. const buffer = {
  161. indices: [],
  162. vertices: [],
  163. normals: [],
  164. uvs: [],
  165. faceVertexUvs: [],
  166. colors: []
  167. };
  168. let result;
  169. const patternBody = /end_header\s([\s\S]*)$/;
  170. let body = '';
  171. if ( ( result = patternBody.exec( data ) ) !== null ) {
  172. body = result[ 1 ];
  173. }
  174. const lines = body.split( '\n' );
  175. let currentElement = 0;
  176. let currentElementCount = 0;
  177. for ( let i = 0; i < lines.length; i ++ ) {
  178. let line = lines[ i ];
  179. line = line.trim();
  180. if ( line === '' ) {
  181. continue;
  182. }
  183. if ( currentElementCount >= header.elements[ currentElement ].count ) {
  184. currentElement ++;
  185. currentElementCount = 0;
  186. }
  187. const element = parseASCIIElement( header.elements[ currentElement ].properties, line );
  188. handleElement( buffer, header.elements[ currentElement ].name, element );
  189. currentElementCount ++;
  190. }
  191. return postProcess( buffer );
  192. }
  193. function postProcess( buffer ) {
  194. let geometry = new BufferGeometry();
  195. // mandatory buffer data
  196. if ( buffer.indices.length > 0 ) {
  197. geometry.setIndex( buffer.indices );
  198. }
  199. geometry.setAttribute( 'position', new Float32BufferAttribute( buffer.vertices, 3 ) );
  200. // optional buffer data
  201. if ( buffer.normals.length > 0 ) {
  202. geometry.setAttribute( 'normal', new Float32BufferAttribute( buffer.normals, 3 ) );
  203. }
  204. if ( buffer.uvs.length > 0 ) {
  205. geometry.setAttribute( 'uv', new Float32BufferAttribute( buffer.uvs, 2 ) );
  206. }
  207. if ( buffer.colors.length > 0 ) {
  208. geometry.setAttribute( 'color', new Float32BufferAttribute( buffer.colors, 3 ) );
  209. }
  210. if ( buffer.faceVertexUvs.length > 0 ) {
  211. geometry = geometry.toNonIndexed();
  212. geometry.setAttribute( 'uv', new Float32BufferAttribute( buffer.faceVertexUvs, 2 ) );
  213. }
  214. geometry.computeBoundingSphere();
  215. return geometry;
  216. }
  217. function handleElement( buffer, elementName, element ) {
  218. if ( elementName === 'vertex' ) {
  219. buffer.vertices.push( element.x, element.y, element.z );
  220. if ( 'nx' in element && 'ny' in element && 'nz' in element ) {
  221. buffer.normals.push( element.nx, element.ny, element.nz );
  222. }
  223. if ( 's' in element && 't' in element ) {
  224. buffer.uvs.push( element.s, element.t );
  225. }
  226. if ( 'red' in element && 'green' in element && 'blue' in element ) {
  227. buffer.colors.push( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
  228. }
  229. } else if ( elementName === 'face' ) {
  230. const vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338
  231. const texcoord = element.texcoord;
  232. if ( vertex_indices.length === 3 ) {
  233. buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] );
  234. if ( texcoord && texcoord.length === 6 ) {
  235. buffer.faceVertexUvs.push( texcoord[ 0 ], texcoord[ 1 ] );
  236. buffer.faceVertexUvs.push( texcoord[ 2 ], texcoord[ 3 ] );
  237. buffer.faceVertexUvs.push( texcoord[ 4 ], texcoord[ 5 ] );
  238. }
  239. } else if ( vertex_indices.length === 4 ) {
  240. buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] );
  241. buffer.indices.push( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] );
  242. }
  243. }
  244. }
  245. function binaryRead( dataview, at, type, little_endian ) {
  246. switch ( type ) {
  247. // corespondences for non-specific length types here match rply:
  248. case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
  249. case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
  250. case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
  251. case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
  252. case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
  253. case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
  254. case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
  255. case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
  256. }
  257. }
  258. function binaryReadElement( dataview, at, properties, little_endian ) {
  259. const element = {};
  260. let result, read = 0;
  261. for ( let i = 0; i < properties.length; i ++ ) {
  262. if ( properties[ i ].type === 'list' ) {
  263. const list = [];
  264. result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian );
  265. const n = result[ 0 ];
  266. read += result[ 1 ];
  267. for ( let j = 0; j < n; j ++ ) {
  268. result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian );
  269. list.push( result[ 0 ] );
  270. read += result[ 1 ];
  271. }
  272. element[ properties[ i ].name ] = list;
  273. } else {
  274. result = binaryRead( dataview, at + read, properties[ i ].type, little_endian );
  275. element[ properties[ i ].name ] = result[ 0 ];
  276. read += result[ 1 ];
  277. }
  278. }
  279. return [ element, read ];
  280. }
  281. function parseBinary( data, header ) {
  282. const buffer = {
  283. indices: [],
  284. vertices: [],
  285. normals: [],
  286. uvs: [],
  287. faceVertexUvs: [],
  288. colors: []
  289. };
  290. const little_endian = ( header.format === 'binary_little_endian' );
  291. const body = new DataView( data, header.headerLength );
  292. let result, loc = 0;
  293. for ( let currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
  294. for ( let currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) {
  295. result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian );
  296. loc += result[ 1 ];
  297. const element = result[ 0 ];
  298. handleElement( buffer, header.elements[ currentElement ].name, element );
  299. }
  300. }
  301. return postProcess( buffer );
  302. }
  303. //
  304. let geometry;
  305. const scope = this;
  306. if ( data instanceof ArrayBuffer ) {
  307. const text = LoaderUtils.decodeText( new Uint8Array( data ) );
  308. const header = parseHeader( text );
  309. geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header );
  310. } else {
  311. geometry = parseASCII( data, parseHeader( data ) );
  312. }
  313. return geometry;
  314. }
  315. }
  316. export { PLYLoader };