26#define GLM_FORCE_RADIANS
31#include <wx/datetime.h>
34#include <wx/stdpaths.h>
54#define MASK_3D_CACHE "3D_CACHE"
59static bool checkTag(
const char* aTag,
void* aPluginMgrPtr )
61 if(
nullptr == aTag ||
nullptr == aPluginMgrPtr )
147 *aCachePtr =
nullptr;
151 if( full3Dpath.empty() )
154 wxLogTrace(
MASK_3D_CACHE, wxT(
"%s:%s:%d\n * [3D model] could not find model '%s'\n" ),
155 __FILE__, __FUNCTION__, __LINE__, aModelFile );
162 std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
167 wxFileName fname( full3Dpath );
169 if( fname.FileExists() )
172 wxDateTime fmdate = fname.GetModificationTime();
174 if( fmdate != mi->second->modTime )
177 getHash( full3Dpath, hashSum );
178 mi->second->modTime = fmdate;
180 if( hashSum != mi->second->m_hash )
182 mi->second->SetHash( hashSum );
189 if(
nullptr != mi->second->sceneData )
192 mi->second->sceneData =
nullptr;
195 if(
nullptr != mi->second->renderData )
199 mi->second->pluginInfo );
203 if(
nullptr != aCachePtr )
204 *aCachePtr = mi->second;
216 return load( aModelFile, aBasePath,
nullptr, aEmbeddedFiles );
223 *aCachePtr =
nullptr;
228 wxFileName fname( aFileName );
229 ep->
modTime = fname.GetModificationTime();
237 if(
m_CacheMap.emplace( aFileName, ep ).second ==
false )
240 wxT(
"%s:%s:%d\n * [BUG] duplicate entry in map file; key = '%s'" ),
241 __FILE__, __FUNCTION__, __LINE__, aFileName );
255 if(
m_CacheMap.emplace( aFileName, ep ).second ==
false )
258 wxT(
"%s:%s:%d\n * [BUG] duplicate entry in map file; key = '%s'" ),
259 __FILE__, __FUNCTION__, __LINE__, aFileName );
272 wxString cachename =
m_CacheDir + bname + wxT(
".3dc" );
289 if( aFileName.empty() )
291 wxLogTrace(
MASK_3D_CACHE, wxT(
"%s:%s:%d\n * [BUG] empty filename" ),
292 __FILE__, __FUNCTION__, __LINE__ );
298 FILE* fp = _wfopen( aFileName.wc_str(), L
"rb" );
300 FILE* fp = fopen( aFileName.ToUTF8(),
"rb" );
307 std::vector<char> block( 4096 );
310 while( ( bsize = fread( block.data(), 1, 4096, fp ) ) > 0 )
326 wxT(
" * [3D model] cannot load cached model; no file hash available" ) );
334 wxT(
" * [3D model] cannot load cached model; config directory unknown" ) );
339 wxString fname =
m_CacheDir + bname + wxT(
".3dc" );
341 if( !wxFileName::FileExists( fname ) )
343 wxLogTrace(
MASK_3D_CACHE, wxT(
" * [3D model] cannot open file '%s'" ), fname.GetData() );
361 if(
nullptr == aCacheItem )
363 wxLogTrace(
MASK_3D_CACHE, wxT(
"%s:%s:%d\n * NULL passed for aCacheItem" ),
364 __FILE__, __FUNCTION__, __LINE__ );
371 wxLogTrace(
MASK_3D_CACHE, wxT(
"%s:%s:%d\n * aCacheItem has no valid scene data" ),
372 __FILE__, __FUNCTION__, __LINE__ );
382 wxT(
" * [3D model] cannot load cached model; no file hash available" ) );
390 wxT(
" * [3D model] cannot load cached model; config directory unknown" ) );
395 wxString fname =
m_CacheDir + bname + wxT(
".3dc" );
397 if( wxFileName::Exists( fname ) )
399 if( !wxFileName::FileExists( fname ) )
402 wxT(
" * [3D model] path exists but is not a regular file '%s'" ), fname );
422 if( !cfgdir.DirExists() )
424 cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
426 if( !cfgdir.DirExists() )
429 wxT(
"%s:%s:%d\n * failed to create 3D configuration directory '%s'" ),
430 __FILE__, __FUNCTION__, __LINE__, cfgdir.GetPath() );
442 wxT(
"%s:%s:%d\n * could not set 3D Config Directory on filename resolver\n"
443 " * config directory: '%s'" ),
456 cacheDir.AppendDir( wxT(
"3d" ) );
458 if( !cacheDir.DirExists() )
460 cacheDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
462 if( !cacheDir.DirExists() )
465 wxT(
"%s:%s:%d\n * failed to create 3D cache directory '%s'" ),
466 __FILE__, __FUNCTION__, __LINE__, cacheDir.GetPath() );
481 bool hasChanged =
false;
487 std::list< S3D_CACHE_ENTRY* >::iterator sL =
m_CacheList.begin();
488 std::list< S3D_CACHE_ENTRY* >::iterator eL =
m_CacheList.end();
525 std::list< S3D_CACHE_ENTRY* >::iterator sCL =
m_CacheList.begin();
526 std::list< S3D_CACHE_ENTRY* >::iterator eCL =
m_CacheList.end();
553 SCENEGRAPH* sp =
load( aModelFileName, aBasePath, &cp, aEmbeddedFiles );
561 wxT(
"%s:%s:%d\n * [BUG] model loaded with no associated S3D_CACHE_ENTRY" ),
562 __FILE__, __FUNCTION__, __LINE__ );
579 wxString fileSpec = wxT(
"*.3dc" );
580 wxArrayString fileList;
581 size_t numFilesFound = 0;
584 wxDateTime lastAccess, thresholdDate;
585 wxDateSpan durationInDays;
588 durationInDays.SetDays( aNumDaysOld );
589 thresholdDate = wxDateTime::Now() - durationInDays;
597 numFilesFound = dir.GetAllFiles(
m_CacheDir, &fileList, fileSpec );
599 for(
unsigned int i = 0; i < numFilesFound; i++ )
602 thisFile.SetFullName( fileList[i] );
605 if( thisFile.GetTimes( &lastAccess,
nullptr,
nullptr ) )
607 if( lastAccess.IsEarlierThan( thresholdDate ) )
610 wxRemoveFile( thisFile.GetFullPath() );
static std::mutex mutex3D_cache
static bool checkTag(const char *aTag, void *aPluginMgrPtr)
defines the basic data associated with a single 3D model.
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
Provide an extensible class to resolve 3D model paths.
bool Set3DConfigDir(const wxString &aConfigDir)
Set the user's configuration directory for 3D models.
bool SetProject(PROJECT *aProject, bool *flgChanged=nullptr)
Set the current KiCad project directory as the first entry in the model path list.
void SetProgramBase(PGM_BASE *aBase)
Set a pointer to the application's PGM_BASE instance used to extract the local env vars.
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath, const EMBEDDED_FILES *aFiles)
Determines the full path of the given file name.
A streaming C++ equivalent for MurmurHash3_x64_128.
FORCE_INLINE void add(const std::string &input)
FORCE_INLINE HASH_128 digest()
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Container for data for KiCad programs.
Container for project specific data.
S3D_CACHE_ENTRY & operator=(const S3D_CACHE_ENTRY &source)
S3D_CACHE_ENTRY(const S3D_CACHE_ENTRY &source)
void SetHash(const HASH_128 &aHash)
const wxString GetCacheBaseName()
SCENEGRAPH * Load(const wxString &aModelFile, const wxString &aBasePath, const EMBEDDED_FILES *aEmbeddedFiles)
Attempt to load the scene data for a model.
void SetProgramBase(PGM_BASE *aBase)
Set the filename resolver's pointer to the application's PGM_BASE instance.
bool loadCacheData(S3D_CACHE_ENTRY *aCacheItem)
void FlushCache(bool closePlugins=true)
Free all data in the cache and by default closes all plugins.
bool Set3DConfigDir(const wxString &aConfigDir)
Sets the configuration directory to be used by the model manager for storing 3D model manager configu...
S3DMODEL * GetModel(const wxString &aModelFileName, const wxString &aBasePath, const EMBEDDED_FILES *aEmbeddedFiles)
Attempt to load the scene data for a model and to translate it into an S3D_MODEL structure for displa...
SCENEGRAPH * load(const wxString &aModelFile, const wxString &aBasePath, S3D_CACHE_ENTRY **aCachePtr=nullptr, const EMBEDDED_FILES *aEmbeddedFiles=nullptr)
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=nullptr)
Find or create cache entry for file name.
S3D_PLUGIN_MANAGER * m_Plugins
bool saveCacheData(S3D_CACHE_ENTRY *aCacheItem)
std::list< wxString > const * GetFileFilters() const
Return the list of file filters retrieved from the plugins.
FILENAME_RESOLVER * GetResolver() noexcept
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
bool SetProject(PROJECT *aProject)
Set the current project's working directory; this affects the model search path.
void CleanCacheDir(int aNumDaysOld)
Delete up old cache files in cache directory.
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
void ClosePlugins()
Unload plugins to free memory.
bool getHash(const wxString &aFileName, HASH_128 &aHash)
Calculate the SHA1 hash of the given file.
FILENAME_RESOLVER * m_FNResolver
void ClosePlugins(void)
Iterate through all discovered plugins and closes them to reclaim memory.
SCENEGRAPH * Load3DModel(const wxString &aFileName, std::string &aPluginInfo)
std::list< wxString > const * GetFileFilters(void) const noexcept
Return the list of file filters; this will contain at least the default "All Files (*....
bool CheckTag(const char *aTag)
Check the given tag and returns true if the plugin named in the tag is not loaded or the plugin is lo...
Define the basic data set required to represent a 3D model.
The base class of all Scene Graph nodes.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
bool m_Skip3DModelMemoryCache
Skip reading/writing 3D model memory caches.
defines the API calls for the manipulation of SG* classes
SGLIB_API SGNODE * ReadCache(const char *aFileName, void *aPluginMgr, bool(*aTagCheck)(const char *, void *))
Function ReadCache reads a binary cache file and creates an SGNODE tree.
SGLIB_API bool WriteCache(const char *aFileName, bool overwrite, SGNODE *aNode, const char *aPluginInfo)
Function WriteCache writes the SGNODE tree to a binary cache file.
SGLIB_API void DestroyNode(SGNODE *aNode) noexcept
Function DestroyNode deletes the given SG* class node.
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
SGLIB_API void Destroy3DModel(S3DMODEL **aModel)
Function Destroy3DModel frees memory used by an S3DMODEL structure and sets the pointer to the struct...
A storage class for 128-bit hash value.
std::string ToString() const
Store the a model based on meshes and materials.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().