Forward compatibility: flex
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / triSurfaceTools / triSurfaceCopyParts / triSurfaceCopyParts.C
blobea36c084b9607ab597c879a1820229ac33481c7f
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 "triSurfaceCopyParts.H"
29 #include "triSurfModifier.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 void triSurfaceCopyParts::markFacetsForCopying
40     const wordList& parts,
41     boolList& copyFacets
42 ) const
44     copyFacets.setSize(surf_.size());
45     copyFacets = false;
47     const geometricSurfacePatchList& patches = surf_.patches();
49     //- mark patches which will be copied
50     boolList copyPatch(patches.size(), false);
52     forAll(patches, patchI)
53     {
54         const word name = patches[patchI].name();
56         forAll(parts, partI)
57         {
58             if( parts[partI] == name )
59             {
60                 copyPatch[patchI] = true;
61                 break;
62             }
63         }
64     }
66     //- select facets affected by the deletion of a patch
67     forAll(surf_, triI)
68     {
69         if( copyPatch[surf_[triI].region()] )
70             copyFacets[triI] = true;
71     }
73     //- mark facets contained in selected subsets
74     DynList<label> facetSubsetsIDs;
75     surf_.facetSubsetIndices(facetSubsetsIDs);
77     forAll(facetSubsetsIDs, i)
78     {
79         const word fsName = surf_.facetSubsetName(facetSubsetsIDs[i]);
81         forAll(parts, partI)
82         {
83             if( parts[partI] == fsName )
84             {
85                 labelLongList containedFacets;
86                 surf_.facetsInSubset(facetSubsetsIDs[i], containedFacets);
88                 forAll(containedFacets, cfI)
89                     copyFacets[containedFacets[cfI]] = true;
91                 break;
92             }
93         }
94     }
97 void triSurfaceCopyParts::copySurfaceMesh
99     const boolList& copyFacets,
100     triSurf& s
101 ) const
103     Info << "Starting copying surface parts" << endl;
105     const pointField& pts = surf_.points();
107     labelLongList newPointLabel(pts.size(), -1);
109     label nPoints(0);
111     //- create the modifier and delete data if there is any
112     triSurfModifier sm(s);
113     sm.facetsAccess().clear();
114     sm.featureEdgesAccess().clear();
115     sm.patchesAccess().setSize(surf_.patches().size());
116     forAll(surf_.patches(), patchI)
117         sm.patchesAccess()[patchI] = surf_.patches()[patchI];
119     //- copy selected patches
120     labelLongList newTriangleLabel(surf_.size(), -1);
121     forAll(copyFacets, triI)
122     {
123         if( !copyFacets[triI] )
124             continue;
126         const labelledTri& tri = surf_[triI];
128         labelledTri newTri;
129         newTri.region() = tri.region();
131         forAll(tri, pI)
132         {
133             if( newPointLabel[tri[pI]] == -1 )
134             {
135                 newPointLabel[tri[pI]] = nPoints;
136                 ++nPoints;
137             }
139             newTri[pI] = newPointLabel[tri[pI]];
140         }
142         newTriangleLabel[triI] = s.size();
143         s.appendTriangle(newTri);
144     }
146     Info << "Copied triangles " << s.size() << endl;
147     Info << "Number of vertices " << nPoints << endl;
149     //- copy vertices
150     pointField& newPts = sm.pointsAccess();
151     newPts.setSize(nPoints);
153     forAll(newPointLabel, i)
154     {
155         if( newPointLabel[i] < 0 )
156             continue;
158         newPts[newPointLabel[i]] = pts[i];
159     }
161     //- copy point subsets
162     DynList<label> subsetIds;
163     surf_.pointSubsetIndices(subsetIds);
164     forAll(subsetIds, subsetI)
165     {
166         const label origId = subsetIds[subsetI];
167         const word sName = surf_.pointSubsetName(origId);
169         labelLongList pointsInSubset;
170         surf_.pointsInSubset(origId, pointsInSubset);
172         const label newId = s.addPointSubset(sName);
173         forAll(pointsInSubset, i)
174         {
175             const label newPointI = newPointLabel[pointsInSubset[i]];
177             if( newPointI < 0 )
178                 continue;
180             s.addPointToSubset(newId, newPointI);
181         }
182     }
184     //- copy facet subsets
185     surf_.facetSubsetIndices(subsetIds);
186     forAll(subsetIds, subsetI)
187     {
188         const label origId = subsetIds[subsetI];
189         const word sName = surf_.facetSubsetName(origId);
191         labelLongList trianglesInSubset;
192         surf_.facetsInSubset(origId, trianglesInSubset);
194         const label newId = s.addFacetSubset(sName);
195         forAll(trianglesInSubset, i)
196         {
197             const label newTriI = newTriangleLabel[trianglesInSubset[i]];
199             if( newTriI < 0 )
200                 continue;
202             s.addFacetToSubset(newId, newTriI);
203         }
204     }
206     //- copy feature edges
207     labelLongList newEdgeLabel(surf_.nFeatureEdges(), -1);
208     const VRWGraph& pointEdges = surf_.pointEdges();
209     const edgeLongList& edges = surf_.edges();
210     const VRWGraph& edgeFacets = surf_.edgeFacets();
211     forAll(newEdgeLabel, edgeI)
212     {
213         const edge& e = surf_.featureEdges()[edgeI];
214         label eI(-1);
215         forAllRow(pointEdges, e.start(), peI)
216         {
217             const label eJ = pointEdges(e.start(), peI);
218             if( edges[eJ] == e )
219             {
220                 eI = eJ;
221                 break;
222             }
223         }
225         if( newPointLabel[e.start()] < 0 )
226             continue;
227         if( newPointLabel[e.end()] < 0 )
228             continue;
229         bool foundTriangle(false);
230         forAllRow(edgeFacets, eI, efI)
231         {
232             if( newTriangleLabel[edgeFacets(eI, efI)] >= 0 )
233             {
234                 foundTriangle = true;
235                 break;
236             }
237         }
238         if( !foundTriangle )
239             continue;
241         newEdgeLabel[edgeI] = sm.featureEdgesAccess().size();
242         sm.featureEdgesAccess().append
243         (
244             edge(newPointLabel[e.start()], newPointLabel[e.end()])
245         );
246     }
248     //- copy subsets of feature edges
249     surf_.edgeSubsetIndices(subsetIds);
250     forAll(subsetIds, subsetI)
251     {
252         const label origId = subsetIds[subsetI];
253         const word sName = surf_.edgeSubsetName(origId);
255         labelLongList edgesInSubset;
256         surf_.edgesInSubset(origId, edgesInSubset);
258         const label newId = s.addEdgeSubset(sName);
259         forAll(edgesInSubset, i)
260         {
261             const label newEdgeI = newEdgeLabel[edgesInSubset[i]];
263             if( newEdgeI < 0 )
264                 continue;
266             s.addEdgeToSubset(newId, newEdgeI);
267         }
268     }
270     Info << "Finished copying surface parts" << endl;
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 triSurfaceCopyParts::triSurfaceCopyParts(const triSurf& surface)
277     surf_(surface)
280 triSurfaceCopyParts::~triSurfaceCopyParts()
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
285 void triSurfaceCopyParts::copySurface(const wordList& patches, triSurf& s) const
287     boolList copyFacets(surf_.size(), false);
289     markFacetsForCopying(patches, copyFacets);
291     copySurfaceMesh(copyFacets, s);
294 triSurf* triSurfaceCopyParts::copySurface(const wordList& patches) const
296     boolList copyFacets(surf_.size(), false);
298     markFacetsForCopying(patches, copyFacets);
300     triSurf* sPtr = new triSurf();
302     copySurfaceMesh(copyFacets, *sPtr);
304     return sPtr;
307 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 } // End namespace Foam
311 // ************************************************************************* //