surfaceCoarsen.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "argList.H"
00037 #include "fileName.H"
00038 #include "triSurface.H"
00039 #include "OFstream.H"
00040 #include "triFace.H"
00041 #include "triFaceList.H"
00042
00043
00044 #include "progmesh.h"
00045
00046 using namespace Foam;
00047
00048
00049
00050 int mapVertex(::List<int>& collapse_map, int a, int mx)
00051 {
00052 if (mx <= 0)
00053 {
00054 return 0;
00055 }
00056 while (a >= mx)
00057 {
00058 a = collapse_map[a];
00059 }
00060 return a;
00061 }
00062
00063
00064
00065
00066 int main(int argc, char *argv[])
00067 {
00068 argList::noParallel();
00069 argList::validArgs.clear();
00070 argList::validArgs.append("Foam surface file");
00071 argList::validArgs.append("reduction factor");
00072 argList::validArgs.append("Foam output file");
00073 argList args(argc, argv);
00074
00075 fileName inFileName(args.additionalArgs()[0]);
00076
00077 scalar reduction(readScalar(IStringStream(args.additionalArgs()[1])()));
00078
00079 if (reduction <= 0 || reduction > 1)
00080 {
00081 FatalErrorIn(args.executable())
00082 << "Reduction factor " << reduction
00083 << " should be within 0..1" << endl
00084 << "(it is the reduction in number of vertices)"
00085 << exit(FatalError);
00086 }
00087
00088 fileName outFileName(args.additionalArgs()[2]);
00089
00090 Info<< "Input surface :" << inFileName << endl
00091 << "Reduction factor:" << reduction << endl
00092 << "Output surface :" << outFileName << endl << endl;
00093
00094 const triSurface surf(inFileName);
00095
00096 Info<< "Surface:" << endl;
00097 surf.writeStats(Info);
00098 Info<< endl;
00099
00100
00101 ::List< ::Vector> vert;
00102 ::List< ::tridata> tri;
00103
00104
00105
00106
00107 const pointField& pts = surf.points();
00108
00109 forAll(pts, ptI)
00110 {
00111 const point& pt = pts[ptI];
00112
00113 vert.Add( ::Vector(pt.x(), pt.y(), pt.z()));
00114 }
00115
00116 forAll(surf, faceI)
00117 {
00118 const labelledTri& f = surf[faceI];
00119
00120 tridata td;
00121 td.v[0]=f[0];
00122 td.v[1]=f[1];
00123 td.v[2]=f[2];
00124 tri.Add(td);
00125 }
00126
00127 ::List<int> collapse_map;
00128 ::List<int> permutation;
00129
00130 ::ProgressiveMesh(vert,tri,collapse_map,permutation);
00131
00132
00133 ::List< ::Vector> temp_list;
00134 for(int i=0;i<vert.num;i++)
00135 {
00136 temp_list.Add(vert[i]);
00137 }
00138 for(int i=0;i<vert.num;i++)
00139 {
00140 vert[permutation[i]]=temp_list[i];
00141 }
00142
00143
00144 for(int i=0;i<tri.num;i++)
00145 {
00146 for(int j=0;j<3;j++)
00147 {
00148 tri[i].v[j] = permutation[tri[i].v[j]];
00149 }
00150 }
00151
00152
00153 int render_num = int(reduction * surf.nPoints());
00154
00155 Info<< "Reducing to " << render_num << " vertices" << endl;
00156
00157
00158
00159 Foam::List<labelledTri> newTris(surf.size());
00160
00161 label newI = 0;
00162
00163 for (int i=0; i<tri.num; i++)
00164 {
00165 int p0 = mapVertex(collapse_map, tri[i].v[0], render_num);
00166 int p1 = mapVertex(collapse_map, tri[i].v[1], render_num);
00167 int p2 = mapVertex(collapse_map, tri[i].v[2], render_num);
00168
00169
00170
00171
00172 if (p0 == p1 || p1 == p2 || p2 == p0)
00173 {
00174 continue;
00175 }
00176
00177 newTris[newI++] = labelledTri(p0, p1, p2, 0);
00178 }
00179 newTris.setSize(newI);
00180
00181
00182 pointField newPoints(vert.num);
00183
00184 for(int i=0; i<vert.num; i++)
00185 {
00186 const ::Vector & v = vert[i];
00187
00188 newPoints[i] = point(v.x, v.y, v.z);
00189 }
00190
00191 triSurface surf2(newTris, newPoints);
00192
00193 triSurface outSurf
00194 (
00195 surf2.localFaces(),
00196 surf2.patches(),
00197 surf2.localPoints()
00198 );
00199
00200 Info<< "Coarsened surface:" << endl;
00201 surf2.writeStats(Info);
00202 Info<< endl;
00203
00204 Info<< "Writing to file " << outFileName << endl << endl;
00205
00206 surf2.write(outFileName);
00207
00208 Info << "End\n" << endl;
00209
00210 return 0;
00211 }
00212
00213
00214
Copyright © 2000-2009 OpenCFD Ltd