surfaceMeshConvertTesting.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 {
73  #include "removeCaseOptions.H"
74 
76  (
77  "convert between surface formats, "
78  "but primarily for testing functionality\n"
79  "Normally use surfaceMeshConvert instead."
80  );
81 
82  argList::validArgs.append("surface file");
83  argList::validArgs.append("output surface file");
84 
85  argList::addBoolOption("clean");
86  argList::addBoolOption("orient");
87  argList::addBoolOption("surfMesh");
88  argList::addBoolOption("triSurface");
89  argList::addBoolOption("unsorted");
90  argList::addBoolOption("triFace");
91 
93  (
94  "scale",
95  "factor",
96  "geometry scaling factor - default is 1"
97  );
98 
99  #include "setRootCase.H"
100 
101  const scalar scaleFactor = args.optionLookupOrDefault("scale", 0.0);
102 
103  const fileName importName = args[1];
104  const fileName exportName = args[2];
105 
106  if (importName == exportName)
107  {
109  << "Output file " << exportName << " would overwrite input file."
110  << exit(FatalError);
111  }
112 
113  if
114  (
115  !MeshedSurface<face>::canRead(importName, true)
116  || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
117  )
118  {
119  return 1;
120  }
121 
122  if (args.optionFound("triSurface"))
123  {
124  triSurface surf(importName);
125 
126  Info<< "Read surface:" << endl;
127  surf.writeStats(Info);
128  Info<< endl;
129 
130  if (args.optionFound("orient"))
131  {
132  Info<< "Checking surface orientation" << endl;
133  PatchTools::checkOrientation(surf, true);
134  Info<< endl;
135  }
136 
137  if (args.optionFound("clean"))
138  {
139  Info<< "Cleaning up surface" << endl;
140  surf.cleanup(true);
141  surf.writeStats(Info);
142  Info<< endl;
143  }
144 
145  Info<< "writing " << exportName;
146  if (scaleFactor <= 0)
147  {
148  Info<< " without scaling" << endl;
149  }
150  else
151  {
152  Info<< " with scaling " << scaleFactor << endl;
153  surf.scalePoints(scaleFactor);
154  surf.writeStats(Info);
155  Info<< endl;
156  }
157 
158  // write sorted by region
159  surf.write(exportName, true);
160  }
161  else if (args.optionFound("unsorted"))
162  {
163  UnsortedMeshedSurface<face> surf(importName);
164 
165  Info<< "Read surface:" << endl;
166  surf.writeStats(Info);
167  Info<< endl;
168 
169  if (args.optionFound("orient"))
170  {
171  Info<< "Checking surface orientation" << endl;
172  PatchTools::checkOrientation(surf, true);
173  Info<< endl;
174  }
175 
176  if (args.optionFound("clean"))
177  {
178  Info<< "Cleaning up surface" << endl;
179  surf.cleanup(true);
180  surf.writeStats(Info);
181  Info<< endl;
182  }
183 
184  Info<< "writing " << exportName;
185  if (scaleFactor <= 0)
186  {
187  Info<< " without scaling" << endl;
188  }
189  else
190  {
191  Info<< " with scaling " << scaleFactor << endl;
192  surf.scalePoints(scaleFactor);
193  surf.writeStats(Info);
194  Info<< endl;
195  }
196  surf.write(exportName);
197  }
198 #if 1
199  else if (args.optionFound("triFace"))
200  {
201  MeshedSurface<triFace> surf(importName);
202 
203  Info<< "Read surface:" << endl;
204  surf.writeStats(Info);
205  Info<< endl;
206 
207  if (args.optionFound("orient"))
208  {
209  Info<< "Checking surface orientation" << endl;
210  PatchTools::checkOrientation(surf, true);
211  Info<< endl;
212  }
213 
214  if (args.optionFound("clean"))
215  {
216  Info<< "Cleaning up surface" << endl;
217  surf.cleanup(true);
218  surf.writeStats(Info);
219  Info<< endl;
220  }
221 
222  Info<< "writing " << exportName;
223  if (scaleFactor <= 0)
224  {
225  Info<< " without scaling" << endl;
226  }
227  else
228  {
229  Info<< " with scaling " << scaleFactor << endl;
230  surf.scalePoints(scaleFactor);
231  surf.writeStats(Info);
232  Info<< endl;
233  }
234  surf.write(exportName);
235  }
236 #endif
237  else
238  {
239  MeshedSurface<face> surf(importName);
240 
241  Info<< "Read surface:" << endl;
242  surf.writeStats(Info);
243  Info<< endl;
244 
245  if (args.optionFound("orient"))
246  {
247  Info<< "Checking surface orientation" << endl;
248  PatchTools::checkOrientation(surf, true);
249  Info<< endl;
250  }
251 
252  if (args.optionFound("clean"))
253  {
254  Info<< "Cleaning up surface" << endl;
255  surf.cleanup(true);
256  surf.writeStats(Info);
257  Info<< endl;
258  }
259 
260 
261  Info<< "writing " << exportName;
262  if (scaleFactor <= 0)
263  {
264  Info<< " without scaling" << endl;
265  }
266  else
267  {
268  Info<< " with scaling " << scaleFactor << endl;
269  surf.scalePoints(scaleFactor);
270  surf.writeStats(Info);
271  Info<< endl;
272  }
273  surf.write(exportName);
274 
275  if (args.optionFound("surfMesh"))
276  {
278  (
279  args.rootPath(),
280  args.caseName()
281  );
282 
283  // start with "constant"
285 
286  Info<< "runTime.instance() = " << runTime.instance() << endl;
287  Info<< "runTime.timeName() = " << runTime.timeName() << endl;
288 
289 
290  Info<< "write MeshedSurface 'yetAnother' via proxy as surfMesh"
291  << endl;
292  surf.write
293  (
294  runTime,
295  "yetAnother"
296  );
297 
298  surfMesh surfIn
299  (
300  IOobject
301  (
302  "default",
303  runTime.timeName(),
304  runTime,
307  )
308  );
309 
310 
311  MeshedSurface<face> surfIn2(runTime, "foobar");
312 
313  Info<<"surfIn2 = " << surfIn2.size() << endl;
314 
315  Info<< "surfIn = " << surfIn.size() << endl;
316 
317 
318  Info<< "writing surfMesh as obj = oldSurfIn.obj" << endl;
319 
320  using Foam::surfMesh;
321  surfIn.write(fileName("oldSurfIn.obj"));
322 
323 
324  Info<< "runTime.instance() = " << runTime.instance() << endl;
325 
326  surfMesh surfOut
327  (
328  IOobject
329  (
330  "mySurf",
331  runTime.instance(),
332  runTime,
335  false
336  ),
337  move(surf)
338  );
339 
340  Info<< "writing surfMesh as well: " << surfOut.objectPath() << endl;
341  surfOut.write();
342 
343  surfLabelField zoneIds
344  (
345  IOobject
346  (
347  "zoneIds",
348  surfOut.instance(),
349  surfOut,
352  ),
353  surfOut,
354  dimless
355  );
356 
357  Info<<" surf name= " << surfOut.name() <<nl;
358  Info<< "rename to anotherSurf" << endl;
359  surfOut.rename("anotherSurf");
360 
361  Info<<" surf name= " << surfOut.name() <<nl;
362 
363  // advance time to 1
364  runTime.setTime(instant(1), 1);
365  surfOut.setInstance(runTime.timeName());
366 
367 
368 
369  Info<< "writing surfMesh again well: " << surfOut.objectPath()
370  << endl;
371  surfOut.write();
372 
373  // write directly
374  surfOut.surfMesh::write(fileName("someName.ofs"));
375 
376 #if 1
377  const surfZoneList& zones = surfOut.surfZones();
378  forAll(zones, zoneI)
379  {
381  (
382  zoneIds,
383  zones[zoneI].size(),
384  zones[zoneI].start()
385  ) = zoneI;
386  }
387 
388  Info<< "write zoneIds (for testing only): "
389  << zoneIds.objectPath() << endl;
390  zoneIds.write();
391 
392  surfPointLabelField pointIds
393  (
394  IOobject
395  (
396  "zoneIds.",
397 // "pointIds",
398  surfOut.instance(),
399 // "pointFields",
400  surfOut,
403  ),
404  surfOut,
405  dimless
406  );
407 
408  forAll(pointIds, i)
409  {
410  pointIds[i] = i;
411  }
412 
413  Info<< "write pointIds (for testing only): "
414  << pointIds.objectPath() << endl;
415  pointIds.write();
416 
417  Info<<"surfMesh with these names: " << surfOut.names() << endl;
418 
419 #endif
420  }
421  }
422 
423  Info<< "\nEnd\n" << endl;
424 
425  return 0;
426 }
427 
428 // ************************************************************************* //
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:74
static bool checkOrientation(const PrimitivePatch< FaceList, PointField > &, const bool report=false, labelHashSet *marked=0)
Check for orientation issues.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
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
const fileName & rootPath() const
Return root path.
Definition: argListI.H:36
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:163
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:626
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
static void write(const fileName &, const surfMesh &)
Write to file.
Definition: surfMesh.C:410
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:287
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
A List obtained as a section of another List.
Definition: SubList.H:53
const word & constant() const
Return constant name.
Definition: TimePaths.H:124
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:127
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:873
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:55
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
static const char nl
Definition: Ostream.H:265
An instant of time. Contains the time value and name.
Definition: instant.H:66
const fileName & instance() const
Definition: IOobject.H:378
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...
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:117
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:158
Triangulated surface description with patch information.
Definition: triSurface.H:66
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
Namespace for OpenFOAM.