FMSToSurface.C 4.92 KB
Newer Older
Franjo's avatar
Franjo committed
1
2
3
4
5
6
7
8
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | cfMesh: A library for mesh generation
   \\    /   O peration     |
    \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
     \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
-------------------------------------------------------------------------------
License
Andrew Heather's avatar
Andrew Heather committed
9
    This file is part of OpenFOAM.
Franjo's avatar
Franjo committed
10

Andrew Heather's avatar
Andrew Heather committed
11
12
13
14
    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.
Franjo's avatar
Franjo committed
15

Andrew Heather's avatar
Andrew Heather committed
16
    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
Franjo's avatar
Franjo committed
17
18
19
20
21
    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
Andrew Heather's avatar
Andrew Heather committed
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
Franjo's avatar
Franjo committed
23
24

Description
Mark Olesen's avatar
Mark Olesen committed
25
    Convert a FMS file to another surface format
Franjo's avatar
Franjo committed
26
27
28
29
30
31
32
33
34
35

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

#include "argList.H"
#include "triSurf.H"
#include "triSurfaceCopyParts.H"
#include "demandDrivenData.H"
#include "OFstream.H"

using namespace Foam;
36
using namespace Foam::Module;
Franjo's avatar
Franjo committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

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

void exportFeatureEdges
(
    const triSurf& origSurf,
    const fileName& edgeFileName
)
{
    OFstream file(edgeFileName);

    const pointField& points = origSurf.points();
    labelList newPointLabel(points.size(), -1);
    label nPoints(0);

    const edgeLongList& featureEdges = origSurf.featureEdges();
    forAll(featureEdges, feI)
    {
        const edge& e = featureEdges[feI];

Andrew Heather's avatar
Andrew Heather committed
57
        if (newPointLabel[e[0]] == -1)
Franjo's avatar
Franjo committed
58
            newPointLabel[e[0]] = nPoints++;
Andrew Heather's avatar
Andrew Heather committed
59
        if (newPointLabel[e[1]] == -1)
Franjo's avatar
Franjo committed
60
61
62
63
64
65
            newPointLabel[e[1]] = nPoints++;
    }

    pointField pCopy(nPoints);
    forAll(newPointLabel, pI)
    {
Andrew Heather's avatar
Andrew Heather committed
66
        if (newPointLabel[pI] < 0)
Franjo's avatar
Franjo committed
67
68
69
70
71
            continue;

        pCopy[newPointLabel[pI]] = points[pI];
    }

Andrew Heather's avatar
Andrew Heather committed
72
    // write the header
Franjo's avatar
Franjo committed
73
74
75
76
77
    file << "# vtk DataFile Version 3.0\n";
    file << "vtk output\n";
    file << "ASCII\n";
    file << "DATASET POLYDATA\n";

Andrew Heather's avatar
Andrew Heather committed
78
    // write points
Franjo's avatar
Franjo committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    file << "POINTS " << pCopy.size() << " float\n";
    forAll(pCopy, pI)
    {
        const point& p = pCopy[pI];
        file << p.x() << ' ' << p.y() << ' ' << p.z() << '\n';
    }

    file << "\nLINES " << featureEdges.size()
         << ' ' << 3*featureEdges.size() << nl;
    forAll(featureEdges, edgeI)
    {
        const edge& e = featureEdges[edgeI];
        file << "2 " << newPointLabel[e[0]]
             << token::SPACE << newPointLabel[e[1]] << nl;
    }
    file << nl;

Andrew Heather's avatar
Andrew Heather committed
96
97
98
    if (!file)
        FatalErrorInFunction
            << "Writting of feature edges failed!" << exit(FatalError);
Franjo's avatar
Franjo committed
99
100
}

Andrew Heather's avatar
Andrew Heather committed
101

Franjo's avatar
Franjo committed
102
103
int main(int argc, char *argv[])
{
Mark Olesen's avatar
Mark Olesen committed
104
105
106
107
108
109
    argList::addNote
    (
        "(cfmesh)\n"
        "Convert a FMS file to another surface format."
    );

Franjo's avatar
Franjo committed
110
111
112
113
114
    argList::noParallel();
    argList::validArgs.clear();

    argList::validArgs.append("input surface file");
    argList::validArgs.append("output surface file");
Mark Olesen's avatar
Mark Olesen committed
115
116
    argList::addBoolOption("exportSubsets");
    argList::addBoolOption("exportFeatureEdges");
Franjo's avatar
Franjo committed
117
118
    argList args(argc, argv);

Mark Olesen's avatar
Mark Olesen committed
119
120
    const fileName inFileName(args[1]);
    const fileName outFileName(args[2]);
Franjo's avatar
Franjo committed
121
122
123
124

    fileName outFileNoExt = outFileName.lessExt();
    fileName outExtension = outFileName.ext();

Andrew Heather's avatar
Andrew Heather committed
125
126
    Info<< "Out file no ext " << outFileNoExt << endl;
    Info<< "Extension " << outExtension << endl;
Franjo's avatar
Franjo committed
127

Andrew Heather's avatar
Andrew Heather committed
128
    // read the inout surface
Franjo's avatar
Franjo committed
129
130
    triSurf origSurf(inFileName);

Andrew Heather's avatar
Andrew Heather committed
131
    // write the surface in the requated format
Franjo's avatar
Franjo committed
132
133
    origSurf.writeSurface(outFileName);

Andrew Heather's avatar
Andrew Heather committed
134
    // export surface subsets as separate surface meshes
135
    if (args.found("exportSubsets"))
Franjo's avatar
Franjo committed
136
137
138
139
140
141
142
143
    {
        DynList<label> subsetIDs;
        origSurf.facetSubsetIndices(subsetIDs);

        triSurfaceCopyParts copyParts(origSurf);

        forAll(subsetIDs, subsetI)
        {
Andrew Heather's avatar
Andrew Heather committed
144
            // get the name of the subset
Franjo's avatar
Franjo committed
145
146
147
148
            triSurf copySurf;
            wordList subsetName(1);
            subsetName[0] = origSurf.facetSubsetName(subsetIDs[subsetI]);

Andrew Heather's avatar
Andrew Heather committed
149
            // create a surface mesh corresponding to the subset
Franjo's avatar
Franjo committed
150
151
            copyParts.copySurface(subsetName, copySurf);

Andrew Heather's avatar
Andrew Heather committed
152
            // write the mesh on disk
Franjo's avatar
Franjo committed
153
154
155
156
157
158
159
            fileName fName = outFileNoExt+"_facetSubset_"+subsetName[0];
            fName += '.'+outExtension;

            copySurf.writeSurface(fName);
        }
    }

160
    if (args.found("exportFeatureEdges"))
Franjo's avatar
Franjo committed
161
162
163
164
165
166
    {
        fileName fName = outFileNoExt+"_featureEdges";
        fName += ".vtk";
        exportFeatureEdges(origSurf, fName);
    }

Andrew Heather's avatar
Andrew Heather committed
167
    Info<< "End\n" << endl;
Franjo's avatar
Franjo committed
168
169
170
    return 0;
}

Andrew Heather's avatar
Andrew Heather committed
171

Franjo's avatar
Franjo committed
172
// ************************************************************************* //