Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / surfMesh / MeshedSurface / MeshedSurface.H
blob36f1b076b37a6769d49be721a0814d3fa582a247
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::MeshedSurface
28 Description
29     A surface geometry mesh with zone information, not to be confused with
30     the similarly named surfaceMesh, which actually refers to the cell faces
31     of a volume mesh.
33     A MeshedSurface can have zero or more surface zones (roughly equivalent
34     to faceZones for a polyMesh). If surface zones are defined, they must
35     be contiguous and cover all of the faces.
37     The MeshedSurface is intended for surfaces from a variety of sources.
38     - A set of points and faces without any surface zone information.
39     - A set of points and faces with randomly ordered zone information.
40       This could arise, for example, from reading external file formats
41       such as STL, etc.
43 SourceFiles
44     MeshedSurface.C
46 \*---------------------------------------------------------------------------*/
48 #ifndef MeshedSurface_H
49 #define MeshedSurface_H
51 #include <OpenFOAM/PrimitivePatch_.H>
52 #include <OpenFOAM/PatchTools.H>
53 #include <OpenFOAM/pointField.H>
54 #include <OpenFOAM/face.H>
55 #include <OpenFOAM/triFace.H>
57 #include <surfMesh/surfZoneList.H>
58 #include <surfMesh/surfaceFormatsCore.H>
59 #include <OpenFOAM/runTimeSelectionTables.H>
60 #include <OpenFOAM/memberFunctionSelectionTables.H>
61 #include <OpenFOAM/HashSet.H>
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 namespace Foam
68 // Forward declaration of friend functions and operators
70 class Time;
71 class surfMesh;
72 class polyBoundaryMesh;
74 template<class Face> class MeshedSurface;
75 template<class Face> class MeshedSurfaceProxy;
76 template<class Face> class UnsortedMeshedSurface;
78 /*---------------------------------------------------------------------------*\
79                       Class MeshedSurface Declaration
80 \*---------------------------------------------------------------------------*/
82 template<class Face>
83 class MeshedSurface
85     public PrimitivePatch<Face, ::Foam::List, pointField, point>,
86     public fileFormats::surfaceFormatsCore
88     // friends - despite different face representationsx
89     template<class Face2> friend class MeshedSurface;
90     template<class Face2> friend class UnsortedMeshedSurface;
91     friend class surfMesh;
93 private:
95     //- Private typedefs for convenience
97         typedef PrimitivePatch
98         <
99             Face,
100             ::Foam::List,
101             pointField,
102             point
103         >
104         ParentType;
106         typedef UnsortedMeshedSurface<Face>  FriendType;
107         typedef MeshedSurfaceProxy<Face>     ProxyType;
109     // Private Member Data
111         //- Zone information
112         // (face ordering nFaces/startFace only used during reading/writing)
113         List<surfZone> zones_;
116     // Private member functions
118 protected:
120     // Protected Member functions
122         //- Transfer points/zones and transcribe face -> triFace
123         void transcribe(MeshedSurface<face>&);
125         //- basic sanity check on zones
126         void checkZones();
128         //- Non-const access to global points
129         pointField& storedPoints()
130         {
131             return const_cast<pointField&>(ParentType::points());
132         }
134         //- Non-const access to the faces
135         List<Face>& storedFaces()
136         {
137             return static_cast<List<Face> &>(*this);
138         }
140         //- Non-const access to the zones
141         surfZoneList& storedZones()
142         {
143             return zones_;
144         }
146         //- sort faces by zones and store sorted faces
147         void sortFacesAndStore
148         (
149             const Xfer< List<Face> >& unsortedFaces,
150             const Xfer< List<label> >& zoneIds,
151             const bool sorted
152         );
154         //- Set new zones from faceMap
155         virtual void remapFaces(const UList<label>& faceMap);
157 public:
159         //- Runtime type information
160         ClassName("MeshedSurface");
162     // Static
164         //- Face storage only handles triangulated faces
165         inline static bool isTri();
167         //- Can we read this file format?
168         static bool canRead(const fileName&, const bool verbose=false);
170         //- Can we read this file format?
171         static bool canReadType(const word& ext, const bool verbose=false);
173         //- Can we write this file format?
174         static bool canWriteType(const word& ext, const bool verbose=false);
176         static wordHashSet readTypes();
177         static wordHashSet writeTypes();
179     // Constructors
181         //- Construct null
182         MeshedSurface();
184         //- Construct by transferring components (points, faces, zones).
185         MeshedSurface
186         (
187             const Xfer< pointField >&,
188             const Xfer< List<Face> >&,
189             const Xfer< surfZoneList >&
190         );
192         //- Construct by transferring components (points, faces).
193         //  Use zone information if available
194         MeshedSurface
195         (
196             const Xfer< pointField >&,
197             const Xfer< List<Face> >&,
198             const UList<label>& zoneSizes = UList<label>(),
199             const UList<word>& zoneNames = UList<word>()
200         );
202         //- Construct as copy
203         MeshedSurface(const MeshedSurface&);
205         //- Construct from a UnsortedMeshedSurface
206         MeshedSurface(const UnsortedMeshedSurface<Face>&);
208         //- Construct from a boundary mesh with local points/faces
209         MeshedSurface
210         (
211             const polyBoundaryMesh&,
212             const bool globalPoints=false
213         );
215         //- Construct from a surfMesh
216         MeshedSurface(const surfMesh&);
218         //- Construct by transferring the contents from a UnsortedMeshedSurface
219         MeshedSurface(const Xfer<UnsortedMeshedSurface<Face> >&);
221         //- Construct by transferring the contents from a MeshedSurface
222         MeshedSurface(const Xfer<MeshedSurface<Face> >&);
224         //- Construct from file name (uses extension to determine type)
225         MeshedSurface(const fileName&);
227         //- Construct from file name (uses extension to determine type)
228         MeshedSurface(const fileName&, const word& ext);
230         //- Construct from database
231         MeshedSurface(const Time&, const word& surfName="");
233     // Declare run-time constructor selection table
235         declareRunTimeSelectionTable
236         (
237             autoPtr,
238             MeshedSurface,
239             fileExtension,
240             (
241                 const fileName& name
242             ),
243             (name)
244         );
246     // Selectors
248         //- Select constructed from filename (explicit extension)
249         static autoPtr<MeshedSurface> New
250         (
251             const fileName&,
252             const word& ext
253         );
255         //- Select constructed from filename (implicit extension)
256         static autoPtr<MeshedSurface> New(const fileName&);
258     // Destructor
260         virtual ~MeshedSurface();
263     // Member Function Selectors
265         declareMemberFunctionSelectionTable
266         (
267             void,
268             UnsortedMeshedSurface,
269             write,
270             fileExtension,
271             (
272                 const fileName& name,
273                 const MeshedSurface<Face>& surf
274             ),
275             (name, surf)
276         );
278         //- Write to file
279         static void write(const fileName&, const MeshedSurface<Face>&);
282     // Member Functions
284     // Access
286         //- The surface size is the number of faces
287         label size() const
288         {
289             return ParentType::size();
290         }
292         //- Return const access to the faces
293         inline const List<Face>& faces() const
294         {
295             return static_cast<const List<Face> &>(*this);
296         }
298         //- Const access to the surface zones.
299         //  If zones are defined, they must be contiguous and cover the entire
300         //  surface.
301         const List<surfZone>& surfZones() const
302         {
303             return zones_;
304         }
306         //- Add surface zones
307         virtual void addZones
308         (
309             const UList<surfZone>&,
310             const bool cullEmpty=false
311         );
313         //- Add surface zones
314         virtual void addZones
315         (
316             const UList<label>& sizes,
317             const UList<word>& names,
318             const bool cullEmpty=false
319         );
321         //- Add surface zones
322         virtual void addZones
323         (
324             const UList<label>& sizes,
325             const bool cullEmpty=false
326         );
328         //- Remove surface zones
329         virtual void removeZones();
332     // Edit
334         //- Clear all storage
335         virtual void clear();
337         //- Move points
338         virtual void movePoints(const pointField&);
340         //- Scale points. A non-positive factor is ignored
341         virtual void scalePoints(const scalar&);
343         //- Reset primitive data (points, faces and zones)
344         //  Note, optimized to avoid overwriting data (with Xfer::null)
345         virtual void reset
346         (
347             const Xfer< pointField >& points,
348             const Xfer< List<Face> >& faces,
349             const Xfer< surfZoneList >& zones
350         );
352         //- Reset primitive data (points, faces and zones)
353         //  Note, optimized to avoid overwriting data (with Xfer::null)
354         virtual void reset
355         (
356             const Xfer< List<point> >& points,
357             const Xfer< List<Face> >& faces,
358             const Xfer< surfZoneList >& zones
359         );
361         //- Remove invalid faces
362         virtual void cleanup(const bool verbose);
364         virtual bool stitchFaces
365         (
366             const scalar tol=SMALL,
367             const bool verbose=false
368         );
370         virtual bool checkFaces
371         (
372             const bool verbose=false
373         );
375         //- Triangulate in-place, returning the number of triangles added
376         virtual label triangulate();
378         //- Triangulate in-place, returning the number of triangles added
379         //  and setting a map of original face Ids.
380         //  The faceMap is zero-sized when no triangulation was done.
381         virtual label triangulate(List<label>& faceMap);
384         //- Return new surface.
385         //  Returns return pointMap, faceMap from subsetMeshMap
386         MeshedSurface subsetMesh
387         (
388             const labelHashSet& include,
389             labelList& pointMap,
390             labelList& faceMap
391         ) const;
393         //- Return new surface.
394         MeshedSurface subsetMesh
395         (
396             const labelHashSet& include
397         ) const;
399         //- Transfer the contents of the argument and annull the argument
400         void transfer(MeshedSurface<Face>&);
402         //- Transfer the contents of the argument and annull the argument
403         void transfer(UnsortedMeshedSurface<Face>&);
405         //- Transfer contents to the Xfer container
406         Xfer< MeshedSurface<Face> > xfer();
408     // Read
410         //- Read from file. Chooses reader based on explicit extension
411         bool read(const fileName&, const word& ext);
413         //- Read from file. Chooses reader based on detected extension
414         virtual bool read(const fileName&);
417     // Write
419         void writeStats(Ostream& os) const;
421         //- Generic write routine. Chooses writer based on extension.
422         virtual void write(const fileName& name) const
423         {
424             write(name, *this);
425         }
427         //- Write to database
428         void write(const Time&, const word& surfName="") const;
431     // Member operators
433         void operator=(const MeshedSurface<Face>&);
435         //- Conversion operator to MeshedSurfaceProxy
436         operator MeshedSurfaceProxy<Face>() const;
441 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
443 //- Specialization for holding triangulated information
444 template<>
445 inline bool MeshedSurface<triFace>::isTri()
447     return true;
450 //- Specialization for holding triangulated information
451 template<>
452 inline label MeshedSurface<triFace>::triangulate()
454     return 0;
457 //- Specialization for holding triangulated information
458 template<>
459 inline label MeshedSurface<triFace>::triangulate(List<label>& faceMap)
461     if (&faceMap)
462     {
463         faceMap.clear();
464     }
466     return 0;
469 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
471 } // End namespace Foam
473 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
475 #ifdef NoRepository
476 #   include "MeshedSurface.C"
477 #endif
479 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
481 #endif
483 // ************************ vim: set sw=4 sts=4 et: ************************ //