1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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
29 A collection of tools for triSurfaceMesh
34 \*---------------------------------------------------------------------------*/
36 #ifndef triSurfaceTools_H
37 #define triSurfaceTools_H
40 #include "pointField.H"
41 #include "DynamicList.H"
43 #include "FixedList.H"
45 #include "triPointRef.H"
46 #include "surfaceLocation.H"
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 // Forward declaration of classes
57 class polyBoundaryMesh;
60 /*---------------------------------------------------------------------------*\
61 Class triSurfaceTools Declaration
62 \*---------------------------------------------------------------------------*/
66 // Private Member Functions
76 static void calcRefineStatus
78 const triSurface& surf,
80 List<refineType>& refine
82 static void greenRefine
84 const triSurface& surf,
87 const label newPointI,
88 DynamicList<labelledTri>& newFaces
90 static triSurface doRefine
92 const triSurface& surf,
93 const List<refineType>& refineStatus
99 static scalar faceCosAngle
107 static void protectNeighbours
109 const triSurface& surf,
111 labelList& faceStatus
114 //- faces to collapse because of edge collapse
115 static labelHashSet getCollapsedFaces
117 const triSurface& surf,
121 // Return value of faceUsed for faces using vertI (local numbering).
123 static label vertexUsesFace
125 const triSurface& surf,
126 const labelHashSet& faceUsed,
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
138 static void getMergedEdges
140 const triSurface& surf,
142 const labelHashSet& collapsedFaces,
143 HashTable<label, label, Hash<label> >& edgeToEdge,
144 HashTable<label, label, Hash<label> >& edgeToFace
147 //- Calculates (cos of) angle across edgeI of faceI,
148 // taking into account updated addressing (resulting from edge
150 static scalar edgeCosAngle
152 const triSurface& surf,
155 const labelHashSet& collapsedFaces,
156 const HashTable<label, label, Hash<label> >& edgeToEdge,
157 const HashTable<label, label, Hash<label> >& edgeToFace,
162 //- Calculate minimum (cos of) edge angle using addressing from
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
168 const triSurface& surf,
171 const labelHashSet& collapsedFaces,
172 const HashTable<label, label, Hash<label> >& edgeToEdge,
173 const HashTable<label, label, Hash<label> >& edgeToFace
176 //- Like collapseMinCosAngle but return true for value < minCos
177 bool collapseCreatesFold
179 const triSurface& surf,
182 const labelHashSet& collapsedFaces,
183 const HashTable<label, label, Hash<label> >& edgeToEdge,
184 const HashTable<label, label, Hash<label> >& edgeToFace,
188 ////- Checks if edge collapse creates triangles on top of each
190 //static bool collapseCreatesDuplicates
192 // const triSurface& surf,
193 // const label edgeI,
194 // const HashTable<bool, label, Hash<label> >& collapsedFaces
199 //- Finds the triangle edge/point cut by the plane between
200 // a point inside/on edge of a triangle and a point outside.
202 // - location on edge/point and hit()
203 // - or miss() if no intersection found
204 static surfaceLocation cutEdge
208 const label excludeEdgeI,
209 const label excludePointI,
210 const point& triPoint,
211 const plane& cutPlane,
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
220 const surfaceLocation& endInfo,
221 surfaceLocation& current
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.
229 // - hit() if triangle contains endpoint
230 // - triangle()=-1 if no triangle found
231 // - nearest triangle/edge otherwise
232 static surfaceLocation visitFaces
235 const labelList& eFaces,
236 const surfaceLocation& start,
237 const label excludeEdgeI,
238 const label excludePointI,
239 const surfaceLocation& end,
240 const plane& cutPlane
248 //- Write pointField to OBJ format file
251 const fileName& fName,
252 const pointField& pts
255 //- Write vertex subset to OBJ format file
258 const triSurface& surf,
259 const fileName& fName,
260 const boolList& markedVerts
264 // Additional addressing
266 //- Get all triangles using edge endpoint
267 static void getVertexTriangles
269 const triSurface& surf,
274 //- Get all vertices (local numbering) connected to vertices of edge
275 static labelList getVertexVertices
277 const triSurface& surf,
281 ////- Order vertices consistent with face
282 //static void orderVertices
284 // const labelledTri& f,
291 //- Get face connected to edge not faceI
292 static label otherFace
294 const triSurface& surf,
299 //- Get the two edges on faceI counterclockwise after edgeI
300 static void otherEdges
302 const triSurface& surf,
309 //- Get the two vertices (local numbering) on faceI counterclockwise
311 static void otherVertices
313 const triSurface& surf,
320 //- Get edge opposite vertex (local numbering)
321 static label oppositeEdge
323 const triSurface& surf,
328 //- Get vertex (local numbering) opposite edge
329 static label oppositeVertex
331 const triSurface& surf,
336 //- Returns edge label connecting v1, v2 (local numbering)
339 const triSurface& surf,
344 //- Return index of triangle (or -1) using all three edges
345 static label getTriangle
347 const triSurface& surf,
355 //- Create new triSurface by collapsing edges to edge mids.
356 static triSurface collapseEdges
358 const triSurface& surf,
359 const labelList& collapsableEdges
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
377 static triSurface collapseEdges
379 const triSurface& surf,
380 const labelList& collapsableEdges,
381 const pointField& edgeMids,
382 labelList& faceStatus
388 //- Refine edges by splitting to opposite vertex
389 static triSurface greenRefine
391 const triSurface& surf,
392 const labelList& refineEdges
395 //- Refine face by splitting all edges. Neighbouring face is
397 static triSurface redGreenRefine
399 const triSurface& surf,
400 const labelList& refineFaces
406 //- Returns element in edgeIndices with minimum length
409 const triSurface& surf,
410 const labelList& edgeIndices
413 //- Returns element in edgeIndices with minimum length
416 const triSurface& surf,
417 const labelList& edgeIndices
420 //- Merge points within distance
421 static triSurface mergePoints
423 const triSurface& surf,
424 const scalar mergeTol
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.
430 static vector surfaceNormal
432 const triSurface& surf,
433 const label nearestFaceI,
434 const point& nearestPt
437 //- on which side of surface
440 UNKNOWN, // cannot be determined (e.g. non-manifold)
445 //- if nearest point is on edgeI, determine on which side of surface
447 static sideType edgeSide
449 const triSurface& surf,
451 const point& nearestPoint,
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
460 const triSurface& surf,
462 const label nearestFaceI, // nearest face
463 const point& nearestPt, // nearest point on nearest face
464 const scalar tol // tolerance for nearness test.
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
474 const polyBoundaryMesh& mBesh,
475 const labelHashSet& includePatches,
476 const bool verbose = false
479 //- Face-centre triangulation of (selected patches of) boundaryMesh.
481 // polyMesh (or polyBoundaryMesh) since only at this level are the
482 // triangles on neighbouring patches connected.
483 triSurface triangulateFaceCentre
485 const polyBoundaryMesh& mBesh,
486 const labelHashSet& includePatches,
487 const bool verbose = false
491 // Triangulation and interpolation
493 //- Calculate linear interpolation weights for point (guaranteed to be
495 static void calcInterpolationWeights
499 FixedList<scalar, 3>& weights
502 // Calculate weighting factors from samplePts to triangle it is in.
503 // Uses linear search to find triangle.
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
512 const pointField& samplePts,
513 List<FixedList<label, 3> >& verts,
514 List<FixedList<scalar, 3> >& weights
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>&);
524 //- Test point on plane of triangle to see if on edge or point
526 static surfaceLocation classify
530 const point& trianglePoint
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)
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
542 // - end position+triangle/edge/point (so has to be on surface!)
543 // - plane to follow. Has to go through end point!
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()
549 static surfaceLocation trackToEdge
552 const surfaceLocation& start,
553 const surfaceLocation& end,
554 const plane& cutPlane
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.
565 const surfaceLocation& endInfo,
566 const plane& cutPlane,
567 surfaceLocation& hitInfo
572 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
574 } // End namespace Foam
576 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
580 // ************************************************************************* //