1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "MeshedSurface.H"
27 #include "UnsortedMeshedSurface.H"
28 #include "MeshedSurfaceProxy.H"
32 #include "polyBoundaryMesh.H"
35 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
38 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::readTypes()
40 return wordHashSet(*fileExtensionConstructorTablePtr_);
45 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::writeTypes()
47 return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
52 bool Foam::UnsortedMeshedSurface<Face>::canReadType
58 return fileFormats::surfaceFormatsCore::checkSupport
60 readTypes() | ParentType::readTypes(),
69 bool Foam::UnsortedMeshedSurface<Face>::canWriteType
75 return fileFormats::surfaceFormatsCore::checkSupport
86 bool Foam::UnsortedMeshedSurface<Face>::canRead
92 word ext = name.ext();
95 ext = name.lessExt().ext();
97 return canReadType(ext, verbose);
102 void Foam::UnsortedMeshedSurface<Face>::write
104 const fileName& name,
105 const UnsortedMeshedSurface<Face>& surf
110 Info<< "UnsortedMeshedSurface::write"
111 "(const fileName&, const UnsortedMeshedSurface&) : "
112 "writing to " << name
116 const word ext = name.ext();
118 typename writefileExtensionMemberFunctionTable::iterator mfIter =
119 writefileExtensionMemberFunctionTablePtr_->find(ext);
121 if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
123 // no direct writer, delegate to proxy if possible
124 wordHashSet supported = ProxyType::writeTypes();
126 if (supported.found(ext))
128 MeshedSurfaceProxy<Face>(surf).write(name);
134 "UnsortedMeshedSurface::write"
135 "(const fileName&, const UnsortedMeshedSurface&)"
136 ) << "Unknown file extension " << ext << nl << nl
137 << "Valid types are :" << endl
138 << (supported | writeTypes())
144 mfIter()(name, surf);
149 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
152 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface()
159 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
161 const Xfer<pointField>& pointLst,
162 const Xfer<List<Face> >& faceLst,
163 const Xfer<List<label> >& zoneIds,
164 const Xfer<surfZoneIdentifierList>& zoneTofc
167 ParentType(pointLst, faceLst),
174 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
176 const Xfer<pointField>& pointLst,
177 const Xfer<List<Face> >& faceLst,
178 const labelUList& zoneSizes,
179 const UList<word>& zoneNames
182 ParentType(pointLst, faceLst)
184 if (zoneSizes.size())
186 if (zoneNames.size())
188 setZones(zoneSizes, zoneNames);
203 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
205 const UnsortedMeshedSurface<Face>& surf
210 xferCopy(surf.points()),
211 xferCopy(surf.faces())
213 zoneIds_(surf.zoneIds()),
214 zoneToc_(surf.zoneToc())
219 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
221 const MeshedSurface<Face>& surf
226 xferCopy(surf.points()),
227 xferCopy(surf.faces())
230 setZones(surf.surfZones());
235 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
237 const Xfer<UnsortedMeshedSurface<Face> >& surf
247 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
249 const Xfer<MeshedSurface<Face> >& surf
259 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
261 const fileName& name,
272 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface(const fileName& name)
281 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
289 MeshedSurface<Face> surf(t, surfName);
294 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
297 Foam::UnsortedMeshedSurface<Face>::~UnsortedMeshedSurface()
301 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
304 void Foam::UnsortedMeshedSurface<Face>::setOneZone()
306 zoneIds_.setSize(size());
312 zoneName = zoneToc_[0].name();
314 if (zoneName.empty())
319 // set single default zone
321 zoneToc_[0] = surfZoneIdentifier(zoneName, 0);
326 void Foam::UnsortedMeshedSurface<Face>::setZones
328 const surfZoneList& zoneLst
331 zoneIds_.setSize(size());
332 zoneToc_.setSize(zoneLst.size());
334 forAll(zoneToc_, zoneI)
336 const surfZone& zone = zoneLst[zoneI];
337 zoneToc_[zoneI] = zone;
339 // assign sub-zone Ids
340 SubList<label> subZone(zoneIds_, zone.size(), zone.start());
347 void Foam::UnsortedMeshedSurface<Face>::setZones
349 const labelUList& sizes,
350 const UList<word>& names
353 zoneIds_.setSize(size());
354 zoneToc_.setSize(sizes.size());
357 forAll(zoneToc_, zoneI)
359 zoneToc_[zoneI] = surfZoneIdentifier(names[zoneI], zoneI);
361 // assign sub-zone Ids
362 SubList<label> subZone(zoneIds_, sizes[zoneI], start);
365 start += sizes[zoneI];
371 void Foam::UnsortedMeshedSurface<Face>::setZones
373 const labelUList& sizes
376 zoneIds_.setSize(size());
377 zoneToc_.setSize(sizes.size());
380 forAll(zoneToc_, zoneI)
382 zoneToc_[zoneI] = surfZoneIdentifier
384 word("zone") + ::Foam::name(zoneI),
388 // assign sub-zone Ids
389 SubList<label> subZone(zoneIds_, sizes[zoneI], start);
392 start += sizes[zoneI];
398 void Foam::UnsortedMeshedSurface<Face>::remapFaces
400 const labelUList& faceMap
403 // re-assign the zone Ids
404 if (&faceMap && faceMap.size())
406 if (zoneToc_.empty())
410 else if (zoneToc_.size() == 1)
412 // optimized for single-zone case
417 List<label> newZones(faceMap.size());
419 forAll(faceMap, faceI)
421 newZones[faceI] = zoneIds_[faceMap[faceI]];
423 zoneIds_.transfer(newZones);
429 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
432 void Foam::UnsortedMeshedSurface<Face>::setSize(const label s)
434 this->storedFaces().setSize(s);
435 // if zones extend: set with last zoneId
436 zoneIds_.setSize(s, zoneToc_.size() - 1);
441 void Foam::UnsortedMeshedSurface<Face>::clear()
450 Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
455 // supply some zone names
457 forAll(zoneToc_, zoneI)
459 zoneNames.insert(zoneI, zoneToc_[zoneI].name());
462 // std::sort() really seems to mix up the order.
463 // and std::stable_sort() might take too long / too much memory
465 // Assuming that we have relatively fewer zones compared to the
466 // number of items, just do it ourselves
468 // step 1: get zone sizes and store (origId => zoneI)
470 forAll(zoneIds_, faceI)
472 const label origId = zoneIds_[faceI];
474 Map<label>::iterator fnd = lookup.find(origId);
475 if (fnd != lookup.end())
481 lookup.insert(origId, 1);
485 // step 2: assign start/size (and name) to the newZones
486 // re-use the lookup to map (zoneId => zoneI)
487 surfZoneList zoneLst(lookup.size());
490 forAllIter(Map<label>, lookup, iter)
492 label origId = iter.key();
495 Map<word>::const_iterator fnd = zoneNames.find(origId);
496 if (fnd != zoneNames.end())
502 name = word("zone") + ::Foam::name(zoneI);
505 zoneLst[zoneI] = surfZone
508 0, // initialize with zero size
513 // increment the start for the next zone
514 // and save the (zoneId => zoneI) mapping
520 // step 3: build the re-ordering
521 faceMap.setSize(zoneIds_.size());
523 forAll(zoneIds_, faceI)
525 label zoneI = lookup[zoneIds_[faceI]];
526 faceMap[faceI] = zoneLst[zoneI].start() + zoneLst[zoneI].size()++;
529 // with reordered faces registered in faceMap
535 Foam::UnsortedMeshedSurface<Face>
536 Foam::UnsortedMeshedSurface<Face>::subsetMesh
538 const labelHashSet& include,
543 const pointField& locPoints = this->localPoints();
544 const List<Face>& locFaces = this->localFaces();
546 // Fill pointMap, faceMap
547 PatchTools::subsetMap(*this, include, pointMap, faceMap);
549 // Create compact coordinate list and forward mapping array
550 pointField newPoints(pointMap.size());
551 labelList oldToNew(locPoints.size());
552 forAll(pointMap, pointI)
554 newPoints[pointI] = locPoints[pointMap[pointI]];
555 oldToNew[pointMap[pointI]] = pointI;
558 // Renumber face node labels and compact
559 List<Face> newFaces(faceMap.size());
560 List<label> newZones(faceMap.size());
562 forAll(faceMap, faceI)
564 const label origFaceI = faceMap[faceI];
565 newFaces[faceI] = Face(locFaces[origFaceI]);
567 // Renumber labels for face
568 Face& f = newFaces[faceI];
571 f[fp] = oldToNew[f[fp]];
574 newZones[faceI] = zoneIds_[origFaceI];
578 // construct a sub-surface
579 return UnsortedMeshedSurface
590 Foam::UnsortedMeshedSurface<Face> Foam::UnsortedMeshedSurface<Face>::subsetMesh
592 const labelHashSet& include
595 labelList pointMap, faceMap;
596 return subsetMesh(include, pointMap, faceMap);
601 void Foam::UnsortedMeshedSurface<Face>::reset
603 const Xfer<pointField>& pointLst,
604 const Xfer<List<Face> >& faceLst,
605 const Xfer<List<label> >& zoneIds
617 zoneIds_.transfer(zoneIds());
623 void Foam::UnsortedMeshedSurface<Face>::reset
625 const Xfer<List<point> >& pointLst,
626 const Xfer<List<Face> >& faceLst,
627 const Xfer<List<label> >& zoneIds
639 zoneIds_.transfer(zoneIds());
645 void Foam::UnsortedMeshedSurface<Face>::transfer
647 UnsortedMeshedSurface<Face>& surf
652 xferMove(surf.storedPoints()),
653 xferMove(surf.storedFaces()),
657 zoneIds_.transfer(surf.zoneIds_);
658 zoneToc_.transfer(surf.zoneToc_);
665 void Foam::UnsortedMeshedSurface<Face>::transfer
667 MeshedSurface<Face>& surf
672 xferMove(surf.storedPoints()),
673 xferMove(surf.storedFaces()),
677 setZones(surf.surfZones());
683 Foam::Xfer<Foam::UnsortedMeshedSurface<Face> >
684 Foam::UnsortedMeshedSurface<Face>::xfer()
686 return xferMove(*this);
690 // Read from file, determine format from extension
692 bool Foam::UnsortedMeshedSurface<Face>::read(const fileName& name)
694 word ext = name.ext();
697 fileName unzipName = name.lessExt();
698 return read(unzipName, unzipName.ext());
702 return read(name, ext);
707 // Read from file in given format
709 bool Foam::UnsortedMeshedSurface<Face>::read
711 const fileName& name,
717 // read via use selector mechanism
718 transfer(New(name, ext)());
724 void Foam::UnsortedMeshedSurface<Face>::write
730 MeshedSurfaceProxy<Face>(*this).write(t, surfName);
734 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
737 void Foam::UnsortedMeshedSurface<Face>::operator=
739 const UnsortedMeshedSurface<Face>& surf
744 this->storedPoints() = surf.points();
745 this->storedFaces() = surf.faces();
746 zoneIds_ = surf.zoneIds_;
747 zoneToc_ = surf.zoneToc_;
752 Foam::UnsortedMeshedSurface<Face>::operator
753 Foam::MeshedSurfaceProxy<Face>() const
756 List<surfZone> zoneLst = this->sortedZones(faceMap);
758 return MeshedSurfaceProxy<Face>
768 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
770 #include "UnsortedMeshedSurfaceNew.C"
772 // ************************************************************************* //