X3DsurfaceFormat.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) 2011 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  (
69  "fileFormats::X3DsurfaceFormat::write"
70  "(const fileName&, const MeshedSurfaceProxy<Face>&)"
71  )
72  << "Cannot open file for writing " << filename
73  << exit(FatalError);
74  }
75 
76  writeHeader(os);
77 
78  os << "\n"
79  "<Group>\n"
80  " <Shape>\n";
81 
82  writeAppearance(os);
83 
84 
85  // NOTE: we could provide an optimized IndexedTriangleSet output for
86  // triangulated surfaces too
87 
88  os <<
89  " <IndexedFaceSet coordIndex='\n";
90 
91  label faceIndex = 0;
92  forAll(zones, zoneI)
93  {
94  const surfZone& zone = zones[zoneI];
95 
96  if (useFaceMap)
97  {
98  forAll(zone, localFaceI)
99  {
100  const Face& f = faceLst[faceMap[faceIndex++]];
101 
102  forAll(f, fp)
103  {
104  os << f[fp] << ' ';
105  }
106  os << "-1\n";
107  }
108  }
109  else
110  {
111  forAll(zone, localFaceI)
112  {
113  const Face& f = faceLst[faceIndex++];
114 
115  forAll(f, fp)
116  {
117  os << f[fp] << ' ';
118  }
119  os << "-1\n";
120  }
121  }
122  }
123 
124  os <<
125  "' >\n"
126  " <Coordinate point='\n";
127 
128  // Write vertex coords
129  forAll(pointLst, ptI)
130  {
131  const point& pt = pointLst[ptI];
132 
133  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
134  }
135 
136  os <<
137  "' />\n" // end Coordinate
138  " </IndexedFaceSet>\n"
139  " </Shape>\n"
140  " </Group>\n"
141  "</X3D>\n";
142 
143 }
144 
145 
146 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
bool useFaceMap() const
Use faceMap?
labelList f(nPoints)
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Various functions to operate on Lists.
const pointField & points() const
Return const access to the points.
const List< Face > & faces() const
Return const access to the faces.
static const char nl
Definition: Ostream.H:260
const Cmpt & y() const
Definition: VectorI.H:71
Base class for zones.
Definition: zone.H:57
const List< surfZone > & surfZones() const
Const access to the surface zones.
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
const Cmpt & x() const
Definition: VectorI.H:65
const Cmpt & z() const
Definition: VectorI.H:77
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
A class for handling file names.
Definition: fileName.H:69
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.