通过平面缓冲区有效访问和遍历非基本数组吗?

我正在使用flatbuffers通过网络将点云数据传输到Unity中内置的应用程序。

说我有以下架构文件:

namespace PCFrame;

struct Vec3 {
    x: float;
    y: float;
    z: float;
}

struct RGB {
    R: ubyte;
    G: ubyte;
    B: ubyte;
}

struct PCPoint {
    Position: Vec3;
    Color: RGB;
}

table PCFrame {
    FrameNo: uint;
    Points: [PCPoint];
}

file_identifier "FBPC";

root_type PCFrame;

我已经成功地序列化并传输了缓冲区。但是在收到C#中的ByteBuffer并将其反序列化为PCFrame对象之后,我访问Points数组中的点的唯一方法是调用方法Points(n) 。这是极其低效的,尤其是当数组可以包含成千上万个点并且我需要遍历所有点时。

有什么方法可以更有效地访问阵列?

编辑: 我最终放弃了更清晰的PCPoint结构,而是将点数据创建为一个浮点数组(排列为x1,y1,z1,r1,g1,b1,x2,y2 ...)。因此,现在更简单的架构如下所示:

namespace PCFrame;

table PCFrame 
{
    FrameNo: uint;
    Points: [float];  // x1,y1,z1,r1,g1,b1,x2,y2,z2,r2,g2,b2,x3,...
}

file_identifier "FBPC";

root_type PCFrame;

使用基本类型的数组可以直接使用以下命令提取字节数组:

ArraySegment<byte> pointsArraySegment = fb_frame.GetPointsBytes();
byte[] pointsByteArray = pointsArraySegment.Value.Array;

依次允许我进行Buffer.Block将整个数组立即复制回float数组:

int offset = pointsArraySegment.Value.Offset;
float[] pointsArray = new float[(pointsByteArray.Length - offset) / sizeof(double)];
Buffer.BlockCopy(pointsByteArray,offset,pointsArray,pointsByteArray.Length - offset);

以这种方式从float数组访问点数据要高效得多。 我仍然不确定为什么flatBuffers不允许您在非基本数组上执行此操作。

iCMS 回答:通过平面缓冲区有效访问和遍历非基本数组吗?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2261438.html

大家都在问