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

face.C

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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "face.H"
00028 #include "triFace.H"
00029 #include "triPointRef.H"
00030 #include "mathematicalConstants.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 const char* const Foam::face::typeName = "face";
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 Foam::tmp<Foam::vectorField>
00039 Foam::face::calcEdges(const pointField& points) const
00040 {
00041     tmp<vectorField> tedges(new vectorField(size()));
00042     vectorField& edges = tedges();
00043 
00044     forAll(*this, i)
00045     {
00046         label ni = fcIndex(i);
00047 
00048         point thisPt = points[operator[](i)];
00049         point nextPt = points[operator[](ni)];
00050 
00051         vector vec(nextPt - thisPt);
00052         vec /= Foam::mag(vec) + VSMALL;
00053 
00054         edges[i] = vec;
00055     }
00056 
00057     return tedges;
00058 }
00059 
00060 
00061 Foam::scalar Foam::face::edgeCos
00062 (
00063     const vectorField& edges,
00064     const label index
00065 ) const
00066 {
00067     label leftEdgeI = left(index);
00068     label rightEdgeI = right(index);
00069 
00070     // note negate on left edge to get correct left-pointing edge.
00071     return -(edges[leftEdgeI] & edges[rightEdgeI]);
00072 }
00073 
00074 
00075 Foam::label Foam::face::mostConcaveAngle
00076 (
00077     const pointField& points,
00078     const vectorField& edges,
00079     scalar& maxAngle
00080 ) const
00081 {
00082     vector n(normal(points));
00083 
00084     label index = 0;
00085     maxAngle = -GREAT;
00086 
00087     forAll(edges, i)
00088     {
00089         label leftEdgeI = left(i);
00090         label rightEdgeI = right(i);
00091 
00092         vector edgeNormal = edges[rightEdgeI] ^ edges[leftEdgeI];
00093 
00094         scalar edgeCos = edges[leftEdgeI] & edges[rightEdgeI];
00095         scalar edgeAngle = acos(max(-1.0, min(1.0, edgeCos)));
00096 
00097         scalar angle;
00098 
00099         if ((edgeNormal & n) > 0)
00100         {
00101             // Concave angle.
00102             angle = mathematicalConstant::pi + edgeAngle;
00103         }
00104         else
00105         {
00106             // Convex angle. Note '-' to take into account that rightEdge
00107             // and leftEdge are head-to-tail connected.
00108             angle = mathematicalConstant::pi - edgeAngle;
00109         }
00110 
00111         if (angle > maxAngle)
00112         {
00113             maxAngle = angle;
00114             index = i;
00115         }
00116     }
00117 
00118     return index;
00119 }
00120 
00121 
00122 Foam::label Foam::face::split
00123 (
00124     const face::splitMode mode,
00125     const pointField& points,
00126     label& triI,
00127     label& quadI,
00128     faceList& triFaces,
00129     faceList& quadFaces
00130 ) const
00131 {
00132     label oldIndices = (triI + quadI);
00133 
00134     if (size() <= 2)
00135     {
00136         FatalErrorIn
00137         (
00138             "face::split"
00139             "(const face::splitMode, const pointField&, label&, label&"
00140             ", faceList&, faceList&)"
00141         )
00142             << "Serious problem: asked to split a face with < 3 vertices"
00143             << abort(FatalError);
00144     }
00145     if (size() == 3)
00146     {
00147         // Triangle. Just copy.
00148         if (mode == COUNTTRIANGLE || mode == COUNTQUAD)
00149         {
00150             triI++;
00151         }
00152         else
00153         {
00154             triFaces[triI++] = *this;
00155         }
00156     }
00157     else if (size() == 4)
00158     {
00159         if (mode == COUNTTRIANGLE)
00160         {
00161             triI += 2;
00162         }
00163         if (mode == COUNTQUAD)
00164         {
00165             quadI++;
00166         }
00167         else if (mode == SPLITTRIANGLE)
00168         {
00169             //  Start at point with largest internal angle.
00170             const vectorField edges(calcEdges(points));
00171 
00172             scalar minAngle;
00173             label startIndex = mostConcaveAngle(points, edges, minAngle);
00174 
00175             label nextIndex = fcIndex(startIndex);
00176             label splitIndex = fcIndex(nextIndex);
00177 
00178             // Create triangles
00179             face triFace(3);
00180             triFace[0] = operator[](startIndex);
00181             triFace[1] = operator[](nextIndex);
00182             triFace[2] = operator[](splitIndex);
00183 
00184             triFaces[triI++] = triFace;
00185 
00186             triFace[0] = operator[](splitIndex);
00187             triFace[1] = operator[](fcIndex(splitIndex));
00188             triFace[2] = operator[](startIndex);
00189 
00190             triFaces[triI++] = triFace;
00191         }
00192         else
00193         {
00194             quadFaces[quadI++] = *this;
00195         }
00196     }
00197     else
00198     {
00199         // General case. Like quad: search for largest internal angle.
00200 
00201         const vectorField edges(calcEdges(points));
00202 
00203         scalar minAngle = 1;
00204         label startIndex = mostConcaveAngle(points, edges, minAngle);
00205 
00206         scalar bisectAngle = minAngle/2;
00207         vector rightEdge = edges[right(startIndex)];
00208 
00209         //
00210         // Look for opposite point which as close as possible bisects angle
00211         //
00212 
00213         // split candidate starts two points away.
00214         label index = fcIndex(fcIndex(startIndex));
00215 
00216         label minIndex = index;
00217         scalar minDiff = Foam::mathematicalConstant::pi;
00218 
00219         for(label i = 0; i < size() - 3; i++)
00220         {
00221             vector splitEdge
00222             (
00223                 points[operator[](index)]
00224               - points[operator[](startIndex)]
00225             );
00226             splitEdge /= Foam::mag(splitEdge) + VSMALL;
00227 
00228             const scalar splitCos = splitEdge & rightEdge;
00229             const scalar splitAngle = acos(max(-1.0, min(1.0, splitCos)));
00230             const scalar angleDiff = fabs(splitAngle - bisectAngle);
00231 
00232             if (angleDiff < minDiff)
00233             {
00234                 minDiff = angleDiff;
00235                 minIndex = index;
00236             }
00237 
00238             // go to next candidate
00239             index = fcIndex(index);
00240         }
00241 
00242 
00243         // Split into two subshapes.
00244         //     face1: startIndex to minIndex
00245         //     face2: minIndex to startIndex
00246 
00247         // Get sizes of the two subshapes
00248         label diff = 0;
00249         if (minIndex > startIndex)
00250         {
00251             diff = minIndex - startIndex;
00252         }
00253         else
00254         {
00255             // folded around
00256             diff = minIndex + size() - startIndex;
00257         }
00258 
00259         label nPoints1 = diff + 1;
00260         label nPoints2 = size() - diff + 1;
00261 
00262         // Collect face1 points
00263         face face1(nPoints1);
00264 
00265         index = startIndex;
00266         for (label i = 0; i < nPoints1; i++)
00267         {
00268             face1[i] = operator[](index);
00269             index = fcIndex(index);
00270         }
00271 
00272         // Collect face2 points
00273         face face2(nPoints2);
00274 
00275         index = minIndex;
00276         for (label i = 0; i < nPoints2; i++)
00277         {
00278             face2[i] = operator[](index);
00279             index = fcIndex(index);
00280         }
00281 
00282         // Split faces
00283         face1.split(mode, points, triI, quadI, triFaces, quadFaces);
00284         face2.split(mode, points, triI, quadI, triFaces, quadFaces);
00285     }
00286 
00287     return (triI + quadI - oldIndices);
00288 }
00289 
00290 
00291 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00292 
00293 Foam::face::face(const triFace& f)
00294 :
00295     labelList(f)
00296 {}
00297 
00298 
00299 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
00300 
00301 
00302 // return
00303 //   0: no match
00304 //  +1: identical
00305 //  -1: same face, but different orientation
00306 int Foam::face::compare(const face& a, const face& b)
00307 {
00308     // Basic rule: we assume that the sequence of labels in each list
00309     // will be circular in the same order (but not necessarily in the
00310     // same direction or from the same starting point).
00311 
00312     // Trivial reject: faces are different size
00313     label sizeA = a.size();
00314     label sizeB = b.size();
00315 
00316     if (sizeA != sizeB)
00317     {
00318         return 0;
00319     }
00320 
00321 
00322     // Full list comparison
00323     const label firstA = a[0];
00324     label Bptr = -1;
00325 
00326     forAll (b, i)
00327     {
00328         if (b[i] == firstA)
00329         {
00330             Bptr = i;        // 'found match' at element 'i'
00331             break;
00332         }
00333     }
00334 
00335     // If no match was found, return 0
00336     if (Bptr < 0)
00337     {
00338         return 0;
00339     }
00340 
00341     // Now we must look for the direction, if any
00342     label secondA = a[1];
00343 
00344     if (sizeA > 1 && (secondA == firstA || firstA == a[sizeA - 1]))
00345     {
00346         face ca = a;
00347         ca.collapse();
00348 
00349         face cb = b;
00350         cb.collapse();
00351 
00352         return face::compare(ca, cb);
00353     }
00354 
00355     int dir = 0;
00356 
00357     // Check whether at top of list
00358     Bptr++;
00359     if (Bptr == b.size())
00360     {
00361         Bptr = 0;
00362     }
00363 
00364     // Test whether upward label matches second A label
00365     if (b[Bptr] == secondA)
00366     {
00367         // Yes - direction is 'up'
00368         dir = 1;
00369     }
00370     else
00371     {
00372         // No - so look downwards, checking whether at bottom of list
00373         Bptr -= 2;
00374 
00375         if (Bptr < 0)
00376         {
00377             // wraparound
00378             Bptr += b.size();
00379         }
00380 
00381         // Test whether downward label matches second A label
00382         if (b[Bptr] == secondA)
00383         {
00384             // Yes - direction is 'down'
00385             dir = -1;
00386         }
00387     }
00388 
00389     // Check whether a match was made at all, and exit 0 if not
00390     if (dir == 0)
00391     {
00392         return 0;
00393     }
00394 
00395     // Decrement size by 2 to account for first searches
00396     sizeA -= 2;
00397 
00398     // We now have both direction of search and next element
00399     // to search, so we can continue search until no more points.
00400     label Aptr = 1;
00401     if (dir > 0)
00402     {
00403         while (sizeA--)
00404         {
00405             Aptr++;
00406             if (Aptr >= a.size())
00407             {
00408                 Aptr = 0;
00409             }
00410 
00411             Bptr++;
00412             if (Bptr >= b.size())
00413             {
00414                 Bptr = 0;
00415             }
00416 
00417             if (a[Aptr] != b[Bptr])
00418             {
00419                 return 0;
00420             }
00421         }
00422     }
00423     else
00424     {
00425         while (sizeA--)
00426         {
00427             Aptr++;
00428             if (Aptr >= a.size())
00429             {
00430                 Aptr = 0;
00431             }
00432 
00433             Bptr--;
00434             if (Bptr < 0)
00435             {
00436                 Bptr = b.size() - 1;
00437             }
00438 
00439             if (a[Aptr] != b[Bptr])
00440             {
00441                 return 0;
00442             }
00443         }
00444     }
00445 
00446     // They must be equal - return direction
00447     return dir;
00448 }
00449 
00450 
00451 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00452 
00453 
00454 Foam::label Foam::face::collapse()
00455 {
00456     if (size() > 1)
00457     {
00458         label ci = 0;
00459         for (label i=1; i<size(); i++)
00460         {
00461             if (operator[](i) != operator[](ci))
00462             {
00463                 operator[](++ci) = operator[](i);
00464             }
00465         }
00466 
00467         if (operator[](ci) != operator[](0))
00468         {
00469             ci++;
00470         }
00471 
00472         setSize(ci);
00473     }
00474 
00475     return size();
00476 }
00477 
00478 
00479 Foam::point Foam::face::centre(const pointField& meshPoints) const
00480 {
00481     // Calculate the centre by breaking the face into triangles and
00482     // area-weighted averaging their centres
00483 
00484     // If the face is a triangle, do a direct calculation
00485     if (size() == 3)
00486     {
00487         return
00488             (1.0/3.0)
00489            *(
00490                meshPoints[operator[](0)]
00491              + meshPoints[operator[](1)]
00492              + meshPoints[operator[](2)]
00493             );
00494     }
00495 
00496     label nPoints = size();
00497 
00498     point centrePoint = point::zero;
00499     for (register label pI=0; pI<nPoints; pI++)
00500     {
00501         centrePoint += meshPoints[operator[](pI)];
00502     }
00503     centrePoint /= nPoints;
00504 
00505     scalar sumA = 0;
00506     vector sumAc = vector::zero;
00507 
00508     for (register label pI=0; pI<nPoints; pI++)
00509     {
00510         const point& nextPoint = meshPoints[operator[]((pI + 1) % nPoints)];
00511 
00512         // Calculate 3*triangle centre
00513         vector ttc
00514         (
00515             meshPoints[operator[](pI)]
00516           + nextPoint
00517           + centrePoint
00518         );
00519 
00520         // Calculate 2*triangle area
00521         scalar ta = Foam::mag
00522         (
00523             (meshPoints[operator[](pI)] - centrePoint)
00524           ^ (nextPoint - centrePoint)
00525         );
00526 
00527         sumA += ta;
00528         sumAc += ta*ttc;
00529     }
00530 
00531     if (sumA > VSMALL)
00532     {
00533         return sumAc/(3*sumA);
00534     }
00535     else
00536     {
00537         return centrePoint;
00538     }
00539 }
00540 
00541 
00542 Foam::vector Foam::face::normal(const pointField& p) const
00543 {
00544     // Calculate the normal by summing the face triangle normals.
00545     // Changed to deal with small concavity by using a central decomposition
00546     //
00547 
00548     // If the face is a triangle, do a direct calculation to avoid round-off
00549     // error-related problems
00550     //
00551     if (size() == 3)
00552     {
00553         return triPointRef
00554         (
00555             p[operator[](0)],
00556             p[operator[](1)],
00557             p[operator[](2)]
00558         ).normal();
00559     }
00560 
00561     label nPoints = size();
00562 
00563     register label pI;
00564 
00565     point centrePoint = vector::zero;
00566     for (pI = 0; pI < nPoints; pI++)
00567     {
00568         centrePoint += p[operator[](pI)];
00569     }
00570     centrePoint /= nPoints;
00571 
00572     vector n = vector::zero;
00573 
00574     point nextPoint = centrePoint;
00575 
00576     for (pI = 0; pI < nPoints; pI++)
00577     {
00578         if (pI < nPoints - 1)
00579         {
00580             nextPoint = p[operator[](pI + 1)];
00581         }
00582         else
00583         {
00584             nextPoint = p[operator[](0)];
00585         }
00586 
00587         // Note: for best accuracy, centre point always comes last
00588         //
00589         n += triPointRef
00590         (
00591             p[operator[](pI)],
00592             nextPoint,
00593             centrePoint
00594         ).normal();
00595     }
00596 
00597     return n;
00598 }
00599 
00600 
00601 Foam::face Foam::face::reverseFace() const
00602 {
00603     // reverse the label list and return
00604     // The starting points of the original and reverse face are identical.
00605 
00606     const labelList& f = *this;
00607     labelList newList(size());
00608 
00609     newList[0] = f[0];
00610 
00611     for (label pointI = 1; pointI < newList.size(); pointI++)
00612     {
00613         newList[pointI] = f[size() - pointI];
00614     }
00615 
00616     return face(xferMove(newList));
00617 }
00618 
00619 
00620 Foam::label Foam::face::which(const label globalIndex) const
00621 {
00622     label pointInFace = -1;
00623     const labelList& f = *this;
00624 
00625     forAll (f, i)
00626     {
00627         if (f[i] == globalIndex)
00628         {
00629             pointInFace = i;
00630             break;
00631         }
00632     }
00633 
00634     return pointInFace;
00635 }
00636 
00637 
00638 Foam::scalar Foam::face::sweptVol
00639 (
00640     const pointField& oldPoints,
00641     const pointField& newPoints
00642 ) const
00643 {
00644     scalar sv = 0;
00645 
00646     // Calculate the swept volume by breaking the face into triangles and
00647     // summing their swept volumes.
00648     // Changed to deal with small concavity by using a central decomposition
00649 
00650     point centreOldPoint = centre(oldPoints);
00651     point centreNewPoint = centre(newPoints);
00652 
00653     label nPoints = size();
00654 
00655     point nextOldPoint = centreOldPoint;
00656     point nextNewPoint = centreNewPoint;
00657 
00658     register label pI;
00659 
00660     for (pI = 0; pI < nPoints; pI++)
00661     {
00662         if (pI < nPoints - 1)
00663         {
00664             nextOldPoint = oldPoints[operator[](pI + 1)];
00665             nextNewPoint = newPoints[operator[](pI + 1)];
00666         }
00667         else
00668         {
00669             nextOldPoint = oldPoints[operator[](0)];
00670             nextNewPoint = newPoints[operator[](0)];
00671         }
00672 
00673         // Note: for best accuracy, centre point always comes last
00674         sv += triPointRef
00675               (
00676                   centreOldPoint,
00677                   oldPoints[operator[](pI)],
00678                   nextOldPoint
00679               ).sweptVol
00680               (
00681                   triPointRef
00682                   (
00683                       centreNewPoint,
00684                       newPoints[operator[](pI)],
00685                       nextNewPoint
00686                   )
00687               );
00688     }
00689 
00690     return sv;
00691 }
00692 
00693 
00694 Foam::edgeList Foam::face::edges() const
00695 {
00696     const labelList& points = *this;
00697 
00698     edgeList e(points.size());
00699 
00700     label pointI;
00701 
00702     for (pointI = 0; pointI < points.size() - 1; pointI++)
00703     {
00704         e[pointI] = edge(points[pointI], points[pointI + 1]);
00705     }
00706 
00707     // add last edge
00708     e[points.size() - 1] = edge(points[points.size() - 1], points[0]);
00709 
00710     return e;
00711 }
00712 
00713 
00714 int Foam::face::edgeDirection(const edge& e) const
00715 {
00716     forAll (*this, i)
00717     {
00718         if (operator[](i) == e.start())
00719         {
00720             if (operator[](rcIndex(i)) == e.end())
00721             {
00722                 // reverse direction
00723                 return -1;
00724             }
00725             else if (operator[](fcIndex(i)) == e.end())
00726             {
00727                 // forward direction
00728                 return 1;
00729             }
00730 
00731             // no match
00732             return 0;
00733         }
00734         else if (operator[](i) == e.end())
00735         {
00736             if (operator[](rcIndex(i)) == e.start())
00737             {
00738                 // forward direction
00739                 return 1;
00740             }
00741             else if (operator[](fcIndex(i)) == e.start())
00742             {
00743                 // reverse direction
00744                 return -1;
00745             }
00746 
00747             // no match
00748             return 0;
00749         }
00750     }
00751 
00752     // not found
00753     return 0;
00754 }
00755 
00756 
00757 // Number of triangles directly known from number of vertices
00758 Foam::label Foam::face::nTriangles(const pointField&) const
00759 {
00760     return nTriangles();
00761 }
00762 
00763 
00764 Foam::label Foam::face::triangles
00765 (
00766     const pointField& points,
00767     label& triI,
00768     faceList& triFaces
00769 ) const
00770 {
00771     label quadI = 0;
00772     faceList quadFaces;
00773 
00774     return split(SPLITTRIANGLE, points, triI, quadI, triFaces, quadFaces);
00775 }
00776 
00777 
00778 Foam::label Foam::face::nTrianglesQuads
00779 (
00780     const pointField& points,
00781     label& triI,
00782     label& quadI
00783 ) const
00784 {
00785     faceList triFaces;
00786     faceList quadFaces;
00787 
00788     return split(COUNTQUAD, points, triI, quadI, triFaces, quadFaces);
00789 }
00790 
00791 
00792 Foam::label Foam::face::trianglesQuads
00793 (
00794     const pointField& points,
00795     label& triI,
00796     label& quadI,
00797     faceList& triFaces,
00798     faceList& quadFaces
00799 ) const
00800 {
00801     return split(SPLITQUAD, points, triI, quadI, triFaces, quadFaces);
00802 }
00803 
00804 
00805 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00806 
00807 
00808 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00809 
00810 // ************************************************************************* //
00811 
Copyright © 2000-2009 OpenCFD Ltd