views:

69

answers:

3

I have a file format (.STL, stereo lithography, the structure is standard and can not be changed. Not to be confused with standard template library) that uses a 50 byte data structure. Reading from the HD directly leads to wrong data being read as 50 bytes is not a multiple of 4.

Out of the entire 50 byte structure, I only need 36 of those bytes. The method I am using right now to extract the data is to manually offset the read position of the file to the point where the next set of data begins, read off the 36 bytes into a temporary variable and then dump the data in the temporary variable into it's proper position on the array.

Here's the code chunk:

threePoints* output = new threePoints [numTriangles]; // create an array to hold the entire file
threePoints* temp = new threePoints [1]; // temp variable to pull data out of each "cell"

// extract each triangle individualy
for (int i = 0; i < numTriangles; i++)
{
    stlFile.seekg (96 + i * 50, ios::beg);  //read vertex data and put them into tempoary array
                                            // offset = 80 header + 4 #triangles + 12 normal vector
    stlFile.read(reinterpret_cast<char*>(temp), (36)); // read the next 36 data blocks into holder, 3 points * 3 axis * 4 bytes per float
    output[i] = temp[0]; // dump values in holder into proper array
}

This method works but is slow. How would I go about doing this to make it faster and more efficiently?

Edit: I know manually disabling the byte alignment would solve this problem but I need to use the resulting array intensively and iterate over it many times. I've been told that disabling byte alignment can result in performance issues, thus i avoided it.

+6  A: 

Instead of doing a bunch of small seeks & reads, allocate a large buffer (say, enough for 1000 structures) and read a bunch of records at once. (Then iterate through that buffer copying out the 36-byte chunks you need).

David Gelhar
Good answer. The problem isn't misalignment, the problem is doing a bunch of redundant read requests.
Mark Ransom
How would I go about reading a chunk of data directly from HD into memory and then deleting the unnecessary part? I guess this question is really not a matter if it is possible, more of how do I do it since I don't know.
Faken
Read into a `char` buffer. The location of the N'th record in the buffer is then buf+(50*N), so you can say `output[i] = *(reinterpret_cast<threePoints*>(buf+(50*N));`, if your struct matches the alignment on disk. If (as in other answers) you don't want to constrain your alignment to match the disk file format, declare `packed` and `unpacked` versions of your struct and copy from one to the other. (Isolating your in-memory representation from the disk format is a good idea, because you're more flexible to differences in byte order etc. if you ever port to a different architechure)
David Gelhar
A: 

Bracket the struct definition with #pragma pack(1,push) and #pragma pack(pop). This is supported by most compilers. Then no automatic padding should be added.

The GCC manual claims it's a Microsoft compatibility feature, but it's been around forever… probably longer than GCC itself.

Potatoswatter
I'm concerned about performance upon iterating through the array of structure later. I use this array intensively and speed is important. I was told last time that disabling byte alignment can seriously hurt performance.
Faken
@Faken: On the other hand, expanding the total size of the array, or even just copying it once can also hurt performance. The misalignment penalty may also be very small. I recommend trying it both ways.
Potatoswatter
A: 

I don't have STL data to test with but how about something like this?...

#include <fstream>

#pragma pack(push, 1)

struct stl_hdr {
   unsigned int header[80];
   unsigned int triangle_cnt;
};

struct triangle {
   float norm;
   float v1;
   float v2;
   float v3;
   short attr_cnt;
};

#pragma pack(pop)

using namespace std;

int main(int argc, char* argv[]) {
   ifstream file(argv[1], ios::in | ios::binary);

   if (file.is_open()) {
      stl_hdr hdr;
      file.read((char*)&hdr, sizeof(hdr));

      triangle* tris = new triangle[hdr.triangle_cnt];

      file.read((char*)tris, hdr.triangle_cnt * sizeof(triangle));

      file.close();
   }

   return 0;
}

I created two structs based on Wikipedia's description of the STL format. The code above makes the same assumption that your code did...the attribute count is zero. And I left out error handling / cleanup.

dgnorton