简体   繁体   中英

Two vectors with same data but different types

I'm working on a flow visualization task where I need to analyze the data in some way. The visualization iswritten by someone else and expects a vector of GLFloat containing the data. However, I would much prefer to have a class structure like below. Is there any way to achieve this without copying the data (like a union)?

struct Vertex
{
    math::vec3 pos;
    float time;
    float velocity;
};

class Pathline
{
    std::vector<Vertex> points;
};

//these have the same data
std::vector<Pathline> lines;
std::vector<GLfloat> lineData;

Thanks

From your example, several issues come to my mind:

  • GLfloat is not required to be compatible with float.
  • You do not know how many Pathlines you have, so you could have 3 pathlines of 2 vertex, or 6 pathlines of 1 vertex, or any other combination.
  • std::vector do not ensure a number multiple of 5 GLfloat, so your conversion relay on this semantic constraint.
  • Class/structs are usually aligned by the compiler, that mean your Vertex could be more than expected.

Probably you could reword your structure to something like:

struct Vertex
{
    GLfloat[3] pos;
    GLfloat time;
    GLFloat velocity;
};

std::vector<Vertex> lineVertexData;
std::vector<GLfloat> lineData;

And if you have additional external data to define the pathlines, you could go one step further.

All depends on how far you want to do"philosophically correct" C++ code. Some times difficult to achieve with OpenGL.

As soon as your 2 structures are equivalent, you may cast your pointer/reference between. But this equivalence is not quite easy.

Answering your question:

  • Is it possible? no with your current code.
  • Is it possible with some changes? yes, even if not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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