surfaceMeshConvert.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  surfaceMeshConvert
26 
27 Description
28  Converts between surface formats with optional scaling or
29  transformations (rotate/translate) on a coordinateSystem.
30 
31 Usage
32  \b surfaceMeshConvert inputFile outputFile [OPTION]
33 
34  Options:
35  - \par -clean
36  Perform some surface checking/cleanup on the input surface.
37 
38  - \par -scaleIn <scale>
39  Specify a scaling factor when reading files.
40 
41  - \par -scaleOut <scale>
42  Specify a scaling factor when writing files.
43 
44  - \par -dict <dictionary>
45  Specify an alternative dictionary for constant/coordinateSystems.
46 
47  - \par -from <coordinateSystem>
48  Specify a coordinate System when reading files.
49 
50  - \par -to <coordinateSystem>
51  Specify a coordinate System when writing files.
52 
53  - \par -tri
54  Triangulate surface.
55 
56 Note
57  The filename extensions are used to determine the file format type.
58 
59 \*---------------------------------------------------------------------------*/
60 
61 #include "argList.H"
62 #include "Time.H"
63 
64 #include "MeshedSurfaces.H"
65 #include "coordinateSystems.H"
66 
67 using namespace Foam;
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 
71 int main(int argc, char *argv[])
72 {
74  (
75  "convert between surface formats"
76  );
77 
79  argList::validArgs.append("inputFile");
80  argList::validArgs.append("outputFile");
81 
83  (
84  "clean",
85  "perform some surface checking/cleanup on the input surface"
86  );
88  (
89  "scaleIn",
90  "factor",
91  "geometry scaling factor on input"
92  );
94  (
95  "scaleOut",
96  "factor",
97  "geometry scaling factor on output"
98  );
99  #include "addDictOption.H"
101  (
102  "from",
103  "system",
104  "specify the source coordinate system, applied after '-scaleIn'"
105  );
107  (
108  "to",
109  "system",
110  "specify the target coordinate system, applied before '-scaleOut'"
111  );
113  (
114  "tri",
115  "triangulate surface"
116  );
117 
118 
119  argList args(argc, argv);
120  Time runTime(args.rootPath(), args.caseName());
121 
122  const fileName importName = args[1];
123  const fileName exportName = args[2];
124 
125  // disable inplace editing
126  if (importName == exportName)
127  {
129  << "Output file " << exportName << " would overwrite input file."
130  << exit(FatalError);
131  }
132 
133  // check that reading/writing is supported
134  if
135  (
136  !MeshedSurface<face>::canRead(importName, true)
137  || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
138  )
139  {
140  return 1;
141  }
142 
143 
144  // get the coordinate transformations
145  autoPtr<coordinateSystem> fromCsys;
147 
148  if (args.optionFound("from") || args.optionFound("to"))
149  {
150  autoPtr<IOobject> csDictIoPtr;
151 
152  const word dictName("coordinateSystems::typeName");
153 
154  // Note: cannot use setSystemRunTimeDictionaryIO.H since dictionary
155  // is in constant
156 
157  fileName dictPath = "";
158  if (args.optionFound("dict"))
159  {
160  dictPath = args["dict"];
161  if (isDir(dictPath))
162  {
163  dictPath = dictPath / dictName;
164  }
165  }
166 
167  if (dictPath.size())
168  {
169  csDictIoPtr.set
170  (
171  new IOobject
172  (
173  dictPath,
174  runTime,
177  false
178  )
179  );
180  }
181  else
182  {
183  csDictIoPtr.set
184  (
185  new IOobject
186  (
187  dictName,
188  runTime.constant(),
189  runTime,
192  false
193  )
194  );
195  }
196 
197 
198  if (!csDictIoPtr->headerOk())
199  {
201  << "Cannot open coordinateSystems file\n "
202  << csDictIoPtr->objectPath() << nl
203  << exit(FatalError);
204  }
205 
206  coordinateSystems csLst(csDictIoPtr());
207 
208  if (args.optionFound("from"))
209  {
210  const word csName = args["from"];
211 
212  const label csIndex = csLst.findIndex(csName);
213  if (csIndex < 0)
214  {
216  << "Cannot find -from " << csName << nl
217  << "available coordinateSystems: " << csLst.toc() << nl
218  << exit(FatalError);
219  }
220 
221  fromCsys.reset(new coordinateSystem(csLst[csIndex]));
222  }
223 
224  if (args.optionFound("to"))
225  {
226  const word csName = args["to"];
227 
228  const label csIndex = csLst.findIndex(csName);
229  if (csIndex < 0)
230  {
232  << "Cannot find -to " << csName << nl
233  << "available coordinateSystems: " << csLst.toc() << nl
234  << exit(FatalError);
235  }
236 
237  toCsys.reset(new coordinateSystem(csLst[csIndex]));
238  }
239 
240 
241  // maybe fix this later
242  if (fromCsys.valid() && toCsys.valid())
243  {
245  << "Only allowed '-from' or '-to' option at the moment."
246  << exit(FatalError);
247  }
248  }
249 
250 
251  {
252  MeshedSurface<face> surf(importName);
253 
254  if (args.optionFound("clean"))
255  {
256  surf.cleanup(true);
257  }
258 
259  scalar scaleIn = 0;
260  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
261  {
262  Info<< " -scaleIn " << scaleIn << endl;
263  surf.scalePoints(scaleIn);
264  }
265 
266 
267  if (fromCsys.valid())
268  {
269  Info<< " -from " << fromCsys().name() << endl;
270  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
271  surf.movePoints(tpf());
272  }
273 
274  if (toCsys.valid())
275  {
276  Info<< " -to " << toCsys().name() << endl;
277  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
278  surf.movePoints(tpf());
279  }
280 
281  scalar scaleOut = 0;
282  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
283  {
284  Info<< " -scaleOut " << scaleOut << endl;
285  surf.scalePoints(scaleOut);
286  }
287 
288  if (args.optionFound("tri"))
289  {
290  Info<< "triangulate" << endl;
291  surf.triangulate();
292  }
293 
294  Info<< "writing " << exportName;
295  surf.write(exportName);
296  }
297 
298  Info<< "\nEnd\n" << endl;
299 
300  return 0;
301 }
302 
303 // ************************************************************************* //
Base class for other coordinate system specifications.
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
Provides a centralized coordinateSystem collection.
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:486
const fileName & rootPath() const
Return root path.
Definition: argListI.H:36
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
A class for handling words, derived from string.
Definition: word.H:59
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:93
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
static const char nl
Definition: Ostream.H:262
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
word dictName("noiseDict")
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:283
messageStream Info
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:83
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:124
A class for managing temporary objects.
Definition: PtrList.H:54
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Namespace for OpenFOAM.