Loading...
Searching...
No Matches
TRIsurfaceFormat.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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2023 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "TRIsurfaceFormat.H"
30#include "TRIReader.H"
31#include "OFstream.H"
32#include "ListOps.H"
33
34// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35
36template<class Face>
37inline void Foam::fileFormats::TRIsurfaceFormat<Face>::writeShell
38(
39 Ostream& os,
40 const UList<point>& pts,
41 const Face& f,
42 const label zoneI
43)
44{
45 // simple triangulation about f[0].
46 // better triangulation should have been done before
47 const point& p0 = pts[f[0]];
48 for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
49 {
50 const label fp2 = f.fcIndex(fp1);
51
52 const point& p1 = pts[f[fp1]];
53 const point& p2 = pts[f[fp2]];
54
55 os << p0.x() << ' ' << p0.y() << ' ' << p0.z() << ' '
56 << p1.x() << ' ' << p1.y() << ' ' << p1.z() << ' '
57 << p2.x() << ' ' << p2.y() << ' ' << p2.z() << ' '
58 // zone as colour
59 << "0x" << hex << zoneI << dec << nl;
60 }
61}
62
63
64// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
65
66template<class Face>
68(
69 const fileName& filename
70)
71{
72 read(filename);
73}
74
75
76// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77
78template<class Face>
80(
81 const fileName& filename
82)
83{
84 // Clear everything
85 this->clear();
86
87 // Read in the values
88 TRIReader reader(filename);
89
90 // Get the map for stitched surface points
91 labelList pointMap;
92 const label nUniquePoints = reader.mergePointsMap(pointMap);
93
94 const auto& readpts = reader.points();
95
96 // Assign points
97 pointField& pointLst = this->storedPoints();
98 pointLst.setSize(nUniquePoints);
99 forAll(readpts, pointi)
100 {
101 pointLst[pointMap[pointi]] = readpts[pointi];
102 }
103
104 // Retrieve the original zone information
105 List<label> sizes(std::move(reader.sizes()));
106 List<label> zoneIds(std::move(reader.zoneIds()));
107
108 // Generate the (sorted) faces
109 List<Face> faceLst(zoneIds.size());
110
111 if (reader.is_sorted())
112 {
113 // Already sorted - generate directly
114 forAll(faceLst, facei)
115 {
116 const label startPt = 3*facei;
117 faceLst[facei] = Face
118 {
119 pointMap[startPt],
120 pointMap[startPt+1],
121 pointMap[startPt+2]
122 };
123 }
124 }
125 else
126 {
127 // Determine the sorted order:
128 // use sortedOrder directly (the intermediate list is discared anyhow)
129 labelList faceMap(sortedOrder(zoneIds));
130
131 // Generate sorted faces
132 forAll(faceMap, facei)
133 {
134 const label startPt = 3*faceMap[facei];
135 faceLst[facei] = Face
136 {
137 pointMap[startPt],
138 pointMap[startPt+1],
139 pointMap[startPt+2]
140 };
141 }
142 }
143 zoneIds.clear();
144
145 // Transfer
146 this->storedFaces().transfer(faceLst);
147
148 this->addZones(sizes);
149 this->addZonesToFaces(); // for labelledTri
150
151 return true;
152}
153
154
155template<class Face>
157(
158 const fileName& filename,
159 const MeshedSurfaceProxy<Face>& surf,
160 IOstreamOption streamOpt,
161 const dictionary&
162)
163{
164 // ASCII only, allow output compression
165 streamOpt.format(IOstreamOption::ASCII);
166
167 const UList<point>& pointLst = surf.points();
168 const UList<Face>& faceLst = surf.surfFaces();
169 const UList<label>& faceMap = surf.faceMap();
170
171 const surfZoneList zones
172 (
173 surf.surfZones().empty()
175 : surf.surfZones()
176 );
177
178 const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
179
180 OFstream os(filename, streamOpt);
181 if (!os.good())
182 {
184 << "Cannot write file " << filename << nl
185 << exit(FatalError);
186 }
187
188 label faceIndex = 0;
189 label zoneIndex = 0;
190 for (const surfZone& zone : zones)
191 {
192 for (label nLocal = zone.size(); nLocal--; ++faceIndex)
193 {
194 const label facei =
195 (useFaceMap ? faceMap[faceIndex] : faceIndex);
196
197 const Face& f = faceLst[facei];
198
199 writeShell(os, pointLst, f, zoneIndex);
200 }
202 ++zoneIndex;
203 }
204}
205
206
207template<class Face>
209(
210 const fileName& filename,
211 const UnsortedMeshedSurface<Face>& surf,
212 IOstreamOption streamOpt,
213 const dictionary&
214)
215{
216 // ASCII only, allow output compression
217 streamOpt.format(IOstreamOption::ASCII);
218
219 const UList<point>& pointLst = surf.points();
220 const UList<Face>& faceLst = surf.surfFaces();
221
222 OFstream os(filename, streamOpt);
223 if (!os.good())
224 {
226 << "Cannot write file " << filename << nl
227 << exit(FatalError);
228 }
229
230 // A single zone needs no sorting
231 if (surf.zoneToc().size() == 1)
232 {
233 const UList<label>& zoneIds = surf.zoneIds();
234
235 forAll(faceLst, facei)
236 {
237 writeShell(os, pointLst, faceLst[facei], zoneIds[facei]);
238 }
239 }
240 else
241 {
243 List<surfZone> zoneLst = surf.sortedZones(faceMap);
244
245 label faceIndex = 0;
246 label zoneIndex = 0;
247
248 for (const surfZone& zone : zoneLst)
249 {
250 for (label nLocal = zone.size(); nLocal--; ++faceIndex)
251 {
252 const label facei = faceMap[faceIndex];
253
254 const Face& f = faceLst[facei];
255
256 writeShell(os, pointLst, f, zoneIndex);
257 }
258
259 ++zoneIndex;
260 }
261 }
262}
263
264
265// ************************************************************************* //
Various functions to operate on Lists.
A simple container for options an IOstream can normally have.
streamFormat format() const noexcept
Get the current stream format.
@ ASCII
"ascii" (normal default)
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void setSize(label n)
Alias for resize().
Definition List.H:536
void clear()
Clear the list, i.e. set size to zero.
Definition ListI.H:133
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
const UList< surfZone > & surfZones() const noexcept
Const access to the surface zones.
const UList< Face > & surfFaces() const noexcept
Return const access to the faces.
const labelUList & faceMap() const noexcept
Const access to the faceMap, zero-sized when unused.
const pointField & points() const noexcept
Return const access to the points.
bool useFaceMap() const noexcept
Can/should use faceMap?
pointField & storedPoints()
Non-const access to global points.
virtual void addZones(const UList< surfZone > &, const bool cullEmpty=false)
Add surface zones.
const List< Face > & surfFaces() const
Return const access to the faces.
List< Face > & storedFaces()
Non-const access to the faces.
friend class UnsortedMeshedSurface
bool addZonesToFaces()
Propagate zone information on face regions.
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
const Field< point_type > & points() const noexcept
Return reference to global points.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
surfZoneList sortedZones(labelList &faceMap) const
Sort faces according to zoneIds.
virtual const labelList & zoneIds() const
Return const access to the zone ids.
const List< surfZoneIdentifier > & zoneToc() const
Return const access to the zone table-of-contents.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
TRI (triangle) file reader.
Definition TRIReader.H:60
label mergePointsMap(labelList &pointMap) const
Calculate merge points mapping, return old to new pointMap.
Definition TRIReader.C:188
List< STLpoint > & points() noexcept
Return full access to the points.
Definition TRIReader.H:138
bool is_sorted() const noexcept
File read was already in sorted order.
Definition TRIReader.H:133
List< label > & sizes() noexcept
The list of zone sizes in the order of their first appearance.
Definition TRIReader.H:153
List< label > & zoneIds() noexcept
Return full access to the zones.
Definition TRIReader.H:143
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &=dictionary::null)
Write surface mesh components (by proxy) in TRI format.
virtual bool read(const fileName &filename) override
Read from file.
TRIsurfaceFormat(const fileName &filename)
Construct from file name.
static List< surfZone > oneZone(const Container &container, const word &name="zone0")
Return a surfZone list with a single entry, the size of which corresponds to that of the container.
A class for handling file names.
Definition fileName.H:75
A surface zone on a MeshedSurface.
Definition surfZone.H:55
Base class for mesh zones.
Definition zone.H:63
const volScalarField & p0
Definition EEqn.H:36
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
surface1 clear()
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
List< label > labelList
A List of labels.
Definition List.H:62
List< surfZone > surfZoneList
List of surfZone.
IOstream & hex(IOstream &io)
Definition IOstream.H:579
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
IOstream & dec(IOstream &io)
Definition IOstream.H:573
vector point
Point is a vector.
Definition point.H:37
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
vectorField pointField
pointField is a vectorField.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
labelList f(nPoints)
const pointField & pts
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299