bool Foam::fileFormats::TRIsurfaceFormatCore::read ( const fileName& filename ) { this->clear(); sorted_ = true; IFstream is(filename); if (!is.good()) { FatalErrorIn ( "fileFormats::TRIsurfaceFormatCore::read(const fileName&)" ) << "Cannot read file " << filename << exit(FatalError); } // uses similar structure as STL, just some points // the rest of the reader resembles the STL binary reader DynamicList<point> dynPoints; DynamicList<label> dynZones; DynamicList<label> dynSizes; HashTable<label> lookup; // place faces without a group in zone0 label zoneI = 0; dynSizes.append(zoneI); lookup.insert("zoneI", zoneI); while (is.good()) { string line = this->getLineNoComment(is); // handle continuations ? // if (line[line.size()-1] == '\\') // { // line.substr(0, line.size()-1); // line += this->getLineNoComment(is); // } IStringStream lineStream(line); point p ( readScalar(lineStream), readScalar(lineStream), readScalar(lineStream) ); if (!lineStream) break; dynPoints.append(p); dynPoints.append ( point ( readScalar(lineStream), readScalar(lineStream), readScalar(lineStream) ) ); dynPoints.append ( point ( readScalar(lineStream), readScalar(lineStream), readScalar(lineStream) ) ); // zone/colour in .tri file starts with 0x. Skip. // ie, instead of having 0xFF, skip 0 and leave xFF to // get read as a word and name it "zoneFF" char zero; lineStream >> zero; word rawName(lineStream); word name("zone" + rawName(1, rawName.size()-1)); HashTable<label>::const_iterator fnd = lookup.find(name); if (fnd != lookup.end()) { if (zoneI != fnd()) { // group appeared out of order sorted_ = false; } zoneI = fnd(); } else { zoneI = dynSizes.size(); lookup.insert(name, zoneI); dynSizes.append(0); } dynZones.append(zoneI); dynSizes[zoneI]++; } // skip empty groups label nZone = 0; forAll(dynSizes, zoneI) { if (dynSizes[zoneI]) { if (nZone != zoneI) { dynSizes[nZone] = dynSizes[zoneI]; } nZone++; } } // truncate addressed size dynSizes.setCapacity(nZone); // transfer to normal lists points_.transfer(dynPoints); zoneIds_.transfer(dynZones); sizes_.transfer(dynSizes); return true; }
Foam::searchableSurfaceControl::searchableSurfaceControl ( const Time& runTime, const word& name, const dictionary& controlFunctionDict, const conformationSurfaces& geometryToConformTo, const scalar& defaultCellSize ) : cellSizeAndAlignmentControl ( runTime, name, controlFunctionDict, geometryToConformTo, defaultCellSize ), surfaceName_(controlFunctionDict.lookupOrDefault<word>("surface", name)), searchableSurface_(geometryToConformTo.geometry()[surfaceName_]), geometryToConformTo_(geometryToConformTo), cellSizeFunctions_(1), regionToCellSizeFunctions_(searchableSurface_.regions().size(), -1), maxPriority_(-1) { Info<< indent << "Master settings:" << endl; Info<< incrIndent; cellSizeFunctions_.set ( 0, cellSizeFunction::New ( controlFunctionDict, searchableSurface_, defaultCellSize_, labelList() ) ); Info<< decrIndent; PtrList<cellSizeFunction> regionCellSizeFunctions; DynamicList<label> defaultCellSizeRegions; label nRegionCellSizeFunctions = 0; // Loop over regions - if any entry is not specified they should // inherit values from the parent surface. if (controlFunctionDict.found("regions")) { const dictionary& regionsDict = controlFunctionDict.subDict("regions"); const wordList& regionNames = searchableSurface_.regions(); label nRegions = regionsDict.size(); regionCellSizeFunctions.setSize(nRegions); defaultCellSizeRegions.setCapacity(nRegions); forAll(regionNames, regionI) { const word& regionName = regionNames[regionI]; label regionID = geometryToConformTo_.geometry().findSurfaceRegionID ( this->name(), regionName ); if (regionsDict.found(regionName)) { // Get the dictionary for region const dictionary& regionDict = regionsDict.subDict(regionName); Info<< indent << "Region " << regionName << " (ID = " << regionID << ")" << " settings:" << endl; Info<< incrIndent; regionCellSizeFunctions.set ( nRegionCellSizeFunctions, cellSizeFunction::New ( regionDict, searchableSurface_, defaultCellSize_, labelList(1, regionID) ) ); Info<< decrIndent; regionToCellSizeFunctions_[regionID] = nRegionCellSizeFunctions; nRegionCellSizeFunctions++; } else { // Add to default list defaultCellSizeRegions.append(regionID); } } } if (defaultCellSizeRegions.empty() && !regionCellSizeFunctions.empty()) { cellSizeFunctions_.transfer(regionCellSizeFunctions); } else if (nRegionCellSizeFunctions > 0) { regionCellSizeFunctions.setSize(nRegionCellSizeFunctions + 1); regionCellSizeFunctions.set ( nRegionCellSizeFunctions, cellSizeFunction::New ( controlFunctionDict, searchableSurface_, defaultCellSize_, labelList() ) ); const wordList& regionNames = searchableSurface_.regions(); forAll(regionNames, regionI) { if (regionToCellSizeFunctions_[regionI] == -1) { regionToCellSizeFunctions_[regionI] = nRegionCellSizeFunctions; } } cellSizeFunctions_.transfer(regionCellSizeFunctions); }
int main(int argc, char *argv[]) { List<DynamicList<label, 1, 0> > ldl(2); ldl[0](0) = 0; ldl[0](2) = 2; ldl[0](3) = 3; ldl[0](1) = 1; ldl[0].setCapacity(5); // increase allocated size ldl[1].setCapacity(10); // increase allocated size ldl[0].reserve(15); // should increase allocated size ldl[1].reserve(5); // should not decrease allocated size ldl[1](3) = 2; // allocates space and sets value // this works without a segfault, but doesn't change the list size ldl[0][4] = 4; ldl[1] = 3; Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: "; forAll(ldl, i) { Info<< " " << ldl[i].size() << "/" << ldl[i].capacity(); } Info<< endl; List<List<label> > ll(2); ll[0].transfer(ldl[0]); ll[1].transfer(ldl[1].shrink()); Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: "; forAll(ldl, i) { Info<< " " << ldl[i].size() << "/" << ldl[i].capacity(); } Info<< endl; Info<< "<ll>" << ll << "</ll>" << nl << endl; // test the transfer between DynamicLists DynamicList<label, 1, 0> dlA; DynamicList<label, 1, 0> dlB; for (label i = 0; i < 5; i++) { dlA.append(i); } dlA.setCapacity(10); Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: " << " " << dlA.size() << "/" << dlA.capacity() << endl; dlB.transfer(dlA); // provokes memory error if previous transfer did not maintain // the correct allocated space dlB[6] = 6; Info<< "Transferred to dlB" << endl; Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: " << " " << dlA.size() << "/" << dlA.capacity() << endl; Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: " << " " << dlB.size() << "/" << dlB.capacity() << endl; // try with a normal list: List<label> lstA; lstA.transfer(dlB); Info<< "Transferred to normal list" << endl; Info<< "<lstA>" << lstA << "</lstA>" << nl << "sizes: " << " " << lstA.size() << endl; Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: " << " " << dlB.size() << "/" << dlB.capacity() << endl; // Copy back and append a few time for (label i=0; i < 3; i++) { dlB.append(lstA); } Info<< "appended list a few times" << endl; Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: " << " " << dlB.size() << "/" << dlB.capacity() << endl; // assign the list (should maintain allocated space) dlB = lstA; Info<< "assigned list" << endl; Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: " << " " << dlB.size() << "/" << dlB.capacity() << endl; // Copy back and append a few time for (label i=0; i < 3; i++) { dlB.append(lstA); } // check allocation granularity DynamicList<label, 6, 0> dlC; Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: " << " " << dlC.size() << "/" << dlC.capacity() << endl; dlC.reserve(dlB.size()); dlC = dlB; Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: " << " " << dlC.size() << "/" << dlC.capacity() << endl; List<label> lstB(dlC.xfer()); Info<< "Transferred to normal list via the xfer() method" << endl; Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: " << " " << lstB.size() << endl; Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: " << " " << dlC.size() << "/" << dlC.capacity() << endl; DynamicList<label> dlD(lstB.xfer()); Info<< "Transfer construct from normal list" << endl; Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: " << " " << lstB.size() << endl; Info<< "<dlD>" << dlD << "</dlD>" << nl << "sizes: " << " " << dlD.size() << "/" << dlD.capacity() << endl; DynamicList<label,10> dlE1(10); DynamicList<label> dlE2(dlE1); // construct dissimilar Info<< "<dlE1>" << dlE1 << "</dlE1>" << nl << "sizes: " << " " << dlE1.size() << "/" << dlE1.capacity() << endl; Info<< "<dlE2>" << dlE2 << "</dlE2>" << nl << "sizes: " << " " << dlE2.size() << "/" << dlE2.capacity() << endl; for (label elemI=0; elemI < 5; ++elemI) { dlE1.append(4 - elemI); dlE2.append(elemI); } Info<< "<dlE2>" << dlE2 << "</dlE2>" << endl; DynamicList<label> dlE3(dlE2); // construct identical Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl; dlE3 = dlE1; // assign dissimilar Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl; dlE3 = dlE2; // assign identical Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl; Info<< "\nEnd\n"; return 0; }