OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

triSurfaceTools.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-2009 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     Foam::triSurfaceTools
00027 
00028 Description
00029     A collection of tools for triSurfaceMesh
00030 
00031 SourceFiles
00032     triSurfaceTools.C
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #ifndef triSurfaceTools_H
00037 #define triSurfaceTools_H
00038 
00039 #include "boolList.H"
00040 #include "pointField.H"
00041 #include "DynamicList.H"
00042 #include "HashSet.H"
00043 #include "FixedList.H"
00044 #include "vector2D.H"
00045 #include "triPointRef.H"
00046 #include "surfaceLocation.H"
00047 
00048 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00049 
00050 namespace Foam
00051 {
00052 
00053 // Forward declaration of classes
00054 class triSurface;
00055 class edge;
00056 class labelledTri;
00057 class polyBoundaryMesh;
00058 class plane;
00059 
00060 /*---------------------------------------------------------------------------*\
00061                            Class triSurfaceTools Declaration
00062 \*---------------------------------------------------------------------------*/
00063 
00064 class triSurfaceTools
00065 {
00066     // Private Member Functions
00067 
00068         // Refinement
00069 
00070             enum refineType
00071             {
00072                 NONE,
00073                 RED,
00074                 GREEN
00075             };
00076             static void calcRefineStatus
00077             (
00078                 const triSurface& surf,
00079                 const label faceI,
00080                 List<refineType>& refine
00081             );
00082             static void greenRefine
00083             (
00084                 const triSurface& surf,
00085                 const label faceI,
00086                 const label edgeI,
00087                 const label newPointI,
00088                 DynamicList<labelledTri>& newFaces
00089             );
00090             static triSurface doRefine
00091             (
00092                 const triSurface& surf,
00093                 const List<refineType>& refineStatus
00094             );
00095 
00096 
00097         // Coarsening
00098 
00099             static scalar faceCosAngle
00100             (
00101                 const point& pStart,
00102                 const point& pEnd,
00103                 const point& pLeft,
00104                 const point& pRight
00105             );
00106 
00107             static void protectNeighbours
00108             (
00109                 const triSurface& surf,
00110                 const label vertI,
00111                 labelList& faceStatus
00112             );
00113 
00114             //- faces to collapse because of edge collapse
00115             static labelHashSet getCollapsedFaces
00116             (
00117                 const triSurface& surf,
00118                 label edgeI
00119             );
00120 
00121             // Return value of faceUsed for faces using vertI (local numbering).
00122             // Used internally.
00123             static label vertexUsesFace
00124             (
00125                 const triSurface& surf,
00126                 const labelHashSet& faceUsed,
00127                 const label vertI
00128             );
00129 
00130             // Get new connections between faces (because of edge collapse)
00131             // in form of tables:
00132             //  - given edge get other edge
00133             //  - given edge get other face
00134             // A face using point v1 on edge will get connected to a face using
00135             //  point v2 if they share a common vertex
00136             //  (but not a common edge since then the triangles collapse to
00137             //  nothing)
00138             static void getMergedEdges
00139             (
00140                 const triSurface& surf,
00141                 const label edgeI,
00142                 const labelHashSet& collapsedFaces,
00143                 HashTable<label, label, Hash<label> >& edgeToEdge,
00144                 HashTable<label, label, Hash<label> >& edgeToFace
00145             );
00146 
00147             //- Calculates (cos of) angle across edgeI of faceI,
00148             //  taking into account updated addressing (resulting from edge
00149             //  collapse)
00150             static scalar edgeCosAngle
00151             (
00152                 const triSurface& surf,
00153                 const label v1,
00154                 const point& pt,
00155                 const labelHashSet& collapsedFaces,
00156                 const HashTable<label, label, Hash<label> >& edgeToEdge,
00157                 const HashTable<label, label, Hash<label> >& edgeToFace,
00158                 const label faceI,
00159                 const label edgeI
00160             );
00161 
00162             //- Calculate minimum (cos of) edge angle using addressing from
00164             //  edge to v1 at pt. Returns 1 if v1 is on edge without neighbours
00165             //  (and hence no edge angle can be defined)
00166             static scalar collapseMinCosAngle
00167             (
00168                 const triSurface& surf,
00169                 const label v1,
00170                 const point& pt,
00171                 const labelHashSet& collapsedFaces,
00172                 const HashTable<label, label, Hash<label> >& edgeToEdge,
00173                 const HashTable<label, label, Hash<label> >& edgeToFace
00174             );
00175 
00176             //- Like collapseMinCosAngle but return true for value < minCos
00177             bool collapseCreatesFold
00178             (
00179                 const triSurface& surf,
00180                 const label v1,
00181                 const point& pt,
00182                 const labelHashSet& collapsedFaces,
00183                 const HashTable<label, label, Hash<label> >& edgeToEdge,
00184                 const HashTable<label, label, Hash<label> >& edgeToFace,
00185                 const scalar minCos
00186             );
00187 
00190             //static bool collapseCreatesDuplicates
00191             //(
00192             //    const triSurface& surf,
00193             //    const label edgeI,
00194             //    const HashTable<bool, label, Hash<label> >& collapsedFaces
00195             //);
00196 
00197         // Tracking
00198 
00199             //- Finds the triangle edge/point cut by the plane between
00200             //  a point inside/on edge of a triangle and a point outside.
00201             //  Returns
00202             //  - location on edge/point and hit()
00203             //  - or miss() if no intersection found
00204             static surfaceLocation cutEdge
00205             (
00206                 const triSurface& s,
00207                 const label triI,
00208                 const label excludeEdgeI,
00209                 const label excludePointI,
00210                 const point& triPoint,
00211                 const plane& cutPlane,
00212                 const point& toPoint
00213             );
00214 
00215             //- Checks if current is on the same triangle as the endpoint
00216             //  and shifts it there. If so updates current and sets a hit.
00217             static void snapToEnd
00218             (
00219                 const triSurface& s,
00220                 const surfaceLocation& endInfo,
00221                 surfaceLocation& current
00222             );
00223 
00224             //- Visits faces eFaces around start. Does not visit triangle
00225             //  start.triangle() nor edge excludeEdgeI.
00226             //  Returns edge, triangle (if more than one choice) which gets
00227             //  us nearer endpoint.
00228             //  Returns
00229             //  - hit() if triangle contains endpoint
00230             //  - triangle()=-1 if no triangle found
00231             //  - nearest triangle/edge otherwise
00232             static surfaceLocation visitFaces
00233             (
00234                 const triSurface& s,
00235                 const labelList& eFaces,
00236                 const surfaceLocation& start,
00237                 const label excludeEdgeI,
00238                 const label excludePointI,
00239                 const surfaceLocation& end,
00240                 const plane& cutPlane
00241             );
00242 
00243 
00244 public:
00245 
00246     // OBJ writing
00247 
00248         //- Write pointField to OBJ format file
00249         static void writeOBJ
00250         (
00251             const fileName& fName,
00252             const pointField& pts
00253         );
00254 
00255         //- Write vertex subset to OBJ format file
00256         static void writeOBJ
00257         (
00258             const triSurface& surf,
00259             const fileName& fName,
00260             const boolList& markedVerts
00261         );
00262 
00263 
00264     // Additional addressing
00265 
00266         //- Get all triangles using edge endpoint
00267         static void getVertexTriangles
00268         (
00269             const triSurface& surf,
00270             const label edgeI,
00271             labelList& edgeTris
00272         );
00273 
00274         //- Get all vertices (local numbering) connected to vertices of edge
00275         static labelList getVertexVertices
00276         (
00277             const triSurface& surf,
00278             const edge& e
00279         );
00280 
00282         //static void orderVertices
00283         //(
00284         //    const labelledTri& f,
00285         //    const label v1,
00286         //    const label v2,
00287         //    label& vA,
00288         //    label& vB
00289         //);
00290 
00291         //- Get face connected to edge not faceI
00292         static label otherFace
00293         (
00294             const triSurface& surf,
00295             const label faceI,
00296             const label edgeI
00297         );
00298 
00299         //- Get the two edges on faceI counterclockwise after edgeI
00300         static void otherEdges
00301         (
00302             const triSurface& surf,
00303             const label faceI,
00304             const label edgeI,
00305             label& e1,
00306             label& e2
00307         );
00308 
00309         //- Get the two vertices (local numbering) on faceI counterclockwise
00310         //  vertI
00311         static void otherVertices
00312         (
00313             const triSurface& surf,
00314             const label faceI,
00315             const label vertI,
00316             label& vert1I,
00317             label& vert2I
00318         );
00319 
00320         //- Get edge opposite vertex (local numbering)
00321         static label oppositeEdge
00322         (
00323             const triSurface& surf,
00324             const label faceI,
00325             const label vertI
00326         );
00327 
00328         //- Get vertex (local numbering) opposite edge
00329         static label oppositeVertex
00330         (
00331             const triSurface& surf,
00332             const label faceI,
00333             const label edgeI
00334         );
00335 
00336         //- Returns edge label connecting v1, v2 (local numbering)
00337         static label getEdge
00338         (
00339             const triSurface& surf,
00340             const label vert1I,
00341             const label vert2I
00342         );
00343 
00344         //- Return index of triangle (or -1) using all three edges
00345         static label getTriangle
00346         (
00347             const triSurface& surf,
00348             const label e0I,
00349             const label e1I,
00350             const label e2I
00351         );
00352 
00353     // Coarsening
00354 
00355         //- Create new triSurface by collapsing edges to edge mids.
00356         static triSurface collapseEdges
00357         (
00358             const triSurface& surf,
00359             const labelList& collapsableEdges
00360         );
00361 
00362 
00363         //- Face collapse status.
00364         //  anyEdge: any edge can be collapsed
00365         //  noEdge: no edge can be collapsed
00366         //  collapsed: already collapsed
00367         //  >0: edge label that can be collapsed
00368         static const label ANYEDGE;
00369         static const label NOEDGE;
00370         static const label COLLAPSED;
00371 
00372         //- Create new triSurface by collapsing edges to specified
00373         //  positions. faceStatus allows
00374         //  explicit control over which faces need to be protected (see above).
00375         //  faceStatus gets updated to protect collapsing already collapsed
00376         //  faces.
00377         static triSurface collapseEdges
00378         (
00379             const triSurface& surf,
00380             const labelList& collapsableEdges,
00381             const pointField& edgeMids,
00382             labelList& faceStatus
00383         );
00384 
00385 
00386     // Refinement
00387 
00388         //- Refine edges by splitting to opposite vertex
00389         static triSurface greenRefine
00390         (
00391             const triSurface& surf,
00392             const labelList& refineEdges
00393         );
00394 
00395         //- Refine face by splitting all edges. Neighbouring face is
00396         //  greenRefine'd.
00397         static triSurface redGreenRefine
00398         (
00399             const triSurface& surf,
00400             const labelList& refineFaces
00401         );
00402 
00403 
00404     // Geometric
00405 
00406         //- Returns element in edgeIndices with minimum length
00407         static label minEdge
00408         (
00409             const triSurface& surf,
00410             const labelList& edgeIndices
00411         );
00412 
00413         //- Returns element in edgeIndices with minimum length
00414         static label maxEdge
00415         (
00416             const triSurface& surf,
00417             const labelList& edgeIndices
00418         );
00419 
00420         //- Merge points within distance
00421         static triSurface mergePoints
00422         (
00423             const triSurface& surf,
00424             const scalar mergeTol
00425         );
00426 
00427         //- Triangle (unit) normal. If nearest point to triangle on edge use
00428         //  edge normal (calculated on the fly); if on vertex use vertex normal.
00429         //  Uses planarTol.
00430         static vector surfaceNormal
00431         (
00432             const triSurface& surf,
00433             const label nearestFaceI,
00434             const point& nearestPt
00435         );
00436 
00437         //- on which side of surface
00438         enum sideType
00439         {
00440             UNKNOWN,    // cannot be determined (e.g. non-manifold)
00441             INSIDE,     // inside
00442             OUTSIDE     // outside
00443         };
00444 
00445         //- if nearest point is on edgeI, determine on which side of surface
00446         //  sample is.
00447         static sideType edgeSide
00448         (
00449             const triSurface& surf,
00450             const point& sample,
00451             const point& nearestPoint,
00452             const label edgeI
00453         );
00454 
00455         //- Given nearest point (to sample) on surface determines which side
00456         //  sample is. Uses either face normal, edge normal or point normal
00457         //  (non-trivial). Uses triangle::classify.
00458         static sideType surfaceSide
00459         (
00460             const triSurface& surf,
00461             const point& sample,
00462             const label nearestFaceI,   // nearest face
00463             const point& nearestPt,     // nearest point on nearest face
00464             const scalar tol            // tolerance for nearness test.
00465         );
00466 
00467     // Triangulation of faces
00468 
00469         //- Simple triangulation of (selected patches of) boundaryMesh. Needs
00470         //  polyMesh (or polyBoundaryMesh) since only at this level are the
00471         //  triangles on neighbouring patches connected.
00472         static triSurface triangulate
00473         (
00474             const polyBoundaryMesh& mBesh,
00475             const labelHashSet& includePatches,
00476             const bool verbose = false
00477         );
00478 
00479         //- Face-centre triangulation of (selected patches of) boundaryMesh.
00480         //  Needs
00481         //  polyMesh (or polyBoundaryMesh) since only at this level are the
00482         //  triangles on neighbouring patches connected.
00483         triSurface triangulateFaceCentre
00484         (
00485             const polyBoundaryMesh& mBesh,
00486             const labelHashSet& includePatches,
00487             const bool verbose = false
00488         );
00489 
00490 
00491     // Triangulation and interpolation
00492 
00493         //- Calculate linear interpolation weights for point (guaranteed to be
00494         //  inside triangle)
00495         static void calcInterpolationWeights
00496         (
00497             const triPointRef&,
00498             const point&,
00499             FixedList<scalar, 3>& weights
00500         );
00501 
00502         // Calculate weighting factors from samplePts to triangle it is in.
00503         // Uses linear search to find triangle.
00504         // Vertices are:
00505         //   (a b c)  : vertices of the triangle abc the point is in
00506         // or if the point is outside all triangles:
00507         //   (a b -1) : the edge ab the point is nearest to.
00508         //   (a -1 -1) : the vertex a the point is nearest to
00509         static void calcInterpolationWeights
00510         (
00511             const triSurface& s,
00512             const pointField& samplePts,
00513             List<FixedList<label, 3> >& verts,
00514             List<FixedList<scalar, 3> >& weights
00515         );
00516 
00517         //- Do unconstrained Delaunay of points. Returns triSurface with 3D
00518         //  points with z=0. All triangles in region 0.
00519         static triSurface delaunay2D(const List<vector2D>&);
00520 
00521 
00522     // Tracking
00523 
00524         //- Test point on plane of triangle to see if on edge or point
00525         //  or inside.
00526         static surfaceLocation classify
00527         (
00528             const triSurface&,
00529             const label triI,
00530             const point& trianglePoint
00531         );
00532 
00533         //- Track on surface to get closer to point. Possible situations:
00534         //  - 1. reached endpoint
00535         //  - 2. reached edge (normal situation)
00536         //  - 3. reached end of surface (edge on single face)
00537         //  Input:
00538         //  - starting position+triangle/edge/point (so has to be on surface!)
00539         //  - (optional) previous position+triangle/edge/point to prevent
00540         //    going back. Set index (of triangle/edge/point) to -1 if not
00541         //    used.
00542         //  - end position+triangle/edge/point (so has to be on surface!)
00543         //  - plane to follow. Has to go through end point!
00544         //  Returns:
00545         //  - true if end point reached (situation 1)
00546         //  - new position+triangle/edge/point
00547         //  Caller has to check for situation 3 by checking that triangle()
00548         //  is not set.
00549         static surfaceLocation trackToEdge
00550         (
00551             const triSurface&,
00552             const surfaceLocation& start,
00553             const surfaceLocation& end,
00554             const plane& cutPlane
00555         );
00556 
00557         //- Track from edge to edge across surface. Uses trackToEdge.
00558         //  Not really useful by itself, more example of how to use trackToEdge.
00559         //  endInfo should be location on surface.
00560         //  hitInfo should be initialised to starting location (on surface as
00561         //  well). Upon return is set to end location.
00562         static void track
00563         (
00564             const triSurface&,
00565             const surfaceLocation& endInfo,
00566             const plane& cutPlane,
00567             surfaceLocation& hitInfo
00568         );
00569 };
00570 
00571 
00572 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00573 
00574 } // End namespace Foam
00575 
00576 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00577 
00578 #endif
00579 
00580 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd