File size: 12,536 Bytes
5a600cf | 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2019 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/>.
\*---------------------------------------------------------------------------*/
#include "AC3DsurfaceFormat.H"
#include "clock.H"
#include "IStringStream.H"
#include "tensor.H"
#include "primitivePatch.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Face>
Foam::fileFormats::AC3DsurfaceFormat<Face>::AC3DsurfaceFormat
(
const fileName& filename
)
{
read(filename);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Face>
bool Foam::fileFormats::AC3DsurfaceFormat<Face>::read
(
const fileName& filename
)
{
const bool mustTriangulate = this->isTri();
this->clear();
IFstream is(filename);
if (!is.good())
{
FatalErrorInFunction
<< "Cannot read file " << filename
<< exit(FatalError);
}
string line, cmd, args;
is.getLine(line);
string version = line.substr(4);
if (version != "b")
{
WarningInFunction
<< "When reading AC3D file " << filename
<< " read header " << line << " with version "
<< version << endl
<< "Only tested reading with version 'b'."
<< " This might give problems" << endl;
}
if (!cueTo(is, "OBJECT", args) || (args != "world"))
{
FatalErrorInFunction
<< "Cannot find \"OBJECT world\" in file " << filename
<< exit(FatalError);
}
// # of kids is the # of zones
args = cueToOrDie(is, "kids");
label nZones = parse<int>(args);
// Start of vertices for object/zones
label vertexOffset = 0;
DynamicList<point> dynPoints;
DynamicList<Face> dynFaces;
List<word> names(nZones);
List<label> sizes(nZones, 0);
for (label zoneI = 0; zoneI < nZones; ++zoneI)
{
names[zoneI] = word("zone") + Foam::name(zoneI);
args = cueToOrDie(is, "OBJECT", "while reading " + names[zoneI]);
// number of vertices for this zone
label nZonePoints = 0;
vector location(Zero);
// tensor rotation(I);
// Read all info for current zone
while (is.good())
{
// Read line and get first word. If end of file break since
// zone should always end with 'kids' command ?not sure.
if (!readCmd(is, cmd, args))
{
FatalErrorInFunction
<< "Did not read up to \"kids 0\" while reading zone "
<< zoneI << " from file " << filename
<< exit(FatalError);
}
if (cmd == "name")
{
// name %s
string str = parse<string>(args);
string::stripInvalid<word>(str);
names[zoneI] = str;
}
else if (cmd == "rot")
{
// rot %f %f %f %f %f %f %f %f %f
// IStringStream lineStream(args);
//
// lineStream
// >> rotation.xx() >> rotation.xy() >> rotation.xz()
// >> rotation.yx() >> rotation.yy() >> rotation.yz()
// >> rotation.zx() >> rotation.zy() >> rotation.zz();
WarningInFunction
<< "rot (rotation tensor) command not implemented"
<< "Line:" << cmd << ' ' << args << endl
<< "while reading zone " << zoneI << endl;
}
else if (cmd == "loc")
{
// loc %f %f %f
IStringStream lineStream(args);
lineStream
>> location.x()
>> location.y()
>> location.z();
}
else if (cmd == "numvert")
{
// numvert %d
nZonePoints = parse<int>(args);
for (label vertI = 0; vertI < nZonePoints; ++vertI)
{
is.getLine(line);
IStringStream lineStream(line);
point pt;
lineStream
>> pt.x() >> pt.y() >> pt.z();
// Offset with current translation vector
dynPoints.append(location + pt);
}
}
else if (cmd == "numsurf")
{
label nFaces = parse<int>(args);
for (label facei = 0; facei < nFaces; ++facei)
{
static string errorMsg =
string(" while reading face ")
+ Foam::name(facei) + " on zone "
+ Foam::name(zoneI)
+ " from file " + filename;
cueToOrDie(is, "SURF", errorMsg);
cueToOrDie(is, "mat", errorMsg);
args = cueToOrDie(is, "refs", errorMsg);
label nVert = parse<int>(args);
List<label> verts(nVert);
forAll(verts, vertI)
{
is.getLine(line);
verts[vertI] = parse<int>(line) + vertexOffset;
}
labelUList& f = static_cast<labelUList&>(verts);
if (mustTriangulate && f.size() > 3)
{
// simple face triangulation about f[0]
// points may be incomplete
for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
{
label fp2 = f.fcIndex(fp1);
dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
sizes[zoneI]++;
}
}
else
{
dynFaces.append(Face(f));
sizes[zoneI]++;
}
}
// Done the current zone.
// Increment the offset vertices are stored at
vertexOffset += nZonePoints;
}
else if (cmd == "kids")
{
// 'kids' denotes the end of the current zone.
label nKids = parse<int>(args);
if (nKids != 0)
{
FatalErrorInFunction
<< "Can only read objects without kids."
<< " Encountered " << nKids << " kids when"
<< " reading zone " << zoneI
<< exit(FatalError);
}
// Done reading current zone
break;
}
}
}
// transfer to normal lists
this->storedPoints().transfer(dynPoints);
this->storedFaces().transfer(dynFaces);
// add zones, culling empty ones
this->addZones(sizes, names, true);
this->stitchFaces(small);
return true;
}
template<class Face>
void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
(
const fileName& filename,
const MeshedSurfaceProxy<Face>& surf
)
{
const pointField& pointLst = surf.points();
const List<Face>& faceLst = surf.faces();
const List<surfZone>& zones =
(
surf.surfZones().size()
? surf.surfZones()
: surfaceFormatsCore::oneZone(faceLst)
);
const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
if (useFaceMap)
{
FatalErrorInFunction
<< "output with faceMap is not supported " << filename
<< exit(FatalError);
}
OFstream os(filename);
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< exit(FatalError);
}
writeHeader(os, zones);
forAll(zones, zoneI)
{
const surfZone& zone = zones[zoneI];
os << "OBJECT poly" << nl
<< "name \"" << zone.name() << "\"\n";
// Temporary PrimitivePatch to calculate compact points & faces
// use 'UList' to avoid allocations!
PrimitivePatch<UList<Face>, const pointField&> patch
(
SubList<Face>
(
faceLst,
zone.size(),
zone.start()
),
pointLst
);
os << "numvert " << patch.nPoints() << endl;
forAll(patch.localPoints(), ptI)
{
const point& pt = patch.localPoints()[ptI];
os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
}
os << "numsurf " << patch.localFaces().size() << endl;
forAll(patch.localFaces(), localFacei)
{
const Face& f = patch.localFaces()[localFacei];
os << "SURF 0x20" << nl // polygon
<< "mat " << zoneI << nl
<< "refs " << f.size() << nl;
forAll(f, fp)
{
os << f[fp] << " 0 0" << nl;
}
}
os << "kids 0" << endl;
}
}
template<class Face>
void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
(
const fileName& filename,
const UnsortedMeshedSurface<Face>& surf
)
{
labelList faceMap;
List<surfZone> zoneLst = surf.sortedZones(faceMap);
if (zoneLst.size() <= 1)
{
write
(
filename,
MeshedSurfaceProxy<Face>
(
surf.points(),
surf.faces(),
zoneLst
)
);
}
else
{
OFstream os(filename);
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< exit(FatalError);
}
writeHeader(os, zoneLst);
label faceIndex = 0;
forAll(zoneLst, zoneI)
{
const surfZone& zone = zoneLst[zoneI];
os << "OBJECT poly" << nl
<< "name \"" << zone.name() << "\"\n";
// Create zone with only zone faces included for ease of addressing
labelHashSet include(surf.size());
forAll(zone, localFacei)
{
const label facei = faceMap[faceIndex++];
include.insert(facei);
}
UnsortedMeshedSurface<Face> subm = surf.subsetMesh(include);
// Now we have isolated surface for this patch alone. Write it.
os << "numvert " << subm.nPoints() << endl;
forAll(subm.localPoints(), ptI)
{
const point& pt = subm.localPoints()[ptI];
os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
}
os << "numsurf " << subm.localFaces().size() << endl;
forAll(subm.localFaces(), localFacei)
{
const Face& f = subm.localFaces()[localFacei];
os << "SURF 0x20" << nl // polygon
<< "mat " << zoneI << nl
<< "refs " << f.size() << nl;
forAll(f, fp)
{
os << f[fp] << " 0 0" << nl;
}
}
os << "kids 0" << endl;
}
}
}
// ************************************************************************* //
|