c – Are .STL files missing triangles? This STL parsing code is consistently missing triangles

This is my second project that parses binary STL files. I’m running into the same problem I experienced several years ago in which the representations of my STL file ends up missing several triangles. I’ve tried reading more than the number of triangles indicated in the STL header to see if there are more triangles than indicated but I end up with the same missing triangles.

I’m confident this is an issue with the STL or the way I am reading it and not my visualization because I’ve encountered an identical issue before in a project that was essentially just the reading of an STL using code almost identical to the C code below.

Gomboc STL missing triangles after being run through my slicing project code

Low-poly Cat STL missing triangles after being run through my slicing project. Seen from the inside for clarity

struct tri* readSTL(char* filename, int* numTriangles) {
    FILE* fp;
    char wordIn = '\0'; 

    fp = fopen(filename, "r");

    for (int i = 0; i < 80; i++) {
        fread(&wordIn, sizeof(char), 1, fp);

    fread(numTriangles, sizeof(char), 4, fp);

    struct tri* triangles = calloc(*numTriangles, sizeof(struct tri));

    for (int i = 0; i < *numTriangles; i++) {

      fread(&(triangles[i].normal.X), sizeof(char), 4, fp);
      fread(&(triangles[i].normal.Y), sizeof(char), 4, fp);
      fread(&(triangles[i].normal.Z), sizeof(char), 4, fp);

      //READ X
      fread(&(triangles[i].p1.X), sizeof(char), 4, fp);
      fread(&(triangles[i].p1.Y), sizeof(char), 4, fp);
      fread(&(triangles[i].p1.Z), sizeof(char), 4, fp);

      //READ Y
      fread(&(triangles[i].p2.X), sizeof(char), 4, fp);
      fread(&(triangles[i].p2.Y), sizeof(char), 4, fp);
      fread(&(triangles[i].p2.Z), sizeof(char), 4, fp);

      //READ Z
      fread(&(triangles[i].p3.X), sizeof(char), 4, fp);
      fread(&(triangles[i].p3.Y), sizeof(char), 4, fp);
      fread(&(triangles[i].p3.Z), sizeof(char), 4, fp);

      int throwaway;
      fread(&throwaway, sizeof(char), 2, fp);
      printf("FILE NORMAL: %f, %f, %f\n", triangles[i].normal.X,
              triangles[i].normal.Y, triangles[i].normal.Z);

      struct point Normal = computeNormal(triangles[i]);
      printf("Computed NORMAL: %f, %f, %f\n\n", Normal.X, Normal.Y, Normal.Z);


    return triangles;

Is there something I’m missing about the STL data structure that would account for this? This issue seems to come up in any STL I read above ~100 triangles. Parsing a 6 sided cube composed of 12 triangles looks perfect.

EDIT 1 This code is currently only set up to read BINARY STL files and that is all that is being given to it to read.

EDIT 2 My leading theory right now is that missing Triangles is standard in most softwares exporting to STL and if a triangle is defined by the triangles around it then it is not added. The odd thing is how these unspecified triangles are decided as they do not have a visual or symmetric pattern on objects, nor do missing Triangles appear in every spot where they could be reasonably inferred from the surrounding triangles.

Read more here: Source link