假设我有以下简单的结构:
struct Vector3
{
double x;
double y;
double z;
};
我创建了一个顶点列表:
std::vector<Vector3> verticesList;
除此之外,我还需要使用第三方库。该库具有具有以下签名的函数:
typedef double[3] Real3;
external void createMesh(const Real3* vertices, const size_t verticesCount);
转换verticesList
成可以createMesh()
作为vertices
参数传递的东西的最佳方法是什么?
目前我使用以下方法:
static const size_t MAX_VERTICES = 1024;
if (verticesList.size() > MAX_VERTICES)
throw std::exception("Number of vertices is too big");
Real3 rawVertices[MAX_VERTICES];
for (size_t vertexInd = 0; vertexInd < verticesList.size(); ++vertexInd)
{
const Vector3& vertex = verticesList[vertexInd];
rawVertices[vertexInd][0] = vertex.x;
rawVertices[vertexInd][1] = vertex.y;
rawVertices[vertexInd][2] = vertex.z;
}
createMesh(rawVertices, verticesList.size());
但肯定不是解决问题的最佳方法。