OBJstream.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) 2012-2020 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 \*---------------------------------------------------------------------------*/
25 
26 #include "OBJstream.H"
27 #include "primitivePatch.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(OBJstream, 0);
34 }
35 
36 
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 
39 void Foam::OBJstream::writeAndCheck(const char c)
40 {
41  if (c == '\n')
42  {
43  startOfLine_ = true;
44  }
45  else if (startOfLine_)
46  {
47  startOfLine_ = false;
48  if (c == 'v')
49  {
50  nVertices_++;
51  }
52  }
53  OFstream::write(c);
54 }
55 
56 
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58 
60 (
61  const fileName& filePath,
62  streamFormat format,
63  versionNumber version,
64  compressionType compression
65 )
66 :
67  OFstream(filePath, format, version, compression),
68  startOfLine_(true),
69  nVertices_(0)
70 {}
71 
72 
73 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
74 
76 {}
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 {
83  writeAndCheck(c);
84  return *this;
85 }
86 
87 
89 {
90  for (const char* p = str; *p != '\0'; ++p)
91  {
92  writeAndCheck(*p);
93  }
94  return *this;
95 }
96 
97 
99 {
100  write(str.c_str());
101  return *this;
102 }
103 
104 
106 {
108 
109  int backslash = 0;
110  for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
111  {
112  char c = *iter;
113 
114  if (c == '\\')
115  {
116  backslash++;
117  // suppress output until we know if other characters follow
118  continue;
119  }
120  else if (c == token::NL)
121  {
122  lineNumber_++;
123  backslash++; // backslash escape for newline
124  }
125  else if (c == token::END_STRING)
126  {
127  backslash++; // backslash escape for quote
128  }
129 
130  // output pending backslashes
131  while (backslash)
132  {
133  OFstream::write('\\');
134  backslash--;
135  }
136 
137  writeAndCheck(c);
138  }
139 
140  // silently drop any trailing backslashes
141  // they would otherwise appear like an escaped end-quote
142 
144  return *this;
145 }
146 
147 
149 (
150  const std::string& str,
151  const bool quoted
152 )
153 {
154  if (quoted)
155  {
157 
158  int backslash = 0;
159  for
160  (
161  string::const_iterator iter = str.begin();
162  iter != str.end();
163  ++iter
164  )
165  {
166  char c = *iter;
167 
168  if (c == '\\')
169  {
170  backslash++;
171  // suppress output until we know if other characters follow
172  continue;
173  }
174  else if (c == token::NL)
175  {
176  lineNumber_++;
177  backslash++; // backslash escape for newline
178  }
179  else if (c == token::END_STRING)
180  {
181  backslash++; // backslash escape for quote
182  }
183 
184  // output pending backslashes
185  while (backslash)
186  {
187  OFstream::write('\\');
188  backslash--;
189  }
190 
191  writeAndCheck(c);
192  }
193 
194  // silently drop any trailing backslashes
195  // they would otherwise appear like an escaped end-quote
197  }
198  else
199  {
200  // output unquoted string, only advance line number on newline
201  write(str.c_str());
202  }
203 
204  return *this;
205 }
206 
207 
209 {
210  write("v ") << pt.x() << ' ' << pt.y() << ' ' << pt.z()
211  << nl;
212  return *this;
213 }
214 
215 
217 {
218  write(pt);
219  OFstream::write("vn ") << n.x() << ' ' << n.y()
220  << ' ' << n.z() << nl;
221  return *this;
222 }
223 
224 
226 {
227  write(points[e[0]]);
228  write(points[e[1]]);
229  write("l ") << nVertices_-1 << ' ' << nVertices_ << nl;
230  return *this;
231 }
232 
233 
235 {
236  write(ln.start());
237  write(ln.end());
238  write("l ") << nVertices_-1 << ' ' << nVertices_ << nl;
239  return *this;
240 }
241 
242 
244 (
245  const linePointRef& ln,
246  const vector& n0,
247  const vector& n1
248 )
249 {
250  write(ln.start(), n0);
251  write(ln.end(), n1);
252  write("l ") << nVertices_-1 << ' ' << nVertices_ << nl;
253  return *this;
254 }
255 
256 
258 (
259  const triPointRef& f,
260  const bool lines
261 )
262 {
263  label start = nVertices_;
264  write(f.a());
265  write(f.b());
266  write(f.c());
267  if (lines)
268  {
269  write('l');
270  for (int i = 0; i < 3; i++)
271  {
272  write(' ') << start+1+i;
273  }
274  write(' ') << start+1 << '\n';
275  }
276  else
277  {
278  write('f');
279  for (int i = 0; i < 3; i++)
280  {
281  write(' ') << start+1+i;
282  }
283  write('\n');
284  }
285  return *this;
286 }
287 
288 
290 (
291  const face& f,
292  const UList<point>& points,
293  const bool lines
294 )
295 {
296  label start = nVertices_;
297  forAll(f, i)
298  {
299  write(points[f[i]]);
300  }
301  if (lines)
302  {
303  write('l');
304  forAll(f, i)
305  {
306  write(' ') << start+1+i;
307  }
308  write(' ') << start+1 << '\n';
309  }
310  else
311  {
312  write('f');
313  forAll(f, i)
314  {
315  write(' ') << start+1+i;
316  }
317  write('\n');
318  }
319  return *this;
320 }
321 
322 
324 (
325  const faceList& fcs,
326  const pointField& points,
327  const bool lines
328 )
329 {
330  SubList<face> allFcs(fcs, fcs.size());
331 
332  primitivePatch pp(allFcs, points);
333 
334  const pointField& localPoints = pp.localPoints();
335  const faceList& localFaces = pp.localFaces();
336 
337  label start = nVertices_;
338 
339  forAll(localPoints, i)
340  {
341  write(localPoints[i]);
342  }
343 
344  if (lines)
345  {
346  const edgeList& edges = pp.edges();
347  forAll(edges, edgeI)
348  {
349  const edge& e = edges[edgeI];
350 
351  write("l ") << start+e[0]+1 << ' ' << start+e[1]+1 << nl;
352  }
353  }
354  else
355  {
356  forAll(localFaces, facei)
357  {
358  const face& f = localFaces[facei];
359  write('f');
360  forAll(f, i)
361  {
362  write(' ') << start+f[i]+1;
363  }
364  write('\n');
365  }
366  }
367  return *this;
368 }
369 
370 
371 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A triangle primitive used to calculate face areas and swept volumes.
Definition: triangle.H:57
A line primitive.
Definition: line.H:56
A class for handling file names.
Definition: fileName.H:79
const Point & c() const
Return third vertex.
Definition: triangleI.H:82
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
const Cmpt & z() const
Definition: VectorI.H:87
virtual Ostream & write(const char)
Write character.
Definition: OBJstream.C:81
const Cmpt & y() const
Definition: VectorI.H:81
A list of faces which address into the list of points.
A List obtained as a section of another List.
Definition: SubList.H:53
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
A class for handling words, derived from string.
Definition: word.H:59
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
PointRef start() const
Return first vertex.
Definition: lineI.H:60
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
const Cmpt & x() const
Definition: VectorI.H:75
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
defineTypeNameAndDebug(combustionModel, 0)
virtual Ostream & write(const char)
Write character.
Definition: OSstream.C:32
const Point & a() const
Return first vertex.
Definition: triangleI.H:70
const Point & b() const
Return second vertex.
Definition: triangleI.H:76
PointRef end() const
Return second vertex.
Definition: lineI.H:66
Version number type.
Definition: IOstream.H:96
label n
~OBJstream()
Destructor.
Definition: OBJstream.C:75
OBJstream(const fileName &filePath, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct from filePath.
Definition: OBJstream.C:60
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
volScalarField & p
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OBJstream.C:149
Namespace for OpenFOAM.