readTRI.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) 2011-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 Description
25  TRI (triangle) file reader. Comes out of e.g. AC3D.
26  lines are 9 floats (3 points, each 3 floats) followed by hex colour.
27  Is converted into regions: regions numbered from 0 up, each colour is
28  region.
29  Most of reading/stitching taken from STL reader.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #include "triSurface.H"
34 #include "STLpoint.H"
35 #include "SLList.H"
36 #include "IFstream.H"
37 #include "readHexLabel.H"
38 #include "stringList.H"
39 
40 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
41 
42 bool Foam::triSurface::readTRI(const fileName& TRIfileName)
43 {
44  IFstream TRIfile(TRIfileName);
45 
46  if (!TRIfile.good())
47  {
49  << "Cannot read file " << TRIfileName
50  << exit(FatalError);
51  }
52 
53  SLList<STLpoint> STLpoints;
54  SLList<label> STLlabels;
55  HashTable<label, string> STLsolidNames;
56 
57  // Max region number so far
58  label maxRegion = 0;
59 
60  while (TRIfile)
61  {
62  string line = getLineNoComment(TRIfile);
63 
64  if (line.empty())
65  {
66  break;
67  }
68 
69  IStringStream lineStream(line);
70 
71  STLpoint p
72  (
73  readScalar(lineStream),
74  readScalar(lineStream),
75  readScalar(lineStream)
76  );
77 
78  if (!lineStream) break;
79 
80  STLpoints.append(p);
81  STLpoints.append
82  (
83  STLpoint
84  (
85  readScalar(lineStream),
86  readScalar(lineStream),
87  readScalar(lineStream)
88  )
89  );
90  STLpoints.append
91  (
92  STLpoint
93  (
94  readScalar(lineStream),
95  readScalar(lineStream),
96  readScalar(lineStream)
97  )
98  );
99 
100  // Region/colour in .tri file starts with 0x. Skip.
101 
102  char zero;
103  lineStream >> zero;
104 
105  word rawSolidName(lineStream);
106 
107  word solidName("patch" + rawSolidName(1, rawSolidName.size()-1));
108 
109  label region = -1;
110 
112  STLsolidNames.find(solidName);
113 
114  if (fnd != STLsolidNames.end())
115  {
116  region = fnd();
117  }
118  else
119  {
120  Pout<< "Mapping triangle colour 0" << rawSolidName
121  << " to region " << maxRegion << " name " << solidName
122  << endl;
123 
124  region = maxRegion++;
125  STLsolidNames.insert(solidName, region);
126  }
127  STLlabels.append(region);
128  }
129 
130 
131  pointField rawPoints(STLpoints.size());
132 
133  label pointi = 0;
134  forAllConstIter(SLList<STLpoint>, STLpoints, iter)
135  {
136  rawPoints[pointi++] = *iter;
137  }
138 
139  setSize(STLlabels.size());
140 
141  // Assign triangles
142  pointi = 0;
143  SLList<label>::const_iterator iter = STLlabels.begin();
144  forAll(*this, i)
145  {
146  operator[](i)[0] = pointi++;
147  operator[](i)[1] = pointi++;
148  operator[](i)[2] = pointi++;
149  operator[](i).region() = *iter;
150  ++iter;
151  }
152 
153  // Assign coordinates
154  storedPoints().transfer(rawPoints);
155  // Merge duplicate points
156  stitchTriangles();
157 
158  // Convert solidNames into regionNames
159  stringList names(STLsolidNames.toc());
160 
161  patches_.setSize(names.size());
162 
163  forAll(names, nameI)
164  {
165  patches_[nameI].name() = names[nameI];
166  patches_[nameI].geometricType() = "empty";
167  }
168 
169  return true;
170 }
171 
172 
173 // ************************************************************************* //
Non-intrusive singly-linked list.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
friend class const_iterator
Declare friendship with the const_iterator.
Definition: HashTable.H:197
friend class const_iterator
Definition: LList.H:85
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
void setSize(const label)
Reset size of List.
Definition: List.C:281
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
pointField & storedPoints()
Non-const access to global points.
Definition: triSurface.H:225
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
error FatalError
List< string > stringList
A List of strings.
Definition: stringList.H:50
Read a hex label from an input stream.
volScalarField & p