如何讀取和解析一個大的STL文件?我編寫了一個應用程序來查看3D STL文件。 3D STL文件可以是二進制或ACSII。我需要從SD卡讀取STL文件,然後解析數據以使用OpenGL進行渲染。但是,當3D STL很大時,我有「內存不足」。如何解決這個問題。我使用兩個數組列表來存儲方面的形式和頂點。如何讀取和解析一個大的STL文件?
下面的代碼實現「閱讀和分析二進制STL文件」:
private void processBinarySTL(){
Thread processThread =new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
File file=new File("/mnt/sdcard/3D/Lost_Pleiade_Ready.stl");
InputStream inputStream = null;
try {
inputStream = mContext.getContentResolver().openInputStream(Uri.fromFile(file));
int n = 0;
byte[] buffer = new byte[4];
inputStream.skip(80);
n = inputStream.read(buffer);
System.out.println("n=="+n);
int size=getIntWithLittleEndian(buffer,0);
System.out.println("size=="+size);
List<Float> normalList=new ArrayList<Float>();
List<Float> vertexList = new ArrayList<Float>();
for (int i = 0; i < size; i++) {
//normal
for(int k=0;k<3;k++){
inputStream.read(buffer);
normalList.add(Float.intBitsToFloat(getIntWithLittleEndian(buffer, 0)));
}
for(int j=0;j<3;j++){
inputStream.read(buffer);
float x = Float.intBitsToFloat(getIntWithLittleEndian(buffer, 0));
inputStream.read(buffer);
float y = Float.intBitsToFloat(getIntWithLittleEndian(buffer, 0));
inputStream.read(buffer);
float z = Float.intBitsToFloat(getIntWithLittleEndian(buffer, 0));
adjustMaxMin(x, y, z);
vertexList.add(x);
vertexList.add(y);
vertexList.add(z);
}
inputStream.skip(2);
}
System.out.println("normalList size== "+normalList.size());
System.out.println("vertexList size== "+vertexList.size());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null)
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
processThread.start();
}
是你尋找http://code.j3d.org/javadoc/org/j3d/loaders/stl/STLFileReader.html –
@KarthikT謝謝您的答覆。我不知道這個資源。我會研究它。 – Judy
@KarthikT我看看j3d。但這不是我的解決方案。我需要在Android應用程序中實現「查看3D STL文件」。 – Judy