title: Improvement of my code that reads STL files (c++)
body: <p>in order to create a 3D simulation with DirectX I have to import huge datasets in ASCII-STL format. The thing is that my reader is working but the performance is terrible. I made a comparision with the Autodesk Inventor STL importer and the different is significant (5sec vs 1.5min). I'd highly appreciate suggestions or ideas to improve my code. Thank you!</p>
  
  <p>Furthermore, the STL format is very inefficient. For each face the vertices are listed seperately. It looks like the follwing:</p>
  
  <p>facet normal 0 0.999229 0.0392606</p>
  
  <p>outer loop</p>
  
  <p>vertex -3173.54 1993.84 -23184.5</p>
  
  <p>vertex -3099.94 1993.84 -23184.5</p>
  
  <p>vertex -3099.94 2000 -23341.5</p>
  
  <p>endloop</p>
  
  <p>endfacet</p>
  
  <p>The result is that vertices occur several times during the processing of the file. I tried to check for double vertices but it takes forever for huge files (iterations become longer and longer).</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>    std::ifstream stlFile;
  stlFile.open(mFilename);
  if(!stlFile) // check if file can be found
  {
      MessageBox(0, "STL file not found.", 0, 0);
      return false;
  }
  
  std::string ignore;
  stlFile &gt;&gt; ignore &gt;&gt; ignore; //ignore header: solid t=x.xx
  UINT index = 0;
  
  int iIndex = 0;
  int vIndex = 0;
  WORD indexTmp = 0;
  
  while(1)
  {
      stlFile &gt;&gt; ignore &gt;&gt; ignore; // ignore "normal"
      if (ignore == "CUBE")
          break;
  
      float normal[3];
  
      stlFile &gt;&gt; normal[0] &gt;&gt; normal[1] &gt;&gt; normal[2]; // read and save the face normal
      stlFile &gt;&gt; ignore &gt;&gt; ignore; // ignore "outer loop"
      for(int i = 0; i &lt;= 2; ++i) // read the three vertices of a face
      {
          VERTEX vertexTmp;
          vertexTmp.Normal.x = normal[0]; vertexTmp.Normal.y = normal[1]; vertexTmp.Normal.z = normal[2];
          stlFile &gt;&gt; ignore &gt;&gt; vertexTmp.Pos.x &gt;&gt; vertexTmp.Pos.y &gt;&gt; vertexTmp.Pos.z;// &gt;&gt; ignore &gt;&gt; ignore;  
  
          //if (!ContainsVertexIndex(vertexTmp, vertices, indexTmp)) // return vertex index of double 
          //{
              mVertices.push_back(vertexTmp); //save newly created vertex     
  
              indexTmp = vIndex; // create index reference to the new vertex
              vIndex++; // increment index
          //}
          mIndices.push_back(indexTmp);
          iIndex++; // increment index
      }
      stlFile &gt;&gt; ignore &gt;&gt; ignore; // endloop // endfacet
  }
  
  stlFile.close();
  </code></pre>
  
  <p>Edit: I changed the vectors to fixed arrays but the performance does not increase significantly. Any other suggestions.</p>