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-2017 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("surface file");
82  argList::validArgs.append("output surface file");
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 
319  using Foam::surfMesh;
320  surfIn.write(fileName("oldSurfIn.obj"));
321 
322 
323  Info<< "runTime.instance() = " << runTime.instance() << endl;
324 
325  surfMesh surfOut
326  (
327  IOobject
328  (
329  "mySurf",
330  runTime.instance(),
331  runTime,
334  false
335  ),
336  surf.xfer()
337  );
338 
339  Info<< "writing surfMesh as well: " << surfOut.objectPath() << endl;
340  surfOut.write();
341 
342  surfLabelField zoneIds
343  (
344  IOobject
345  (
346  "zoneIds",
347  surfOut.instance(),
348  surfOut,
351  ),
352  surfOut,
353  dimless
354  );
355 
356  Info<<" surf name= " << surfOut.name() <<nl;
357  Info<< "rename to anotherSurf" << endl;
358  surfOut.rename("anotherSurf");
359 
360  Info<<" surf name= " << surfOut.name() <<nl;
361 
362  // advance time to 1
363  runTime.setTime(instant(1), 1);
364  surfOut.setInstance(runTime.timeName());
365 
366 
367 
368  Info<< "writing surfMesh again well: " << surfOut.objectPath()
369  << endl;
370  surfOut.write();
371 
372  // write directly
373  surfOut.surfMesh::write(fileName("someName.ofs"));
374 
375 #if 1
376  const surfZoneList& zones = surfOut.surfZones();
377  forAll(zones, zoneI)
378  {
380  (
381  zoneIds,
382  zones[zoneI].size(),
383  zones[zoneI].start()
384  ) = zoneI;
385  }
386 
387  Info<< "write zoneIds (for testing only): "
388  << zoneIds.objectPath() << endl;
389  zoneIds.write();
390 
391  surfPointLabelField pointIds
392  (
393  IOobject
394  (
395  "zoneIds.",
396 // "pointIds",
397  surfOut.instance(),
398 // "pointFields",
399  surfOut,
402  ),
403  surfOut,
404  dimless
405  );
406 
407  forAll(pointIds, i)
408  {
409  pointIds[i] = i;
410  }
411 
412  Info<< "write pointIds (for testing only): "
413  << pointIds.objectPath() << endl;
414  pointIds.write();
415 
416  Info<<"surfMesh with these names: " << surfOut.names() << endl;
417 
418 #endif
419  }
420  }
421 
422  Info<< "\nEnd\n" << endl;
423 
424  return 0;
425 }
426 
427 // ************************************************************************* //
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:60
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static void noParallel()
Remove the parallel options.
Definition: argList.C:148
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
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:428
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:284
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
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:95
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:856
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:262
An instant of time. Contains the time value and name.
Definition: instant.H:66
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:85
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:126
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:92
Namespace for OpenFOAM.