surfaceGenerateBoundingBox.C 5.97 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 25 26 27 28 29 30 31

Description
    Finds feature edges and corners of a triangulated surface

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

#include "argList.H"
#include "IFstream.H"
#include "fileName.H"
32 33
#include "triSurf.H"
#include "triSurfModifier.H"
Franjo's avatar
Franjo committed
34 35
#include "boundBox.H"
#include "OFstream.H"
36

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 57 58
#include <cstdlib>
#include <sstream>

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
using namespace Foam;

int main(int argc, char *argv[])
{
    argList::noParallel();
    argList::validArgs.clear();
    argList::validArgs.append("input surface file");
    argList::validArgs.append("output surface file");
    argList::validArgs.append("x-neg");
    argList::validArgs.append("x-pos");
    argList::validArgs.append("y-neg");
    argList::validArgs.append("y-pos");
    argList::validArgs.append("z-neg");
    argList::validArgs.append("z-pos");

    argList args(argc, argv);

Mark Olesen's avatar
Mark Olesen committed
59 60
    fileName inFileName(args[1]);
    fileName outFileName(args[2]);
Franjo's avatar
Franjo committed
61 62 63

    if (outFileName == inFileName)
    {
Andrew Heather's avatar
Andrew Heather committed
64
        FatalErrorInFunction
Franjo's avatar
Franjo committed
65 66 67 68 69
            << "Output file " << outFileName
            << " would overwrite the input file."
            << exit(FatalError);
    }

70 71 72
    triSurf origSurface(inFileName);
    triSurfModifier sMod(origSurface);
    pointField& points = sMod.pointsAccess();
Franjo's avatar
Franjo committed
73 74 75 76

    const boundBox bb(points);

    vector negOffset, posOffset;
Andrew Heather's avatar
Andrew Heather committed
77
    for (label i = 3; i < 9; ++i)
Franjo's avatar
Franjo committed
78 79
    {
        std::stringstream ss;
Mark Olesen's avatar
Mark Olesen committed
80
        ss << args[i];
Franjo's avatar
Franjo committed
81 82 83 84

        scalar s;
        ss >> s;

Andrew Heather's avatar
Andrew Heather committed
85
        if (i % 2)
Franjo's avatar
Franjo committed
86
        {
Andrew Heather's avatar
Andrew Heather committed
87
            negOffset[(i - 3)/2] = s;
Franjo's avatar
Franjo committed
88 89 90
        }
        else
        {
Andrew Heather's avatar
Andrew Heather committed
91
            posOffset[(i - 3)/2] = s;
Franjo's avatar
Franjo committed
92 93 94
        }
    }

Andrew Heather's avatar
Andrew Heather committed
95 96
    Info<< "Neg offset " << negOffset << endl;
    Info<< "Pos offset " << posOffset << endl;
Franjo's avatar
Franjo committed
97 98

    const boundBox newBB(bb.min()-negOffset, bb.max()+posOffset);
Andrew Heather's avatar
Andrew Heather committed
99 100
    Info<< "Surface bounding box " << bb << endl;
    Info<< "Generated bounding box " << newBB << endl;
Franjo's avatar
Franjo committed
101

Andrew Heather's avatar
Andrew Heather committed
102
    // generate bounding box points
Franjo's avatar
Franjo committed
103
    const label nPoints = points.size();
104
    points.setSize(nPoints + 8);
Franjo's avatar
Franjo committed
105

106
    points[nPoints] = newBB.min();
Andrew Heather's avatar
Andrew Heather committed
107
    points[nPoints + 1] =
Franjo's avatar
Franjo committed
108
        point(newBB.max().x(), newBB.min().y(), newBB.min().z());
Andrew Heather's avatar
Andrew Heather committed
109
    points[nPoints + 2] =
Franjo's avatar
Franjo committed
110
        point(newBB.min().x(), newBB.max().y(), newBB.min().z());
Andrew Heather's avatar
Andrew Heather committed
111
    points[nPoints + 3] =
Franjo's avatar
Franjo committed
112
        point(newBB.max().x(), newBB.max().y(), newBB.min().z());
Andrew Heather's avatar
Andrew Heather committed
113
    points[nPoints + 4] =
Franjo's avatar
Franjo committed
114
        point(newBB.min().x(), newBB.min().y(), newBB.max().z());
Andrew Heather's avatar
Andrew Heather committed
115
    points[nPoints + 5] =
Franjo's avatar
Franjo committed
116
        point(newBB.max().x(), newBB.min().y(), newBB.max().z());
Andrew Heather's avatar
Andrew Heather committed
117
    points[nPoints + 6] =
Franjo's avatar
Franjo committed
118
        point(newBB.min().x(), newBB.max().y(), newBB.max().z());
Andrew Heather's avatar
Andrew Heather committed
119
    points[nPoints + 7] = newBB.max();
Franjo's avatar
Franjo committed
120

Andrew Heather's avatar
Andrew Heather committed
121
    // generate bounding bound triangles
Franjo's avatar
Franjo committed
122
    const label nTriangles = origSurface.size();
123
    LongList<labelledTri>& newTriangles = sMod.facetsAccess();
Andrew Heather's avatar
Andrew Heather committed
124
    newTriangles.setSize(nTriangles + 12);
Franjo's avatar
Franjo committed
125

Andrew Heather's avatar
Andrew Heather committed
126
    // create patches
127
    geometricSurfacePatchList& newPatches = sMod.patchesAccess();
Franjo's avatar
Franjo committed
128
    const label nPatches = origSurface.patches().size();
Andrew Heather's avatar
Andrew Heather committed
129
    newPatches.setSize(nPatches + 6);
130

Franjo's avatar
Franjo committed
131
    newPatches[nPatches].name() = "xMin";
Andrew Heather's avatar
Andrew Heather committed
132 133 134 135 136
    newPatches[nPatches + 1].name() = "xMax";
    newPatches[nPatches + 2].name() = "yMin";
    newPatches[nPatches + 3].name() = "yMax";
    newPatches[nPatches + 4].name() = "zMin";
    newPatches[nPatches + 5].name() = "zMax";
Franjo's avatar
Franjo committed
137

Andrew Heather's avatar
Andrew Heather committed
138
    // negative x direction
Franjo's avatar
Franjo committed
139
    newTriangles[nTriangles] =
Andrew Heather's avatar
Andrew Heather committed
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
        labelledTri(nPoints, nPoints + 6, nPoints + 2, nPatches);
    newTriangles[nTriangles + 1] =
        labelledTri(nPoints, nPoints + 4, nPoints + 6, nPatches);
    // positive x direction
    newTriangles[nTriangles + 2] =
        labelledTri(nPoints + 1, nPoints + 3, nPoints + 7, nPatches + 1);
    newTriangles[nTriangles + 3] =
        labelledTri(nPoints + 1, nPoints + 7, nPoints + 5, nPatches + 1);
    // negative y direction
    newTriangles[nTriangles + 4] =
        labelledTri(nPoints, nPoints + 1, nPoints + 5, nPatches + 2);
    newTriangles[nTriangles + 5] =
        labelledTri(nPoints, nPoints + 5, nPoints + 4, nPatches + 2);
    // positive y direction
    newTriangles[nTriangles + 6] =
        labelledTri(nPoints + 2, nPoints + 7, nPoints + 3, nPatches + 3);
    newTriangles[nTriangles + 7] =
        labelledTri(nPoints + 2, nPoints + 6, nPoints + 7, nPatches + 3);
    // negative z direction
    newTriangles[nTriangles + 8] =
        labelledTri(nPoints, nPoints + 2, nPoints + 3, nPatches + 4);
    newTriangles[nTriangles + 9] =
        labelledTri(nPoints, nPoints + 3, nPoints + 1, nPatches + 4);
    // positive z direction
    newTriangles[nTriangles + 10] =
        labelledTri(nPoints + 4, nPoints + 7, nPoints + 6, nPatches + 5);
    newTriangles[nTriangles + 11] =
        labelledTri(nPoints + 4, nPoints + 5, nPoints + 7, nPatches + 5);

    // write the surface
170
    origSurface.writeSurface(outFileName);
Franjo's avatar
Franjo committed
171

Andrew Heather's avatar
Andrew Heather committed
172
    Info<< "End\n" << endl;
Franjo's avatar
Franjo committed
173 174 175 176 177 178

    return 0;
}


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