Forward compatibility: flex
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / triSurfaceTools / triSurfacePatchManipulator / triSurfacePatchManipulator.C
blob525132bb65eff8eaa79d225f48cc7f6b2c918c62
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | cfMesh: A library for mesh generation
4    \\    /   O peration     |
5     \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6      \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9     This file is part of cfMesh.
11     cfMesh 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 3 of the License, or (at your
14     option) any later version.
16     cfMesh 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 cfMesh.  If not, see <http://www.gnu.org/licenses/>.
24 Description
26 \*---------------------------------------------------------------------------*/
28 #include "triSurfacePatchManipulator.H"
29 #include "helperFunctions.H"
30 #include "demandDrivenData.H"
31 #include "checkMeshDict.H"
33 #include <map>
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 namespace Foam
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 triSurfacePatchManipulator::triSurfacePatchManipulator(const triSurf& surface)
44     surf_(surface),
45     featureEdges_(surf_.edges().size(), direction(0)),
46     facetInPatch_(),
47     nPatches_(),
48     newPatchNames_(),
49     newPatchTypes_()
51     allocateFeatureEdges();
53     createPatches();
56 triSurfacePatchManipulator::~triSurfacePatchManipulator()
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 void triSurfacePatchManipulator::detectedSurfaceRegions
63     VRWGraph& graph
64 ) const
66     graph.setSize(nPatches_);
68     labelLongList nFacetsInPatch(nPatches_, 0);
70     forAll(facetInPatch_, triI)
71         ++nFacetsInPatch[facetInPatch_[triI]];
73     graph.setSizeAndRowSize(nFacetsInPatch);
75     nFacetsInPatch = 0;
76     forAll(facetInPatch_, triI)
77     {
78         const label patchI = facetInPatch_[triI];
80         graph(patchI, nFacetsInPatch[patchI]) = triI;
81         ++nFacetsInPatch[patchI];
82     }
85 const triSurf* triSurfacePatchManipulator::surfaceWithPatches
87     IOdictionary* meshDictPtr,
88     const word prefix,
89     const bool forceOverwrite
90 ) const
92     //- collect patch information
93     VRWGraph facetsInPatch;
94     detectedSurfaceRegions(facetsInPatch);
96     //- create new list of boundary patches
97     LongList<labelledTri> newTriangles(facetInPatch_.size());
98     label counter(0);
99     geometricSurfacePatchList newPatches(nPatches_);
101     if( forceOverwrite )
102     {
103         forAll(newPatches, patchI)
104         {
105             newPatches[patchI].name() = prefix+help::scalarToText(patchI);
106             newPatches[patchI].geometricType() = "patch";
107             newPatches[patchI].index() = patchI;
108         }
109     }
110     else
111     {
112         forAll(facetsInPatch, patchI)
113         {
114             forAllRow(facetsInPatch, patchI, fpI)
115             {
116                 const label origPatchI =
117                     surf_[facetsInPatch(patchI, fpI)].region();
118                 newPatches[patchI].name() =
119                     surf_.patches()[origPatchI].name() + '_' +
120                     help::scalarToText(patchI);
121                 newPatches[patchI].geometricType() =
122                     surf_.patches()[origPatchI].geometricType();
123                 newPatches[patchI].index() = patchI;
124             }
125         }
126     }
128     //- create triangles for the new surface
129     labelLongList newFacetLabel(newTriangles.size(), -1);
131     forAll(facetsInPatch, patchI)
132         forAllRow(facetsInPatch, patchI, tI)
133         {
134             newFacetLabel[facetsInPatch(patchI, tI)] = counter;
135             labelledTri tria = surf_[facetsInPatch(patchI, tI)];
136             tria.region() = patchI;
137             newTriangles[counter++] = tria;
138         }
140     //- create and return a new surface mesh
141     triSurf* newSurfPtr =
142         new triSurf
143         (
144             newTriangles,
145             newPatches,
146             edgeLongList(),
147             surf_.points()
148         );
150     //- transfer facet subsets
151     DynList<label> subsetIDs;
152     surf_.facetSubsetIndices(subsetIDs);
153     forAll(subsetIDs, subsetI)
154     {
155         const word sName = surf_.facetSubsetName(subsetIDs[subsetI]);
157         const label newID = newSurfPtr->addFacetSubset(sName);
159         labelLongList facetsInSubset;
160         surf_.facetsInSubset(subsetIDs[subsetI], facetsInSubset);
162         forAll(facetsInSubset, i)
163         {
164             const label fI = newFacetLabel[facetsInSubset[i]];
166             newSurfPtr->addFacetToSubset(newID, fI);
167         }
168     }
170     //- transfer point subsets
171     surf_.pointSubsetIndices(subsetIDs);
172     forAll(subsetIDs, subsetI)
173     {
174         const word sName = surf_.pointSubsetName(subsetIDs[subsetI]);
176         const label newID = newSurfPtr->addPointSubset(sName);
178         labelLongList pointsInSubset;
179         surf_.pointsInSubset(subsetIDs[subsetI], pointsInSubset);
181         forAll(pointsInSubset, i)
182             newSurfPtr->addPointToSubset(newID, pointsInSubset[i]);
183     }
185     if( meshDictPtr )
186     {
187         //- create mapping between the patches on the original surface
188         //- and the renamed patches
189         std::map<word, wordList> patchesForPatch;
190         std::map<word, word> patchTypes;
192         const geometricSurfacePatchList& origPatches = surf_.patches();
193         forAll(origPatches, patchI)
194             patchTypes[origPatches[patchI].name()] =
195                 origPatches[patchI].geometricType();
197         //- find the mapping of patch ids
198         List<labelHashSet> patchToNewPatches(origPatches.size());
199         forAll(facetsInPatch, patchI)
200         {
201             forAllRow(facetsInPatch, patchI, fI)
202             {
203                 const label opatchI = surf_[facetsInPatch(patchI, fI)].region();
205                 patchToNewPatches[opatchI].insert(patchI);
206             }
207         }
209         forAll(patchToNewPatches, patchI)
210         {
211             const word& pName = origPatches[patchI].name();
212             patchesForPatch[pName].setSize
213             (
214                 patchToNewPatches[patchI].size()
215             );
217             label counter(0);
219             forAllConstIter(labelHashSet, patchToNewPatches[patchI], it)
220                 patchesForPatch[pName][counter++] = newPatches[it.key()].name();
221         }
223         //- update the values in meshDict based on the created patches
224         checkMeshDict(*meshDictPtr).updateDictionaries
225         (
226             patchesForPatch,
227             patchTypes
228         );
229     }
231     return newSurfPtr;
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 } // End namespace Foam
238 // ************************************************************************* //