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

triSurface.H

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 Class
00025     Foam::triSurface
00026 
00027 Description
00028     Triangulated surface description with patch information.
00029 
00030 SourceFiles
00031     triSurface.C
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #ifndef triSurface_H
00036 #define triSurface_H
00037 
00038 #include <OpenFOAM/PrimitivePatch_.H>
00039 #include <OpenFOAM/pointField.H>
00040 #include <triSurface/labelledTri.H>
00041 #include <OpenFOAM/boolList.H>
00042 #include <triSurface/geometricSurfacePatchList.H>
00043 #include <triSurface/surfacePatchList.H>
00044 #include <OpenFOAM/triFaceList.H>
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 namespace Foam
00049 {
00050 
00051 class Time;
00052 class IFstream;
00053 
00054 /*---------------------------------------------------------------------------*\
00055                            Class triSurface Declaration
00056 \*---------------------------------------------------------------------------*/
00057 
00058 class triSurface
00059 :
00060     public PrimitivePatch<labelledTri, ::Foam::List, pointField, point>
00061 {
00062     // Private typedefs
00063 
00064     //- Typedefs for convenience
00065         typedef labelledTri Face;
00066         typedef PrimitivePatch
00067         <
00068             labelledTri,
00069             ::Foam::List,
00070             pointField,
00071             point
00072         >
00073         ParentType;
00074 
00075 
00076     // Private data
00077 
00078         //- The number of bytes in the STL header
00079         static const int STLheaderSize = 80;
00080 
00081         //- Patch information (face ordering nFaces/startFace only used
00082         //  during reading and writing)
00083         geometricSurfacePatchList patches_;
00084 
00085 
00086     // Demand driven private data.
00087 
00088         //- Edge-face addressing (sorted)
00089         mutable labelListList* sortedEdgeFacesPtr_;
00090 
00091         //- Label of face that 'owns' edge (i.e. e.vec() is righthanded walk
00092         //  along face)
00093         mutable labelList* edgeOwnerPtr_;
00094 
00095 
00096     // Private member functions
00097 
00098         //- Calculate sorted edgeFaces
00099         void calcSortedEdgeFaces() const;
00100 
00101         //- Calculate owner
00102         void calcEdgeOwner() const;
00103 
00104         //- Sort faces according to region. Returns patch list
00105         //  and sets faceMap to index of labelledTri inside *this.
00106         surfacePatchList calcPatches(labelList& faceMap) const;
00107 
00108         //- Sets default values for patches
00109         void setDefaultPatches();
00110 
00111         //- Function to stitch the triangles by removing duplicate points.
00112         //  Returns true if any points merged
00113         bool stitchTriangles
00114         (
00115             const pointField& rawPoints,
00116             const scalar tol = SMALL,
00117             const bool verbose = false
00118         );
00119 
00120         //- Read in Foam format
00121         bool read(Istream&);
00122 
00123         //- Generic read routine. Chooses reader based on extension.
00124         bool read(const fileName&, const word& ext, const bool check = true);
00125 
00126         bool readSTL(const fileName&);
00127         bool readSTLASCII(const fileName&);
00128         bool readSTLBINARY(const fileName&);
00129         bool readGTS(const fileName&);
00130         bool readOBJ(const fileName&);
00131         bool readOFF(const fileName&);
00132         bool readTRI(const fileName&);
00133         bool readAC(const fileName&);
00134         bool readNAS(const fileName&);
00135 
00136         //- Generic write routine. Chooses writer based on extension.
00137         void write(const fileName&, const word& ext, const bool sort) const;
00138 
00139         //- Write to Ostream in ASCII STL format.
00140         //  Each region becomes 'solid' 'endsolid' block.
00141         void writeSTLASCII(Ostream&) const;
00142 
00143         //- Write to std::ostream in BINARY STL format
00144         void writeSTLBINARY(std::ostream&) const;
00145 
00146         //- Write to Ostream in GTS (Gnu Tri Surface library)
00147         //  format.
00148         void writeGTS(const bool writeSorted, Ostream&) const;
00149 
00150         //- Write to Ostream in OBJ (Lightwave) format.
00151         //  writeSorted=true: sort faces acc. to region and write as single
00152         //  group. =false: write in normal order.
00153         void writeOBJ(const bool writeSorted, Ostream&) const;
00154 
00155         //- Write to Ostream in OFF (Geomview) format.
00156         //  writeSorted=true: sort faces acc. to region and write as single
00157         //  group. =false: write in normal order.
00158         void writeOFF(const bool writeSorted, Ostream&) const;
00159 
00160         //- Write to VTK legacy format.
00161         void writeVTK(const bool writeSorted, Ostream&) const;
00162 
00163         //- Write to Ostream in TRI (AC3D) format
00164         //  Ac3d .tri format (unmerged triangle format)
00165         void writeTRI(const bool writeSorted, Ostream&) const;
00166 
00167         //- Write to Ostream in SMESH (tetgen) format
00168         void writeSMESH(const bool writeSorted, Ostream&) const;
00169 
00170         //- Write to Ostream in AC3D format. Always sorted by patch.
00171         void writeAC(Ostream&) const;
00172 
00173         //- For DX writing.
00174         void writeDX(const bool, Ostream&) const;
00175         void writeDXGeometry(const bool, Ostream&) const;
00176         void writeDXTrailer(Ostream&) const;
00177 
00178     // Static private functions
00179 
00180         //- Convert faces to labelledTri. All get same region.
00181         static List<labelledTri> convertToTri
00182         (
00183             const faceList&,
00184             const label defaultRegion = 0
00185         );
00186 
00187         //- Convert triFaces to labelledTri. All get same region.
00188         static List<labelledTri> convertToTri
00189         (
00190             const triFaceList&,
00191             const label defaultRegion = 0
00192         );
00193 
00194         //- helper function to print triangle info
00195         static void printTriangle
00196         (
00197             Ostream&,
00198             const Foam::string& pre,
00199             const labelledTri&,
00200             const pointField&
00201         );
00202 
00203         //- read non-comment line
00204         static string getLineNoComment(IFstream&);
00205 
00206 protected:
00207 
00208     // Protected Member Functions
00209 
00210         //- Non-const access to global points
00211         pointField& storedPoints()
00212         {
00213             return const_cast<pointField&>(ParentType::points());
00214         }
00215 
00216         //- Non-const access to the faces
00217         List<Face>& storedFaces()
00218         {
00219             return static_cast<List<Face>&>(*this);
00220         }
00221 
00222 public:
00223 
00224     // Public typedefs
00225 
00226         //- Placeholder only, but do not remove - it is needed for GeoMesh
00227         typedef bool BoundaryMesh;
00228 
00229 
00230         //- Runtime type information
00231         ClassName("triSurface");
00232 
00233 
00234     // Static
00235 
00236         //- Name of triSurface directory to use.
00237         static fileName triSurfInstance(const Time&);
00238 
00239 
00240     // Constructors
00241 
00242         //- Construct null
00243         triSurface();
00244 
00245         //- Construct from triangles, patches, points.
00246         triSurface
00247         (
00248             const List<labelledTri>&,
00249             const geometricSurfacePatchList&,
00250             const pointField&
00251         );
00252 
00253         //- Construct from triangles, patches, points. Reuse storage.
00254         triSurface
00255         (
00256             List<labelledTri>&,
00257             const geometricSurfacePatchList&,
00258             pointField&,
00259             const bool reUse
00260         );
00261 
00262         //- Construct from triangles, points. Set patchnames to default.
00263         triSurface(const List<labelledTri>&, const pointField&);
00264 
00265         //- Construct from triangles, points. Set region to 0 and default
00266         //  patchName.
00267         triSurface(const triFaceList&, const pointField&);
00268 
00269         //- Construct from file name (uses extension to determine type)
00270         triSurface(const fileName&);
00271 
00272         //- Construct from Istream
00273         triSurface(Istream&);
00274 
00275         //- Construct from objectRegistry
00276         triSurface(const Time& d);
00277 
00278         //- Construct as copy
00279         triSurface(const triSurface&);
00280 
00281 
00282 
00283     // Destructor
00284 
00285         ~triSurface();
00286 
00287         void clearOut();
00288 
00289         void clearTopology();
00290 
00291         void clearPatchMeshAddr();
00292 
00293 
00294     // Member Functions
00295 
00296         // Access
00297 
00298             const geometricSurfacePatchList& patches() const
00299             {
00300                 return patches_;
00301             }
00302 
00303             geometricSurfacePatchList& patches()
00304             {
00305                 return patches_;
00306             }
00307 
00308             //- Return edge-face addressing sorted (for edges with more than
00309             //  2 faces) according to the angle around the edge.
00310             //  Orientation is anticlockwise looking from
00311             //  edge.vec(localPoints())
00312             const labelListList& sortedEdgeFaces() const;
00313 
00314             //- If 2 face neighbours: label of face where ordering of edge
00315             //  is consistent with righthand walk.
00316             //  If 1 neighbour: label of only face.
00317             //  If >2 neighbours: undetermined.
00318             const labelList& edgeOwner() const;
00319 
00320         // Edit
00321 
00322             //- Move points
00323             virtual void movePoints(const pointField&);
00324 
00325             //- Scale points. A non-positive factor is ignored
00326             virtual void scalePoints(const scalar&);
00327 
00328             //- Check/remove duplicate/degenerate triangles
00329             void checkTriangles(const bool verbose);
00330 
00331             //- Check triply (or more) connected edges.
00332             void checkEdges(const bool verbose);
00333 
00334             //- Remove non-valid triangles
00335             void cleanup(const bool verbose);
00336 
00337             //- Fill faceZone with currentZone for every face reachable
00338             //  from faceI without crossing edge marked in borderEdge.
00339             //  Note: faceZone has to be sized nFaces before calling this fun.
00340             void markZone
00341             (
00342                 const boolList& borderEdge,
00343                 const label faceI,
00344                 const label currentZone,
00345                 labelList& faceZone
00346             ) const;
00347 
00348             //- (size and) fills faceZone with zone of face. Zone is area
00349             //  reachable by edge crossing without crossing borderEdge
00350             //  (bool for every edge in surface). Returns number of zones.
00351             label markZones
00352             (
00353                 const boolList& borderEdge,
00354                 labelList& faceZone
00355             ) const;
00356 
00357             //- 'Create' sub mesh, including only faces for which
00358             //  boolList entry is true
00359             //  Sets: pointMap: from new to old localPoints
00360             //        faceMap: new to old faces
00361             void subsetMeshMap
00362             (
00363                 const boolList& include,
00364                 labelList& pointMap,
00365                 labelList& faceMap
00366             ) const;
00367 
00368             //- Return new surface. Returns pointMap, faceMap from
00369             //  subsetMeshMap
00370             triSurface subsetMesh
00371             (
00372                 const boolList& include,
00373                 labelList& pointMap,
00374                 labelList& faceMap
00375             ) const;
00376 
00377         // Write
00378 
00379             //- Write to Ostream in simple FOAM format
00380             void write(Ostream&) const;
00381 
00382             //- Generic write routine. Chooses writer based on extension.
00383             void write(const fileName&, const bool sortByRegion = false) const;
00384 
00385             //- Write to database
00386             void write(const Time&) const;
00387 
00388             //- Write to Ostream in OpenDX format
00389             void writeDX(const scalarField&, Ostream&) const;
00390             void writeDX(const vectorField&, Ostream&) const;
00391 
00392             //- Write some statistics
00393             void writeStats(Ostream&) const;
00394 
00395 
00396     // Member operators
00397 
00398         void operator=(const triSurface&);
00399 
00400 
00401     // Ostream Operator
00402 
00403         friend Ostream& operator<<(Ostream&, const triSurface&);
00404 };
00405 
00406 
00407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00408 
00409 } // End namespace Foam
00410 
00411 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00412 
00413 #endif
00414 
00415 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines