readSTLBINARY.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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
00068 char header[STLheaderSize];
00069 STLfile.read(header, STLheaderSize);
00070
00071
00072 if (!STLfile)
00073 {
00074 return false;
00075 }
00076
00077
00078
00079 int nTris;
00080 STLfile.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
00081
00082
00083
00084 if (!STLfile || nTris < 0)
00085 {
00086 return false;
00087 }
00088
00089
00090
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
00102
00103
00104 pointField rawPoints(3*nTris);
00105
00106
00107 setSize(nTris);
00108
00109 label rawPointI = 0;
00110
00111
00112 forAll(*this, i)
00113 {
00114
00115 STLtriangle stlTri(STLfile);
00116
00117
00118
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
00132
00133 stitchTriangles(rawPoints);
00134
00135 return true;
00136 }
00137
00138
00139
00140
00141 }
00142
00143
Copyright © 2000-2009 OpenCFD Ltd