X3DsurfaceFormat.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-2021 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 \*---------------------------------------------------------------------------*/
25 
26 #include "X3DsurfaceFormat.H"
27 #include "clock.H"
28 #include "IFstream.H"
29 #include "IStringStream.H"
30 #include "Ostream.H"
31 #include "OFstream.H"
32 #include "ListOps.H"
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 template<class Face>
38 {}
39 
40 
41 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
42 
43 template<class Face>
45 (
46  const fileName& filename,
47  const MeshedSurfaceProxy<Face>& surf
48 )
49 {
50  const pointField& pointLst = surf.points();
51  const List<Face>& faceLst = surf.faces();
52  const List<label>& faceMap = surf.faceMap();
53 
54  // for no zones, suppress the group name
55  const List<surfZone>& zones =
56  (
57  surf.surfZones().empty()
58  ? surfaceFormatsCore::oneZone(faceLst, "")
59  : surf.surfZones()
60  );
61 
62  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
63 
64  OFstream os(filename);
65  if (!os.good())
66  {
68  << "Cannot open file for writing " << filename
69  << exit(FatalError);
70  }
71 
72  writeHeader(os);
73 
74  os << "\n"
75  "<Group>\n"
76  " <Shape>\n";
77 
78  writeAppearance(os);
79 
80 
81  // NOTE: we could provide an optimised IndexedTriangleSet output for
82  // triangulated surfaces too
83 
84  os <<
85  " <IndexedFaceSet coordIndex='\n";
86 
87  label faceIndex = 0;
88  forAll(zones, zoneI)
89  {
90  const surfZone& zone = zones[zoneI];
91 
92  if (useFaceMap)
93  {
94  forAll(zone, localFacei)
95  {
96  const Face& f = faceLst[faceMap[faceIndex++]];
97 
98  forAll(f, fp)
99  {
100  os << f[fp] << ' ';
101  }
102  os << "-1\n";
103  }
104  }
105  else
106  {
107  forAll(zone, localFacei)
108  {
109  const Face& f = faceLst[faceIndex++];
110 
111  forAll(f, fp)
112  {
113  os << f[fp] << ' ';
114  }
115  os << "-1\n";
116  }
117  }
118  }
119 
120  os <<
121  "' >\n"
122  " <Coordinate point='\n";
123 
124  // Write vertex coords
125  forAll(pointLst, ptI)
126  {
127  const point& pt = pointLst[ptI];
128 
129  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
130  }
131 
132  os <<
133  "' />\n" // end Coordinate
134  " </IndexedFaceSet>\n"
135  " </Shape>\n"
136  " </Group>\n"
137  "</X3D>\n";
138 
139 }
140 
141 
142 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Output to file stream.
Definition: OFstream.H:82
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
const Cmpt & z() const
Definition: VectorI.H:87
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
const Cmpt & y() const
Definition: VectorI.H:81
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Various functions to operate on Lists.
bool useFaceMap() const
Use faceMap?
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
Base class for zones.
Definition: zone.H:57
const List< Face > & faces() const
Return const access to the faces.
const List< surfZone > & surfZones() const
Const access to the surface zones.
const Cmpt & x() const
Definition: VectorI.H:75
static const char nl
Definition: Ostream.H:260
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
labelList f(nPoints)
const pointField & points() const
Return const access to the points.
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.