VTKLoader.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.VTKLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.VTKLoader.prototype = {
  8. constructor: THREE.VTKLoader,
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.XHRLoader( scope.manager );
  12. loader.load( url, function ( text ) {
  13. onLoad( scope.parse( text ) );
  14. }, onProgress, onError );
  15. },
  16. parse: function ( data ) {
  17. var indices = [];
  18. var positions = [];
  19. var result;
  20. // float float float
  21. var pat3Floats = /([\-]?[\d]+[\.]?[\d|\-|e]*)[ ]+([\-]?[\d]+[\.]?[\d|\-|e]*)[ ]+([\-]?[\d]+[\.]?[\d|\-|e]*)/g;
  22. var patTriangle = /^3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/;
  23. var patQuad = /^4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/;
  24. var patPOINTS = /^POINTS /;
  25. var patPOLYGONS = /^POLYGONS /;
  26. var inPointsSection = false;
  27. var inPolygonsSection = false;
  28. var lines = data.split('\n');
  29. for ( var i = 0; i < lines.length; ++i ) {
  30. line = lines[i];
  31. if ( inPointsSection ) {
  32. // get the vertices
  33. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  34. positions.push( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
  35. }
  36. }
  37. else if ( inPolygonsSection ) {
  38. result = patTriangle.exec(line);
  39. if ( result !== null ) {
  40. // 3 int int int
  41. // triangle
  42. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
  43. }
  44. else {
  45. result = patQuad.exec(line);
  46. if ( result !== null ) {
  47. // 4 int int int int
  48. // break quad into two triangles
  49. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 4 ] ) );
  50. indices.push( parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
  51. }
  52. }
  53. }
  54. if ( patPOLYGONS.exec(line) !== null ) {
  55. inPointsSection = false;
  56. inPolygonsSection = true;
  57. }
  58. if ( patPOINTS.exec(line) !== null ) {
  59. inPolygonsSection = false;
  60. inPointsSection = true;
  61. }
  62. }
  63. var geometry = new THREE.BufferGeometry();
  64. geometry.setIndex( new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  65. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  66. return geometry;
  67. }
  68. };
  69. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );