1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "MeshedSurface.H"
28 #include "UnsortedMeshedSurface.H"
29 #include "MeshedSurfaceProxy.H"
33 #include "polyBoundaryMesh.H"
36 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
39 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::readTypes()
41 return wordHashSet(*fileExtensionConstructorTablePtr_);
46 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::writeTypes()
48 return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
53 bool Foam::UnsortedMeshedSurface<Face>::canReadType
61 readTypes() | ParentType::readTypes(),
70 bool Foam::UnsortedMeshedSurface<Face>::canWriteType
87 bool Foam::UnsortedMeshedSurface<Face>::canRead
93 word ext = name.ext();
96 ext = name.lessExt().ext();
98 return canReadType(ext, verbose);
103 void Foam::UnsortedMeshedSurface<Face>::write
105 const fileName& name,
106 const UnsortedMeshedSurface<Face>& surf
111 Info<< "UnsortedMeshedSurface::write"
112 "(const fileName&, const UnsortedMeshedSurface&) : "
113 "writing to " << name
117 const word ext = name.ext();
119 typename writefileExtensionMemberFunctionTable::iterator mfIter =
120 writefileExtensionMemberFunctionTablePtr_->find(ext);
122 if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
124 // no direct writer, delegate to proxy if possible
125 wordHashSet supported = ProxyType::writeTypes();
127 if (supported.found(ext))
129 MeshedSurfaceProxy<Face>(surf).write(name);
135 "UnsortedMeshedSurface::write"
136 "(const fileName&, const UnsortedMeshedSurface&)"
137 ) << "Unknown file extension " << ext << nl << nl
138 << "Valid types are :" << endl
139 << (supported | writeTypes())
145 mfIter()(name, surf);
150 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
153 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface()
160 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
162 const Xfer< pointField >& pointLst,
163 const Xfer< List<Face> >& faceLst,
164 const Xfer< List<label> >& zoneIds,
165 const Xfer< surfZoneIdentifierList >& zoneTofc
168 ParentType(pointLst, faceLst),
175 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
177 const Xfer< pointField >& pointLst,
178 const Xfer< List<Face> >& faceLst,
179 const UList<label>& zoneSizes,
180 const UList<word>& zoneNames
183 ParentType(pointLst, faceLst)
185 if (zoneSizes.size())
187 if (zoneNames.size())
189 setZones(zoneSizes, zoneNames);
204 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
206 const UnsortedMeshedSurface<Face>& surf
211 xferCopy(surf.points()),
212 xferCopy(surf.faces())
214 zoneIds_(surf.zoneIds()),
215 zoneToc_(surf.zoneToc())
220 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
222 const MeshedSurface<Face>& surf
227 xferCopy(surf.points()),
228 xferCopy(surf.faces())
231 setZones(surf.surfZones());
236 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
238 const Xfer< UnsortedMeshedSurface<Face> >& surf
248 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
250 const Xfer< MeshedSurface<Face> >& surf
260 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
262 const fileName& name,
273 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface(const fileName& name)
282 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
290 MeshedSurface<Face> surf(t, surfName);
295 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
298 Foam::UnsortedMeshedSurface<Face>::~UnsortedMeshedSurface()
302 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
305 void Foam::UnsortedMeshedSurface<Face>::setOneZone()
307 zoneIds_.setSize(size());
313 zoneName = zoneToc_[0].name();
315 if (zoneName.empty())
320 // set single default zone
322 zoneToc_[0] = surfZoneIdentifier(zoneName, 0);
327 void Foam::UnsortedMeshedSurface<Face>::setZones
329 const surfZoneList& zoneLst
332 zoneIds_.setSize(size());
333 zoneToc_.setSize(zoneLst.size());
335 forAll(zoneToc_, zoneI)
337 const surfZone& zone = zoneLst[zoneI];
338 zoneToc_[zoneI] = zone;
340 // assign sub-zone Ids
341 SubList<label> subZone(zoneIds_, zone.size(), zone.start());
348 void Foam::UnsortedMeshedSurface<Face>::setZones
350 const UList<label>& sizes,
351 const UList<word>& names
354 zoneIds_.setSize(size());
355 zoneToc_.setSize(sizes.size());
358 forAll(zoneToc_, zoneI)
360 zoneToc_[zoneI] = surfZoneIdentifier(names[zoneI], zoneI);
362 // assign sub-zone Ids
363 SubList<label> subZone(zoneIds_, sizes[zoneI], start);
366 start += sizes[zoneI];
372 void Foam::UnsortedMeshedSurface<Face>::setZones
374 const UList<label>& sizes
377 zoneIds_.setSize(size());
378 zoneToc_.setSize(sizes.size());
381 forAll(zoneToc_, zoneI)
383 zoneToc_[zoneI] = surfZoneIdentifier
385 word("zone") + ::Foam::name(zoneI),
389 // assign sub-zone Ids
390 SubList<label> subZone(zoneIds_, sizes[zoneI], start);
393 start += sizes[zoneI];
399 void Foam::UnsortedMeshedSurface<Face>::remapFaces
401 const UList<label>& faceMap
404 // re-assign the zone Ids
405 if (&faceMap && faceMap.size())
407 if (zoneToc_.empty())
411 else if (zoneToc_.size() == 1)
413 // optimized for single-zone case
418 List<label> newZones(faceMap.size());
420 forAll(faceMap, faceI)
422 newZones[faceI] = zoneIds_[faceMap[faceI]];
424 zoneIds_.transfer(newZones);
430 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
433 void Foam::UnsortedMeshedSurface<Face>::setSize(const label s)
435 this->storedFaces().setSize(s);
436 // if zones extend: set with last zoneId
437 zoneIds_.setSize(s, zoneToc_.size() - 1);
442 void Foam::UnsortedMeshedSurface<Face>::clear()
451 Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
456 // supply some zone names
458 forAll(zoneToc_, zoneI)
460 zoneNames.insert(zoneI, zoneToc_[zoneI].name());
463 // std::sort() really seems to mix up the order.
464 // and std::stable_sort() might take too long / too much memory
466 // Assuming that we have relatively fewer zones compared to the
467 // number of items, just do it ourselves
469 // step 1: get zone sizes and store (origId => zoneI)
471 forAll(zoneIds_, faceI)
473 const label origId = zoneIds_[faceI];
475 Map<label>::iterator fnd = lookup.find(origId);
476 if (fnd != lookup.end())
482 lookup.insert(origId, 1);
486 // step 2: assign start/size (and name) to the newZones
487 // re-use the lookup to map (zoneId => zoneI)
488 surfZoneList zoneLst(lookup.size());
491 forAllIter(Map<label>, lookup, iter)
493 label origId = iter.key();
496 Map<word>::const_iterator fnd = zoneNames.find(origId);
497 if (fnd != zoneNames.end())
503 name = word("zone") + ::Foam::name(zoneI);
506 zoneLst[zoneI] = surfZone
509 0, // initialize with zero size
514 // increment the start for the next zone
515 // and save the (zoneId => zoneI) mapping
521 // step 3: build the re-ordering
522 faceMap.setSize(zoneIds_.size());
524 forAll(zoneIds_, faceI)
526 label zoneI = lookup[zoneIds_[faceI]];
527 faceMap[faceI] = zoneLst[zoneI].start() + zoneLst[zoneI].size()++;
530 // with reordered faces registered in faceMap
536 Foam::UnsortedMeshedSurface<Face>
537 Foam::UnsortedMeshedSurface<Face>::subsetMesh
539 const labelHashSet& include,
544 const pointField& locPoints = this->localPoints();
545 const List<Face>& locFaces = this->localFaces();
547 // Fill pointMap, faceMap
548 PatchTools::subsetMap(*this, include, pointMap, faceMap);
550 // Create compact coordinate list and forward mapping array
551 pointField newPoints(pointMap.size());
552 labelList oldToNew(locPoints.size());
553 forAll(pointMap, pointI)
555 newPoints[pointI] = locPoints[pointMap[pointI]];
556 oldToNew[pointMap[pointI]] = pointI;
559 // Renumber face node labels and compact
560 List<Face> newFaces(faceMap.size());
561 List<label> newZones(faceMap.size());
563 forAll(faceMap, faceI)
565 const label origFaceI = faceMap[faceI];
566 newFaces[faceI] = Face(locFaces[origFaceI]);
568 // Renumber labels for face
569 Face& f = newFaces[faceI];
572 f[fp] = oldToNew[f[fp]];
575 newZones[faceI] = zoneIds_[origFaceI];
579 // construct a sub-surface
580 return UnsortedMeshedSurface
591 Foam::UnsortedMeshedSurface<Face> Foam::UnsortedMeshedSurface<Face>::subsetMesh
593 const labelHashSet& include
596 labelList pointMap, faceMap;
597 return subsetMesh(include, pointMap, faceMap);
602 void Foam::UnsortedMeshedSurface<Face>::reset
604 const Xfer< pointField >& pointLst,
605 const Xfer< List<Face> >& faceLst,
606 const Xfer< List<label> >& zoneIds
618 zoneIds_.transfer(zoneIds());
624 void Foam::UnsortedMeshedSurface<Face>::reset
626 const Xfer< List<point> >& pointLst,
627 const Xfer< List<Face> >& faceLst,
628 const Xfer< List<label> >& zoneIds
640 zoneIds_.transfer(zoneIds());
646 void Foam::UnsortedMeshedSurface<Face>::transfer
648 UnsortedMeshedSurface<Face>& surf
653 xferMove(surf.storedPoints()),
654 xferMove(surf.storedFaces()),
658 zoneIds_.transfer(surf.zoneIds_);
659 zoneToc_.transfer(surf.zoneToc_);
666 void Foam::UnsortedMeshedSurface<Face>::transfer
668 MeshedSurface<Face>& surf
673 xferMove(surf.storedPoints()),
674 xferMove(surf.storedFaces()),
678 setZones(surf.surfZones());
684 Foam::Xfer< Foam::UnsortedMeshedSurface<Face> >
685 Foam::UnsortedMeshedSurface<Face>::xfer()
687 return xferMove(*this);
691 // Read from file, determine format from extension
693 bool Foam::UnsortedMeshedSurface<Face>::read(const fileName& name)
695 word ext = name.ext();
698 fileName unzipName = name.lessExt();
699 return read(unzipName, unzipName.ext());
703 return read(name, ext);
708 // Read from file in given format
710 bool Foam::UnsortedMeshedSurface<Face>::read
712 const fileName& name,
718 // read via use selector mechanism
719 transfer(New(name, ext)());
725 void Foam::UnsortedMeshedSurface<Face>::write
731 MeshedSurfaceProxy<Face>(*this).write(t, surfName);
735 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
738 void Foam::UnsortedMeshedSurface<Face>::operator=
740 const UnsortedMeshedSurface<Face>& surf
745 this->storedPoints() = surf.points();
746 this->storedFaces() = surf.faces();
747 zoneIds_ = surf.zoneIds_;
748 zoneToc_ = surf.zoneToc_;
753 Foam::UnsortedMeshedSurface<Face>::operator
754 Foam::MeshedSurfaceProxy<Face>() const
757 List<surfZone> zoneLst = this->sortedZones(faceMap);
759 return MeshedSurfaceProxy<Face>
769 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
771 #include "UnsortedMeshedSurfaceNew.C"
773 // ************************************************************************* //