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