BUGFIX: Illegal use of uninitialised value (backport)
[foam-extend-3.2.git] / applications / utilities / surface / surfaceMeshConvert / surfaceMeshConvert.C
blob106adb721e165903366e73e53e93e4f5e537e1f1
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
26     surfaceMeshConvert
28 Description
30     Convert between surface formats with optional scaling or
31     transformations (rotate/translate) on a coordinateSystem.
33 Usage
34     - surfaceMeshConvert inputFile outputFile [OPTION]
36     @param -clean \n
37     Perform some surface checking/cleanup on the input surface.
39     @param -scaleIn \<scale\> \n
40     Specify a scaling factor when reading files.
42     @param -scaleOut \<scale\> \n
43     Specify a scaling factor when writing files.
45     @param -dict \<dictionary\> \n
46     Specify an alternative dictionary for constant/coordinateSystems.
48     @param -from \<coordinateSystem\> \n
49     Specify a coordinate System when reading files.
51     @param -to \<coordinateSystem\> \n
52     Specify a coordinate System when writing files.
54 Note
55     The filename extensions are used to determine the file format type.
57 \*---------------------------------------------------------------------------*/
59 #include "argList.H"
60 #include "timeSelector.H"
61 #include "objectRegistry.H"
62 #include "Time.H"
63 #include "MeshedSurfaces.H"
64 #include "coordinateSystems.H"
66 using namespace Foam;
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
69 //  Main program:
71 int main(int argc, char *argv[])
73     argList::noParallel();
74     argList::validArgs.append("inputFile");
75     argList::validArgs.append("outputFile");
76     argList::validOptions.insert("clean", "");
77     argList::validOptions.insert("scaleIn",  "scale");
78     argList::validOptions.insert("scaleOut", "scale");
79     argList::validOptions.insert("dict", "coordinateSystemsDict");
80     argList::validOptions.insert("from", "sourceCoordinateSystem");
81     argList::validOptions.insert("to",   "targetCoordinateSystem");
83     argList args(argc, argv);
84     Time runTime(args.rootPath(), args.caseName());
85     const stringList& params = args.additionalArgs();
87     fileName importName(params[0]);
88     fileName exportName(params[1]);
90     // disable inplace editing
91     if (importName == exportName)
92     {
93         FatalErrorIn(args.executable())
94             << "Output file " << exportName << " would overwrite input file."
95             << exit(FatalError);
96     }
98     // check that reading/writing is supported
99     if
100     (
101         !MeshedSurface<face>::canRead(importName, true)
102      || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
103     )
104     {
105         return 1;
106     }
109     // get the coordinate transformations
110     autoPtr<coordinateSystem> fromCsys;
111     autoPtr<coordinateSystem> toCsys;
113     if (args.optionFound("from") || args.optionFound("to"))
114     {
115         autoPtr<IOobject> csDictIoPtr;
117         if (args.optionFound("dict"))
118         {
119             fileName dictPath(args.option("dict"));
121             csDictIoPtr.set
122             (
123                 new IOobject
124                 (
125                     (
126                         isDir(dictPath)
127                       ? dictPath/coordinateSystems::typeName
128                       : dictPath
129                     ),
130                     runTime,
131                     IOobject::MUST_READ,
132                     IOobject::NO_WRITE,
133                     false
134                 )
135             );
136         }
137         else
138         {
139             csDictIoPtr.set
140             (
141                 new IOobject
142                 (
143                     coordinateSystems::typeName,
144                     runTime.constant(),
145                     runTime,
146                     IOobject::MUST_READ,
147                     IOobject::NO_WRITE,
148                     false
149                 )
150             );
151         }
154         if (!csDictIoPtr->headerOk())
155         {
156             FatalErrorIn(args.executable())
157                 << "Cannot open coordinateSystems file\n    "
158                 << csDictIoPtr->objectPath() << nl
159                 << exit(FatalError);
160         }
162         coordinateSystems csLst(csDictIoPtr());
164         if (args.optionFound("from"))
165         {
166             const word csName(args.option("from"));
168             label csId = csLst.find(csName);
169             if (csId < 0)
170             {
171                 FatalErrorIn(args.executable())
172                     << "Cannot find -from " << csName << nl
173                     << "available coordinateSystems: " << csLst.toc() << nl
174                     << exit(FatalError);
175             }
177             fromCsys.reset(new coordinateSystem(csLst[csId]));
178         }
180         if (args.optionFound("to"))
181         {
182             const word csName(args.option("to"));
184             label csId = csLst.find(csName);
185             if (csId < 0)
186             {
187                 FatalErrorIn(args.executable())
188                     << "Cannot find -to " << csName << nl
189                     << "available coordinateSystems: " << csLst.toc() << nl
190                     << exit(FatalError);
191             }
193             toCsys.reset(new coordinateSystem(csLst[csId]));
194         }
197         // maybe fix this later
198         if (fromCsys.valid() && toCsys.valid())
199         {
200             FatalErrorIn(args.executable())
201                 << "Only allowed  '-from' or '-to' option at the moment."
202                 << exit(FatalError);
203         }
204     }
207     {
208         MeshedSurface<face> surf(importName);
210         if (args.optionFound("clean"))
211         {
212             surf.cleanup(true);
213         }
215         scalar scaleIn = 0;
216         if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
217         {
218             Info<< " -scaleIn " << scaleIn << endl;
219             surf.scalePoints(scaleIn);
220         }
223         if (fromCsys.valid())
224         {
225             Info<< " -from " << fromCsys().name() << endl;
226             tmp<pointField> tpf = fromCsys().localPosition(surf.points());
227             surf.movePoints(tpf());
228         }
230         if (toCsys.valid())
231         {
232             Info<< " -to " << toCsys().name() << endl;
233             tmp<pointField> tpf = toCsys().globalPosition(surf.points());
234             surf.movePoints(tpf());
235         }
237         scalar scaleOut = 0;
238         if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
239         {
240             Info<< " -scaleOut " << scaleOut << endl;
241             surf.scalePoints(scaleOut);
242         }
244         Info<< "writing " << exportName;
245         surf.write(exportName);
246     }
248     Info<< "\nEnd\n" << endl;
250     return 0;
253 // ************************************************************************* //