如何读取和解析一个大的 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();
}
最佳答案
在这种情况下解决内存不足问题的最简单方法是增加 JVM 的最大内存分配。这可以使用 -Xmx
命令行开关进行设置,并指定更大的最大内存使用量。
例如。
java Main -Xmx 2G
将程序 Main 的最大内存使用量设置为 2 GB。默认的最大内存分配是物理内存的 1/4。
参见:http://javarevisited.blogspot.sg/2011/11/hotspot-jvm-options-java-examples.html
关于java - 如何读取和解析一个大的 STL 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17334031/