简体   繁体   English

如何在Java,Android中初始化巨大的float数组?

[英]How to initialize huge float arrays in java, android?

I was creating a opengl android application. 我正在创建一个opengl android应用程序。 I was trying to render a opengl object with vertices more than 50,000. 我正在尝试渲染一个顶点超过50,000的opengl对象。

float itemVerts [] = {
// f 231/242/231 132/142/132 131/141/131
0.172233487787643f, -0.0717437751698985f, 0.228589675538813f,
0.176742968653347f, -0.0680393472738536f, 0.2284149434494f,
0.167979223684599f, -0.0670168837233226f, 0.24286384937854f,
// f 131/141/131 230/240/230 231/242/231
0.167979223684599f, -0.0670168837233226f, 0.24286384937854f,
0.166391290343292f, -0.0686544011752973f, 0.241920432968569f,......

and many more.... But when i do this in a function or constructor i get a error while compiling that The code of method () is exceeding the 65535 bytes limit. 还有更多....但是当我在函数或构造函数中执行此操作时,在编译方法()的代码超过65535字节限制时遇到错误。 So I was wondering if there is a different way to do this. 所以我想知道是否有其他方法可以做到这一点。

I tried storing the value in file and reading it back. 我尝试将值存储在文件中并读回。 But the IO operation, with string parsing of such huge record is very slow. 但是具有如此大的记录的字符串解析的IO操作非常慢。 Takes more than 60 sec. 耗时超过60秒。 Which is not good. 哪个不好

Please let me know if there is any other way to do this. 请让我知道是否还有其他方法可以做到这一点。 Thank you for your time and help. 感谢您的时间和帮助。

But when i do this in a function or constructor i get a error while compiling that The code of method () is exceeding the 65535 bytes limit. 但是当我在函数或构造函数中执行此操作时,在编译方法()的代码超过65535字节限制时收到错误消息。 So I was wondering if there is a different way to do this. 所以我想知道是否有其他方法可以做到这一点。

Put it outside the constructor (as a class variable or field)? 将其放在构造函数之外(作为类变量或字段)? If this doesn't change, just make it a constant. 如果这没有改变,请将其设为常数。 If it does change, make it a constant anyway and copy it in the constructor. 如果确实发生更改,则无论如何都要使其保持不变并将其复制到构造函数中。

I tried storing the value in file and reading it back. 我尝试将值存储在文件中并读回。 But the IO operation, with string parsing of such huge record is very slow. 但是具有如此大的记录的字符串解析的IO操作非常慢。 Takes more than 60 sec. 耗时超过60秒。 Which is not good. 哪个不好

If you do decide to keep it in an external file and read it in, don't read it as a string, just serialize it somehow (Java serialization, Protocol Buffers, etc.). 如果您决定将其保存在外部文件中并读入,请不要将其读取为字符串,而应以某种方式对其进行序列化(Java序列化,协议缓冲区等)。

The program don't have to parse the float if we preprocess the data. 如果我们预处理数据,则程序不必解析浮点数。

Write another program that write all float to a binary file using DataOutputStream . 编写另一个程序,使用DataOutputStream将所有float写入二进制文件。

In your program, read them back using DataInputStream . 在程序中,使用DataInputStream读回它们。 You might want to chain it with BufferedInputStream . 您可能希望将其与BufferedInputStream

对于这种情况,我通常使用assets文件夹以二进制格式存储文件(即使您可以定义某种文件格式以包括顶点,法线等),也可以在应用程序初始化时将其分配,如wannik解释的那样。

I would proprocess and store floats in binary form, then mmap it as byte buffer and create fload array out of it. 我将以二进制形式处理和存储浮点数,然后将其映射为字节缓冲区并从中创建fload数组。 This way you get float array, without parsing or allocation of space. 这样,您就可以获取浮点数组,而无需解析或分配空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM