surfaceMeshConvertTesting.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  surfaceMeshConvertTesting
26 
27 Description
28  Converts from one surface mesh format to another, but primarily
29  used for testing functionality.
30 
31 Usage
32  \b surfaceMeshConvertTesting inputFile outputFile [OPTION]
33 
34  Options:
35  - \par -clean
36  Perform some surface checking/cleanup on the input surface
37 
38  - \par -orient
39  Check face orientation on the input surface
40 
41  - \par -scale <scale>
42  Specify a scaling factor for writing the files
43 
44  - \par -triSurface
45  Use triSurface library for input/output
46 
47  - \par -keyed
48  Use keyedSurface for input/output
49 
50 Note
51  The filename extensions are used to determine the file format type.
52 
53 \*---------------------------------------------------------------------------*/
54 
55 #include "argList.H"
56 #include "Time.H"
57 #include "polyMesh.H"
58 #include "triSurface.H"
59 #include "surfMesh.H"
60 #include "surfFields.H"
61 #include "surfPointFields.H"
62 #include "PackedBoolList.H"
63 
64 #include "MeshedSurfaces.H"
65 #include "UnsortedMeshedSurfaces.H"
66 
67 using namespace Foam;
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 
71 int main(int argc, char *argv[])
72 {
74  (
75  "convert between surface formats, "
76  "but primarily for testing functionality\n"
77  "Normally use surfaceMeshConvert instead."
78  );
79 
81  argList::validArgs.append("inputFile");
82  argList::validArgs.append("outputFile");
83 
84  argList::addBoolOption("clean");
85  argList::addBoolOption("orient");
86  argList::addBoolOption("surfMesh");
87  argList::addBoolOption("triSurface");
88  argList::addBoolOption("unsorted");
89  argList::addBoolOption("triFace");
90 
92  (
93  "scale",
94  "factor",
95  "geometry scaling factor - default is 1"
96  );
97 
98  #include "setRootCase.H"
99 
100  const scalar scaleFactor = args.optionLookupOrDefault("scale", 0.0);
101 
102  const fileName importName = args[1];
103  const fileName exportName = args[2];
104 
105  if (importName == exportName)
106  {
108  << "Output file " << exportName << " would overwrite input file."
109  << exit(FatalError);
110  }
111 
112  if
113  (
114  !MeshedSurface<face>::canRead(importName, true)
115  || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
116  )
117  {
118  return 1;
119  }
120 
121  if (args.optionFound("triSurface"))
122  {
123  triSurface surf(importName);
124 
125  Info<< "Read surface:" << endl;
126  surf.writeStats(Info);
127  Info<< endl;
128 
129  if (args.optionFound("orient"))
130  {
131  Info<< "Checking surface orientation" << endl;
132  PatchTools::checkOrientation(surf, true);
133  Info<< endl;
134  }
135 
136  if (args.optionFound("clean"))
137  {
138  Info<< "Cleaning up surface" << endl;
139  surf.cleanup(true);
140  surf.writeStats(Info);
141  Info<< endl;
142  }
143 
144  Info<< "writing " << exportName;
145  if (scaleFactor <= 0)
146  {
147  Info<< " without scaling" << endl;
148  }
149  else
150  {
151  Info<< " with scaling " << scaleFactor << endl;
152  surf.scalePoints(scaleFactor);
153  surf.writeStats(Info);
154  Info<< endl;
155  }
156 
157  // write sorted by region
158  surf.write(exportName, true);
159  }
160  else if (args.optionFound("unsorted"))
161  {
162  UnsortedMeshedSurface<face> surf(importName);
163 
164  Info<< "Read surface:" << endl;
165  surf.writeStats(Info);
166  Info<< endl;
167 
168  if (args.optionFound("orient"))
169  {
170  Info<< "Checking surface orientation" << endl;
171  PatchTools::checkOrientation(surf, true);
172  Info<< endl;
173  }
174 
175  if (args.optionFound("clean"))
176  {
177  Info<< "Cleaning up surface" << endl;
178  surf.cleanup(true);
179  surf.writeStats(Info);
180  Info<< endl;
181  }
182 
183  Info<< "writing " << exportName;
184  if (scaleFactor <= 0)
185  {
186  Info<< " without scaling" << endl;
187  }
188  else
189  {
190  Info<< " with scaling " << scaleFactor << endl;
191  surf.scalePoints(scaleFactor);
192  surf.writeStats(Info);
193  Info<< endl;
194  }
195  surf.write(exportName);
196  }
197 #if 1
198  else if (args.optionFound("triFace"))
199  {
200  MeshedSurface<triFace> surf(importName);
201 
202  Info<< "Read surface:" << endl;
203  surf.writeStats(Info);
204  Info<< endl;
205 
206  if (args.optionFound("orient"))
207  {
208  Info<< "Checking surface orientation" << endl;
209  PatchTools::checkOrientation(surf, true);
210  Info<< endl;
211  }
212 
213  if (args.optionFound("clean"))
214  {
215  Info<< "Cleaning up surface" << endl;
216  surf.cleanup(true);
217  surf.writeStats(Info);
218  Info<< endl;
219  }
220 
221  Info<< "writing " << exportName;
222  if (scaleFactor <= 0)
223  {
224  Info<< " without scaling" << endl;
225  }
226  else
227  {
228  Info<< " with scaling " << scaleFactor << endl;
229  surf.scalePoints(scaleFactor);
230  surf.writeStats(Info);
231  Info<< endl;
232  }
233  surf.write(exportName);
234  }
235 #endif
236  else
237  {
238  MeshedSurface<face> surf(importName);
239 
240  Info<< "Read surface:" << endl;
241  surf.writeStats(Info);
242  Info<< endl;
243 
244  if (args.optionFound("orient"))
245  {
246  Info<< "Checking surface orientation" << endl;
247  PatchTools::checkOrientation(surf, true);
248  Info<< endl;
249  }
250 
251  if (args.optionFound("clean"))
252  {
253  Info<< "Cleaning up surface" << endl;
254  surf.cleanup(true);
255  surf.writeStats(Info);
256  Info<< endl;
257  }
258 
259 
260  Info<< "writing " << exportName;
261  if (scaleFactor <= 0)
262  {
263  Info<< " without scaling" << endl;
264  }
265  else
266  {
267  Info<< " with scaling " << scaleFactor << endl;
268  surf.scalePoints(scaleFactor);
269  surf.writeStats(Info);
270  Info<< endl;
271  }
272  surf.write(exportName);
273 
274  if (args.optionFound("surfMesh"))
275  {
276  Foam::Time runTime
277  (
278  args.rootPath(),
279  args.caseName()
280  );
281 
282  // start with "constant"
283  runTime.setTime(instant(0, runTime.constant()), 0);
284 
285  Info<< "runTime.instance() = " << runTime.instance() << endl;
286  Info<< "runTime.timeName() = " << runTime.timeName() << endl;
287 
288 
289  Info<< "write MeshedSurface 'yetAnother' via proxy as surfMesh"
290  << endl;
291  surf.write
292  (
293  runTime,
294  "yetAnother"
295  );
296 
297  surfMesh surfIn
298  (
299  IOobject
300  (
301  "default",
302  runTime.timeName(),
303  runTime,
306  )
307  );
308 
309 
310  MeshedSurface<face> surfIn2(runTime, "foobar");
311 
312  Info<<"surfIn2 = " << surfIn2.size() << endl;
313 
314  Info<< "surfIn = " << surfIn.size() << endl;
315 
316 
317  Info<< "writing surfMesh as obj = oldSurfIn.obj" << endl;
318  surfIn.write("oldSurfIn.obj");
319 
320 
321  Info<< "runTime.instance() = " << runTime.instance() << endl;
322 
323  surfMesh surfOut
324  (
325  IOobject
326  (
327  "mySurf",
328  runTime.instance(),
329  runTime,
332  false
333  ),
334  surf.xfer()
335  );
336 
337  Info<< "writing surfMesh as well: " << surfOut.objectPath() << endl;
338  surfOut.write();
339 
340  surfLabelField zoneIds
341  (
342  IOobject
343  (
344  "zoneIds",
345  surfOut.instance(),
346  surfOut,
349  ),
350  surfOut,
351  dimless
352  );
353 
354  Info<<" surf name= " << surfOut.name() <<nl;
355  Info<< "rename to anotherSurf" << endl;
356  surfOut.rename("anotherSurf");
357 
358  Info<<" surf name= " << surfOut.name() <<nl;
359 
360  // advance time to 1
361  runTime.setTime(instant(1), 1);
362  surfOut.setInstance(runTime.timeName());
363 
364 
365 
366  Info<< "writing surfMesh again well: " << surfOut.objectPath()
367  << endl;
368  surfOut.write();
369 
370  // write directly
371  surfOut.write("someName.ofs");
372 
373 #if 1
374  const surfZoneList& zones = surfOut.surfZones();
375  forAll(zones, zoneI)
376  {
378  (
379  zoneIds,
380  zones[zoneI].size(),
381  zones[zoneI].start()
382  ) = zoneI;
383  }
384 
385  Info<< "write zoneIds (for testing only): "
386  << zoneIds.objectPath() << endl;
387  zoneIds.write();
388 
389  surfPointLabelField pointIds
390  (
391  IOobject
392  (
393  "zoneIds.",
394 // "pointIds",
395  surfOut.instance(),
396 // "pointFields",
397  surfOut,
400  ),
401  surfOut,
402  dimless
403  );
404 
405  forAll(pointIds, i)
406  {
407  pointIds[i] = i;
408  }
409 
410  Info<< "write pointIds (for testing only): "
411  << pointIds.objectPath() << endl;
412  pointIds.write();
413 
414  Info<<"surfMesh with these names: " << surfOut.names() << endl;
415 
416 #endif
417  }
418  }
419 
420  Info<< "\nEnd\n" << endl;
421 
422  return 0;
423 }
424 
425 // ************************************************************************* //
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:74
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
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
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
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
A List obtained as a section of another List.
Definition: SubList.H:53
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static bool checkOrientation(const PrimitivePatch< Face, FaceList, PointField, PointType > &, const bool report=false, labelHashSet *marked=0)
Check for orientation issues.
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
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:924
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:55
static const char nl
Definition: Ostream.H:262
An instant of time. Contains the time value and name.
Definition: instant.H:64
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:47
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:283
messageStream Info
virtual Ostream & write(const token &)=0
Write next token to stream.
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
Triangulated surface description with patch information.
Definition: triSurface.H:65
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.