OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

vtkSurfaceWriter.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "vtkSurfaceWriter.H"
00028 
00029 #include "OFstream.H"
00030 #include "OSspecific.H"
00031 
00032 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00033 
00034 template<class Type>
00035 void Foam::vtkSurfaceWriter<Type>::writeGeometry
00036 (
00037     Ostream& os,
00038     const pointField& points,
00039     const faceList& faces
00040 )
00041 {
00042     // header
00043     os
00044         << "# vtk DataFile Version 2.0" << nl
00045         << "sampleSurface" << nl
00046         << "ASCII" << nl
00047         << "DATASET POLYDATA" << nl;
00048 
00049     // Write vertex coords
00050     os  << "POINTS " << points.size() << " float" << nl;
00051     forAll(points, pointI)
00052     {
00053         const point& pt = points[pointI];
00054         os  << float(pt.x()) << ' '
00055             << float(pt.y()) << ' '
00056             << float(pt.z()) << nl;
00057     }
00058     os  << nl;
00059 
00060 
00061     // Write faces
00062     label nNodes = 0;
00063     forAll(faces, faceI)
00064     {
00065         nNodes += faces[faceI].size();
00066     }
00067 
00068     os  << "POLYGONS " << faces.size() << ' '
00069         << faces.size() + nNodes << nl;
00070 
00071     forAll(faces, faceI)
00072     {
00073         const face& f = faces[faceI];
00074 
00075         os << f.size();
00076         forAll(f, fp)
00077         {
00078             os << ' ' << f[fp];
00079         }
00080         os << nl;
00081     }
00082 }
00083 
00084 
00085 namespace Foam
00086 {
00087 
00088     // Write scalarField in vtk format
00089     template<>
00090     void Foam::vtkSurfaceWriter<Foam::scalar>::writeData
00091     (
00092         Ostream& os,
00093         const Field<Foam::scalar>& values
00094     )
00095     {
00096         os << "1 " << values.size() << " float" << nl;
00097 
00098         forAll(values, elemI)
00099         {
00100             if (elemI)
00101             {
00102                 if (elemI % 10)
00103                 {
00104                     os << ' ';
00105                 }
00106                 else
00107                 {
00108                     os << nl;
00109                 }
00110             }
00111 
00112             const scalar& v = values[elemI];
00113             os << float(v);
00114         }
00115         os << nl;
00116     }
00117 
00118     // Write vectorField in vtk format
00119     template<>
00120     void Foam::vtkSurfaceWriter<Foam::vector>::writeData
00121     (
00122         Ostream& os,
00123         const Field<Foam::vector>& values
00124     )
00125     {
00126         os << "3 " << values.size() << " float" << nl;
00127 
00128         forAll(values, elemI)
00129         {
00130             const vector& v = values[elemI];
00131             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
00132                 << nl;
00133         }
00134     }
00135 
00136 
00137     // Write sphericalTensorField in vtk format
00138     template<>
00139     void Foam::vtkSurfaceWriter<Foam::sphericalTensor>::writeData
00140     (
00141         Ostream& os,
00142         const Field<sphericalTensor>& values
00143     )
00144     {
00145         os << "1 " << values.size() << " float" << nl;
00146 
00147         forAll(values, elemI)
00148         {
00149             const sphericalTensor& v = values[elemI];
00150             os << float(v[0]) << nl;
00151         }
00152     }
00153 
00154 
00155     // Write symmTensorField in vtk format
00156     template<>
00157     void Foam::vtkSurfaceWriter<Foam::symmTensor>::writeData
00158     (
00159         Ostream& os,
00160         const Field<symmTensor>& values
00161     )
00162     {
00163         os << "6 " << values.size() << " float" << nl;
00164 
00165         forAll(values, elemI)
00166         {
00167             const symmTensor& v = values[elemI];
00168             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
00169                 << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
00170                 << nl;
00171 
00172         }
00173     }
00174 
00175 
00176     // Write tensorField in vtk format
00177     template<>
00178     void Foam::vtkSurfaceWriter<Foam::tensor>::writeData
00179     (
00180         Ostream& os,
00181         const Field<tensor>& values
00182     )
00183     {
00184         os << "9 " << values.size() << " float" << nl;
00185 
00186         forAll(values, elemI)
00187         {
00188             const tensor& v = values[elemI];
00189             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
00190                 << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
00191                 << float(v[6]) << ' ' << float(v[7]) << ' ' << float(v[8])
00192                 << nl;
00193         }
00194     }
00195 
00196 }
00197 
00198 
00199 // Write generic field in vtk format
00200 template<class Type>
00201 void Foam::vtkSurfaceWriter<Type>::writeData
00202 (
00203     Ostream& os,
00204     const Field<Type>& values
00205 )
00206 {
00207     os << "1 " << values.size() << " float" << nl;
00208 
00209     forAll(values, elemI)
00210     {
00211         os << float(0) << nl;
00212     }
00213 }
00214 
00215 
00216 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00217 
00218 // Construct from components
00219 template<class Type>
00220 Foam::vtkSurfaceWriter<Type>::vtkSurfaceWriter()
00221 :
00222     surfaceWriter<Type>()
00223 {}
00224 
00225 
00226 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00227 
00228 template<class Type>
00229 Foam::vtkSurfaceWriter<Type>::~vtkSurfaceWriter()
00230 {}
00231 
00232 
00233 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00234 
00235 template<class Type>
00236 void Foam::vtkSurfaceWriter<Type>::write
00237 (
00238     const fileName& outputDir,
00239     const fileName& surfaceName,
00240     const pointField& points,
00241     const faceList& faces,
00242     const bool verbose
00243 ) const
00244 {
00245     if (!isDir(outputDir))
00246     {
00247         mkDir(outputDir);
00248     }
00249 
00250     fileName fName(outputDir/surfaceName + ".vtk");
00251 
00252     if (verbose)
00253     {
00254         Info<< "Writing geometry to " << fName << endl;
00255     }
00256 
00257     OFstream os(fName);
00258     writeGeometry(os, points, faces);
00259 }
00260 
00261 
00262 template<class Type>
00263 void Foam::vtkSurfaceWriter<Type>::write
00264 (
00265     const fileName& outputDir,
00266     const fileName& surfaceName,
00267     const pointField& points,
00268     const faceList& faces,
00269     const fileName& fieldName,
00270     const Field<Type>& values,
00271     const bool verbose
00272 ) const
00273 {
00274     if (!isDir(outputDir))
00275     {
00276         mkDir(outputDir);
00277     }
00278 
00279     OFstream os
00280     (
00281         outputDir/fieldName + '_' + surfaceName + ".vtk"
00282     );
00283 
00284     if (verbose)
00285     {
00286         Info<< "Writing field " << fieldName << " to " << os.name() << endl;
00287     }
00288 
00289     writeGeometry(os, points, faces);
00290 
00291     // start writing data
00292     if (values.size() == points.size())
00293     {
00294         os  << "POINT_DATA ";
00295     }
00296     else
00297     {
00298         os  << "CELL_DATA ";
00299     }
00300 
00301     os  << values.size() << nl
00302         << "FIELD attributes 1" << nl
00303         << fieldName << " ";
00304 
00305     // Write data
00306     writeData(os, values);
00307 
00308 }
00309 
00310 
00311 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd