FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Description
00025 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #include <triSurface/triSurface.H>
00029 #include "STLtriangle.H"
00030 #include <OpenFOAM/IFstream.H>
00031 #include <OpenFOAM/OSspecific.H>
00032 #include <OpenFOAM/gzstream.h>
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00040 
00041 bool triSurface::readSTLBINARY(const fileName& STLfileName)
00042 {
00043     bool compressed = false;
00044 
00045     autoPtr<istream> STLfilePtr
00046     (
00047         new ifstream(STLfileName.c_str(), std::ios::binary)
00048     );
00049 
00050     // If the file is compressed, decompress it before reading.
00051     if (!STLfilePtr->good() && isFile(STLfileName + ".gz", false))
00052     {
00053         compressed = true;
00054         STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
00055     }
00056     istream& STLfile = STLfilePtr();
00057 
00058     if (!STLfile.good())
00059     {
00060         FatalErrorIn("triSurface::readSTLBINARY(const fileName&)")
00061             << "Cannot read file " << STLfileName
00062             << " or file " << STLfileName + ".gz"
00063             << exit(FatalError);
00064     }
00065 
00066     // Read the STL header
00067     char header[STLheaderSize];
00068     STLfile.read(header, STLheaderSize);
00069 
00070     // Check that stream is OK, if not this maybe an ASCII file
00071     if (!STLfile)
00072     {
00073         return false;
00074     }
00075 
00076     // Read the number of triangles in the STl file
00077     // (note: read as int so we can check whether >2^31)
00078     int nTris;
00079     STLfile.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
00080 
00081     // Check that stream is OK and number of triangles is positive,
00082     // if not this maybe an ASCII file
00083     if (!STLfile || nTris < 0)
00084     {
00085         return false;
00086     }
00087 
00088     // Compare the size of the file with that expected from the number of tris
00089     // If the comparison is not sensible then it maybe an ASCII file
00090     if (!compressed)
00091     {
00092         label dataFileSize = Foam::fileSize(STLfileName) - 80;
00093 
00094         if (nTris < dataFileSize/50 || nTris > dataFileSize/25)
00095         {
00096             return false;
00097         }
00098     }
00099 
00100     // Everything OK so go ahead and read the triangles.
00101 
00102     // Allocate storage for raw points
00103     pointField rawPoints(3*nTris);
00104 
00105     // Allocate storage for triangles
00106     setSize(nTris);
00107 
00108     label rawPointI = 0;
00109 
00110     // Read the triangles
00111     forAll(*this, i)
00112     {
00113         // Read an STL triangle
00114         STLtriangle stlTri(STLfile);
00115 
00116         // Set the rawPoints to the vertices of the STL triangle
00117         // and set the point labels of the labelledTri
00118         rawPoints[rawPointI] = stlTri.a();
00119         operator[](i)[0] = rawPointI++;
00120 
00121         rawPoints[rawPointI] = stlTri.b();
00122         operator[](i)[1] = rawPointI++;
00123 
00124         rawPoints[rawPointI] = stlTri.c();
00125         operator[](i)[2] = rawPointI++;
00126 
00127         operator[](i).region() = stlTri.region();
00128     }
00129 
00130     //STLfile.close();
00131 
00132     stitchTriangles(rawPoints);
00133 
00134     return true;
00135 }
00136 
00137 
00138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00139 
00140 } // End namespace Foam
00141 
00142 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines