Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ofToDataPath simplification #7368

Open
wants to merge 67 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
c96c60f
file utils simplification
dimitre Mar 7, 2023
c199076
cubemap
dimitre Mar 7, 2023
a8e677a
cubemap fix aCachePath
dimitre Mar 7, 2023
ce61301
parent_path() fix
dimitre Mar 7, 2023
9159896
ofToDataPath string
dimitre Mar 7, 2023
130008a
tiny fix
dimitre Mar 7, 2023
c3914c2
updates
dimitre Mar 7, 2023
27b6370
Merge branch 'openframeworks:master' into fs
dimitre Mar 8, 2023
51b141d
Merge branch 'openframeworks:master' into fs
dimitre Mar 9, 2023
e5e5ff2
things to fix yet
dimitre Mar 9, 2023
9dc6022
Merge branch 'openframeworks:master' into fs
dimitre Mar 12, 2023
679f6dd
more changes
dimitre Mar 12, 2023
d86685a
Merge branch 'openframeworks:master' into fs
dimitre Mar 13, 2023
59db08d
tests
dimitre Mar 15, 2023
0a11ede
up
dimitre Mar 15, 2023
90f344d
changedir to data
dimitre Mar 15, 2023
1a69450
no windows check anymore
dimitre Mar 15, 2023
026663d
data folder missing in this test
dimitre Mar 15, 2023
b9e6299
missing xm data folder
dimitre Mar 15, 2023
7badb5f
cubemap in separate pr
dimitre Mar 15, 2023
ed78257
more changes
dimitre Mar 15, 2023
e403845
Merge branch 'openframeworks:master' into fs
dimitre Mar 15, 2023
7249c12
changes
dimitre Mar 15, 2023
c87c967
ok
dimitre Mar 15, 2023
e6f7815
ok
dimitre Mar 15, 2023
642bd80
offile open relative
dimitre Mar 15, 2023
bef7d34
update relative paths if needed.
dimitre Mar 15, 2023
f722555
more cleanup
dimitre Mar 15, 2023
15d7d91
cout::endl fix
dimitre Mar 15, 2023
fdba45b
more cleanup
dimitre Mar 15, 2023
e2dffe5
Merge branch 'openframeworks:master' into fs
dimitre Mar 17, 2023
b5e8112
getCurrentExePathFS, fs namespace
dimitre Apr 17, 2023
d0d04e2
update submodules
dimitre Nov 23, 2023
d73a5bb
Merge branch 'master' into fs
dimitre Nov 23, 2023
75e93af
fix perms
dimitre Nov 24, 2023
da630d2
Merge branch 'master' into fs
dimitre Dec 1, 2023
4126b05
of::filesystem to fs
dimitre Dec 1, 2023
155894e
fix
dimitre Dec 1, 2023
14a4711
up
dimitre Dec 1, 2023
919656c
revert some
dimitre Dec 5, 2023
902f0ca
revert commit
dimitre Dec 5, 2023
76e84bc
revert
dimitre Dec 5, 2023
d2585db
merge master, fix conflicts
dimitre Jan 7, 2024
36090e6
fix conflicts
dimitre Jan 7, 2024
8561228
update tests
dimitre Jan 7, 2024
ad097db
Merge branch 'openframeworks:master' into fs
dimitre May 10, 2024
ac6eeda
Merge branch 'openframeworks:master' into fs
dimitre May 11, 2024
b0f6e88
up
dimitre May 11, 2024
2a4ac9d
group / endgroup in run_tests
dimitre May 11, 2024
c7e933f
group
dimitre May 11, 2024
fd7d7cc
up
dimitre May 11, 2024
42df8d5
Merge branch 'openframeworks:master' into fs
dimitre May 11, 2024
ac54e24
Merge branch 'openframeworks:master' into fs
dimitre May 11, 2024
c0aeaeb
Merge branch 'openframeworks:master' into fs
dimitre May 11, 2024
5f038a8
try to fix
dimitre May 12, 2024
b73badf
ok
dimitre May 12, 2024
f507a92
Merge branch 'master' into fs
dimitre May 16, 2024
8c1eca8
Merge branch 'openframeworks:master' into fs
dimitre May 19, 2024
d742529
up
dimitre Sep 2, 2024
83a0d3f
more conflicts fix
dimitre Sep 2, 2024
9a8e995
up
dimitre Sep 2, 2024
9192592
up win
dimitre Sep 2, 2024
e27babd
more
dimitre Sep 2, 2024
cb01825
update tests
dimitre Sep 2, 2024
7b52456
gitkeep data folder tests
dimitre Sep 2, 2024
c646e2b
Merge branch 'openframeworks:master' into fs
dimitre Oct 15, 2024
c6b18ee
Merge branch 'openframeworks:master' into fs
dimitre Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions libs/openFrameworks/gl/ofCubeMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,30 +383,33 @@ bool ofCubeMap::load( ofCubeMapSettings aSettings ) {
// figure out the number of mip maps //
data->maxMipLevels = log2(data->settings.preFilterRes) + 1;

std::string encFolder = data->settings.cacheDirectory;
of::filesystem::path encFolder = data->settings.cacheDirectory;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should prob be in another PR but fine for this one.

Maybe we should start using auto instead of of::filesystem::path for local vars going forward as it will make the code more adaptable in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes +1 for auto.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this specific one should not be auto, because data->settings.cacheDirectory is a string, so I'm building an of::filesystem::path from that.

if( !encFolder.empty() ) {
if( !ofDirectory::doesDirectoryExist( ofFilePath::removeTrailingSlash( data->settings.cacheDirectory ))) {
#if !defined(TARGET_OPENGLES)
#if !defined(TARGET_OPENGLES)
if(!ofDirectory::createDirectory( data->settings.cacheDirectory )) {
ofLogWarning("ofCubeMap :: load : unable to create directory: ") << data->settings.cacheDirectory;
}
#endif
#endif
} else {

}
// encFolder = ofFilePath::addTrailingSlash( data->settings.cacheDirectory ).string();
encFolder = ofFilePath::addTrailingSlash( data->settings.cacheDirectory );
// encFolder = ofFilePath::addTrailingSlash( data->settings.cacheDirectory );

}
std::string baseName = ofFilePath::getBaseName( data->settings.filePath );
std::string cacheIrrName = baseName+"_irr_"+ofToString(data->settings.irradianceRes,0)+".exr";
std::string cachePrefilterName = baseName+"_pre_"+ofToString(data->settings.preFilterRes,0)+".exr";

// TODO: change everything to of::filesystem
of::filesystem::path baseName = ofFilePath::getBaseName( data->settings.filePath );
of::filesystem::path cacheIrrName = baseName / ("_irr_"+ofToString(data->settings.irradianceRes,0) + ".exr");
of::filesystem::path cachePrefilterName = baseName / ("_pre_"+ofToString(data->settings.preFilterRes,0)+".exr");

bool bHasCachedIrr = false;
bool bHasCachedPre = false;
if( data->settings.useCache && !data->settings.overwriteCache ) {
bHasCachedIrr = _loadIrradianceMap(encFolder+cacheIrrName);
bHasCachedIrr = _loadIrradianceMap(encFolder / cacheIrrName);
ofLogVerbose("ofCubeMap :: _loadIrradianceMap: ") << bHasCachedIrr;
bHasCachedPre = _loadPrefilterMap(encFolder+cachePrefilterName);
bHasCachedPre = _loadPrefilterMap(encFolder / cachePrefilterName);
ofLogVerbose("ofCubeMap :: _loadPrefilterMap: ") << bHasCachedPre;
}

Expand All @@ -418,12 +421,12 @@ bool ofCubeMap::load( ofCubeMapSettings aSettings ) {

if( !bHasCachedIrr ) {
ofLogVerbose("ofCubeMap :: going to create irradiance map");
_createIrradianceMap(cubeFid,bMakeCache, encFolder+cacheIrrName);
_createIrradianceMap(cubeFid,bMakeCache, encFolder / cacheIrrName);
}

if( !bHasCachedPre ) {
ofLogVerbose("ofCubeMap :: going to create pre filtered cube map");
_createPrefilteredCubeMap(cubeFid, srcCubeFSize,bMakeCache,encFolder+cachePrefilterName );
_createPrefilteredCubeMap(cubeFid, srcCubeFSize,bMakeCache,encFolder / cachePrefilterName );
}

glDeleteTextures(1, &cubeFid );
Expand Down Expand Up @@ -737,7 +740,7 @@ void ofCubeMap::_equiRectToCubeMap( GLuint& aCubeTexId, ofTexture& aSrcTex, int
}

//--------------------------------------------------------------
void ofCubeMap::_createIrradianceMap(GLuint aSrcCubeFid, bool aBMakeCache, std::string aCachePath) {
void ofCubeMap::_createIrradianceMap(GLuint aSrcCubeFid, bool aBMakeCache, of::filesystem::path aCachePath) {
if(data->bIrradianceAllocated) {
return;
}
Expand Down Expand Up @@ -878,7 +881,7 @@ void ofCubeMap::_createIrradianceMap(GLuint aSrcCubeFid, bool aBMakeCache, std::
}

//--------------------------------------------------------------
bool ofCubeMap::_loadIrradianceMap(std::string aCachePath) {
bool ofCubeMap::_loadIrradianceMap(of::filesystem::path aCachePath) {

if(data->bIrradianceAllocated) {
return false;
Expand Down Expand Up @@ -937,7 +940,7 @@ bool ofCubeMap::_loadIrradianceMap(std::string aCachePath) {
}

//--------------------------------------------------------------
void ofCubeMap::_createPrefilteredCubeMap(GLuint aSrcCubeFid, int aSrcRes, bool aBMakeCache, std::string aCachePath) {
void ofCubeMap::_createPrefilteredCubeMap(GLuint aSrcCubeFid, int aSrcRes, bool aBMakeCache, of::filesystem::path aCachePath) {
if(data->bPreFilteredMapAllocated) {
return;
}
Expand Down Expand Up @@ -1095,7 +1098,7 @@ void ofCubeMap::_createPrefilteredCubeMap(GLuint aSrcCubeFid, int aSrcRes, bool
}

//--------------------------------------------------------------
bool ofCubeMap::_loadPrefilterMap( std::string aCachePath ) {
bool ofCubeMap::_loadPrefilterMap( of::filesystem::path aCachePath ) {
if(data->bPreFilteredMapAllocated) {
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions libs/openFrameworks/gl/ofCubeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ class ofCubeMap {
GLuint _createFloatCubeMap(ofTexture& aSrcTex, int aSrcRes);
void _equiRectToCubeMap( GLuint& aCubeTexId, ofTexture& aSrcTex, int aSrcRes, bool aBConvertToNonFloat );

void _createIrradianceMap(GLuint aSrcCubeFid, bool aBMakeCache, std::string aCachePath);
bool _loadIrradianceMap(std::string aCachePath);
void _createIrradianceMap(GLuint aSrcCubeFid, bool aBMakeCache, of::filesystem::path aCachePath);
bool _loadIrradianceMap( of::filesystem::path aCachePath );

void _createPrefilteredCubeMap(GLuint aSrcCubeFid, int aSrcRes, bool aBMakeCache, std::string aCachePath);
bool _loadPrefilterMap( std::string aCachePath );
void _createPrefilteredCubeMap(GLuint aSrcCubeFid, int aSrcRes, bool aBMakeCache, of::filesystem::path aCachePath);
bool _loadPrefilterMap( of::filesystem::path aCachePath );

static void _createBrdfLUT();

Expand Down
138 changes: 35 additions & 103 deletions libs/openFrameworks/utils/ofFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@ namespace{
bool enableDataPath = true;

//--------------------------------------------------
// MARK: - near future
// of::filesystem::path defaultDataPath(){
std::string defaultDataPath(){
#if defined TARGET_OSX
try{
return of::filesystem::canonical(ofFilePath::getCurrentExeDir() / of::filesystem::path("../../../data/")).string();
}catch(...){
return (ofFilePath::getCurrentExeDir() / of::filesystem::path("../../../data/")).string();
}
#elif defined TARGET_ANDROID
return string("sdcard/");
#else
try{
return of::filesystem::canonical(ofFilePath::join(ofFilePath::getCurrentExeDir(), "data/")).make_preferred().string();
}catch(...){
return (ofFilePath::getCurrentExeDir() / of::filesystem::path("data/")).string();
}
#endif
of::filesystem::path defaultDataPath(){
#if defined TARGET_OSX

// three levels up because is a directory with path App.app/Contents/MacOS/App
of::filesystem::path data = ofFilePath::getCurrentExeDir().parent_path().parent_path().parent_path() / "data";
if (!of::filesystem::is_directory(data)) {
// bundle data folder inside app
// one level up : MacOS/../Resources/data
data = ofFilePath::getCurrentExeDir().parent_path() / "Resources/data";
}
return data;

#elif defined TARGET_ANDROID
return { "sdcard/" };
#else
return (ofFilePath::getCurrentExeDir() / "data");
#endif
}

//--------------------------------------------------
Expand Down Expand Up @@ -1679,8 +1678,7 @@ string ofFilePath::addLeadingSlash(const of::filesystem::path& _path){
}

//------------------------------------------------------------------------------------------------------------
// MARK: - near future
//of::filesystem::path ofFilePath::addTrailingSlash(const of::filesystem::path & _path){
// MARK: - Remove this function after FS transition
std::string ofFilePath::addTrailingSlash(const of::filesystem::path & _path){
#if OF_USING_STD_FS && !OF_USE_EXPERIMENTAL_FS
if(_path.string().empty()) return "";
Expand Down Expand Up @@ -1776,7 +1774,9 @@ std::string ofFilePath::getEnclosingDirectory(const of::filesystem::path & _file
if(bRelativeToData){
fp = ofToDataPath(fp);
}
return addTrailingSlash(fp.parent_path());

// return addTrailingSlash(fp.parent_path());
return fp.parent_path().string();
}

//------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1854,10 +1854,9 @@ string ofFilePath::getCurrentExePath(){
}

//------------------------------------------------------------------------------------------------------------
// MARK: - near future
//of::filesystem::path ofFilePath::getCurrentExeDir(){
std::string ofFilePath::getCurrentExeDir(){
return ofFilePath::getEnclosingDirectory(ofFilePath::getCurrentExePath(), false);
of::filesystem::path ofFilePath::getCurrentExeDir(){
auto exePath = of::filesystem::path(getCurrentExePath());
return exePath.parent_path();
}

//------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1927,22 +1926,9 @@ void ofSetDataPathRoot(const of::filesystem::path& newRoot){
}

//--------------------------------------------------
// MARK: - near future
//of::filesystem::path ofToDataPath(const of::filesystem::path & path, bool makeAbsolute){
std::string ofToDataPath(const of::filesystem::path & path, bool makeAbsolute){
dimitre marked this conversation as resolved.
Show resolved Hide resolved
if (makeAbsolute && path.is_absolute()) {
// return path;
return path.string();
}

if (!enableDataPath) {
// return path;
return path.string();
}

bool hasTrailingSlash = !path.empty() && path.generic_string().back()=='/';

// if our Current Working Directory has changed (e.g. file open dialog)
// if our Current Working Directory has changed (e.g. file open dialog)
#ifdef TARGET_WIN32
if (defaultWorkingDirectory() != of::filesystem::current_path()) {
// change our cwd back to where it was on app load
Expand All @@ -1952,72 +1938,18 @@ std::string ofToDataPath(const of::filesystem::path & path, bool makeAbsolute){
}
}
#endif

// this could be performed here, or wherever we might think we accidentally change the cwd, e.g. after file dialogs on windows
// FIXME: change to direct returns when return type is fs::path
of::filesystem::path outPath;
const auto & dataPath = dataPathRoot();
of::filesystem::path inputPath(path);
of::filesystem::path outputPath;

// if path is already absolute, just return it
if (inputPath.is_absolute()) {
try {
auto outpath = of::filesystem::canonical(inputPath).make_preferred();
if(of::filesystem::is_directory(outpath) && hasTrailingSlash){
return ofFilePath::addTrailingSlash(outpath);
}else{
return outpath.string();
// return outpath;
}
}
catch (...) {
return inputPath.string();
// return inputPath;
}
}

// here we check whether path already refers to the data folder by looking for common elements
// if the path begins with the full contents of dataPathRoot then the data path has already been added
// we compare inputPath.toString() rather that the input var path to ensure common formatting against dataPath.toString()
auto dirDataPath = dataPath;
// also, we strip the trailing slash from dataPath since `path` may be input as a file formatted path even if it is a folder (i.e. missing trailing slash)
dirDataPath = ofFilePath::addTrailingSlash(dataPath);

auto relativeDirDataPath = ofFilePath::makeRelative(of::filesystem::current_path(), dataPath);
relativeDirDataPath = ofFilePath::addTrailingSlash(relativeDirDataPath);

// FIXME: this can be simplified without using string conversion
// if (inputPath.string().find(dirDataPath.string()) != 0 && inputPath.string().find(relativeDirDataPath.string())!=0) {
if (inputPath.string().find(dirDataPath.string()) != 0 && inputPath.string().find(relativeDirDataPath)!=0) {
// inputPath doesn't contain data path already, so we build the output path as the inputPath relative to the dataPath
if(makeAbsolute){
outputPath = dirDataPath / inputPath;
}else{
outputPath = relativeDirDataPath / inputPath;
}
if (makeAbsolute) {
outPath = dataPath / path;
Copy link
Member

@ofTheo ofTheo Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be an edge case here if you pass in an absolute path and makeAbsolute is true.
ie: path is /Users/me/Desktop/image.jpg if makeAbsolute is true and the path is already absolute then it would prepend dataPath ie: /Users/me/Downloads/OF/apps/myApp/bin/data/Users/Theo/Desktop/image.jpg

does this edge case exist in the original code? looks like it might?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I'll be investigating this better now

} else {
// inputPath already contains data path, so no need to change
outputPath = inputPath;
}

// finally, if we do want an absolute path and we don't already have one
if(makeAbsolute){
// then we return the absolute form of the path
try {
auto outpath = of::filesystem::canonical(of::filesystem::absolute(outputPath)).make_preferred();
if(of::filesystem::is_directory(outpath) && hasTrailingSlash){
return ofFilePath::addTrailingSlash(outpath);
}else{
// return outpath;
return outpath.string();
}
if (!path.is_absolute()) {
auto exeDir = ofFilePath::getCurrentExeDir();
outPath = of::filesystem::relative(dataPath / path, ofFilePath::getCurrentExeDir());
} else {
outPath = path;
}
catch (std::exception &) {
return of::filesystem::absolute(outputPath).string();
// return of::filesystem::absolute(outputPath);
}
}else{
// or output the relative path
// return outputPath;
return outputPath.string();
}
return outPath.string();
}
7 changes: 2 additions & 5 deletions libs/openFrameworks/utils/ofFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ class ofFilePath{
///
/// \param path directory path
/// \returns path + slash
// MARK: - near future
// static of::filesystem::path addTrailingSlash(const of::filesystem::path& path);
// MARK: - remove after fs transition
static std::string addTrailingSlash(const of::filesystem::path& path);

/// Remove a path's trailing slash (if found),
Expand Down Expand Up @@ -437,9 +436,7 @@ class ofFilePath{
/// Mac: the Contents/MacOS folder within the application's .app bundle
///
/// \returns current executable directory
// MARK: - near future
// static of::filesystem::path getCurrentExeDir();
static std::string getCurrentExeDir();
static of::filesystem::path getCurrentExeDir();

/// Get the absolute path to the user's home directory.
///
Expand Down