1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "MeshedSurface.H"
27 #include "UnsortedMeshedSurface.H"
28 #include "MeshedSurfaceProxy.H"
31 #include "polyBoundaryMesh.H"
34 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
37 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::readTypes()
39 return wordHashSet(*fileExtensionConstructorTablePtr_);
44 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::writeTypes()
46 return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
51 bool Foam::UnsortedMeshedSurface<Face>::canReadType
57 return fileFormats::surfaceFormatsCore::checkSupport
59 readTypes() | ParentType::readTypes(),
68 bool Foam::UnsortedMeshedSurface<Face>::canWriteType
74 return fileFormats::surfaceFormatsCore::checkSupport
85 bool Foam::UnsortedMeshedSurface<Face>::canRead
91 word ext = name.ext();
94 ext = name.lessExt().ext();
96 return canReadType(ext, verbose);
101 void Foam::UnsortedMeshedSurface<Face>::write
103 const fileName& name,
104 const UnsortedMeshedSurface<Face>& surf
109 Info<< "UnsortedMeshedSurface::write"
110 "(const fileName&, const UnsortedMeshedSurface&) : "
111 "writing to " << name
115 const word ext = name.ext();
117 typename writefileExtensionMemberFunctionTable::iterator mfIter =
118 writefileExtensionMemberFunctionTablePtr_->find(ext);
120 if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
122 // no direct writer, delegate to proxy if possible
123 wordHashSet supported = ProxyType::writeTypes();
125 if (supported.found(ext))
127 MeshedSurfaceProxy<Face>(surf).write(name);
133 "UnsortedMeshedSurface::write"
134 "(const fileName&, const UnsortedMeshedSurface&)"
135 ) << "Unknown file extension " << ext << nl << nl
136 << "Valid types are :" << endl
137 << (supported | writeTypes())
143 mfIter()(name, surf);
148 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
151 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface()
158 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
160 const Xfer< pointField >& pointLst,
161 const Xfer< List<Face> >& faceLst,
162 const Xfer< List<label> >& zoneIds,
163 const Xfer< surfZoneIdentifierList >& zoneTofc
166 ParentType(pointLst, faceLst),
173 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
175 const Xfer< pointField >& pointLst,
176 const Xfer< List<Face> >& faceLst,
177 const UList<label>& zoneSizes,
178 const UList<word>& zoneNames
181 ParentType(pointLst, faceLst)
183 if (zoneSizes.size())
185 if (zoneNames.size())
187 setZones(zoneSizes, zoneNames);
202 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
204 const UnsortedMeshedSurface<Face>& surf
209 xferCopy(surf.points()),
210 xferCopy(surf.faces())
212 zoneIds_(surf.zoneIds()),
213 zoneToc_(surf.zoneToc())
218 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
220 const MeshedSurface<Face>& surf
225 xferCopy(surf.points()),
226 xferCopy(surf.faces())
229 setZones(surf.surfZones());
234 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
236 const Xfer< UnsortedMeshedSurface<Face> >& surf
246 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
248 const Xfer< MeshedSurface<Face> >& surf
258 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
260 const fileName& name,
271 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface(const fileName& name)
280 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
288 MeshedSurface<Face> surf(t, surfName);
293 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
296 Foam::UnsortedMeshedSurface<Face>::~UnsortedMeshedSurface()
300 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
303 void Foam::UnsortedMeshedSurface<Face>::setOneZone()
305 zoneIds_.setSize(size());
311 zoneName = zoneToc_[0].name();
313 if (zoneName.empty())
318 // set single default zone
320 zoneToc_[0] = surfZoneIdentifier(zoneName, 0);
325 void Foam::UnsortedMeshedSurface<Face>::setZones
327 const surfZoneList& zoneLst
330 zoneIds_.setSize(size());
331 zoneToc_.setSize(zoneLst.size());
333 forAll(zoneToc_, zoneI)
335 const surfZone& zone = zoneLst[zoneI];
336 zoneToc_[zoneI] = zone;
338 // assign sub-zone Ids
339 SubList<label> subZone(zoneIds_, zone.size(), zone.start());
346 void Foam::UnsortedMeshedSurface<Face>::setZones
348 const UList<label>& sizes,
349 const UList<word>& names
352 zoneIds_.setSize(size());
353 zoneToc_.setSize(sizes.size());
356 forAll(zoneToc_, zoneI)
358 zoneToc_[zoneI] = surfZoneIdentifier(names[zoneI], zoneI);
360 // assign sub-zone Ids
361 SubList<label> subZone(zoneIds_, sizes[zoneI], start);
364 start += sizes[zoneI];
370 void Foam::UnsortedMeshedSurface<Face>::setZones
372 const UList<label>& sizes
375 zoneIds_.setSize(size());
376 zoneToc_.setSize(sizes.size());
379 forAll(zoneToc_, zoneI)
381 zoneToc_[zoneI] = surfZoneIdentifier
383 word("zone") + ::Foam::name(zoneI),
387 // assign sub-zone Ids
388 SubList<label> subZone(zoneIds_, sizes[zoneI], start);
391 start += sizes[zoneI];
397 void Foam::UnsortedMeshedSurface<Face>::remapFaces
399 const UList<label>& faceMap
402 // re-assign the zone Ids
403 if (!faceMap.empty())
405 if (zoneToc_.empty())
409 else if (zoneToc_.size() == 1)
411 // optimized for single-zone case
416 List<label> newZones(faceMap.size());
418 forAll(faceMap, faceI)
420 newZones[faceI] = zoneIds_[faceMap[faceI]];
422 zoneIds_.transfer(newZones);
428 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
431 void Foam::UnsortedMeshedSurface<Face>::setSize(const label s)
433 this->storedFaces().setSize(s);
434 // if zones extend: set with last zoneId
435 zoneIds_.setSize(s, zoneToc_.size() - 1);
440 void Foam::UnsortedMeshedSurface<Face>::clear()
449 Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
454 // supply some zone names
456 forAll(zoneToc_, zoneI)
458 zoneNames.insert(zoneI, zoneToc_[zoneI].name());
461 // std::sort() really seems to mix up the order.
462 // and std::stable_sort() might take too long / too much memory
464 // Assuming that we have relatively fewer zones compared to the
465 // number of items, just do it ourselves
467 // step 1: get zone sizes and store (origId => zoneI)
469 forAll(zoneIds_, faceI)
471 const label origId = zoneIds_[faceI];
473 Map<label>::iterator fnd = lookup.find(origId);
474 if (fnd != lookup.end())
480 lookup.insert(origId, 1);
484 // step 2: assign start/size (and name) to the newZones
485 // re-use the lookup to map (zoneId => zoneI)
486 surfZoneList zoneLst(lookup.size());
489 forAllIter(Map<label>, lookup, iter)
491 label origId = iter.key();
494 Map<word>::const_iterator fnd = zoneNames.find(origId);
495 if (fnd != zoneNames.end())
501 name = word("zone") + ::Foam::name(zoneI);
504 zoneLst[zoneI] = surfZone
507 0, // initialize with zero size
512 // increment the start for the next zone
513 // and save the (zoneId => zoneI) mapping
519 // step 3: build the re-ordering
520 faceMap.setSize(zoneIds_.size());
522 forAll(zoneIds_, faceI)
524 label zoneI = lookup[zoneIds_[faceI]];
525 faceMap[faceI] = zoneLst[zoneI].start() + zoneLst[zoneI].size()++;
528 // with reordered faces registered in faceMap
534 Foam::UnsortedMeshedSurface<Face>
535 Foam::UnsortedMeshedSurface<Face>::subsetMesh
537 const labelHashSet& include,
542 const pointField& locPoints = this->localPoints();
543 const List<Face>& locFaces = this->localFaces();
545 // Fill pointMap, faceMap
546 PatchTools::subsetMap(*this, include, pointMap, faceMap);
548 // Create compact coordinate list and forward mapping array
549 pointField newPoints(pointMap.size());
550 labelList oldToNew(locPoints.size());
551 forAll(pointMap, pointI)
553 newPoints[pointI] = locPoints[pointMap[pointI]];
554 oldToNew[pointMap[pointI]] = pointI;
557 // Renumber face node labels and compact
558 List<Face> newFaces(faceMap.size());
559 List<label> newZones(faceMap.size());
561 forAll(faceMap, faceI)
563 const label origFaceI = faceMap[faceI];
564 newFaces[faceI] = Face(locFaces[origFaceI]);
566 // Renumber labels for face
567 Face& f = newFaces[faceI];
570 f[fp] = oldToNew[f[fp]];
573 newZones[faceI] = zoneIds_[origFaceI];
577 // construct a sub-surface
578 return UnsortedMeshedSurface
589 Foam::UnsortedMeshedSurface<Face> Foam::UnsortedMeshedSurface<Face>::subsetMesh
591 const labelHashSet& include
594 labelList pointMap, faceMap;
595 return subsetMesh(include, pointMap, faceMap);
600 void Foam::UnsortedMeshedSurface<Face>::reset
602 const Xfer< pointField >& pointLst,
603 const Xfer< List<Face> >& faceLst,
604 const Xfer< List<label> >& zoneIds
614 if (!zoneIds().empty())
616 zoneIds_.transfer(zoneIds());
622 void Foam::UnsortedMeshedSurface<Face>::reset
624 const Xfer< List<point> >& pointLst,
625 const Xfer< List<Face> >& faceLst,
626 const Xfer< List<label> >& zoneIds
636 if (!zoneIds().empty())
638 zoneIds_.transfer(zoneIds());
644 void Foam::UnsortedMeshedSurface<Face>::transfer
646 UnsortedMeshedSurface<Face>& surf
651 xferMove(surf.storedPoints()),
652 xferMove(surf.storedFaces()),
656 zoneIds_.transfer(surf.zoneIds_);
657 zoneToc_.transfer(surf.zoneToc_);
664 void Foam::UnsortedMeshedSurface<Face>::transfer
666 MeshedSurface<Face>& surf
671 xferMove(surf.storedPoints()),
672 xferMove(surf.storedFaces()),
676 setZones(surf.surfZones());
682 Foam::Xfer< Foam::UnsortedMeshedSurface<Face> >
683 Foam::UnsortedMeshedSurface<Face>::xfer()
685 return xferMove(*this);
689 // Read from file, determine format from extension
691 bool Foam::UnsortedMeshedSurface<Face>::read(const fileName& name)
693 word ext = name.ext();
696 fileName unzipName = name.lessExt();
697 return read(unzipName, unzipName.ext());
701 return read(name, ext);
706 // Read from file in given format
708 bool Foam::UnsortedMeshedSurface<Face>::read
710 const fileName& name,
716 // read via use selector mechanism
717 transfer(New(name, ext)());
723 void Foam::UnsortedMeshedSurface<Face>::write
729 MeshedSurfaceProxy<Face>(*this).write(t, surfName);
733 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
736 void Foam::UnsortedMeshedSurface<Face>::operator=
738 const UnsortedMeshedSurface<Face>& surf
743 this->storedPoints() = surf.points();
744 this->storedFaces() = surf.faces();
745 zoneIds_ = surf.zoneIds_;
746 zoneToc_ = surf.zoneToc_;
751 Foam::UnsortedMeshedSurface<Face>::operator
752 Foam::MeshedSurfaceProxy<Face>() const
755 List<surfZone> zoneLst = this->sortedZones(faceMap);
757 return MeshedSurfaceProxy<Face>
767 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
769 #include "UnsortedMeshedSurfaceNew.C"
771 // ************************************************************************* //