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

AC3DsurfaceFormat.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 "AC3DsurfaceFormat.H"
00028 #include "clock.H"
00029 #include "IStringStream.H"
00030 #include "tensor.H"
00031 #include "primitivePatch.H"
00032 
00033 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00034 
00035 template<class Face>
00036 Foam::fileFormats::AC3DsurfaceFormat<Face>::AC3DsurfaceFormat
00037 (
00038     const fileName& filename
00039 )
00040 {
00041     read(filename);
00042 }
00043 
00044 
00045 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00046 
00047 template<class Face>
00048 bool Foam::fileFormats::AC3DsurfaceFormat<Face>::read
00049 (
00050     const fileName& filename
00051 )
00052 {
00053     const bool mustTriangulate = this->isTri();
00054     this->clear();
00055 
00056     IFstream is(filename);
00057     if (!is.good())
00058     {
00059         FatalErrorIn
00060         (
00061             "fileFormats::AC3DsurfaceFormat::read(const fileName&)"
00062         )
00063             << "Cannot read file " << filename
00064             << exit(FatalError);
00065     }
00066 
00067     string line, cmd, args;
00068 
00069     is.getLine(line);
00070 
00071     string version = line.substr(4);
00072 
00073     if (version != "b")
00074     {
00075         WarningIn
00076         (
00077             "fileFormats::AC3DsurfaceFormat::read(const fileName&)"
00078         )
00079             << "When reading AC3D file " << filename
00080             << " read header " << line << " with version "
00081             << version << endl
00082             << "Only tested reading with version 'b'."
00083             << " This might give problems" << endl;
00084     }
00085 
00086 
00087     if (!cueTo(is, "OBJECT", args) || (args != "world"))
00088     {
00089         FatalErrorIn
00090         (
00091             "fileFormats::AC3DsurfaceFormat::read(const fileName&)"
00092         )
00093             << "Cannot find \"OBJECT world\" in file " << filename
00094             << exit(FatalError);
00095     }
00096 
00097     // # of kids is the # of zones
00098     args = cueToOrDie(is, "kids");
00099     label nZones = parse<int>(args);
00100 
00101     // Start of vertices for object/zones
00102     label vertexOffset = 0;
00103 
00104     DynamicList<point> dynPoints;
00105     DynamicList<Face>  dynFaces;
00106     List<word>         names(nZones);
00107     List<label>        sizes(nZones, 0);
00108 
00109     for (label zoneI = 0; zoneI < nZones; ++zoneI)
00110     {
00111         names[zoneI] = word("zone") + Foam::name(zoneI);
00112 
00113         args = cueToOrDie(is, "OBJECT", "while reading " + names[zoneI]);
00114 
00115         // number of vertices for this zone
00116         label  nZonePoints = 0;
00117         vector location(pTraits<vector>::zero);
00118         // tensor rotation(I);
00119 
00120         // Read all info for current zone
00121         while (is.good())
00122         {
00123             // Read line and get first word. If end of file break since
00124             // zone should always end with 'kids' command ?not sure.
00125             if (!readCmd(is, cmd, args))
00126             {
00127                 FatalErrorIn
00128                 (
00129                     "fileFormats::AC3DsurfaceFormat::read(const fileName&)"
00130                 )
00131                     << "Did not read up to \"kids 0\" while reading zone "
00132                     << zoneI << " from file " << filename
00133                     << exit(FatalError);
00134             }
00135 
00136             if (cmd == "name")
00137             {
00138                 // name %s
00139                 string str = parse<string>(args);
00140                 string::stripInvalid<word>(str);
00141 
00142                 names[zoneI] = str;
00143             }
00144             else if (cmd == "rot")
00145             {
00146                 // rot  %f %f %f  %f %f %f  %f %f %f
00147 
00148                 // IStringStream lineStream(args);
00149                 //
00150                 // lineStream
00151                 //     >> rotation.xx() >> rotation.xy() >> rotation.xz()
00152                 //     >> rotation.yx() >> rotation.yy() >> rotation.yz()
00153                 //     >> rotation.zx() >> rotation.zy() >> rotation.zz();
00154 
00155                 WarningIn
00156                 (
00157                     "fileFormats::AC3DsurfaceFormat::read"
00158                     "(const fileName&)"
00159                 )
00160                     << "rot (rotation tensor) command not implemented"
00161                     << "Line:" << cmd << ' ' << args << endl
00162                     << "while reading zone " << zoneI << endl;
00163             }
00164             else if (cmd == "loc")
00165             {
00166                 // loc  %f %f %f
00167                 IStringStream lineStream(args);
00168 
00169                 lineStream
00170                     >> location.x()
00171                     >> location.y()
00172                     >> location.z();
00173             }
00174             else if (cmd == "numvert")
00175             {
00176                 // numvert  %d
00177                 nZonePoints = parse<int>(args);
00178 
00179                 for (label vertI = 0; vertI < nZonePoints; ++vertI)
00180                 {
00181                     is.getLine(line);
00182                     IStringStream lineStream(line);
00183 
00184                     point pt;
00185                     lineStream
00186                         >> pt.x() >> pt.y() >> pt.z();
00187 
00188                     // Offset with current translation vector
00189                     dynPoints.append(location + pt);
00190                 }
00191             }
00192             else if (cmd == "numsurf")
00193             {
00194                 label nFaces = parse<int>(args);
00195 
00196                 for (label faceI = 0; faceI < nFaces; ++faceI)
00197                 {
00198                     static string errorMsg =
00199                         string(" while reading face ")
00200                             + Foam::name(faceI) + " on zone "
00201                             + Foam::name(zoneI)
00202                             + " from file " + filename;
00203 
00204                     cueToOrDie(is, "SURF", errorMsg);
00205                     cueToOrDie(is, "mat", errorMsg);
00206                     args = cueToOrDie(is, "refs", errorMsg);
00207 
00208                     label nVert = parse<int>(args);
00209 
00210                     List<label> verts(nVert);
00211                     forAll(verts, vertI)
00212                     {
00213                         is.getLine(line);
00214                         verts[vertI] = parse<int>(line) + vertexOffset;
00215                     }
00216 
00217                     UList<label>& f = static_cast<UList<label>&>(verts);
00218 
00219                     if (mustTriangulate && f.size() > 3)
00220                     {
00221                         // simple face triangulation about f[0]
00222                         // points may be incomplete
00223                         for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
00224                         {
00225                             label fp2 = f.fcIndex(fp1);
00226 
00227                             dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
00228                             sizes[zoneI]++;
00229                         }
00230                     }
00231                     else
00232                     {
00233                         dynFaces.append(Face(f));
00234                         sizes[zoneI]++;
00235                     }
00236                 }
00237 
00238                 // Done the current zone.
00239                 // Increment the offset vertices are stored at
00240                 vertexOffset += nZonePoints;
00241             }
00242             else if (cmd == "kids")
00243             {
00244                 // 'kids' denotes the end of the current zone.
00245                 label nKids = parse<int>(args);
00246 
00247                 if (nKids != 0)
00248                 {
00249                     FatalErrorIn
00250                     (
00251                         "fileFormats::AC3DsurfaceFormat::read(const fileName&)"
00252                     )
00253                         << "Can only read objects without kids."
00254                         << " Encountered " << nKids << " kids when"
00255                         << " reading zone " << zoneI
00256                         << exit(FatalError);
00257                 }
00258 
00259                 // Done reading current zone
00260                 break;
00261             }
00262         }
00263     }
00264 
00265     // transfer to normal lists
00266     this->storedPoints().transfer(dynPoints);
00267     this->storedFaces().transfer(dynFaces);
00268 
00269     // add zones, culling empty ones
00270     this->addZones(sizes, names, true);
00271     this->stitchFaces(SMALL);
00272     return true;
00273 }
00274 
00275 
00276 template<class Face>
00277 void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
00278 (
00279     const fileName& filename,
00280     const MeshedSurfaceProxy<Face>& surf
00281 )
00282 {
00283     const pointField& pointLst = surf.points();
00284     const List<Face>&  faceLst = surf.faces();
00285 
00286     const List<surfZone>& zones =
00287     (
00288         surf.surfZones().size()
00289       ? surf.surfZones()
00290       : oneZone(faceLst)
00291     );
00292 
00293     const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
00294 
00295     if (useFaceMap)
00296     {
00297         FatalErrorIn
00298         (
00299             "fileFormats::AC3DsurfaceFormat::write"
00300             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00301         )
00302             << "output with faceMap is not supported " << filename
00303             << exit(FatalError);
00304     }
00305 
00306 
00307     OFstream os(filename);
00308     if (!os.good())
00309     {
00310         FatalErrorIn
00311         (
00312             "fileFormats::AC3DsurfaceFormat::write"
00313             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00314         )
00315             << "Cannot open file for writing " << filename
00316             << exit(FatalError);
00317     }
00318 
00319     writeHeader(os, zones);
00320 
00321     forAll(zones, zoneI)
00322     {
00323         const surfZone& zone = zones[zoneI];
00324 
00325         os  << "OBJECT poly" << nl
00326             << "name \"" << zone.name() << "\"\n";
00327 
00328         // Temporary PrimitivePatch to calculate compact points & faces
00329         // use 'UList' to avoid allocations!
00330         PrimitivePatch<Face, UList, const pointField&> patch
00331         (
00332             SubList<Face>
00333             (
00334                 faceLst,
00335                 zone.size(),
00336                 zone.start()
00337             ),
00338             pointLst
00339         );
00340 
00341         os << "numvert " << patch.nPoints() << endl;
00342 
00343         forAll(patch.localPoints(), ptI)
00344         {
00345             const point& pt = patch.localPoints()[ptI];
00346 
00347             os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00348         }
00349 
00350         os << "numsurf " << patch.localFaces().size() << endl;
00351 
00352         forAll(patch.localFaces(), localFaceI)
00353         {
00354             const Face& f = patch.localFaces()[localFaceI];
00355 
00356             os  << "SURF 0x20" << nl          // polygon
00357                 << "mat " << zoneI << nl
00358                 << "refs " << f.size() << nl;
00359 
00360             forAll(f, fp)
00361             {
00362                 os << f[fp] << " 0 0" << nl;
00363             }
00364         }
00365 
00366         os << "kids 0" << endl;
00367     }
00368 }
00369 
00370 
00371 template<class Face>
00372 void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
00373 (
00374     const fileName& filename,
00375     const UnsortedMeshedSurface<Face>& surf
00376 )
00377 {
00378     labelList faceMap;
00379     List<surfZone> zoneLst = surf.sortedZones(faceMap);
00380 
00381     if (zoneLst.size() <= 1)
00382     {
00383         write
00384         (
00385             filename,
00386             MeshedSurfaceProxy<Face>
00387             (
00388                 surf.points(),
00389                 surf.faces(),
00390                 zoneLst
00391             )
00392         );
00393     }
00394     else
00395     {
00396         OFstream os(filename);
00397         if (!os.good())
00398         {
00399             FatalErrorIn
00400             (
00401                 "fileFormats::AC3DsurfaceFormat::write"
00402                 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00403             )
00404                 << "Cannot open file for writing " << filename
00405                 << exit(FatalError);
00406         }
00407 
00408         writeHeader(os, zoneLst);
00409 
00410         label faceIndex = 0;
00411         forAll(zoneLst, zoneI)
00412         {
00413             const surfZone& zone = zoneLst[zoneI];
00414 
00415             os  << "OBJECT poly" << nl
00416                 << "name \"" << zone.name() << "\"\n";
00417 
00418             // Create zone with only zone faces included for ease of addressing
00419             labelHashSet include(surf.size());
00420 
00421             forAll(zone, localFaceI)
00422             {
00423                 const label faceI = faceMap[faceIndex++];
00424                 include.insert(faceI);
00425             }
00426 
00427             UnsortedMeshedSurface<Face> subm = surf.subsetMesh(include);
00428 
00429             // Now we have isolated surface for this patch alone. Write it.
00430             os << "numvert " << subm.nPoints() << endl;
00431 
00432             forAll(subm.localPoints(), ptI)
00433             {
00434                 const point& pt = subm.localPoints()[ptI];
00435 
00436                 os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00437             }
00438 
00439             os << "numsurf " << subm.localFaces().size() << endl;
00440 
00441             forAll(subm.localFaces(), localFaceI)
00442             {
00443                 const Face& f = subm.localFaces()[localFaceI];
00444 
00445                 os  << "SURF 0x20" << nl          // polygon
00446                     << "mat " << zoneI << nl
00447                     << "refs " << f.size() << nl;
00448 
00449                 forAll(f, fp)
00450                 {
00451                     os << f[fp] << " 0 0" << nl;
00452                 }
00453             }
00454 
00455             os << "kids 0" << endl;
00456         }
00457     }
00458 }
00459 
00460 
00461 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd