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

surfaceFeatureConvert.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 Application
00025     surfaceFeatureConvert
00026 
00027 Description
00028     Extracts and writes surface features to file
00029 
00030 Usage
00031 
00032     - surfaceFeatureConvert [OPTIONS] <input file> <output file>
00033 
00034     @param <input file> \n
00035     @todo Detailed description of argument.
00036 
00037     @param <output file> \n
00038     @todo Detailed description of argument.
00039 
00040     @param -case <dir>\n
00041     Case directory.
00042 
00043     @param -help \n
00044     Display help message.
00045 
00046     @param -doc \n
00047     Display Doxygen API documentation page for this application.
00048 
00049     @param -srcDoc \n
00050     Display Doxygen source documentation page for this application.
00051 
00052 \*---------------------------------------------------------------------------*/
00053 
00054 #include <edgeMesh/featureEdgeMesh.H>
00055 #include <OpenFOAM/argList.H>
00056 #include <OpenFOAM/Time.H>
00057 #include <OpenFOAM/IFstream.H>
00058 #include <OpenFOAM/IStringStream.H>
00059 #include <OpenFOAM/OFstream.H>
00060 #include <OpenFOAM/Map.H>
00061 
00062 using namespace Foam;
00063 
00064 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00065 
00066 void readNASEdges
00067 (
00068     const fileName& inFileName,
00069     pointField& allPoints,
00070     edgeList& allEdges
00071 )
00072 {
00073     IFstream is(inFileName);
00074 
00075     if (!is.good())
00076     {
00077         FatalErrorIn("readNASEdges")
00078             << "Cannot read file " << inFileName
00079             << exit(FatalError);
00080     }
00081 
00082     // coordinates of point
00083     DynamicList<point> points;
00084     // Nastran index of point
00085     DynamicList<label> pointIndices;
00086 
00087     // beams
00088     DynamicList<edge> edges;
00089     DynamicList<label> edgeIndices;
00090 
00091 
00092     while (is.good())
00093     {
00094         string line;
00095         is.getLine(line);
00096 
00097         if (line.empty() || line[0] == '$')
00098         {
00099             // Skip empty and comment
00100             continue;
00101         }
00102 
00103         // Check if character 72 is continuation
00104         if (line.size() > 72 && line[72] == '+')
00105         {
00106             line = line.substr(0, 72);
00107 
00108             while (true)
00109             {
00110                 string buf;
00111                 is.getLine(buf);
00112 
00113                 if (buf.size() > 72 && buf[72] == '+')
00114                 {
00115                     line += buf.substr(8, 64);
00116                 }
00117                 else
00118                 {
00119                     line += buf.substr(8, buf.size()-8);
00120                     break;
00121                 }
00122             }
00123         }
00124 
00125         // Read first word
00126         IStringStream lineStream(line);
00127         word cmd;
00128         lineStream >> cmd;
00129 
00130         if (cmd == "GRID")
00131         {
00132             label index;
00133             lineStream >> index;
00134             pointIndices.append(index);
00135 
00136             scalar x = readScalar(IStringStream(line.substr(24, 8))());
00137             scalar y = readScalar(IStringStream(line.substr(32, 8))());
00138             scalar z = readScalar(IStringStream(line.substr(40, 8))());
00139             points.append(point(x, y, z));
00140         }
00141         else if (cmd == "CBEAM")
00142         {
00143             // Read shell type since gives patchnames.
00144             label index, group, v0, v1;
00145             lineStream >> index >> group >> v0 >> v1;
00146 
00147             edgeIndices.append(index);
00148             edges.append(edge(v0, v1));
00149         }
00150     }
00151 
00152     points.shrink();
00153     pointIndices.shrink();
00154     edges.shrink();
00155     edgeIndices.shrink();
00156 
00157     Pout<< "Read from " << inFileName
00158         << " edges:" << edges.size() << " points:" << points.size()
00159         << endl;
00160 
00161     {
00162         // Build inverse mapping (index to point)
00163         Map<label> indexToPoint(2*pointIndices.size());
00164         forAll(pointIndices, i)
00165         {
00166             indexToPoint.insert(pointIndices[i], i);
00167         }
00168 
00169         // Relabel edges
00170         forAll(edges, i)
00171         {
00172             edge& e = edges[i];
00173             e[0] = indexToPoint[e[0]];
00174             e[1] = indexToPoint[e[1]];
00175         }
00176     }
00177 
00178     allPoints.transfer(points);
00179     allEdges.transfer(edges);
00180 }
00181 
00182 
00183 
00184 void write
00185 (
00186     const Time& runTime,
00187     const fileName& inFileName,
00188     const fileName& outFileName,
00189     const edgeMesh& eMesh
00190 )
00191 {
00192     if (outFileName.ext() == "eMesh")
00193     {
00194         featureEdgeMesh fem
00195         (
00196             IOobject
00197             (
00198                 outFileName,        // name
00199                 runTime.constant(), // instance
00200                 runTime,            // registry
00201                 IOobject::NO_READ,
00202                 IOobject::AUTO_WRITE,
00203                 false
00204             ),
00205             eMesh.points(),
00206             eMesh.edges()
00207         );
00208 
00209         Pout<< "Writing feature edge mesh to " << fem.objectPath()
00210             << endl;
00211 
00212         fem.write();
00213     }
00214     else if (outFileName.ext() == "vtk")
00215     {
00216         OFstream str(outFileName);
00217 
00218         str << "# vtk DataFile Version 2.0" << nl
00219             << "featureEdgeMesh " << inFileName << nl
00220             << "ASCII" << nl
00221             << "DATASET POLYDATA" << nl;
00222 
00223         str << "POINTS " << eMesh.points().size() << " float" << nl;
00224         forAll(eMesh.points(), pointI)
00225         {
00226             const point& pt = eMesh.points()[pointI];
00227 
00228             str << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00229         }
00230 
00231         str << "LINES " << eMesh.edges().size() << ' '
00232             << 3*eMesh.edges().size() << nl;
00233         forAll(eMesh.edges(), edgeI)
00234         {
00235             const edge& e = eMesh.edges()[edgeI];
00236 
00237             str << "2 " << e[0] << ' ' << e[1] << nl;
00238         }
00239     }
00240     else
00241     {
00242         FatalErrorIn("write")
00243             << "Supported output formats: .eMesh, .vtk"
00244             << exit(FatalError);
00245     }
00246 }
00247 
00248 
00249 // Main program:
00250 
00251 int main(int argc, char *argv[])
00252 {
00253     argList::noParallel();
00254     argList::validArgs.append("input file");
00255     argList::validArgs.append("output file");
00256 #   include <OpenFOAM/setRootCase.H>
00257 #   include <OpenFOAM/createTime.H>
00258 
00259     const fileName inFileName(args.additionalArgs()[0]);
00260     const word outFileName(args.additionalArgs()[1]);
00261 
00262     Pout<< "Input features file  : " << inFileName << nl
00263         << "Output features file : " << outFileName << nl
00264         << endl;
00265 
00266 
00267     // Read
00268     // ~~~~
00269 
00270     if (inFileName.ext() == "nas")
00271     {
00272         pointField points;
00273         edgeList edges;
00274         readNASEdges(inFileName, points, edges);
00275 
00276         edgeMesh eMesh(points, edges);
00277 
00278         write(runTime, inFileName, outFileName, eMesh);
00279     }
00280     else if (inFileName.ext() == "eMesh")
00281     {
00282         featureEdgeMesh fem
00283         (
00284             IOobject
00285             (
00286                 inFileName,         // name
00287                 runTime.constant(), // instance
00288                 runTime,            // registry
00289                 IOobject::MUST_READ,
00290                 IOobject::AUTO_WRITE,
00291                 false
00292             )
00293         );
00294 
00295         Pout<< "Read from " << inFileName
00296             << " edges:" << fem.edges().size()
00297             << " points:" << fem.points().size()
00298             << endl;
00299 
00300         write(runTime, inFileName, outFileName, fem);
00301     }
00302     else
00303     {
00304         FatalErrorIn(args.executable())
00305             << "Can only handle NASTRAN data formats (.nas extension)."
00306             << exit(FatalError);
00307     }
00308 
00309     Pout<< "End\n" << endl;
00310 
00311     return 0;
00312 }
00313 
00314 
00315 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines