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 #include "STLsurfaceFormat.H"
00028 #include "ListOps.H"
00029 #include "triPointRef.H"
00030
00031
00032
00033 template<class Face>
00034 inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
00035 (
00036 Ostream& os,
00037 const pointField& pointLst,
00038 const Face& f
00039 )
00040 {
00041
00042 vector norm = triPointRef
00043 (
00044 pointLst[f[0]],
00045 pointLst[f[1]],
00046 pointLst[f[2]]
00047 ).normal();
00048 norm /= mag(norm) + VSMALL;
00049
00050
00051
00052 const point& p0 = pointLst[f[0]];
00053 for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
00054 {
00055 label fp2 = f.fcIndex(fp1);
00056
00057 const point& p1 = pointLst[f[fp1]];
00058 const point& p2 = pointLst[f[fp2]];
00059
00060
00061 os << " facet normal "
00062 << norm.x() << ' ' << norm.y() << ' ' << norm.z() << nl
00063 << " outer loop\n"
00064 << " vertex " << p0.x() << ' ' << p0.y() << ' ' << p0.z() << nl
00065 << " vertex " << p1.x() << ' ' << p1.y() << ' ' << p1.z() << nl
00066 << " vertex " << p2.x() << ' ' << p2.y() << ' ' << p2.z() << nl
00067 << " endloop\n"
00068 << " endfacet" << endl;
00069 }
00070 }
00071
00072
00073 template<class Face>
00074 inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
00075 (
00076 ostream& os,
00077 const pointField& pointLst,
00078 const Face& f,
00079 const label zoneI
00080 )
00081 {
00082
00083 vector norm = triPointRef
00084 (
00085 pointLst[f[0]],
00086 pointLst[f[1]],
00087 pointLst[f[2]]
00088 ).normal();
00089 norm /= mag(norm) + VSMALL;
00090
00091
00092
00093 const point& p0 = pointLst[f[0]];
00094 for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
00095 {
00096 label fp2 = f.fcIndex(fp1);
00097
00098 STLtriangle stlTri
00099 (
00100 norm,
00101 p0,
00102 pointLst[f[fp1]],
00103 pointLst[f[fp2]],
00104 zoneI
00105 );
00106
00107 stlTri.write(os);
00108 }
00109 }
00110
00111
00112
00113
00114 template<class Face>
00115 Foam::fileFormats::STLsurfaceFormat<Face>::STLsurfaceFormat
00116 (
00117 const fileName& filename
00118 )
00119 {
00120 read(filename);
00121 }
00122
00123
00124
00125
00126 template<class Face>
00127 bool Foam::fileFormats::STLsurfaceFormat<Face>::read
00128 (
00129 const fileName& filename
00130 )
00131 {
00132 this->clear();
00133
00134
00135 STLsurfaceFormatCore reader(filename);
00136
00137
00138 this->storedPoints().transfer(reader.points());
00139
00140
00141 List<word> names(reader.names().xfer());
00142 List<label> sizes(reader.sizes().xfer());
00143 List<label> zoneIds(reader.zoneIds().xfer());
00144
00145
00146 List<Face> faceLst(zoneIds.size());
00147
00148 if (reader.sorted())
00149 {
00150
00151 forAll(faceLst, faceI)
00152 {
00153 const label startPt = 3*faceI;
00154 faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
00155 }
00156 }
00157 else
00158 {
00159
00160
00161 List<label> faceMap;
00162 sortedOrder(zoneIds, faceMap);
00163
00164
00165 forAll(faceMap, faceI)
00166 {
00167 const label startPt = 3*faceMap[faceI];
00168 faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
00169 }
00170 }
00171 zoneIds.clear();
00172
00173
00174 this->storedFaces().transfer(faceLst);
00175
00176 if (names.size())
00177 {
00178 this->addZones(sizes, names);
00179 }
00180 else
00181 {
00182 this->addZones(sizes);
00183 }
00184
00185 this->stitchFaces(SMALL);
00186 return true;
00187 }
00188
00189
00190
00191 template<class Face>
00192 void Foam::fileFormats::STLsurfaceFormat<Face>::writeAscii
00193 (
00194 const fileName& filename,
00195 const MeshedSurfaceProxy<Face>& surf
00196 )
00197 {
00198 OFstream os(filename);
00199 if (!os.good())
00200 {
00201 FatalErrorIn
00202 (
00203 "fileFormats::STLsurfaceFormat::writeAscii"
00204 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00205 )
00206 << "Cannot open file for writing " << filename
00207 << exit(FatalError);
00208 }
00209
00210 const pointField& pointLst = surf.points();
00211 const List<Face>& faceLst = surf.faces();
00212 const List<label>& faceMap = surf.faceMap();
00213
00214 const List<surfZone>& zones =
00215 (
00216 surf.surfZones().size() > 1
00217 ? surf.surfZones()
00218 : oneZone(faceLst)
00219 );
00220
00221 const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
00222
00223 label faceIndex = 0;
00224 forAll(zones, zoneI)
00225 {
00226
00227 const surfZone& zone = zones[zoneI];
00228
00229 os << "solid " << zone.name() << nl;
00230
00231 if (useFaceMap)
00232 {
00233 forAll(zone, localFaceI)
00234 {
00235 const label faceI = faceMap[faceIndex++];
00236 writeShell(os, pointLst, faceLst[faceI]);
00237 }
00238 }
00239 else
00240 {
00241 forAll(zone, localFaceI)
00242 {
00243 writeShell(os, pointLst, faceLst[faceIndex++]);
00244 }
00245 }
00246 os << "endsolid " << zone.name() << endl;
00247 }
00248 }
00249
00250
00251 template<class Face>
00252 void Foam::fileFormats::STLsurfaceFormat<Face>::writeBinary
00253 (
00254 const fileName& filename,
00255 const MeshedSurfaceProxy<Face>& surf
00256 )
00257 {
00258 std::ofstream os(filename.c_str(), std::ios::binary);
00259 if (!os.good())
00260 {
00261 FatalErrorIn
00262 (
00263 "fileFormats::STLsurfaceFormat::writeBinary"
00264 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00265 )
00266 << "Cannot open file for writing " << filename
00267 << exit(FatalError);
00268 }
00269
00270
00271 const pointField& pointLst = surf.points();
00272 const List<Face>& faceLst = surf.faces();
00273 const List<label>& faceMap = surf.faceMap();
00274
00275 const List<surfZone>& zones =
00276 (
00277 surf.surfZones().size() > 1
00278 ? surf.surfZones()
00279 : oneZone(faceLst)
00280 );
00281
00282 const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
00283
00284
00285 unsigned int nTris = 0;
00286 if (MeshedSurface<Face>::isTri())
00287 {
00288 nTris = faceLst.size();
00289 }
00290 else
00291 {
00292
00293 forAll(faceLst, faceI)
00294 {
00295 nTris += faceLst[faceI].size() - 2;
00296 }
00297 }
00298
00299
00300 STLsurfaceFormatCore::writeHeaderBINARY(os, nTris);
00301
00302 label faceIndex = 0;
00303 forAll(zones, zoneI)
00304 {
00305 const surfZone& zone = zones[zoneI];
00306
00307 if (useFaceMap)
00308 {
00309 forAll(zone, localFaceI)
00310 {
00311 writeShell
00312 (
00313 os,
00314 pointLst,
00315 faceLst[faceMap[faceIndex++]],
00316 zoneI
00317 );
00318 }
00319 }
00320 else
00321 {
00322 forAll(zone, localFaceI)
00323 {
00324 writeShell
00325 (
00326 os,
00327 pointLst,
00328 faceLst[faceIndex++],
00329 zoneI
00330 );
00331 }
00332 }
00333 }
00334 }
00335
00336
00337 template<class Face>
00338 void Foam::fileFormats::STLsurfaceFormat<Face>::writeAscii
00339 (
00340 const fileName& filename,
00341 const UnsortedMeshedSurface<Face>& surf
00342 )
00343 {
00344 OFstream os(filename);
00345 if (!os.good())
00346 {
00347 FatalErrorIn
00348 (
00349 "fileFormats::STLsurfaceFormat::writeAscii"
00350 "(const fileName&, const UnsortedMeshedSurface<Face>&)"
00351 )
00352 << "Cannot open file for writing " << filename
00353 << exit(FatalError);
00354 }
00355
00356
00357 if (surf.zoneToc().size() == 1)
00358 {
00359 const pointField& pointLst = surf.points();
00360 const List<Face>& faceLst = surf.faces();
00361
00362 os << "solid " << surf.zoneToc()[0].name() << endl;
00363 forAll(faceLst, faceI)
00364 {
00365 writeShell(os, pointLst, faceLst[faceI]);
00366 }
00367 os << "endsolid " << surf.zoneToc()[0].name() << endl;
00368 }
00369 else
00370 {
00371 labelList faceMap;
00372 List<surfZone> zoneLst = surf.sortedZones(faceMap);
00373
00374 writeAscii
00375 (
00376 filename,
00377 MeshedSurfaceProxy<Face>
00378 (
00379 surf.points(),
00380 surf.faces(),
00381 zoneLst,
00382 faceMap
00383 )
00384 );
00385 }
00386 }
00387
00388
00389 template<class Face>
00390 void Foam::fileFormats::STLsurfaceFormat<Face>::writeBinary
00391 (
00392 const fileName& filename,
00393 const UnsortedMeshedSurface<Face>& surf
00394 )
00395 {
00396 std::ofstream os(filename.c_str(), std::ios::binary);
00397 if (!os.good())
00398 {
00399 FatalErrorIn
00400 (
00401 "fileFormats::STLsurfaceFormat::writeBinary"
00402 "(const fileName&, const UnsortedMeshedSurface<Face>&)"
00403 )
00404 << "Cannot open file for writing " << filename
00405 << exit(FatalError);
00406 }
00407
00408 const pointField& pointLst = surf.points();
00409 const List<Face>& faceLst = surf.faces();
00410 const List<label>& zoneIds = surf.zoneIds();
00411
00412 unsigned int nTris = 0;
00413 if (MeshedSurface<Face>::isTri())
00414 {
00415 nTris = faceLst.size();
00416 }
00417 else
00418 {
00419
00420 forAll(faceLst, faceI)
00421 {
00422 nTris += faceLst[faceI].size() - 2;
00423 }
00424 }
00425
00426
00427 STLsurfaceFormatCore::writeHeaderBINARY(os, nTris);
00428
00429
00430 forAll(faceLst, faceI)
00431 {
00432 writeShell
00433 (
00434 os,
00435 pointLst,
00436 faceLst[faceI],
00437 zoneIds[faceI]
00438 );
00439 }
00440 }
00441
00442
00443 template<class Face>
00444 void Foam::fileFormats::STLsurfaceFormat<Face>::write
00445 (
00446 const fileName& filename,
00447 const MeshedSurfaceProxy<Face>& surf
00448 )
00449 {
00450 const word ext = filename.ext();
00451
00452
00453 if (ext == "stlb")
00454 {
00455 writeBinary(filename, surf);
00456 }
00457 else
00458 {
00459 writeAscii(filename, surf);
00460 }
00461 }
00462
00463
00464 template<class Face>
00465 void Foam::fileFormats::STLsurfaceFormat<Face>::write
00466 (
00467 const fileName& filename,
00468 const UnsortedMeshedSurface<Face>& surf
00469 )
00470 {
00471 word ext = filename.ext();
00472
00473
00474 if (ext == "stlb")
00475 {
00476 writeBinary(filename, surf);
00477 }
00478 else
00479 {
00480 writeAscii(filename, surf);
00481 }
00482 }
00483
00484
00485