OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

readSTLBINARY.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "triSurface.H"
00030 #include "STLtriangle.H"
00031 #include "IFstream.H"
00032 #include "OSspecific.H"
00033 #include "gzstream.h"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039 
00040 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00041 
00042 bool triSurface::readSTLBINARY(const fileName& STLfileName)
00043 {
00044     bool compressed = false;
00045 
00046     autoPtr<istream> STLfilePtr
00047     (
00048         new ifstream(STLfileName.c_str(), std::ios::binary)
00049     );
00050 
00051     // If the file is compressed, decompress it before reading.
00052     if (!STLfilePtr->good() && isFile(STLfileName + ".gz", false))
00053     {
00054         compressed = true;
00055         STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
00056     }
00057     istream& STLfile = STLfilePtr();
00058 
00059     if (!STLfile.good())
00060     {
00061         FatalErrorIn("triSurface::readSTLBINARY(const fileName&)")
00062             << "Cannot read file " << STLfileName
00063             << " or file " << STLfileName + ".gz"
00064             << exit(FatalError);
00065     }
00066 
00067     // Read the STL header
00068     char header[STLheaderSize];
00069     STLfile.read(header, STLheaderSize);
00070 
00071     // Check that stream is OK, if not this maybe an ASCII file
00072     if (!STLfile)
00073     {
00074         return false;
00075     }
00076 
00077     // Read the number of triangles in the STl file
00078     // (note: read as int so we can check whether >2^31)
00079     int nTris;
00080     STLfile.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
00081 
00082     // Check that stream is OK and number of triangles is positive,
00083     // if not this maybe an ASCII file
00084     if (!STLfile || nTris < 0)
00085     {
00086         return false;
00087     }
00088 
00089     // Compare the size of the file with that expected from the number of tris
00090     // If the comparison is not sensible then it maybe an ASCII file
00091     if (!compressed)
00092     {
00093         label dataFileSize = Foam::fileSize(STLfileName) - 80;
00094 
00095         if (nTris < dataFileSize/50 || nTris > dataFileSize/25)
00096         {
00097             return false;
00098         }
00099     }
00100 
00101     // Everything OK so go ahead and read the triangles.
00102 
00103     // Allocate storage for raw points
00104     pointField rawPoints(3*nTris);
00105 
00106     // Allocate storage for triangles
00107     setSize(nTris);
00108 
00109     label rawPointI = 0;
00110 
00111     // Read the triangles
00112     forAll(*this, i)
00113     {
00114         // Read an STL triangle
00115         STLtriangle stlTri(STLfile);
00116 
00117         // Set the rawPoints to the vertices of the STL triangle
00118         // and set the point labels of the labelledTri
00119         rawPoints[rawPointI] = stlTri.a();
00120         operator[](i)[0] = rawPointI++;
00121 
00122         rawPoints[rawPointI] = stlTri.b();
00123         operator[](i)[1] = rawPointI++;
00124 
00125         rawPoints[rawPointI] = stlTri.c();
00126         operator[](i)[2] = rawPointI++;
00127 
00128         operator[](i).region() = stlTri.region();
00129     }
00130 
00131     //STLfile.close();
00132 
00133     stitchTriangles(rawPoints);
00134 
00135     return true;
00136 }
00137 
00138 
00139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00140 
00141 } // End namespace Foam
00142 
00143 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd