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

OBJsurfaceFormat.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "OBJsurfaceFormat.H"
00028 #include "clock.H"
00029 #include "IFstream.H"
00030 #include "IStringStream.H"
00031 #include "Ostream.H"
00032 #include "OFstream.H"
00033 #include "ListOps.H"
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 template<class Face>
00038 Foam::fileFormats::OBJsurfaceFormat<Face>::OBJsurfaceFormat
00039 (
00040     const fileName& filename
00041 )
00042 {
00043     read(filename);
00044 }
00045 
00046 
00047 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00048 
00049 template<class Face>
00050 bool Foam::fileFormats::OBJsurfaceFormat<Face>::read
00051 (
00052     const fileName& filename
00053 )
00054 {
00055     const bool mustTriangulate = this->isTri();
00056     this->clear();
00057 
00058     IFstream is(filename);
00059     if (!is.good())
00060     {
00061         FatalErrorIn
00062         (
00063             "fileFormats::OBJsurfaceFormat::read(const fileName&)"
00064         )
00065             << "Cannot read file " << filename
00066             << exit(FatalError);
00067     }
00068 
00069     // assume that the groups are not intermixed
00070     bool sorted = true;
00071 
00072     DynamicList<point> dynPoints;
00073     DynamicList<Face>  dynFaces;
00074     DynamicList<label> dynZones;
00075     DynamicList<word>  dynNames;
00076     DynamicList<label> dynSizes;
00077     HashTable<label>   lookup;
00078 
00079     // place faces without a group in zone0
00080     label zoneI = 0;
00081     lookup.insert("zone0", zoneI);
00082     dynNames.append("zone0");
00083     dynSizes.append(0);
00084 
00085     while (is.good())
00086     {
00087         string line = this->getLineNoComment(is);
00088 
00089         // handle continuations
00090         if (line[line.size()-1] == '\\')
00091         {
00092             line.substr(0, line.size()-1);
00093             line += this->getLineNoComment(is);
00094         }
00095 
00096         // Read first word
00097         IStringStream lineStream(line);
00098         word cmd;
00099         lineStream >> cmd;
00100 
00101         if (cmd == "v")
00102         {
00103             scalar x, y, z;
00104             lineStream >> x >> y >> z;
00105             dynPoints.append(point(x, y, z));
00106         }
00107         else if (cmd == "g")
00108         {
00109             word name;
00110             lineStream >> name;
00111 
00112             HashTable<label>::const_iterator fnd = lookup.find(name);
00113             if (fnd != lookup.end())
00114             {
00115                 if (zoneI != fnd())
00116                 {
00117                     // group appeared out of order
00118                     sorted = false;
00119                 }
00120                 zoneI = fnd();
00121             }
00122             else
00123             {
00124                 zoneI = dynSizes.size();
00125                 lookup.insert(name, zoneI);
00126                 dynNames.append(name);
00127                 dynSizes.append(0);
00128             }
00129         }
00130         else if (cmd == "f")
00131         {
00132             DynamicList<label> dynVertices;
00133 
00134             // Assume 'f' is followed by space.
00135             string::size_type endNum = 1;
00136 
00137             while (true)
00138             {
00139                 string::size_type startNum =
00140                     line.find_first_not_of(' ', endNum);
00141 
00142                 if (startNum == string::npos)
00143                 {
00144                     break;
00145                 }
00146 
00147                 endNum = line.find(' ', startNum);
00148 
00149                 string vertexSpec;
00150                 if (endNum != string::npos)
00151                 {
00152                     vertexSpec = line.substr(startNum, endNum-startNum);
00153                 }
00154                 else
00155                 {
00156                     vertexSpec = line.substr(startNum, line.size() - startNum);
00157                 }
00158 
00159                 string::size_type slashPos = vertexSpec.find('/');
00160 
00161                 label vertI = 0;
00162                 if (slashPos != string::npos)
00163                 {
00164                     IStringStream intStream(vertexSpec.substr(0, slashPos));
00165 
00166                     intStream >> vertI;
00167                 }
00168                 else
00169                 {
00170                     IStringStream intStream(vertexSpec);
00171 
00172                     intStream >> vertI;
00173                 }
00174                 dynVertices.append(vertI - 1);
00175             }
00176             dynVertices.shrink();
00177 
00178             UList<label>& f = static_cast<UList<label>&>(dynVertices);
00179 
00180             if (mustTriangulate && f.size() > 3)
00181             {
00182                 // simple face triangulation about f[0]
00183                 // points may be incomplete
00184                 for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
00185                 {
00186                     label fp2 = f.fcIndex(fp1);
00187 
00188                     dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
00189                     dynZones.append(zoneI);
00190                     dynSizes[zoneI]++;
00191                 }
00192             }
00193             else
00194             {
00195                 dynFaces.append(Face(f));
00196                 dynZones.append(zoneI);
00197                 dynSizes[zoneI]++;
00198             }
00199         }
00200     }
00201 
00202 
00203     // transfer to normal lists
00204     this->storedPoints().transfer(dynPoints);
00205 
00206     sortFacesAndStore(dynFaces.xfer(), dynZones.xfer(), sorted);
00207 
00208     // add zones, culling empty ones
00209     this->addZones(dynSizes, dynNames, true);
00210     return true;
00211 }
00212 
00213 
00214 template<class Face>
00215 void Foam::fileFormats::OBJsurfaceFormat<Face>::write
00216 (
00217     const fileName& filename,
00218     const MeshedSurfaceProxy<Face>& surf
00219 )
00220 {
00221     const pointField& pointLst = surf.points();
00222     const List<Face>&  faceLst = surf.faces();
00223     const List<label>& faceMap = surf.faceMap();
00224 
00225     // for no zones, suppress the group name
00226     const List<surfZone>& zones =
00227     (
00228         surf.surfZones().size() > 1
00229       ? surf.surfZones()
00230       : oneZone(faceLst, "")
00231     );
00232 
00233     const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
00234 
00235     OFstream os(filename);
00236     if (!os.good())
00237     {
00238         FatalErrorIn
00239         (
00240             "fileFormats::OBJsurfaceFormat::write"
00241             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00242         )
00243             << "Cannot open file for writing " << filename
00244             << exit(FatalError);
00245     }
00246 
00247 
00248     os  << "# Wavefront OBJ file written " << clock::dateTime().c_str() << nl
00249         << "o " << os.name().lessExt().name() << nl
00250         << nl
00251         << "# points : " << pointLst.size() << nl
00252         << "# faces  : " << faceLst.size() << nl
00253         << "# zones  : " << zones.size() << nl;
00254 
00255     // Print zone names as comment
00256     forAll(zones, zoneI)
00257     {
00258         os  << "#   " << zoneI << "  " << zones[zoneI].name()
00259             << "  (nFaces: " << zones[zoneI].size() << ")" << nl;
00260     }
00261 
00262     os  << nl
00263         << "# <points count=\"" << pointLst.size() << "\">" << nl;
00264 
00265     // Write vertex coords
00266     forAll(pointLst, ptI)
00267     {
00268         const point& pt = pointLst[ptI];
00269 
00270         os  << "v " << pt.x() << ' '  << pt.y() << ' '  << pt.z() << nl;
00271     }
00272 
00273     os  << "# </points>" << nl
00274         << nl
00275         << "# <faces count=\"" << faceLst.size() << "\">" << endl;
00276 
00277 
00278     label faceIndex = 0;
00279     forAll(zones, zoneI)
00280     {
00281         const surfZone& zone = zones[zoneI];
00282 
00283         if (zone.name().size())
00284         {
00285             os << "g " << zone.name() << endl;
00286         }
00287 
00288         if (useFaceMap)
00289         {
00290             forAll(zone, localFaceI)
00291             {
00292                 const Face& f = faceLst[faceMap[faceIndex++]];
00293 
00294                 os << 'f';
00295                 forAll(f, fp)
00296                 {
00297                     os << ' ' << f[fp] + 1;
00298                 }
00299                 os << endl;
00300             }
00301         }
00302         else
00303         {
00304             forAll(zone, localFaceI)
00305             {
00306                 const Face& f = faceLst[faceIndex++];
00307 
00308                 os << 'f';
00309                 forAll(f, fp)
00310                 {
00311                     os << ' ' << f[fp] + 1;
00312                 }
00313                 os << endl;
00314             }
00315         }
00316     }
00317     os << "# </faces>" << endl;
00318 }
00319 
00320 
00321 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd