OpenFOAM logo
Open Source CFD Toolkit

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