File: foamyHexMeshSurfaceSimplify_non_octree.C

package info (click to toggle)
openfoam 4.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 163,028 kB
  • ctags: 58,990
  • sloc: cpp: 830,760; sh: 10,227; ansic: 8,215; xml: 745; lex: 437; awk: 194; sed: 91; makefile: 77; python: 18
file content (353 lines) | stat: -rw-r--r-- 9,670 bytes parent folder | download | duplicates (2)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*---------------------------------------------------------------------------*\
 =========                   |
 \\      /   F ield          | OpenFOAM: The Open Source CFD Toolbox
  \\    /    O peration      |
   \\  /     A nd            | Copyright (C) 2012-2016 OpenFOAM Foundation
    \\/      M anipulation   |
-------------------------------------------------------------------------------
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
    foamyHexMeshSurfaceSimplify

Description
    Simplifies surfaces by resampling.

    Uses Thomas Lewiner's topology preserving MarchingCubes.

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

#include "argList.H"
#include "Time.H"
#include "searchableSurfaces.H"
#include "conformationSurfaces.H"
#include "triSurfaceMesh.H"

#include "MarchingCubes.h"


using namespace Foam;

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

// Main program:

int main(int argc, char *argv[])
{
    argList::addNote
    (
        "Re-sample surfaces used in foamyHexMesh operation"
    );
    //argList::validArgs.append("inputFile");
    argList::validArgs.append("(nx ny nz)");
    argList::validArgs.append("outputName");

    #include "setRootCase.H"
    #include "createTime.H"
    runTime.functionObjects().off();

    const Vector<label> n(IStringStream(args.args()[1])());
    const fileName exportName = args.args()[2];

    Info<< "Reading surfaces as specified in the foamyHexMeshDict and"
        << " writing re-sampled " << n << " to " << exportName
        << nl << endl;

    cpuTime timer;

    IOdictionary foamyHexMeshDict
    (
        IOobject
        (
            "foamyHexMeshDict",
            runTime.system(),
            runTime,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    );

    // Define/load all geometry
    searchableSurfaces allGeometry
    (
        IOobject
        (
            "cvSearchableSurfaces",
            runTime.constant(),
            "triSurface",
            runTime,
            IOobject::MUST_READ,
            IOobject::NO_WRITE
        ),
        foamyHexMeshDict.subDict("geometry"),
        foamyHexMeshDict.lookupOrDefault("singleRegionName", true)
    );

    Info<< "Geometry read in = "
        << timer.cpuTimeIncrement() << " s." << nl << endl;


    Random rndGen(64293*Pstream::myProcNo());

    conformationSurfaces geometryToConformTo
    (
        runTime,
        rndGen,
        allGeometry,
        foamyHexMeshDict.subDict("surfaceConformation")
    );

    Info<< "Set up geometry in = "
        << timer.cpuTimeIncrement() << " s." << nl << endl;



    // Extend
    treeBoundBox bb = geometryToConformTo.globalBounds();
    {
        const vector smallVec = 0.1*bb.span();
        bb.min() -= smallVec;
        bb.max() += smallVec;
    }

    Info<< "Meshing to bounding box " << bb << nl << endl;

    const vector span(bb.span());
    const vector d
    (
        span.x()/(n.x()-1),
        span.y()/(n.y()-1),
        span.z()/(n.z()-1)
    );

    MarchingCubes mc(span.x(), span.y(), span.z() ) ;
    mc.set_resolution(n.x(), n.y(), n.z());
    mc.init_all() ;


    // Generate points
    pointField points(mc.size_x()*mc.size_y()*mc.size_z());
    label pointi = 0;

    point pt;
    for( int k = 0 ; k < mc.size_z() ; k++ )
    {
        pt.z() = bb.min().z() + k*d.z();
        for( int j = 0 ; j < mc.size_y() ; j++ )
        {
            pt.y() = bb.min().y() + j*d.y();
            for( int i = 0 ; i < mc.size_x() ; i++ )
            {
                pt.x() = bb.min().x() + i*d.x();
                points[pointi++] = pt;
            }
        }
    }

    Info<< "Generated " << points.size() << " sampling points in = "
        << timer.cpuTimeIncrement() << " s." << nl << endl;


    // Compute data
    const searchableSurfaces& geometry = geometryToConformTo.geometry();
    const labelList& surfaces = geometryToConformTo.surfaces();

    scalarField signedDist;
    labelList nearestSurfaces;
    searchableSurfacesQueries::signedDistance
    (
        geometry,
        surfaces,
        points,
        scalarField(points.size(), sqr(GREAT)),
        searchableSurface::OUTSIDE,     // for non-closed surfaces treat as
                                        // outside
        nearestSurfaces,
        signedDist
    );

    // Fill elements
    pointi = 0;
    for( int k = 0 ; k < mc.size_z() ; k++ )
    {
        for( int j = 0 ; j < mc.size_y() ; j++ )
        {
            for( int i = 0 ; i < mc.size_x() ; i++ )
            {
                mc.set_data(float(signedDist[pointi++]), i, j, k);
            }
        }
    }

    Info<< "Determined signed distance in = "
        << timer.cpuTimeIncrement() << " s." << nl << endl;


    mc.run() ;

    Info<< "Constructed iso surface in = "
        << timer.cpuTimeIncrement() << " s." << nl << endl;


    mc.clean_temps() ;



    // Write output file
    if (mc.ntrigs() > 0)
    {
        Triangle* triangles = mc.triangles();
        List<labelledTri> tris(mc.ntrigs());
        forAll(tris, triI)
        {
            tris[triI] = labelledTri
            (
                triangles[triI].v1,
                triangles[triI].v2,
                triangles[triI].v3,
                0                       // region
            );
        }


        Vertex* vertices = mc.vertices();
        pointField points(mc.nverts());
        forAll(points, pointi)
        {
            Vertex& v = vertices[pointi];
            points[pointi] = point
            (
                bb.min().x() + v.x*span.x()/mc.size_x(),
                bb.min().y() + v.y*span.y()/mc.size_y(),
                bb.min().z() + v.z*span.z()/mc.size_z()
            );
        }


        // Find correspondence to original surfaces
        labelList regionOffsets(surfaces.size());
        label nRegions = 0;
        forAll(surfaces, i)
        {
            const wordList& regions = geometry[surfaces[i]].regions();
            regionOffsets[i] = nRegions;
            nRegions += regions.size();
        }


        geometricSurfacePatchList patches(nRegions);
        nRegions = 0;
        forAll(surfaces, i)
        {
            const wordList& regions = geometry[surfaces[i]].regions();

            forAll(regions, regionI)
            {
                patches[nRegions] = geometricSurfacePatch
                (
                    "patch",
                    geometry[surfaces[i]].name() + "_" + regions[regionI],
                    nRegions
                );
                nRegions++;
            }
        }

        triSurface s(tris, patches, points, true);

        Info<< "Extracted triSurface in = "
            << timer.cpuTimeIncrement() << " s." << nl << endl;


        // Find out region on local surface of nearest point
        {
            List<pointIndexHit> hitInfo;
            labelList hitSurfaces;
            geometryToConformTo.findSurfaceNearest
            (
                s.faceCentres(),
                scalarField(s.size(), sqr(GREAT)),
                hitInfo,
                hitSurfaces
            );

            // Get region
            DynamicList<pointIndexHit> surfInfo(hitSurfaces.size());
            DynamicList<label> surfIndices(hitSurfaces.size());

            forAll(surfaces, surfI)
            {
                // Extract info on this surface
                surfInfo.clear();
                surfIndices.clear();
                forAll(hitSurfaces, triI)
                {
                    if (hitSurfaces[triI] == surfI)
                    {
                        surfInfo.append(hitInfo[triI]);
                        surfIndices.append(triI);
                    }
                }

                // Calculate sideness of these surface points
                labelList region;
                geometry[surfaces[surfI]].getRegion(surfInfo, region);

                forAll(region, i)
                {
                    label triI = surfIndices[i];
                    s[triI].region() = regionOffsets[surfI]+region[i];
                }
            }
        }

        Info<< "Re-patched surface in = "
            << timer.cpuTimeIncrement() << " s." << nl << endl;

        triSurfaceMesh smesh
        (
            IOobject
            (
                exportName,
                runTime.constant(), // instance
                "triSurface",
                runTime,            // registry
                IOobject::NO_READ,
                IOobject::AUTO_WRITE,
                false
            ),
            s
        );

        Info<< "writing surfMesh:\n  "
            << smesh.searchableSurface::objectPath() << nl << endl;
        smesh.searchableSurface::write();

        Info<< "Written surface in = "
            << timer.cpuTimeIncrement() << " s." << nl << endl;
    }

    mc.clean_all() ;


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

    return 0;
}


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