initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / triSurface / triSurfaceTools / triSurfaceTools.H
blobb88d460b77bbc934438df741bc351375eab62ce4
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::triSurfaceTools
28 Description
29     A collection of tools for triSurfaceMesh
31 SourceFiles
32     triSurfaceTools.C
34 \*---------------------------------------------------------------------------*/
36 #ifndef triSurfaceTools_H
37 #define triSurfaceTools_H
39 #include "boolList.H"
40 #include "pointField.H"
41 #include "DynamicList.H"
42 #include "HashSet.H"
43 #include "FixedList.H"
44 #include "vector2D.H"
45 #include "triPointRef.H"
46 #include "surfaceLocation.H"
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 namespace Foam
53 // Forward declaration of classes
54 class triSurface;
55 class edge;
56 class labelledTri;
57 class polyBoundaryMesh;
58 class plane;
60 /*---------------------------------------------------------------------------*\
61                            Class triSurfaceTools Declaration
62 \*---------------------------------------------------------------------------*/
64 class triSurfaceTools
66     // Private Member Functions
68         // Refinement
70             enum refineType
71             {
72                 NONE,
73                 RED,
74                 GREEN
75             };
76             static void calcRefineStatus
77             (
78                 const triSurface& surf,
79                 const label faceI,
80                 List<refineType>& refine
81             );
82             static void greenRefine
83             (
84                 const triSurface& surf,
85                 const label faceI,
86                 const label edgeI,
87                 const label newPointI,
88                 DynamicList<labelledTri>& newFaces
89             );
90             static triSurface doRefine
91             (
92                 const triSurface& surf,
93                 const List<refineType>& refineStatus
94             );
97         // Coarsening
99             static scalar faceCosAngle
100             (
101                 const point& pStart,
102                 const point& pEnd,
103                 const point& pLeft,
104                 const point& pRight
105             );
107             static void protectNeighbours
108             (
109                 const triSurface& surf,
110                 const label vertI,
111                 labelList& faceStatus
112             );
114             //- faces to collapse because of edge collapse
115             static labelHashSet getCollapsedFaces
116             (
117                 const triSurface& surf,
118                 label edgeI
119             );
121             // Return value of faceUsed for faces using vertI (local numbering).
122             // Used internally.
123             static label vertexUsesFace
124             (
125                 const triSurface& surf,
126                 const labelHashSet& faceUsed,
127                 const label vertI
128             );
130             // Get new connections between faces (because of edge collapse)
131             // in form of tables:
132             //  - given edge get other edge
133             //  - given edge get other face
134             // A face using point v1 on edge will get connected to a face using
135             //  point v2 if they share a common vertex
136             //  (but not a common edge since then the triangles collapse to
137             //  nothing)
138             static void getMergedEdges
139             (
140                 const triSurface& surf,
141                 const label edgeI,
142                 const labelHashSet& collapsedFaces,
143                 HashTable<label, label, Hash<label> >& edgeToEdge,
144                 HashTable<label, label, Hash<label> >& edgeToFace
145             );
147             //- Calculates (cos of) angle across edgeI of faceI,
148             //  taking into account updated addressing (resulting from edge
149             //  collapse)
150             static scalar edgeCosAngle
151             (
152                 const triSurface& surf,
153                 const label v1,
154                 const point& pt,
155                 const labelHashSet& collapsedFaces,
156                 const HashTable<label, label, Hash<label> >& edgeToEdge,
157                 const HashTable<label, label, Hash<label> >& edgeToFace,
158                 const label faceI,
159                 const label edgeI
160             );
162             //- Calculate minimum (cos of) edge angle using addressing from
163             /// collapsing
164             //  edge to v1 at pt. Returns 1 if v1 is on edge without neighbours
165             //  (and hence no edge angle can be defined)
166             static scalar collapseMinCosAngle
167             (
168                 const triSurface& surf,
169                 const label v1,
170                 const point& pt,
171                 const labelHashSet& collapsedFaces,
172                 const HashTable<label, label, Hash<label> >& edgeToEdge,
173                 const HashTable<label, label, Hash<label> >& edgeToFace
174             );
176             //- Like collapseMinCosAngle but return true for value < minCos
177             bool collapseCreatesFold
178             (
179                 const triSurface& surf,
180                 const label v1,
181                 const point& pt,
182                 const labelHashSet& collapsedFaces,
183                 const HashTable<label, label, Hash<label> >& edgeToEdge,
184                 const HashTable<label, label, Hash<label> >& edgeToFace,
185                 const scalar minCos
186             );
188             ////- Checks if edge collapse creates triangles on top of each
189             ////  other
190             //static bool collapseCreatesDuplicates
191             //(
192             //    const triSurface& surf,
193             //    const label edgeI,
194             //    const HashTable<bool, label, Hash<label> >& collapsedFaces
195             //);
197         // Tracking
199             //- Finds the triangle edge/point cut by the plane between
200             //  a point inside/on edge of a triangle and a point outside.
201             //  Returns
202             //  - location on edge/point and hit()
203             //  - or miss() if no intersection found
204             static surfaceLocation cutEdge
205             (
206                 const triSurface& s,
207                 const label triI,
208                 const label excludeEdgeI,
209                 const label excludePointI,
210                 const point& triPoint,
211                 const plane& cutPlane,
212                 const point& toPoint
213             );
215             //- Checks if current is on the same triangle as the endpoint
216             //  and shifts it there. If so updates current and sets a hit.
217             static void snapToEnd
218             (
219                 const triSurface& s,
220                 const surfaceLocation& endInfo,
221                 surfaceLocation& current
222             );
224             //- Visits faces eFaces around start. Does not visit triangle
225             //  start.triangle() nor edge excludeEdgeI.
226             //  Returns edge, triangle (if more than one choice) which gets
227             //  us nearer endpoint.
228             //  Returns
229             //  - hit() if triangle contains endpoint
230             //  - triangle()=-1 if no triangle found
231             //  - nearest triangle/edge otherwise
232             static surfaceLocation visitFaces
233             (
234                 const triSurface& s,
235                 const labelList& eFaces,
236                 const surfaceLocation& start,
237                 const label excludeEdgeI,
238                 const label excludePointI,
239                 const surfaceLocation& end,
240                 const plane& cutPlane
241             );
244 public:
246     // OBJ writing
248         //- Write pointField to OBJ format file
249         static void writeOBJ
250         (
251             const fileName& fName,
252             const pointField& pts
253         );
255         //- Write vertex subset to OBJ format file
256         static void writeOBJ
257         (
258             const triSurface& surf,
259             const fileName& fName,
260             const boolList& markedVerts
261         );
264     // Additional addressing
266         //- Get all triangles using edge endpoint
267         static void getVertexTriangles
268         (
269             const triSurface& surf,
270             const label edgeI,
271             labelList& edgeTris
272         );
274         //- Get all vertices (local numbering) connected to vertices of edge
275         static labelList getVertexVertices
276         (
277             const triSurface& surf,
278             const edge& e
279         );
281         ////- Order vertices consistent with face
282         //static void orderVertices
283         //(
284         //    const labelledTri& f,
285         //    const label v1,
286         //    const label v2,
287         //    label& vA,
288         //    label& vB
289         //);
291         //- Get face connected to edge not faceI
292         static label otherFace
293         (
294             const triSurface& surf,
295             const label faceI,
296             const label edgeI
297         );
299         //- Get the two edges on faceI counterclockwise after edgeI
300         static void otherEdges
301         (
302             const triSurface& surf,
303             const label faceI,
304             const label edgeI,
305             label& e1,
306             label& e2
307         );
309         //- Get the two vertices (local numbering) on faceI counterclockwise
310         //  vertI
311         static void otherVertices
312         (
313             const triSurface& surf,
314             const label faceI,
315             const label vertI,
316             label& vert1I,
317             label& vert2I
318         );
320         //- Get edge opposite vertex (local numbering)
321         static label oppositeEdge
322         (
323             const triSurface& surf,
324             const label faceI,
325             const label vertI
326         );
328         //- Get vertex (local numbering) opposite edge
329         static label oppositeVertex
330         (
331             const triSurface& surf,
332             const label faceI,
333             const label edgeI
334         );
336         //- Returns edge label connecting v1, v2 (local numbering)
337         static label getEdge
338         (
339             const triSurface& surf,
340             const label vert1I,
341             const label vert2I
342         );
344         //- Return index of triangle (or -1) using all three edges
345         static label getTriangle
346         (
347             const triSurface& surf,
348             const label e0I,
349             const label e1I,
350             const label e2I
351         );
353     // Coarsening
355         //- Create new triSurface by collapsing edges to edge mids.
356         static triSurface collapseEdges
357         (
358             const triSurface& surf,
359             const labelList& collapsableEdges
360         );
363         //- Face collapse status.
364         //  anyEdge: any edge can be collapsed
365         //  noEdge: no edge can be collapsed
366         //  collapsed: already collapsed
367         //  >0: edge label that can be collapsed
368         static const label ANYEDGE;
369         static const label NOEDGE;
370         static const label COLLAPSED;
372         //- Create new triSurface by collapsing edges to specified
373         //  positions. faceStatus allows
374         //  explicit control over which faces need to be protected (see above).
375         //  faceStatus gets updated to protect collapsing already collapsed
376         //  faces.
377         static triSurface collapseEdges
378         (
379             const triSurface& surf,
380             const labelList& collapsableEdges,
381             const pointField& edgeMids,
382             labelList& faceStatus
383         );
386     // Refinement
388         //- Refine edges by splitting to opposite vertex
389         static triSurface greenRefine
390         (
391             const triSurface& surf,
392             const labelList& refineEdges
393         );
395         //- Refine face by splitting all edges. Neighbouring face is
396         //  greenRefine'd.
397         static triSurface redGreenRefine
398         (
399             const triSurface& surf,
400             const labelList& refineFaces
401         );
404     // Geometric
406         //- Returns element in edgeIndices with minimum length
407         static label minEdge
408         (
409             const triSurface& surf,
410             const labelList& edgeIndices
411         );
413         //- Returns element in edgeIndices with minimum length
414         static label maxEdge
415         (
416             const triSurface& surf,
417             const labelList& edgeIndices
418         );
420         //- Merge points within distance
421         static triSurface mergePoints
422         (
423             const triSurface& surf,
424             const scalar mergeTol
425         );
427         //- Triangle (unit) normal. If nearest point to triangle on edge use
428         //  edge normal (calculated on the fly); if on vertex use vertex normal.
429         //  Uses planarTol.
430         static vector surfaceNormal
431         (
432             const triSurface& surf,
433             const label nearestFaceI,
434             const point& nearestPt
435         );
437         //- on which side of surface
438         enum sideType
439         {
440             UNKNOWN,    // cannot be determined (e.g. non-manifold)
441             INSIDE,     // inside
442             OUTSIDE     // outside
443         };
445         //- if nearest point is on edgeI, determine on which side of surface
446         //  sample is.
447         static sideType edgeSide
448         (
449             const triSurface& surf,
450             const point& sample,
451             const point& nearestPoint,
452             const label edgeI
453         );
455         //- Given nearest point (to sample) on surface determines which side
456         //  sample is. Uses either face normal, edge normal or point normal
457         //  (non-trivial). Uses triangle::classify.
458         static sideType surfaceSide
459         (
460             const triSurface& surf,
461             const point& sample,
462             const label nearestFaceI,   // nearest face
463             const point& nearestPt,     // nearest point on nearest face
464             const scalar tol            // tolerance for nearness test.
465         );
467     // Triangulation of faces
469         //- Simple triangulation of (selected patches of) boundaryMesh. Needs
470         //  polyMesh (or polyBoundaryMesh) since only at this level are the
471         //  triangles on neighbouring patches connected.
472         static triSurface triangulate
473         (
474             const polyBoundaryMesh& mBesh,
475             const labelHashSet& includePatches,
476             const bool verbose = false
477         );
479         //- Face-centre triangulation of (selected patches of) boundaryMesh.
480         //  Needs
481         //  polyMesh (or polyBoundaryMesh) since only at this level are the
482         //  triangles on neighbouring patches connected.
483         triSurface triangulateFaceCentre
484         (
485             const polyBoundaryMesh& mBesh,
486             const labelHashSet& includePatches,
487             const bool verbose = false
488         );
491     // Triangulation and interpolation
493         //- Calculate linear interpolation weights for point (guaranteed to be
494         //  inside triangle)
495         static void calcInterpolationWeights
496         (
497             const triPointRef&,
498             const point&,
499             FixedList<scalar, 3>& weights
500         );
502         // Calculate weighting factors from samplePts to triangle it is in.
503         // Uses linear search to find triangle.
504         // Vertices are:
505         //   (a b c)  : vertices of the triangle abc the point is in
506         // or if the point is outside all triangles:
507         //   (a b -1) : the edge ab the point is nearest to.
508         //   (a -1 -1) : the vertex a the point is nearest to
509         static void calcInterpolationWeights
510         (
511             const triSurface& s,
512             const pointField& samplePts,
513             List<FixedList<label, 3> >& verts,
514             List<FixedList<scalar, 3> >& weights
515         );
517         //- Do unconstrained Delaunay of points. Returns triSurface with 3D
518         //  points with z=0. All triangles in region 0.
519         static triSurface delaunay2D(const List<vector2D>&);
522     // Tracking
524         //- Test point on plane of triangle to see if on edge or point
525         //  or inside.
526         static surfaceLocation classify
527         (
528             const triSurface&,
529             const label triI,
530             const point& trianglePoint
531         );
533         //- Track on surface to get closer to point. Possible situations:
534         //  - 1. reached endpoint
535         //  - 2. reached edge (normal situation)
536         //  - 3. reached end of surface (edge on single face)
537         //  Input:
538         //  - starting position+triangle/edge/point (so has to be on surface!)
539         //  - (optional) previous position+triangle/edge/point to prevent
540         //    going back. Set index (of triangle/edge/point) to -1 if not
541         //    used.
542         //  - end position+triangle/edge/point (so has to be on surface!)
543         //  - plane to follow. Has to go through end point!
544         //  Returns:
545         //  - true if end point reached (situation 1)
546         //  - new position+triangle/edge/point
547         //  Caller has to check for situation 3 by checking that triangle()
548         //  is not set.
549         static surfaceLocation trackToEdge
550         (
551             const triSurface&,
552             const surfaceLocation& start,
553             const surfaceLocation& end,
554             const plane& cutPlane
555         );
557         //- Track from edge to edge across surface. Uses trackToEdge.
558         //  Not really useful by itself, more example of how to use trackToEdge.
559         //  endInfo should be location on surface.
560         //  hitInfo should be initialised to starting location (on surface as
561         //  well). Upon return is set to end location.
562         static void track
563         (
564             const triSurface&,
565             const surfaceLocation& endInfo,
566             const plane& cutPlane,
567             surfaceLocation& hitInfo
568         );
572 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
574 } // End namespace Foam
576 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
578 #endif
580 // ************************************************************************* //