readOBJ.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration | Version: 3.2
5  \\ / A nd | Web: http://www.foam-extend.org
6  \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation, either version 3 of the License, or (at your
14  option) any later version.
15 
16  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurface.H"
29 #include "IFstream.H"
30 #include "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 
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  dynamicLabelList 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  {
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 // ************************************************************************* //
Foam::PrimitivePatch< labelledTri, List, pointField, point >::points
const Field< point > & points() const
Return reference to global points.
Definition: PrimitivePatchTemplate.H:282
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::geometricSurfacePatch
The geometricSurfacePatch is like patchIdentifier but for surfaces. Holds type, name and index.
Definition: geometricSurfacePatch.H:53
Foam::triSurface::readOBJ
bool readOBJ(const fileName &)
Definition: readOBJ.C:39
Foam::HashTable::insert
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
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
IStringStream.H
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::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::triSurface::triSurface
triSurface()
Construct null.
Definition: triSurface.C:608
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
Foam::DynamicList::shrink
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
IFstream.H
Foam::FatalError
error FatalError
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::HashTable::find
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::HashTable< label >
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::HashTable::begin
iterator begin()
Iterator set to the beginning of the HashTable.
Definition: HashTableI.H:417
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::line
A line primitive.
Definition: line.H:56
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
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::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::triSurface::getLineNoComment
static string getLineNoComment(IFstream &)
Read non-comment line.
Definition: triSurface.C:172
y
scalar y
Definition: LISASMDCalcMethod1.H:14