1

in my CAD-like application I store vector coordinates in arrays of following structures:

struct coord
{
   double        x,y,z;
   unsigned char flags;
};

x, y and z hold the 3D coordinates, flags contains additional information (e.g. if a polygon ends here and line to next coordinate in this array has not to be drawn).

Now these data have to be converted to a plain float-array with one float value for x, y and z each and with as many of these triples as there are points in the polygon to be drawn. Reason is: the converted array of coordinates has to be handed over to OpenGL function glVertexAttribPointer() to let OpenGL draw this polygon.

My question: what is the fastest way of converting these data? My only idea is to allocate the target array and to go through it in a loop to cast the coordinate values down to float - which sounds quite slow for me.

Any other ideas?

Thanks! *)

*) I know stackoverflow does not like to be friendly but in fact I like it to be friendly that's why I want to thank all people that try to give an answer here! there are enough unfriendly places on this earth, stackoverflow should not be an other one!

Useless
  • 64,155
  • 6
  • 88
  • 132
Elmi
  • 5,899
  • 15
  • 72
  • 143
  • Write a wrapper class which holds the coordinates x,y,z in an array/vector already, plus a separate, corresponding array of `flags` of the same length. Just hand out the internal array (or vector.data()) to the OpenGL function (instead of copying large amounts of data around), much like `string::c_str()`. – Peter - Reinstate Monica Oct 02 '15 at 11:53
  • Oh, and I think it is possible (i.e. legal) to just convert a pointer to an array of `struct { double x,y,z; }` to a pointer to double. Perhaps you may want to contact a language lawyer about it -- I remember that the standard language was a bit opaque to me. With gcc, -fno-strict-aliasing should solve possible issues. – Peter - Reinstate Monica Oct 02 '15 at 11:59
  • @PeterSchneider No that is not legal. See [What is the strict aliasing rule?](http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) – NathanOliver Oct 02 '15 at 12:16
  • 2
    @elmi I rolled back your edit as it adds nothing to your question. Please stop adding it back – NathanOliver Oct 02 '15 at 12:18
  • @NathanOliver: so being friendly is not wanted here? that's more than embarassing... – Elmi Oct 02 '15 at 12:25
  • 1
    @Elmi The official take is that "Thanks" is useless noise. You feel that it is friendly, but regulars feel like it wastes their time. You can only read through so many "Dear sirs, may I kindly ask..." before you get sick of it and mentally block it out. Removing it from the site makes is a lot friendlier to people. – nwp Oct 02 '15 at 12:27
  • @NathanOliver I think the "Thanks" in the end stole much less of my time (in fact: none) than your anal editing. – Peter - Reinstate Monica Oct 02 '15 at 12:54
  • @PeterSchneider how did my edited out noise waste any of your time? as far as my "anal" edited I am just trying to keep question about questions. platitudes are not needed. that is what up voting and accepting answers is for. – NathanOliver Oct 02 '15 at 13:01
  • If you need doubles during computations for exactness I cannot imagine how you could get around conversions. There just *may* be something possible with SIMD on your CCU (SSE2 has conversions for 2 doubles). Perhaps your graphics card can help too, e.g. with CUDA, but I can imagine that transfering the data back and forth is slower than just doing it by CPU. – Peter - Reinstate Monica Oct 02 '15 at 13:08
  • NathanOliver: when frendliness is a platitude there is something very basic wrong with our world – Elmi Oct 02 '15 at 13:10
  • There's a fine line between courtesy and friendliness - which are certainly not a problem - and padding a question out with unrelated smalltalk and what [esr memorably described as "crude primate politics"](http://www.catb.org/esr/faqs/smart-questions.html#idp59363184). I think this one's fine, and the argument about "friendliness" is more distracting than the original thanks. It is subjective though, and there's nothing unfriendly about just asking the question - you can consider the lack of superfluous social niceties as a display of respect for others' time. – Useless Oct 02 '15 at 13:31

1 Answers1

1

Well, if you can cope with using float instead of double in the first place, you can keep your coordinates in this flat array in the first place:

struct coord { float x,y,x; };
struct point { coord *c; // pointer into global array of coord
               unsigned char flags; };

This causes problems if you need to remove or re-order points though - you need a reverse lookup to rewrite the coord pointer of every affected point.

It also introduces an extra indirection, so may speed up rendering at the expense of other operations.

Finally, it's entirely possible that float really isn't precise enough for your primary use, in which case you'll have to perform the double->float conversion at some point. The only way to avoid doing it just before the GL call is to keep both versions of each coordinate updated all the time (or the hybrid approach of keeping a list of dirty points whose GL coordinates need rewriting).


NB. I forgot to mention - you may need to explicitly mark that coord struct as packed if you want to use it, depending on your padding and alignment rules. You can always just store a flat array of float and point to the first (x) element, though.

Useless
  • 64,155
  • 6
  • 88
  • 132