readOBJ.C
Go to the documentation of this file.
1 
2 /*---------------------------------------------------------------------------*\
3  ========= |
4  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5  \\ / O peration |
6  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
7  \\/ M anipulation |
8 -------------------------------------------------------------------------------
9 License
10  This file is part of OpenFOAM.
11 
12  OpenFOAM is free software: you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 
25 \*---------------------------------------------------------------------------*/
26 
27 #include "triSurface.H"
28 #include "IFstream.H"
29 #include "IStringStream.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 bool Foam::triSurface::readOBJ(const fileName& OBJfileName)
34 {
35  IFstream OBJfile(OBJfileName);
36 
37  if (!OBJfile.good())
38  {
40  << "Cannot read file " << OBJfileName
41  << exit(FatalError);
42  }
43 
44  DynamicList<point> points;
45  DynamicList<labelledTri> faces;
46  HashTable<label> groupToPatch;
47 
48  label groupID = 0;
49  label maxGroupID = 0;
50 
51  while (OBJfile.good())
52  {
53  string line = getLineNoComment(OBJfile);
54 
55  label sz = line.size();
56 
57  if (sz)
58  {
59  if (line[sz-1] == '\\')
60  {
61  line.substr(0, sz-1);
62  line += getLineNoComment(OBJfile);
63  }
64 
65  // Read first word
66  IStringStream lineStream(line);
67  word cmd;
68  lineStream >> cmd;
69 
70  if (cmd == "v")
71  {
72  scalar x, y, z;
73 
74  lineStream >> x >> y >> z;
75 
76  points.append(point(x, y, z));
77  }
78  else if (cmd == "g")
79  {
80  word group;
81 
82  lineStream >> group;
83 
85  groupToPatch.find(group);
86 
87  if (findGroup != groupToPatch.end())
88  {
89  groupID = findGroup();
90  }
91  else
92  {
93  groupID = maxGroupID;
94 
95  groupToPatch.insert(group, groupID);
96 
97  maxGroupID++;
98  }
99  }
100  else if (cmd == "f")
101  {
102  DynamicList<label> verts;
103 
104  // Assume 'f' is followed by space.
105  string::size_type endNum = 1;
106 
107  while (true)
108  {
109  string::size_type startNum =
110  line.find_first_not_of(" \r", endNum);
111 
112  if (startNum == string::npos)
113  {
114  break;
115  }
116 
117  endNum = line.find(' ', startNum);
118 
119  string vertexSpec;
120  if (endNum != string::npos)
121  {
122  vertexSpec = line.substr(startNum, endNum-startNum);
123  }
124  else
125  {
126  vertexSpec = line.substr
127  (
128  startNum,
129  line.size() - startNum
130  );
131  }
132 
133  string::size_type slashPos = vertexSpec.find('/');
134 
135  label vertI = 0;
136  if (slashPos != string::npos)
137  {
138  IStringStream intStream(vertexSpec.substr(0, slashPos));
139 
140  intStream >> vertI;
141  }
142  else
143  {
144  IStringStream intStream(vertexSpec);
145 
146  intStream >> vertI;
147  }
148  verts.append(vertI - 1);
149  }
150 
151  verts.shrink();
152 
153  // Do simple face triangulation around f[0].
154  // Cannot use face::triangulation since no complete points yet.
155  for (label fp = 1; fp < verts.size() - 1; fp++)
156  {
157  label fp1 = verts.fcIndex(fp);
158 
159  labelledTri tri(verts[0], verts[fp], verts[fp1], groupID);
160 
161  faces.append(tri);
162  }
163  }
164  }
165  }
166 
167  points.shrink();
168  faces.shrink();
169 
170  // Convert groupToPatch to patchList.
172 
173  if (maxGroupID == 0)
174  {
175  // Generate default patch
176  patches.setSize(1);
177  patches[0] = geometricSurfacePatch("empty", "patch0", 0);
178  }
179  else
180  {
181  forAllConstIter(HashTable<label>, groupToPatch, iter)
182  {
183  patches[iter()] = geometricSurfacePatch
184  (
185  "empty",
186  iter.key(),
187  iter()
188  );
189  }
190  }
191 
192 
193  // Transfer DynamicLists to straight ones.
194  pointField allPoints(points.xfer());
195 
196  // Create triSurface
197  *this = triSurface(faces, patches, allPoints, true);
198 
199  return true;
200 }
201 
202 
203 // ************************************************************************* //
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Foam::PrimitivePatch< labelledTri, List, pointField, point >::points
const Field< point > & points() const
Return reference to global points.
Definition: PrimitivePatchTemplate.H:282
Foam::triSurface::readOBJ
bool readOBJ(const fileName &)
Definition: readOBJ.C:39
Foam::geometricSurfacePatchList
List< geometricSurfacePatch > geometricSurfacePatchList
Definition: geometricSurfacePatchList.H:44
Foam::constant::atomic::group
const char *const group
Group name for atomic constants.
Definition: atomicConstants.C:40
Foam::triSurface::patches
const geometricSurfacePatchList & patches() const
Definition: triSurface.H:301
Foam::HashTable< label >::const_iterator
friend class const_iterator
Declare friendship with the const_iterator.
Definition: HashTable.H:192
IStringStream.H
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::label
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
Foam::triSurface::triSurface
triSurface()
Construct null.
Definition: triSurface.C:608
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
IFstream.H
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::List::setSize
void setSize(const label)
Reset size of List.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::DelaunayMeshTools::allPoints
tmp< pointField > allPoints(const Triangulation &t)
Extract all points in vertex-index order.
Foam::point
vector point
Point is a vector.
Definition: point.H:41
Foam::triSurface::getLineNoComment
static string getLineNoComment(IFstream &)
Read non-comment line.
Definition: triSurface.C:172
y
scalar y
Definition: LISASMDCalcMethod1.H:14