3DMLoader.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /**
  2. * @author Luis Fraguada / https://github.com/fraguada
  3. */
  4. import {
  5. BufferAttribute,
  6. BufferGeometry,
  7. BufferGeometryLoader,
  8. FileLoader,
  9. Loader,
  10. Object3D,
  11. MeshStandardMaterial,
  12. Mesh,
  13. Color,
  14. Points,
  15. PointsMaterial,
  16. Line,
  17. LineBasicMaterial,
  18. Matrix4
  19. } from "../../../build/three.module.js";
  20. import { CSS2DObject } from '../renderers/CSS2DRenderer.js'
  21. var Rhino3dmLoader = function ( manager ) {
  22. Loader.call( this, manager );
  23. this.libraryPath = '';
  24. this.libraryPending = null;
  25. this.libraryBinary = null;
  26. this.libraryConfig = {};
  27. this.workerLimit = 4;
  28. this.workerPool = [];
  29. this.workerNextTaskID = 1;
  30. this.workerSourceURL = '';
  31. this.workerConfig = {};
  32. };
  33. Rhino3dmLoader.taskCache = new WeakMap();
  34. Rhino3dmLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  35. constructor: Rhino3dmLoader,
  36. setLibraryPath: function ( path ) {
  37. this.libraryPath = path;
  38. return this;
  39. },
  40. setWorkerLimit: function ( workerLimit ) {
  41. this.workerLimit = workerLimit;
  42. return this;
  43. },
  44. load: function ( url, onLoad, onProgress, onError ) {
  45. var loader = new FileLoader( this.manager );
  46. loader.setPath( this.path );
  47. loader.setResponseType( 'arraybuffer' );
  48. loader.load( url, ( buffer ) => {
  49. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  50. // again from this thread.
  51. if ( Rhino3dmLoader.taskCache.has( buffer ) ) {
  52. var cachedTask = Rhino3dmLoader.taskCache.get( buffer );
  53. return cachedTask.promise.then( onLoad ).catch( onError );
  54. }
  55. this.decodeObjects( buffer, url )
  56. .then( onLoad )
  57. .catch( onError );
  58. }, onProgress, onError );
  59. },
  60. debug: function () {
  61. console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) );
  62. },
  63. decodeObjects: function ( buffer, url ) {
  64. var worker;
  65. var taskID;
  66. var taskCost = buffer.byteLength;
  67. var objectPending = this._getWorker( taskCost )
  68. .then( ( _worker ) => {
  69. worker = _worker;
  70. taskID = this.workerNextTaskID ++; //hmmm
  71. return new Promise( ( resolve, reject ) => {
  72. worker._callbacks[ taskID ] = { resolve, reject };
  73. worker.postMessage( { type: 'decode', id: taskID, buffer }, [ buffer ] );
  74. //this.debug();
  75. } );
  76. } )
  77. .then( ( message ) => this._createGeometry( message.data ) );
  78. // Remove task from the task list.
  79. // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
  80. objectPending
  81. .catch( () => true )
  82. .then( () => {
  83. if ( worker && taskID ) {
  84. this._releaseTask( worker, taskID );
  85. //this.debug();
  86. }
  87. } );
  88. // Cache the task result.
  89. Rhino3dmLoader.taskCache.set( buffer, {
  90. url: url,
  91. promise: objectPending
  92. } );
  93. return objectPending;
  94. },
  95. parse: function () {
  96. // parsing logic goes here
  97. // console.log( '3dm parsing' );
  98. },
  99. _createMaterial: function ( material ) {
  100. if ( material === undefined ) {
  101. return new MeshStandardMaterial( {
  102. color: new Color( 1, 1, 1 ),
  103. metalness: 0.8,
  104. name: 'default',
  105. side: 2
  106. } );
  107. }
  108. var _diffuseColor = material.diffuseColor;
  109. var diffusecolor = new Color( _diffuseColor.r / 255.0, _diffuseColor.g / 255.0, _diffuseColor.b / 255.0 );
  110. if ( _diffuseColor.r === 0 && _diffuseColor.g === 0 && _diffuseColor.b === 0 ) {
  111. diffusecolor.r = 1;
  112. diffusecolor.g = 1;
  113. diffusecolor.b = 1;
  114. }
  115. return new MeshStandardMaterial( {
  116. color: diffusecolor,
  117. metalness: 0.8,
  118. name: material.name,
  119. side: 2
  120. } );
  121. },
  122. _createGeometry: function ( data ) {
  123. // console.log(data);
  124. var object = new Object3D();
  125. var instanceDefinitionObjects = [];
  126. var instanceDefinitions = [];
  127. var instanceReferences = [];
  128. object.userData[ 'layers' ] = data.layers;
  129. object.userData[ 'groups' ] = data.groups;
  130. var objects = data.objects;
  131. var materials = data.materials;
  132. for ( var i = 0; i < objects.length; i ++ ) {
  133. var obj = objects[ i ];
  134. var attributes = obj.attributes;
  135. switch ( obj.objectType ) {
  136. case 'InstanceDefinition':
  137. instanceDefinitions.push( obj );
  138. break;
  139. case 'InstanceReference':
  140. instanceReferences.push( obj );
  141. break;
  142. default:
  143. var material = this._createMaterial( materials[ attributes.materialIndex ] );
  144. var _object = this._createObject( obj, material );
  145. if ( attributes.isInstanceDefinitionObject ) {
  146. instanceDefinitionObjects.push( _object );
  147. } else {
  148. object.add( _object );
  149. }
  150. break;
  151. }
  152. }
  153. for ( var i = 0; i < instanceDefinitions.length; i ++ ) {
  154. var iDef = instanceDefinitions[ i ];
  155. var objects = [];
  156. for ( var j = 0; j < iDef.attributes.objectIds.length; j ++ ) {
  157. var objId = iDef.attributes.objectIds[ j ];
  158. for ( var p = 0; p < instanceDefinitionObjects.length; p ++ ) {
  159. var idoId = instanceDefinitionObjects[ p ].userData.attributes.id;
  160. if ( objId === idoId ) {
  161. objects.push( instanceDefinitionObjects[ p ] );
  162. }
  163. }
  164. }
  165. console.log(instanceReferences);
  166. for ( var j = 0; j < instanceReferences.length; j ++ ) {
  167. var iRef = instanceReferences[ j ];
  168. if ( iRef.geometry.parentIdefId === iDef.attributes.id ) {
  169. //console.log(iRef);
  170. var iRefObject = new Object3D();
  171. var xf = iRef.geometry.xform.array;
  172. var matrix = new Matrix4();
  173. matrix.set(xf[0], xf[1], xf[2], xf[3], xf[4], xf[5], xf[6], xf[7],
  174. xf[8], xf[9], xf[10], xf[11], xf[12], xf[13], xf[14], xf[15]);
  175. iRefObject.applyMatrix4( matrix );
  176. for( var p = 0; p < objects.length; p ++ ) {
  177. iRefObject.add( objects[ p ].clone( true ) );
  178. }
  179. object.add( iRefObject );
  180. }
  181. }
  182. }
  183. return object;
  184. },
  185. _createObject: function ( obj, mat ) {
  186. var loader = new BufferGeometryLoader();
  187. var attributes = obj.attributes;
  188. switch( obj.objectType ) {
  189. case 'Point':
  190. case 'PointSet':
  191. var geometry = loader.parse( obj.geometry );
  192. var material = new PointsMaterial( { sizeAttenuation: true, vertexColors: true } );
  193. var points = new Points( geometry, material );
  194. points.userData[ 'attributes' ] = attributes;
  195. points.userData[ 'objectType' ] = obj.objectType;
  196. return points;
  197. case 'Mesh':
  198. case 'Extrusion':
  199. var geometry = loader.parse( obj.geometry );
  200. var mesh = new Mesh( geometry, mat );
  201. mesh.castShadow = attributes.castsShadows;
  202. mesh.receiveShadow = attributes.receivesShadows;
  203. mesh.userData[ 'attributes' ] = attributes;
  204. mesh.userData[ 'objectType' ] = obj.objectType;
  205. return mesh;
  206. case 'Brep':
  207. var brepObject = new Object3D();
  208. for ( var j = 0; j < obj.geometry.length; j ++ ) {
  209. geometry = loader.parse( obj.geometry[ j ] );
  210. var mesh = new Mesh( geometry, mat );
  211. mesh.castShadow = attributes.castsShadows;
  212. mesh.receiveShadow = attributes.receivesShadows;
  213. brepObject.add( mesh );
  214. }
  215. brepObject.userData[ 'attributes' ] = attributes;
  216. brepObject.userData[ 'objectType' ] = obj.objectType;
  217. return brepObject;
  218. case 'Curve':
  219. geometry = loader.parse( obj.geometry );
  220. var _color = attributes.drawColor;
  221. var color = new Color( _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 );
  222. var lines = new Line( geometry, new LineBasicMaterial( { color: color } ) );
  223. lines.userData[ 'attributes' ] = attributes;
  224. lines.userData[ 'objectType' ] = obj.objectType;
  225. return lines;
  226. case 'TextDot':
  227. geometry = obj.geometry;
  228. var dotDiv = document.createElement( 'div' );
  229. dotDiv.style.fontFamily = geometry.fontFace;
  230. dotDiv.style.fontSize = `${geometry.fontHeight}px`;
  231. dotDiv.style.marginTop = '-1em';
  232. dotDiv.style.color = '#FFF';
  233. dotDiv.style.padding = '2px';
  234. dotDiv.style.paddingRight = '5px';
  235. dotDiv.style.paddingLeft = '5px';
  236. dotDiv.style.borderRadius = '5px';
  237. var color = attributes.drawColor;
  238. dotDiv.style.background = `rgba(${color.r},${color.g},${color.b},${color.a})`;
  239. dotDiv.textContent = geometry.text;
  240. var dot = new CSS2DObject( dotDiv );
  241. var location = geometry.point;
  242. dot.position.set( location[ 0 ], location[ 1 ], location[ 2 ] );
  243. dot.userData[ 'attributes' ] = attributes;
  244. dot.userData[ 'objectType' ] = obj.objectType;
  245. return dot;
  246. }
  247. },
  248. _initLibrary: function () {
  249. if ( ! this.libraryPending ) {
  250. // Load rhino3dm wrapper.
  251. var jsLoader = new FileLoader( this.manager );
  252. jsLoader.setPath( this.libraryPath );
  253. var jsContent = new Promise( ( resolve, reject ) => {
  254. jsLoader.load( 'rhino3dm.js', resolve, undefined, reject );
  255. } );
  256. // Load rhino3dm WASM binary.
  257. var binaryLoader = new FileLoader( this.manager );
  258. binaryLoader.setPath( this.libraryPath );
  259. binaryLoader.setResponseType( 'arraybuffer' );
  260. var binaryContent = new Promise( ( resolve, reject ) => {
  261. binaryLoader.load( 'rhino3dm.wasm', resolve, undefined, reject );
  262. } );
  263. this.libraryPending = Promise.all( [ jsContent, binaryContent ] )
  264. .then( ( [ jsContent, binaryContent ] ) => {
  265. //this.libraryBinary = binaryContent;
  266. this.libraryConfig.wasmBinary = binaryContent;
  267. var fn = Rhino3dmLoader.Rhino3dmWorker.toString();
  268. var body = [
  269. '/* rhino3dm.js */',
  270. jsContent,
  271. '/* worker */',
  272. fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
  273. ].join( '\n' );
  274. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  275. } );
  276. }
  277. return this.libraryPending;
  278. },
  279. _getWorker: function ( taskCost ) {
  280. return this._initLibrary().then( () => {
  281. if ( this.workerPool.length < this.workerLimit ) {
  282. var worker = new Worker( this.workerSourceURL );
  283. worker._callbacks = {};
  284. worker._taskCosts = {};
  285. worker._taskLoad = 0;
  286. worker.postMessage( {
  287. type: 'init',
  288. libraryConfig: this.libraryConfig
  289. } );
  290. worker.onmessage = function ( e ) {
  291. var message = e.data;
  292. switch ( message.type ) {
  293. case 'decode':
  294. worker._callbacks[ message.id ].resolve( message );
  295. break;
  296. case 'error':
  297. worker._callbacks[ message.id ].reject( message );
  298. break;
  299. default:
  300. console.error( 'THREE.Rhino3dmLoader: Unexpected message, "' + message.type + '"' );
  301. }
  302. };
  303. this.workerPool.push( worker );
  304. } else {
  305. this.workerPool.sort( function ( a, b ) {
  306. return a._taskLoad > b._taskLoad ? - 1 : 1;
  307. } );
  308. }
  309. var worker = this.workerPool[ this.workerPool.length - 1 ];
  310. worker._taskLoad += taskCost;
  311. return worker;
  312. } );
  313. },
  314. _releaseTask: function ( worker, taskID ) {
  315. worker._taskLoad -= worker._taskCosts[ taskID ];
  316. delete worker._callbacks[ taskID ];
  317. delete worker._taskCosts[ taskID ];
  318. },
  319. dispose: function () {
  320. for ( var i = 0; i < this.workerPool.length; ++ i ) {
  321. this.workerPool[ i ].terminate();
  322. }
  323. this.workerPool.length = 0;
  324. return this;
  325. }
  326. } );
  327. /* WEB WORKER */
  328. Rhino3dmLoader.Rhino3dmWorker = function () {
  329. var libraryPending;
  330. var libraryConfig;
  331. var rhino;
  332. onmessage = function ( e ) {
  333. var message = e.data;
  334. switch ( message.type ) {
  335. case 'init':
  336. libraryConfig = message.libraryConfig;
  337. var wasmBinary = libraryConfig.wasmBinary;
  338. var RhinoModule;
  339. libraryPending = new Promise( function ( resolve ) {
  340. /* Like Basis Loader */
  341. RhinoModule = { wasmBinary, onRuntimeInitialized: resolve };
  342. rhino3dm( RhinoModule );
  343. } ).then( () => {
  344. rhino = RhinoModule;
  345. });
  346. break;
  347. case 'decode':
  348. var buffer = message.buffer;
  349. libraryPending.then( () => {
  350. var data = decodeObjects( rhino, buffer );
  351. self.postMessage( { type: 'decode', id: message.id, data } );
  352. } );
  353. break;
  354. }
  355. };
  356. function decodeObjects( rhino, buffer ) {
  357. var arr = new Uint8Array( buffer );
  358. var doc = rhino.File3dm.fromByteArray( arr );
  359. var objects = [];
  360. var materials = [];
  361. var layers = [];
  362. var views = [];
  363. var namedViews = [];
  364. var groups = [];
  365. var strings = [];
  366. //Handle objects
  367. for ( var i = 0; i < doc.objects().count; i ++ ) {
  368. var _object = doc.objects().get( i );
  369. var object = extractObjectData( _object, doc );
  370. if ( object !== undefined ){
  371. objects.push( object );
  372. }
  373. _object.delete();
  374. }
  375. // Handle instance definitions
  376. for ( var i = 0; i < doc.instanceDefinitions().count(); i ++ ) {
  377. var idef = doc.instanceDefinitions().get( i );
  378. var idefAttributes = extractProperties( idef );
  379. idefAttributes.objectIds = idef.getObjectIds();
  380. objects.push( { geometry: null, attributes: idefAttributes, objectType: 'InstanceDefinition' } );
  381. }
  382. // Handle materials
  383. for ( var i = 0; i < doc.materials().count(); i ++) {
  384. var _material = doc.materials().get( i );
  385. var materialProperties = extractProperties( _material );
  386. var pbMaterialProperties = extractProperties( _material.physicallyBased() );
  387. var material = Object.assign(materialProperties, pbMaterialProperties);
  388. materials.push( material );
  389. _material.delete();
  390. }
  391. // Handle layers
  392. for ( var i = 0; i < doc.layers().count(); i ++) {
  393. var _layer = doc.layers().get( i );
  394. var layer = extractProperties( _layer );
  395. layers.push( layer );
  396. _layer.delete();
  397. }
  398. // Handle views
  399. for ( var i = 0; i < doc.views().count(); i ++) {
  400. var _view = doc.views().get( i );
  401. var view = extractProperties( _view );
  402. views.push( view );
  403. _view.delete();
  404. }
  405. // Handle named views
  406. for ( var i = 0; i < doc.namedViews().count(); i ++) {
  407. var _namedView = doc.namedViews().get( i );
  408. var namedView = extractProperties( _namedView );
  409. namedViews.push( namedView );
  410. _namedView.delete();
  411. }
  412. // Handle groups
  413. for ( var i = 0; i < doc.groups().count(); i ++ ){
  414. var _group = doc.groups().get( i );
  415. var group = extractProperties( _group );
  416. groups.push( group );
  417. _group.delete();
  418. }
  419. // Handle settings
  420. var settings = extractProperties( doc.settings() );
  421. //TODO: Handle other document stuff like dimstyles, instance definitions, bitmaps etc.
  422. // Handle dimstyles
  423. // console.log(`Dimstyle Count: ${doc.dimstyles().count()}`);
  424. // Handle bitmaps
  425. // console.log(`Bitmap Count: ${doc.bitmaps().count()}`);
  426. // Handle instance definitions
  427. // console.log(`Instance Definitions Count: ${doc.instanceDefinitions().count()}`);
  428. // Handle strings -- this seems to be broken at the moment in rhino3dm
  429. // console.log(`Strings Count: ${doc.strings().count()}`);
  430. /*
  431. for( var i = 0; i < doc.strings().count(); i++ ){
  432. var _string= doc.strings().get( i );
  433. console.log(_string);
  434. var string = extractProperties( _group );
  435. strings.push( string );
  436. _string.delete();
  437. }
  438. */
  439. doc.delete();
  440. return { objects, materials, layers, views, namedViews, groups, settings };
  441. }
  442. function extractObjectData( object, doc ) {
  443. var _geometry = object.geometry();
  444. var _attributes = object.attributes();
  445. var objectType = _geometry.objectType;
  446. var geometry = null;
  447. var attributes = null;
  448. // skip instance definition objects
  449. //if( _attributes.isInstanceDefinitionObject ) { continue; }
  450. // TODO: handle other geometry types
  451. switch ( objectType ) {
  452. case rhino.ObjectType.Curve:
  453. var pts = curveToPoints( _geometry, 100 );
  454. var position = {};
  455. var color = {};
  456. var attributes = {}
  457. var data = {};
  458. position.itemSize = 3;
  459. position.type = 'Float32Array';
  460. position.array = [];
  461. for( var j = 0; j < pts.length; j++ ) {
  462. position.array.push( pts[j][0] );
  463. position.array.push( pts[j][1] );
  464. position.array.push( pts[j][2] );
  465. }
  466. attributes.position = position;
  467. data.attributes = attributes;
  468. geometry = { data };
  469. break;
  470. case rhino.ObjectType.Point:
  471. var pt = _geometry.location;
  472. var position = {};
  473. var color = {};
  474. var attributes = {}
  475. var data = {};
  476. position.itemSize = 3;
  477. position.type = 'Float32Array';
  478. position.array = [ pt[0], pt[1], pt[2] ];
  479. _color = _attributes.drawColor( doc );
  480. color.itemSize = 3;
  481. color.type = 'Float32Array';
  482. color.array = [ _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 ];
  483. attributes.position = position;
  484. attributes.color = color;
  485. data.attributes = attributes;
  486. geometry = { data };
  487. break;
  488. case rhino.ObjectType.PointSet:
  489. case rhino.ObjectType.Mesh:
  490. geometry = _geometry.toThreejsJSON();
  491. break;
  492. case rhino.ObjectType.Brep:
  493. var faces = _geometry.faces();
  494. geometry = [];
  495. for (var faceIndex = 0; faceIndex < faces.count; faceIndex++) {
  496. var face = faces.get( faceIndex );
  497. var mesh = face.getMesh( rhino.MeshType.Any );
  498. if ( mesh ) {
  499. geometry.push( mesh.toThreejsJSON() );
  500. mesh.delete();
  501. }
  502. face.delete();
  503. }
  504. faces.delete();
  505. break;
  506. case rhino.ObjectType.Extrusion:
  507. var mesh = _geometry.getMesh(rhino.MeshType.Any);
  508. if( mesh ) {
  509. geometry = mesh.toThreejsJSON();
  510. mesh.delete();
  511. }
  512. break;
  513. case rhino.ObjectType.TextDot:
  514. geometry = extractProperties( _geometry );
  515. break;
  516. case rhino.ObjectType.InstanceReference:
  517. geometry = extractProperties( _geometry );
  518. geometry.xform = extractProperties( _geometry.xform );
  519. geometry.xform.array = _geometry.xform.toFloatArray( true );
  520. break;
  521. /*
  522. case rhino.ObjectType.Light:
  523. case rhino.ObjectType.Annotation:
  524. case rhino.ObjectType.Hatch:
  525. case rhino.ObjectType.SubD:
  526. case rhino.ObjectType.ClipPlane:
  527. */
  528. default:
  529. console.warn(`THREE.3DMLoader: TODO: Implement ${objectType.constructor.name}`);
  530. break;
  531. }
  532. if ( geometry ) {
  533. var attributes = extractProperties( _attributes );
  534. if ( _attributes.groupCount > 0 ) {
  535. attributes.groupIds = _attributes.getGroupList();
  536. }
  537. attributes.drawColor = _attributes.drawColor( doc );
  538. objectType = objectType.constructor.name;
  539. objectType = objectType.substring( 11, objectType.length );
  540. return { geometry, attributes, objectType };
  541. }
  542. }
  543. function extractProperties( object ) {
  544. var result = {};
  545. for ( var property in object ) {
  546. if( typeof object[ property ] !== 'function' ){
  547. result[ property ] = object[ property ];
  548. } else {
  549. // console.log(`${property}: ${object[property]}`);
  550. }
  551. }
  552. return result;
  553. }
  554. function curveToPoints ( curve, pointLimit ) {
  555. var pointCount = pointLimit;
  556. var rc = [];
  557. var ts = [];
  558. // console.log(curve);
  559. if ( curve instanceof rhino.LineCurve ) {
  560. return [ curve.pointAtStart, curve.pointAtEnd ];
  561. }
  562. if ( curve instanceof rhino.PolylineCurve ) {
  563. pointCount = curve.pointCount;
  564. for ( var i = 0; i < pointCount; i ++ ) {
  565. rc.push( curve.point( i )) ;
  566. }
  567. return rc;
  568. }
  569. if ( curve instanceof rhino.PolyCurve ) {
  570. var segmentCount = curve.segmentCount;
  571. for ( var i = 0; i < segmentCount; i ++ ) {
  572. var segment = curve.segmentCurve( i );
  573. var segmentArray = curveToPoints( segment );
  574. rc = rc.concat( segmentArray );
  575. segment.delete();
  576. }
  577. return rc;
  578. }
  579. if ( curve instanceof rhino.NurbsCurve && curve.degree === 1 ) {
  580. console.info( 'degree 1 curve' );
  581. }
  582. var domain = curve.domain;
  583. var divisions = pointCount - 1.0;
  584. for ( var j = 0; j < pointCount; j ++ ) {
  585. var t = domain[ 0 ] + ( j / divisions ) * ( domain[ 1 ] - domain[ 0 ] );
  586. if ( t === domain[ 0 ] || t === domain[ 1 ] ) {
  587. ts.push( t );
  588. continue;
  589. }
  590. var tan = curve.tangentAt( t );
  591. var prevTan = curve.tangentAt( ts.slice( -1 )[ 0 ]);
  592. // Duplicaated from THREE.Vector3
  593. // How to pass imports to worker?
  594. tS = tan[ 0 ] * tan[ 0 ] + tan[ 1 ] * tan[ 1 ] + tan[ 2 ] * tan[ 2 ];
  595. ptS = prevTan[ 0 ] * prevTan[ 0 ] + prevTan[ 1 ] * prevTan[ 1 ] + prevTan[ 2 ] * prevTan[ 2 ];
  596. var denominator = Math.sqrt( tS * ptS );
  597. var angle;
  598. if( denominator === 0 ) {
  599. angle = Math.PI / 2;
  600. } else {
  601. var theta = ( tan.x * prevTan.x + tan.y * prevTan.y + tan.z * prevTan.z ) / denominator;
  602. angle = Math.acos( Math.max( - 1, Math.min( 1, theta ) ) );
  603. }
  604. if ( angle < 0.1 ) { continue; }
  605. ts.push( t );
  606. }
  607. rc = ts.map( t => curve.pointAt( t ) );
  608. return rc;
  609. }
  610. };
  611. export { Rhino3dmLoader };