File: surfaceAdd.C

package info (click to toggle)
openfoam 1812%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 220,284 kB
  • sloc: cpp: 1,038,902; sh: 14,536; ansic: 8,240; lex: 657; xml: 387; python: 300; awk: 212; makefile: 94; sed: 88; csh: 3
file content (319 lines) | stat: -rw-r--r-- 9,263 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application
    surfaceAdd

Group
    grpSurfaceUtilities

Description
    Add two surfaces. Does geometric merge on points.
    Does not check for overlapping/intersecting triangles.

    Keeps patches separate by renumbering.

\*---------------------------------------------------------------------------*/

#include "argList.H"
#include "fileName.H"
#include "triSurface.H"
#include "Fstream.H"
#include "triFace.H"
#include "triFaceList.H"

using namespace Foam;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //


int main(int argc, char *argv[])
{
    argList::addNote
    (
        "Add two surfaces via a geometric merge on points."
        " Does not check for overlapping/intersecting triangles."
    );

    argList::noParallel();
    argList::addArgument("surface1", "The input surface file 1");
    argList::addArgument("surface2", "The input surface file 2");
    argList::addArgument("output", "The output surface file");

    argList::addOption
    (
        "points",
        "file",
        "Provide additional points"
    );
    argList::addBoolOption
    (
        "mergeRegions",
        "Combine regions from both surfaces"
    );
    argList::addOption
    (
        "scale",
        "factor",
        "Geometry scaling factor on input surfaces"
    );

    argList args(argc, argv);

    const fileName inFileName1 = args[1];
    const fileName inFileName2 = args[2];
    const fileName outFileName = args[3];

    const bool addPoint     = args.found("points");
    const bool mergeRegions = args.found("mergeRegions");

    const scalar scaleFactor = args.opt<scalar>("scale", -1);

    if (addPoint)
    {
        Info<< "Reading a surface and adding points from a file"
            << "; merging the points and writing the surface to another file"
            << nl << endl;

        Info<< "Surface  : " << inFileName1<< nl
            << "Points   : " << args["points"] << nl
            << "Writing  : " << outFileName << nl << endl;
    }
    else
    {
        Info<< "Reading two surfaces"
            << "; merging points and writing the surface to another file"
            << nl << endl;

        if (mergeRegions)
        {
            Info<< "Regions from the two files will get merged" << nl
                << "Do not use this option if you want to keep the regions"
                << " separate" << nl << endl;
        }
        else
        {
            Info<< "Regions from the two files will not get merged" << nl
                << "Regions from " << inFileName2 << " will get offset so"
                << " as not to overlap with the regions in " << inFileName1
                << nl << endl;
        }


        Info<< "Surface1 : " << inFileName1<< nl
            << "Surface2 : " << inFileName2<< nl
            << "Writing  : " << outFileName << nl << endl;
    }

    if (scaleFactor > 0)
    {
        Info<< "Scaling  : " << scaleFactor << nl;
    }

    const triSurface surface1(inFileName1, scaleFactor);
    Info<< "Surface1:" << endl;
    surface1.writeStats(Info);
    Info<< endl;

    const pointField& points1 = surface1.points();

    // Final surface
    triSurface combinedSurf;

    if (addPoint)
    {
        IFstream pointsFile(args["points"]);
        const pointField extraPoints(pointsFile);

        Info<< "Additional Points:" << extraPoints.size() << endl;

        vectorField pointsAll(points1);
        label pointi = pointsAll.size();
        pointsAll.setSize(pointsAll.size() + extraPoints.size());

        for (const auto& pt : extraPoints)
        {
            pointsAll[pointi++] = pt;
        }

        combinedSurf = triSurface(surface1, surface1.patches(), pointsAll);
    }
    else
    {
        const triSurface surface2(inFileName2, scaleFactor);
        Info<< "Surface2:" << endl;
        surface2.writeStats(Info);
        Info<< endl;


        // Make new storage
        List<labelledTri> facesAll(surface1.size() + surface2.size());

        const pointField& points2 = surface2.points();

        vectorField pointsAll(points1.size() + points2.size());


        label pointi = 0;
        // Copy points1 into pointsAll
        for (const auto& pt : points1)
        {
            pointsAll[pointi++] = pt;
        }
        // Add surface2 points
        for (const auto& pt : points2)
        {
            pointsAll[pointi++] = pt;
        }


        label trianglei = 0;

        // Determine map for both regions
        label nNewPatches = 0;
        labelList patch1Map(surface1.patches().size());
        labelList patch2Map(surface2.patches().size());

        if (mergeRegions)
        {
            HashTable<label> nameToPatch;

            forAll(surface1.patches(), i)
            {
                const word& name = surface1.patches()[i].name();
                auto iter = nameToPatch.find(name);

                label combinedi;
                if (iter.found())
                {
                    combinedi = iter.object();
                }
                else
                {
                    combinedi = nameToPatch.size();
                    nameToPatch.insert(name, combinedi);
                }
                patch1Map[i] = combinedi;
            }

            // Determine map for surface2 regions

            forAll(surface2.patches(), i)
            {
                const word& name = surface2.patches()[i].name();
                auto iter = nameToPatch.find(name);

                label combinedi;
                if (iter.found())
                {
                    combinedi = iter.object();
                }
                else
                {
                    combinedi = nameToPatch.size();
                    nameToPatch.insert(name, combinedi);
                }
                patch2Map[i] = combinedi;
            }

            nNewPatches = nameToPatch.size();
        }
        else
        {
            Info<< "Surface " << inFileName1
                << " has " << surface1.patches().size()
                << " regions"
                << nl
                << "All region numbers in " << inFileName2 << " will be offset"
                << " by this amount" << nl << endl;

            patch1Map = identity(surface1.patches().size());
            patch2Map = identity(surface2.patches().size(), patch1Map.size());

            nNewPatches = surface1.patches().size()+surface2.patches().size();
        }


        // Copy triangles1 into trianglesAll
        for (const labelledTri& tri : surface1)
        {
            labelledTri& destTri = facesAll[trianglei++];

            destTri.triFace::operator=(tri);
            destTri.region() = patch1Map[tri.region()];
        }

        // Add (renumbered) surface2 triangles
        for (const labelledTri& tri : surface2)
        {
            labelledTri& destTri = facesAll[trianglei++];
            destTri[0] = tri[0] + points1.size();
            destTri[1] = tri[1] + points1.size();
            destTri[2] = tri[2] + points1.size();
            destTri.region() = patch2Map[tri.region()];
        }


        geometricSurfacePatchList newPatches(nNewPatches);
        forAll(surface1.patches(), patchi)
        {
            newPatches[patch1Map[patchi]] = surface1.patches()[patchi];
        }
        forAll(surface2.patches(), patchi)
        {
            newPatches[patch2Map[patchi]] = surface2.patches()[patchi];
        }

        Info<< "New patches:" << nl;
        forAll(newPatches, patchi)
        {
            Info<< "    " << patchi << '\t' << newPatches[patchi].name() << nl;
        }
        Info<< endl;


        // Construct new surface mesh
        combinedSurf = triSurface(facesAll, newPatches, pointsAll);
    }

    // Merge all common points and do some checks
    combinedSurf.cleanup(true);

    Info<< "Merged surface:" << endl;

    combinedSurf.writeStats(Info);

    Info<< endl;

    Info<< "Writing : " << outFileName << endl;

    // If merging regions also sort
    combinedSurf.write(outFileName, mergeRegions);

    Info<< "End\n" << endl;

    return 0;
}


// ************************************************************************* //