如何读取和解析一个大的 STL 文件?我编写了一个应用程序来查看 3D STL 文件。3D STL 文件可以是二进制文件或 ACSII。我需要从 sdcard 读取 STL 文件,然后解析数据以使用 OpenGL 进行渲染。但是当 3D 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();
}