FreeFOAM The Cross-Platform CFD Toolkit
readOBJ.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) 1991-2010 OpenCFD Ltd.
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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include <triSurface/triSurface.H>
29 #include <OpenFOAM/IFstream.H>
30 #include <OpenFOAM/IStringStream.H>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 
39 bool triSurface::readOBJ(const fileName& OBJfileName)
40 {
41  IFstream OBJfile(OBJfileName);
42 
43  if (!OBJfile.good())
44  {
45  FatalErrorIn("triSurface::readOBJ(const fileName&)")
46  << "Cannot read file " << OBJfileName
47  << exit(FatalError);
48  }
49 
50  DynamicList<point> points;
51  DynamicList<labelledTri> faces;
52  HashTable<label> groupToPatch;
53 
54  label groupID = 0;
55  label maxGroupID = 0;
56 
57  while (OBJfile.good())
58  {
59  string line = getLineNoComment(OBJfile);
60 
61  if (line[line.size()-1] == '\\')
62  {
63  line.substr(0, line.size()-1);
64  line += getLineNoComment(OBJfile);
65  }
66 
67  // Read first word
68  IStringStream lineStream(line);
69  word cmd;
70  lineStream >> cmd;
71 
72  if (cmd == "v")
73  {
74  scalar x, y, z;
75 
76  lineStream >> x >> y >> z;
77 
78  points.append(point(x, y, z));
79  }
80  else if (cmd == "g")
81  {
82  word group;
83 
84  lineStream >> group;
85 
87  groupToPatch.find(group);
88 
89  if (findGroup != groupToPatch.end())
90  {
91  groupID = findGroup();
92  }
93  else
94  {
95  groupID = maxGroupID;
96 
97  groupToPatch.insert(group, groupID);
98 
99  maxGroupID++;
100  }
101  }
102  else if (cmd == "f")
103  {
104  DynamicList<label> verts;
105 
106  // Assume 'f' is followed by space.
107  string::size_type endNum = 1;
108 
109  while(true)
110  {
111  string::size_type startNum =
112  line.find_first_not_of(' ', endNum);
113 
114  if (startNum == string::npos)
115  {
116  break;
117  }
118 
119  endNum = line.find(' ', startNum);
120 
121  string vertexSpec;
122  if (endNum != string::npos)
123  {
124  vertexSpec = line.substr(startNum, endNum-startNum);
125  }
126  else
127  {
128  vertexSpec = line.substr(startNum, line.size() - startNum);
129  }
130 
131  string::size_type slashPos = vertexSpec.find('/');
132 
133  label vertI = 0;
134  if (slashPos != string::npos)
135  {
136  IStringStream intStream(vertexSpec.substr(0, slashPos));
137 
138  intStream >> vertI;
139  }
140  else
141  {
142  IStringStream intStream(vertexSpec);
143 
144  intStream >> vertI;
145  }
146  verts.append(vertI - 1);
147  }
148 
149  verts.shrink();
150 
151  // Do simple face triangulation around f[0].
152  // Cannot use face::triangulation since no complete points yet.
153  for (label fp = 1; fp < verts.size() - 1; fp++)
154  {
155  label fp1 = verts.fcIndex(fp);
156 
157  labelledTri tri(verts[0], verts[fp], verts[fp1], groupID);
158 
159  faces.append(tri);
160  }
161  }
162  }
163 
164  points.shrink();
165  faces.shrink();
166 
167  // Convert groupToPatch to patchList.
169 
170  if (maxGroupID == 0)
171  {
172  // Generate default patch
173  patches.setSize(1);
174  patches[0] = geometricSurfacePatch("empty", "patch0", 0);
175  }
176  else
177  {
178  for
179  (
180  HashTable<label>::const_iterator iter = groupToPatch.begin();
181  iter != groupToPatch.end();
182  ++iter
183  )
184  {
185  patches[iter()] = geometricSurfacePatch
186  (
187  "empty",
188  iter.key(),
189  iter()
190  );
191  }
192  }
193 
194 
195  // Transfer DynamicLists to straight ones.
196  pointField allPoints(points.xfer());
197 
198  // Create triSurface
199  *this = triSurface(faces, patches, allPoints, true);
200 
201  return true;
202 }
203 
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 } // End namespace Foam
208 
209 // ************************ vim: set sw=4 sts=4 et: ************************ //