Heartsky / jsc3d

Automatically exported from code.google.com/p/jsc3d
0 stars 0 forks source link

Unable to load STL files locally and asynchronously #43

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
 -Hi! I'm working on an application that involves previewing and uploading STL files. I've been using the HTML5 File api to read in the file and then send the data to stlLoader's parseStl function. However this only works if the files are small. Because it's asychronous, if the file is too large mesh = scene.getChildren() will fail. 

What is the expected output? What do you see instead?
Loading fails because the scene.children array is empty. 

What version of the product are you using? On what operating system?
1.4.2, on ubuntu. 

Please provide any additional information below.

I've been looking at the pattern shown here: 
https://code.google.com/p/jsc3d/issues/detail?id=36

However, it seems that the problem is that because I'm using parseStl on a 
local FileReader object, the onload function isn't being triggered. I tried 
adding a callback to the parseStl function like so:

var stl_loader = new JSC3D.StlLoader()
on_model_load = function(scene) {
    var mesh = scene.getChildren()
    theScene.addChild(mesh[0])
    viewer.init()
    viewer.replaceScene(theScene)
    viewer.update()
}
reader.onload = function(event) {
    var scene = new JSC3D.Scene
    stl_loader.parseStl(scene, event.target.result, on_model_load)
    var mesh = scene.getChildren()
}

JSC3D.StlLoader.prototype.parseStl = function(scene, data, callback) {
  .
  .
  .
   if(!mesh.isTrivial()) {
    if( Math.abs(mesh.faceNormalBuffer[0]) < 1e-6 &&
            Math.abs(mesh.faceNormalBuffer[1]) < 1e-6 &&
        Math.abs(mesh.faceNormalBuffer[2]) < 1e-6 ) {
        mesh.faceNormalBuffer = null;
        }
        scene.addChild(mesh);
        callback(scene) // my addition.
    }
}

This had no effect on the problem: small trigger the callback and load 
correctly, large files do not trigger the callback and don't load. 

Original issue reported on code.google.com by liav.ko...@gmail.com on 1 Oct 2013 at 8:38

GoogleCodeExporter commented 9 years ago
Hmm. I just stepped through the parseStl() function to try to understand why 
the onload callback I'd added wasn't firing for large files. The function is 
returning at the if(reader.size() < expectedLen) test. I'm not sure yet why the 
files aren't being completely read into memory. They are hitting the server in 
complete form. 

Original comment by liav.ko...@gmail.com on 1 Oct 2013 at 9:18

GoogleCodeExporter commented 9 years ago
Okay, I figured it out. I was using the FileReader.readAsText() function to 
read in the data. Should have been using FileReader.readAsBinaryString(). It 
works now!! :) 

Original comment by liav.ko...@gmail.com on 1 Oct 2013 at 11:29

GoogleCodeExporter commented 9 years ago
I think you get it. FileReader.prototype.readAsText() does not produce correct 
result for binary STL files.

Original comment by Humu2...@gmail.com on 2 Oct 2013 at 3:51