void DatVolumeWriter::write(const std::string& filename, const VolumeBase* volumeHandle) throw (tgt::IOException) { tgtAssert(volumeHandle, "No volume handle"); const VolumeRAM* volume = volumeHandle->getRepresentation<VolumeRAM>(); if (!volume) { LWARNING("No volume"); return; } std::string datname = filename; std::string rawname = getFileNameWithoutExtension(filename) + ".raw"; LINFO("saving " << datname << " and " << rawname); std::fstream datout(datname.c_str(), std::ios::out); std::fstream rawout(rawname.c_str(), std::ios::out | std::ios::binary); if (!datout.is_open() || !rawout.is_open() || datout.bad() || rawout.bad()) throw tgt::IOException(); datout << getDatFileString(volumeHandle, rawname); if (datout.bad()) throw tgt::IOException(); datout.close(); // write raw file const char* data = static_cast<const char*>(volume->getData()); size_t numbytes = volume->getNumBytes(); rawout.write(data, numbytes); if (rawout.bad()) throw tgt::IOException(); rawout.close(); }
void MlRankBoostDataset::writeRankBoostDataSet(const char* samplePath) { string fileName, dirPath; getFileNameWithoutExtension(samplePath, fileName); getDirFromFullPath(samplePath, dirPath); // write data file, include tag information ofstream ofs(samplePath); if (! ofs.good()) error("Couldn't open data file for writing: ",samplePath); cout << "Writing: " << samples_.size() << " samples.." << endl; for (size_t i=0; i<samples_.size(); i++) samples_[i].writeToStream(ofs, true); ofs.close(); // write phi file string phiPath = dirPath + "/" + fileName + ".phi"; ofs.open(phiPath.c_str()); if (! ofs.good()) { error("Could not write feedback file: ",phiPath.c_str()); } else { for (size_t i=0; i<phi_.size(); i++) ofs << phi_[i].idx0 << "\t" << phi_[i].idx1 << "\t" << scientific << setprecision(4) << phi_[i].weight << endl; ofs.close(); } }
//============================================================================== bool File::moveToTrash() const { if (! exists()) return true; File trashCan ("~/.Trash"); if (! trashCan.isDirectory()) trashCan = "~/.local/share/Trash/files"; if (! trashCan.isDirectory()) return false; return moveFileTo (trashCan.getNonexistentChildFile (getFileNameWithoutExtension(), getFileExtension())); }
void createNewFile (Project& project, Project::Item parent) override { auto newFile = askUserToChooseNewFile (String (defaultClassName) + ".h", "*.h;*.cpp", parent); if (newFile != File()) { auto headerFile = newFile.withFileExtension (".h"); auto cppFile = newFile.withFileExtension (".cpp"); headerFile.replaceWithText (String()); cppFile.replaceWithText (String()); auto& odm = ProjucerApplication::getApp().openDocumentManager; if (auto* cpp = dynamic_cast<SourceCodeDocument*> (odm.openFile (nullptr, cppFile))) { if (auto* header = dynamic_cast<SourceCodeDocument*> (odm.openFile (nullptr, headerFile))) { std::unique_ptr<JucerDocument> jucerDoc (new ComponentDocument (cpp)); if (jucerDoc != nullptr) { jucerDoc->setClassName (newFile.getFileNameWithoutExtension()); jucerDoc->flushChangesToDocuments (&project); jucerDoc.reset(); cpp->save(); header->save(); odm.closeDocument (cpp, true); odm.closeDocument (header, true); parent.addFileRetainingSortOrder (headerFile, true); parent.addFileRetainingSortOrder (cppFile, true); } } } } }
void NrrdVolumeWriter::write(const std::string& filename, const VolumeHandleBase* volumeHandle) throw (tgt::IOException) { tgtAssert(volumeHandle, "No volume handle"); const Volume* volume = volumeHandle->getRepresentation<Volume>(); if (!volume) { LWARNING("No volume"); return; } std::string nhdrname = filename; std::string rawname = getFileNameWithoutExtension(filename) + ".raw"; LINFO("saving " << nhdrname << " and " << rawname); std::fstream nhdrout(nhdrname.c_str(), std::ios::out); std::fstream rawout(rawname.c_str(), std::ios::out | std::ios::binary); if (nhdrout.bad() || rawout.bad()) { LWARNING("Can't open file"); throw tgt::IOException(); } // write nrrd header std::string type; const char* data = 0; size_t numbytes = 0; if (const VolumeUInt8* vol = dynamic_cast<const VolumeUInt8*>(volume)) { type = "uchar"; data = reinterpret_cast<const char*>(vol->voxel()); numbytes = vol->getNumBytes(); } else if (const VolumeUInt16* vol = dynamic_cast<const VolumeUInt16*>(volume)) { type = "ushort"; data = reinterpret_cast<const char*>(vol->voxel()); numbytes = vol->getNumBytes(); } else if (const Volume4xUInt8* vol = dynamic_cast<const Volume4xUInt8*>(volume)) { type = "uint"; data = reinterpret_cast<const char*>(vol->voxel()); numbytes = vol->getNumBytes(); } else LERROR("Format currently not supported"); tgt::ivec3 dimensions = volumeHandle->getDimensions(); tgt::vec3 spacing = volumeHandle->getSpacing(); nhdrout << "NRRD0001" << std::endl; // magic number nhdrout << "content: " << tgt::FileSystem::fileName(filename) << std::endl; nhdrout << "dimension: 3" << std::endl; nhdrout << "type: " << type << std::endl; nhdrout << "sizes: " << dimensions.x << " " << dimensions.y << " " << dimensions.z << std::endl; nhdrout << "spacings: " << spacing.x << " " << spacing.y << " " << spacing.z << std::endl; nhdrout << "datafile: " << tgt::FileSystem::fileName(rawname) << std::endl; nhdrout << "encoding: raw" << std::endl; nhdrout.close(); // write raw file rawout.write(data, numbytes); rawout.close(); }
size_t MlRankBoostDataset::readRankBoostDataSet(const char* samplePath, size_t maxSampleIdx) { string fileName, dirPath; getFileNameWithoutExtension(samplePath, fileName); getDirFromFullPath(samplePath, dirPath); // read data file, include tag information ifstream ifs(samplePath); if (! ifs.good()) error("Couldn't MlRankBoostDataset for reading: ", samplePath); // count number of samples size_t counter=0; while (ifs.good()) { if (ifs.get() == '>') counter++; } ifs.close(); ifs.open(samplePath); samples_.resize(counter<maxSampleIdx ? counter : maxSampleIdx); counter=0; while (! ifs.eof() && counter < maxSampleIdx) { if (samples_[counter].readFromStream(ifs)) counter++; } if (counter<samples_.size()) samples_.resize(counter); // read phi file phi_.clear(); string phiPath = dirPath + "/" + fileName + ".phi"; ifs.open(phiPath.c_str()); if (! ifs.good()) { cout << "Warning: could not find feedback file " << phiPath << endl; } else { char buffer[64]; while (! ifs.eof()) { ifs.getline(buffer,64); istringstream iss(buffer); OrderedPair op; iss >> op.idx0 >> op.idx1 >> op.weight; if (op.idx0 < maxSampleIdx && op.idx1 < maxSampleIdx) { assert(op.idx0 < op.idx1); if (op.weight < 0.0) op.weight = 1.0; phi_.push_back(op); totalPhiWeight_ += op.weight; } } } return (samples_.size()); }