FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "vtkSurfaceWriter.H"
00027 
00028 #include <OpenFOAM/OFstream.H>
00029 #include <OpenFOAM/OSspecific.H>
00030 
00031 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00032 
00033 template<class Type>
00034 void Foam::vtkSurfaceWriter<Type>::writeGeometry
00035 (
00036     Ostream& os,
00037     const pointField& points,
00038     const faceList& faces
00039 )
00040 {
00041     // header
00042     os
00043         << "# vtk DataFile Version 2.0" << nl
00044         << "sampleSurface" << nl
00045         << "ASCII" << nl
00046         << "DATASET POLYDATA" << nl;
00047 
00048     // Write vertex coords
00049     os  << "POINTS " << points.size() << " float" << nl;
00050     forAll(points, pointI)
00051     {
00052         const point& pt = points[pointI];
00053         os  << float(pt.x()) << ' '
00054             << float(pt.y()) << ' '
00055             << float(pt.z()) << nl;
00056     }
00057     os  << nl;
00058 
00059 
00060     // Write faces
00061     label nNodes = 0;
00062     forAll(faces, faceI)
00063     {
00064         nNodes += faces[faceI].size();
00065     }
00066 
00067     os  << "POLYGONS " << faces.size() << ' '
00068         << faces.size() + nNodes << nl;
00069 
00070     forAll(faces, faceI)
00071     {
00072         const face& f = faces[faceI];
00073 
00074         os << f.size();
00075         forAll(f, fp)
00076         {
00077             os << ' ' << f[fp];
00078         }
00079         os << nl;
00080     }
00081 }
00082 
00083 
00084 namespace Foam
00085 {
00086 
00087     // Write scalarField in vtk format
00088     template<>
00089     void Foam::vtkSurfaceWriter<Foam::scalar>::writeData
00090     (
00091         Ostream& os,
00092         const Field<Foam::scalar>& values
00093     )
00094     {
00095         os << "1 " << values.size() << " float" << nl;
00096 
00097         forAll(values, elemI)
00098         {
00099             if (elemI)
00100             {
00101                 if (elemI % 10)
00102                 {
00103                     os << ' ';
00104                 }
00105                 else
00106                 {
00107                     os << nl;
00108                 }
00109             }
00110 
00111             const scalar& v = values[elemI];
00112             os << float(v);
00113         }
00114         os << nl;
00115     }
00116 
00117     // Write vectorField in vtk format
00118     template<>
00119     void Foam::vtkSurfaceWriter<Foam::vector>::writeData
00120     (
00121         Ostream& os,
00122         const Field<Foam::vector>& values
00123     )
00124     {
00125         os << "3 " << values.size() << " float" << nl;
00126 
00127         forAll(values, elemI)
00128         {
00129             const vector& v = values[elemI];
00130             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
00131                 << nl;
00132         }
00133     }
00134 
00135 
00136     // Write sphericalTensorField in vtk format
00137     template<>
00138     void Foam::vtkSurfaceWriter<Foam::sphericalTensor>::writeData
00139     (
00140         Ostream& os,
00141         const Field<sphericalTensor>& values
00142     )
00143     {
00144         os << "1 " << values.size() << " float" << nl;
00145 
00146         forAll(values, elemI)
00147         {
00148             const sphericalTensor& v = values[elemI];
00149             os << float(v[0]) << nl;
00150         }
00151     }
00152 
00153 
00154     // Write symmTensorField in vtk format
00155     template<>
00156     void Foam::vtkSurfaceWriter<Foam::symmTensor>::writeData
00157     (
00158         Ostream& os,
00159         const Field<symmTensor>& values
00160     )
00161     {
00162         os << "6 " << values.size() << " float" << nl;
00163 
00164         forAll(values, elemI)
00165         {
00166             const symmTensor& v = values[elemI];
00167             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
00168                 << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
00169                 << nl;
00170 
00171         }
00172     }
00173 
00174 
00175     // Write tensorField in vtk format
00176     template<>
00177     void Foam::vtkSurfaceWriter<Foam::tensor>::writeData
00178     (
00179         Ostream& os,
00180         const Field<tensor>& values
00181     )
00182     {
00183         os << "9 " << values.size() << " float" << nl;
00184 
00185         forAll(values, elemI)
00186         {
00187             const tensor& v = values[elemI];
00188             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
00189                 << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
00190                 << float(v[6]) << ' ' << float(v[7]) << ' ' << float(v[8])
00191                 << nl;
00192         }
00193     }
00194 
00195 }
00196 
00197 
00198 // Write generic field in vtk format
00199 template<class Type>
00200 void Foam::vtkSurfaceWriter<Type>::writeData
00201 (
00202     Ostream& os,
00203     const Field<Type>& values
00204 )
00205 {
00206     os << "1 " << values.size() << " float" << nl;
00207 
00208     forAll(values, elemI)
00209     {
00210         os << float(0) << nl;
00211     }
00212 }
00213 
00214 
00215 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00216 
00217 // Construct from components
00218 template<class Type>
00219 Foam::vtkSurfaceWriter<Type>::vtkSurfaceWriter()
00220 :
00221     surfaceWriter<Type>()
00222 {}
00223 
00224 
00225 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00226 
00227 template<class Type>
00228 Foam::vtkSurfaceWriter<Type>::~vtkSurfaceWriter()
00229 {}
00230 
00231 
00232 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00233 
00234 template<class Type>
00235 void Foam::vtkSurfaceWriter<Type>::write
00236 (
00237     const fileName& outputDir,
00238     const fileName& surfaceName,
00239     const pointField& points,
00240     const faceList& faces,
00241     const bool verbose
00242 ) const
00243 {
00244     if (!isDir(outputDir))
00245     {
00246         mkDir(outputDir);
00247     }
00248 
00249     fileName fName(outputDir/surfaceName + ".vtk");
00250 
00251     if (verbose)
00252     {
00253         Info<< "Writing geometry to " << fName << endl;
00254     }
00255 
00256     OFstream os(fName);
00257     writeGeometry(os, points, faces);
00258 }
00259 
00260 
00261 template<class Type>
00262 void Foam::vtkSurfaceWriter<Type>::write
00263 (
00264     const fileName& outputDir,
00265     const fileName& surfaceName,
00266     const pointField& points,
00267     const faceList& faces,
00268     const fileName& fieldName,
00269     const Field<Type>& values,
00270     const bool verbose
00271 ) const
00272 {
00273     if (!isDir(outputDir))
00274     {
00275         mkDir(outputDir);
00276     }
00277 
00278     OFstream os
00279     (
00280         outputDir/fieldName + '_' + surfaceName + ".vtk"
00281     );
00282 
00283     if (verbose)
00284     {
00285         Info<< "Writing field " << fieldName << " to " << os.name() << endl;
00286     }
00287 
00288     writeGeometry(os, points, faces);
00289 
00290     // start writing data
00291     if (values.size() == points.size())
00292     {
00293         os  << "POINT_DATA ";
00294     }
00295     else
00296     {
00297         os  << "CELL_DATA ";
00298     }
00299 
00300     os  << values.size() << nl
00301         << "FIELD attributes 1" << nl
00302         << fieldName.c_str() << " ";
00303 
00304     // Write data
00305     writeData(os, values);
00306 
00307 }
00308 
00309 
00310 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines