KiCad PCB EDA Suite
S3D_CACHE Class Reference

S3D_CACHE. More...

#include <3d_cache.h>

Inheritance diagram for S3D_CACHE:
PROJECT::_ELEM

Public Member Functions

 S3D_CACHE ()
 
virtual ~S3D_CACHE ()
 
KICAD_T Type () override
 
bool Set3DConfigDir (const wxString &aConfigDir)
 Function Set3DConfigDir Sets the configuration directory to be used by the model manager for storing 3D model manager configuration data and the model cache. More...
 
wxString Get3DConfigDir (bool createDefault=false)
 Function Get3DConfigDir returns the current 3D configuration directory on success, otherwise it returns wxEmptyString. More...
 
bool SetProjectDir (const wxString &aProjDir)
 Function SetProjectDir sets the current project's working directory; this affects the model search path. More...
 
void SetProgramBase (PGM_BASE *aBase)
 Function SetProgramBase sets the filename resolver's pointer to the application's PGM_BASE instance; the pointer is used to extract the local env vars. More...
 
wxString GetProjectDir (void)
 Function GetProjectDir returns the current project's working directory. More...
 
SCENEGRAPHLoad (const wxString &aModelFile)
 Function Load attempts to load the scene data for a model; it will consult the internal cache list and load from cache if possible before invoking the load() function of the available plugins. More...
 
FILENAME_RESOLVERGetResolver (void)
 
std::list< wxString > const * GetFileFilters (void) const
 Function GetFileFilters returns the list of file filters retrieved from the plugins; this will contain at least the default "All Files (*.*)|*.*". More...
 
void FlushCache (bool closePlugins=true)
 Function FlushCache frees all data in the cache and by default closes all plugins. More...
 
void ClosePlugins (void)
 Function ClosePlugins unloads plugins to free memory. More...
 
S3DMODELGetModel (const wxString &aModelFileName)
 Function GetModel attempts to load the scene data for a model and to translate it into an S3D_MODEL structure for display by a renderer. More...
 
wxString GetModelHash (const wxString &aModelFileName)
 

Private Member Functions

SCENEGRAPHcheckCache (const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
 Find or create cache entry for file name. More...
 
bool getSHA1 (const wxString &aFileName, unsigned char *aSHA1Sum)
 Function getSHA1 calculates the SHA1 hash of the given file. More...
 
bool loadCacheData (S3D_CACHE_ENTRY *aCacheItem)
 
bool saveCacheData (S3D_CACHE_ENTRY *aCacheItem)
 
SCENEGRAPHload (const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
 

Private Attributes

std::list< S3D_CACHE_ENTRY * > m_CacheList
 cache entries More...
 
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxStringm_CacheMap
 mapping of file names to cache names and data More...
 
FILENAME_RESOLVERm_FNResolver
 object to resolve file names More...
 
S3D_PLUGIN_MANAGERm_Plugins
 plugin manager More...
 
bool m_DirtyCache
 set true if the cache needs to be updated More...
 
wxString m_CacheDir
 3D cache directory More...
 
wxString m_ConfigDir
 base configuration path for 3D items More...
 
wxString m_ProjDir
 current KiCad project dir More...
 

Detailed Description

S3D_CACHE.

Cache for storing the 3D shapes. This cache is able to be stored as a project element (since it inherits from PROJECT::_ELEM).

Definition at line 54 of file 3d_cache.h.

Constructor & Destructor Documentation

◆ S3D_CACHE()

S3D_CACHE::S3D_CACHE ( )

Definition at line 190 of file 3d_cache.cpp.

191 {
192  m_DirtyCache = false;
195 
196  return;
197 }
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
bool m_DirtyCache
set true if the cache needs to be updated
Definition: 3d_cache.h:70
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References m_DirtyCache, m_FNResolver, and m_Plugins.

◆ ~S3D_CACHE()

S3D_CACHE::~S3D_CACHE ( )
virtual

Definition at line 200 of file 3d_cache.cpp.

201 {
202  FlushCache();
203 
204  if( m_FNResolver )
205  delete m_FNResolver;
206 
207  if( m_Plugins )
208  delete m_Plugins;
209 
210  return;
211 }
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
void FlushCache(bool closePlugins=true)
Function FlushCache frees all data in the cache and by default closes all plugins.
Definition: 3d_cache.cpp:702
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References FlushCache(), m_FNResolver, and m_Plugins.

Member Function Documentation

◆ checkCache()

SCENEGRAPH * S3D_CACHE::checkCache ( const wxString &  aFileName,
S3D_CACHE_ENTRY **  aCachePtr = NULL 
)
private

Find or create cache entry for file name.

Searches the cache list for the given filename and retrieves the cache data; a cache entry is created if one does not already exist.

Parameters
[in]aFileNamefile name (full or partial path)
[out]aCachePtroptional return address for cache entry pointer
Returns
SCENEGRAPH object associated with file name
Return values
NULLon error

Definition at line 289 of file 3d_cache.cpp.

290 {
291  if( aCachePtr )
292  *aCachePtr = NULL;
293 
294  unsigned char sha1sum[20];
295 
296  if( !getSHA1( aFileName, sha1sum ) || m_CacheDir.empty() )
297  {
298  // just in case we can't get a hash digest (for example, on access issues)
299  // or we do not have a configured cache file directory, we create an
300  // entry to prevent further attempts at loading the file
302  m_CacheList.push_back( ep );
303  wxFileName fname( aFileName );
304  ep->modTime = fname.GetModificationTime();
305 
306  if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* >
307  ( aFileName, ep ) ).second == false )
308  {
309  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [BUG] duplicate entry in map file; key = '%s'",
310  __FILE__, __FUNCTION__, __LINE__, aFileName );
311 
312  m_CacheList.pop_back();
313  delete ep;
314  }
315  else
316  {
317  if( aCachePtr )
318  *aCachePtr = ep;
319 
320  }
321 
322  return NULL;
323  }
324 
326  m_CacheList.push_back( ep );
327  wxFileName fname( aFileName );
328  ep->modTime = fname.GetModificationTime();
329 
330  if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* >
331  ( aFileName, ep ) ).second == false )
332  {
333  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [BUG] duplicate entry in map file; key = '%s'",
334  __FILE__, __FUNCTION__, __LINE__, aFileName );
335 
336  m_CacheList.pop_back();
337  delete ep;
338  return NULL;
339  }
340 
341  if( aCachePtr )
342  *aCachePtr = ep;
343 
344  ep->SetSHA1( sha1sum );
345 
346  wxString bname = ep->GetCacheBaseName();
347  wxString cachename = m_CacheDir + bname + wxT( ".3dc" );
348 
349  if( wxFileName::FileExists( cachename ) && loadCacheData( ep ) )
350  return ep->sceneData;
351 
352  ep->sceneData = m_Plugins->Load3DModel( aFileName, ep->pluginInfo );
353 
354  if( NULL != ep->sceneData )
355  saveCacheData( ep );
356 
357  return ep->sceneData;
358 }
std::string pluginInfo
Definition: 3d_cache.cpp:142
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:181
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
bool loadCacheData(S3D_CACHE_ENTRY *aCacheItem)
Definition: 3d_cache.cpp:417
SCENEGRAPH * Load3DModel(const wxString &aFileName, std::string &aPluginInfo)
bool saveCacheData(S3D_CACHE_ENTRY *aCacheItem)
Definition: 3d_cache.cpp:458
wxDateTime modTime
Definition: 3d_cache.cpp:140
#define NULL
wxString m_CacheDir
3D cache directory
Definition: 3d_cache.h:73
void SetSHA1(const unsigned char *aSHA1Sum)
Definition: 3d_cache.cpp:166
bool getSHA1(const wxString &aFileName, unsigned char *aSHA1Sum)
Function getSHA1 calculates the SHA1 hash of the given file.
Definition: 3d_cache.cpp:361
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:58
Definition: 3d_cache.cpp:124
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References S3D_CACHE_ENTRY::GetCacheBaseName(), getSHA1(), S3D_PLUGIN_MANAGER::Load3DModel(), loadCacheData(), m_CacheDir, m_CacheList, m_CacheMap, m_Plugins, MASK_3D_CACHE, S3D_CACHE_ENTRY::modTime, NULL, S3D_CACHE_ENTRY::pluginInfo, saveCacheData(), S3D_CACHE_ENTRY::sceneData, and S3D_CACHE_ENTRY::SetSHA1().

Referenced by GetModelHash(), and load().

◆ ClosePlugins()

void S3D_CACHE::ClosePlugins ( void  )

Function ClosePlugins unloads plugins to free memory.

Definition at line 723 of file 3d_cache.cpp.

724 {
725  if( NULL != m_Plugins )
727 
728  return;
729 }
#define NULL
void ClosePlugins(void)
Function ClosePlugins iterates through all discovered plugins and closes them to reclaim memory.
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References S3D_PLUGIN_MANAGER::ClosePlugins(), m_Plugins, and NULL.

Referenced by FlushCache().

◆ FlushCache()

void S3D_CACHE::FlushCache ( bool  closePlugins = true)

Function FlushCache frees all data in the cache and by default closes all plugins.

Definition at line 702 of file 3d_cache.cpp.

703 {
704  std::list< S3D_CACHE_ENTRY* >::iterator sCL = m_CacheList.begin();
705  std::list< S3D_CACHE_ENTRY* >::iterator eCL = m_CacheList.end();
706 
707  while( sCL != eCL )
708  {
709  delete *sCL;
710  ++sCL;
711  }
712 
713  m_CacheList.clear();
714  m_CacheMap.clear();
715 
716  if( closePlugins )
717  ClosePlugins();
718 
719  return;
720 }
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:58
void ClosePlugins(void)
Function ClosePlugins unloads plugins to free memory.
Definition: 3d_cache.cpp:723

References ClosePlugins(), m_CacheList, and m_CacheMap.

Referenced by ~S3D_CACHE().

◆ Get3DConfigDir()

wxString S3D_CACHE::Get3DConfigDir ( bool  createDefault = false)

Function Get3DConfigDir returns the current 3D configuration directory on success, otherwise it returns wxEmptyString.

If the directory was not previously set via Set3DConfigDir() then a default is used which is based on kicad's configuration directory code as of September 2015.

Definition at line 596 of file 3d_cache.cpp.

597 {
598  if( !m_ConfigDir.empty() || !createDefault )
599  return m_ConfigDir;
600 
601  // note: duplicated from common/common.cpp GetKicadConfigPath() to avoid
602  // code coupling; ideally the instantiating code should call
603  // Set3DConfigDir() to set the directory rather than relying on this
604  // directory remaining the same in future KiCad releases.
605  wxFileName cfgpath;
606 
607  // From the wxWidgets wxStandardPaths::GetUserConfigDir() help:
608  // Unix: ~ (the home directory)
609  // Windows: "C:\Documents and Settings\username\Application Data"
610  // Mac: ~/Library/Preferences
611  cfgpath.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
612 
613 #if !defined( __WINDOWS__ ) && !defined( __WXMAC__ )
614  wxString envstr = ExpandEnvVarSubstitutions( "${XDG_CONFIG_HOME}" );
615 
616  if( envstr.IsEmpty() || envstr == "${XDG_CONFIG_HOME}" )
617  {
618  // XDG_CONFIG_HOME is not set, so use the fallback
619  cfgpath.AppendDir( wxT( ".config" ) );
620  }
621  else
622  {
623  // Override the assignment above with XDG_CONFIG_HOME
624  cfgpath.AssignDir( envstr );
625  }
626 #endif
627 
628  cfgpath.AppendDir( wxT( "kicad" ) );
629  cfgpath.AppendDir( wxT( "3d" ) );
630 
631  if( !cfgpath.DirExists() )
632  {
633  cfgpath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
634  }
635 
636  if( !cfgpath.DirExists() )
637  {
638  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * failed to create 3D configuration directory '%s'",
639  __FILE__, __FUNCTION__, __LINE__, cfgpath.GetPath() );
640 
641  return wxT( "" );
642  }
643 
644  if( Set3DConfigDir( cfgpath.GetPath() ) )
645  return m_ConfigDir;
646 
647  return wxEmptyString;
648 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:525
bool Set3DConfigDir(const wxString &aConfigDir)
Function Set3DConfigDir Sets the configuration directory to be used by the model manager for storing ...
Definition: 3d_cache.cpp:512
wxString m_ConfigDir
base configuration path for 3D items
Definition: 3d_cache.h:76

References ExpandEnvVarSubstitutions(), m_ConfigDir, MASK_3D_CACHE, and Set3DConfigDir().

◆ GetFileFilters()

std::list< wxString > const * S3D_CACHE::GetFileFilters ( void  ) const

Function GetFileFilters returns the list of file filters retrieved from the plugins; this will contain at least the default "All Files (*.*)|*.*".

Returns
a pointer to the filter list

Definition at line 696 of file 3d_cache.cpp.

697 {
698  return m_Plugins->GetFileFilters();
699 }
std::list< wxString > const * GetFileFilters(void) const
Function GetFileFilters returns the list of file filters; this will contain at least the default "All...
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References S3D_PLUGIN_MANAGER::GetFileFilters(), and m_Plugins.

◆ GetModel()

S3DMODEL * S3D_CACHE::GetModel ( const wxString &  aModelFileName)

Function GetModel attempts to load the scene data for a model and to translate it into an S3D_MODEL structure for display by a renderer.

Parameters
aModelFileNameis the full path to the model to be loaded
Returns
is a pointer to the render data or NULL if not available

Definition at line 732 of file 3d_cache.cpp.

733 {
734  S3D_CACHE_ENTRY* cp = NULL;
735  SCENEGRAPH* sp = load( aModelFileName, &cp );
736 
737  if( !sp )
738  return NULL;
739 
740  if( !cp )
741  {
742  wxLogTrace( MASK_3D_CACHE,
743  "%s:%s:%d\n * [BUG] model loaded with no associated S3D_CACHE_ENTRY",
744  __FILE__, __FUNCTION__, __LINE__ );
745 
746  return NULL;
747  }
748 
749  if( cp->renderData )
750  return cp->renderData;
751 
752  S3DMODEL* mp = S3D::GetModel( sp );
753  cp->renderData = mp;
754 
755  return mp;
756 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
#define NULL
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:214
S3DMODEL * renderData
Definition: 3d_cache.cpp:144
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
Definition: 3d_cache.cpp:124
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
Definition: ifsg_api.cpp:471

References S3D::GetModel(), load(), MASK_3D_CACHE, NULL, and S3D_CACHE_ENTRY::renderData.

Referenced by C3D_RENDER_OGL_LEGACY::load_3D_models(), C3D_RENDER_RAYTRACING::load_3D_models(), and C3D_MODEL_VIEWER::Set3DModel().

◆ GetModelHash()

wxString S3D_CACHE::GetModelHash ( const wxString &  aModelFileName)

Definition at line 759 of file 3d_cache.cpp.

760 {
761  wxString full3Dpath = m_FNResolver->ResolvePath( aModelFileName );
762 
763  if( full3Dpath.empty() || !wxFileName::FileExists( full3Dpath ) )
764  return wxEmptyString;
765 
766  // check cache if file is already loaded
767  std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
768  mi = m_CacheMap.find( full3Dpath );
769 
770  if( mi != m_CacheMap.end() )
771  return mi->second->GetCacheBaseName();
772 
773  // a cache item does not exist; search the Filename->Cachename map
774  S3D_CACHE_ENTRY* cp = NULL;
775  checkCache( full3Dpath, &cp );
776 
777  if( NULL != cp )
778  return cp->GetCacheBaseName();
779 
780  return wxEmptyString;
781 }
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:181
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
#define NULL
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
Find or create cache entry for file name.
Definition: 3d_cache.cpp:289
wxString ResolvePath(const wxString &aFileName)
Function ResolvePath determines the full path of the given file name.
Definition: 3d_cache.cpp:124

References checkCache(), S3D_CACHE_ENTRY::GetCacheBaseName(), m_CacheMap, m_FNResolver, NULL, and FILENAME_RESOLVER::ResolvePath().

◆ GetProjectDir()

wxString S3D_CACHE::GetProjectDir ( void  )

Function GetProjectDir returns the current project's working directory.

Definition at line 684 of file 3d_cache.cpp.

685 {
686  return m_FNResolver->GetProjectDir();
687 }
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
wxString GetProjectDir(void)

References FILENAME_RESOLVER::GetProjectDir(), and m_FNResolver.

◆ GetResolver()

FILENAME_RESOLVER * S3D_CACHE::GetResolver ( void  )

Definition at line 690 of file 3d_cache.cpp.

691 {
692  return m_FNResolver;
693 }
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64

References m_FNResolver.

Referenced by export_vrml_module().

◆ getSHA1()

bool S3D_CACHE::getSHA1 ( const wxString &  aFileName,
unsigned char *  aSHA1Sum 
)
private

Function getSHA1 calculates the SHA1 hash of the given file.

Parameters
[in]aFileNamefile name (full path)
[out]aSHA1Suma 20 byte character array to hold the SHA1 hash
Return values
truesuccess
falsefailure

Definition at line 361 of file 3d_cache.cpp.

362 {
363  if( aFileName.empty() )
364  {
365  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [BUG] empty filename",
366  __FILE__, __FUNCTION__, __LINE__ );
367 
368  return false;
369  }
370 
371  if( NULL == aSHA1Sum )
372  {
373  wxLogTrace( MASK_3D_CACHE, "%s\n * [BUG] NULL pointer passed for aMD5Sum",
374  __FILE__, __FUNCTION__, __LINE__ );
375 
376  return false;
377  }
378 
379  #ifdef _WIN32
380  FILE* fp = _wfopen( aFileName.wc_str(), L"rb" );
381  #else
382  FILE* fp = fopen( aFileName.ToUTF8(), "rb" );
383  #endif
384 
385  if( NULL == fp )
386  return false;
387 
388  boost::uuids::detail::sha1 dblock;
389  unsigned char block[4096];
390  size_t bsize = 0;
391 
392  while( ( bsize = fread( &block, 1, 4096, fp ) ) > 0 )
393  dblock.process_bytes( block, bsize );
394 
395  fclose( fp );
396  unsigned int digest[5];
397  dblock.get_digest( digest );
398 
399  // ensure MSB order
400  for( int i = 0; i < 5; ++i )
401  {
402  int idx = i << 2;
403  unsigned int tmp = digest[i];
404  aSHA1Sum[idx+3] = tmp & 0xff;
405  tmp >>= 8;
406  aSHA1Sum[idx+2] = tmp & 0xff;
407  tmp >>= 8;
408  aSHA1Sum[idx+1] = tmp & 0xff;
409  tmp >>= 8;
410  aSHA1Sum[idx] = tmp & 0xff;
411  }
412 
413  return true;
414 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
#define NULL

References MASK_3D_CACHE, and NULL.

Referenced by checkCache(), and load().

◆ load()

SCENEGRAPH * S3D_CACHE::load ( const wxString &  aModelFile,
S3D_CACHE_ENTRY **  aCachePtr = NULL 
)
private

Definition at line 214 of file 3d_cache.cpp.

215 {
216  if( aCachePtr )
217  *aCachePtr = NULL;
218 
219  wxString full3Dpath = m_FNResolver->ResolvePath( aModelFile );
220 
221  if( full3Dpath.empty() )
222  {
223  // the model cannot be found; we cannot proceed
224  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [3D model] could not find model '%s'\n",
225  __FILE__, __FUNCTION__, __LINE__, aModelFile );
226  return NULL;
227  }
228 
229  // check cache if file is already loaded
230  std::lock_guard<std::mutex> lock( mutex3D_cache );
231 
232  std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
233  mi = m_CacheMap.find( full3Dpath );
234 
235  if( mi != m_CacheMap.end() )
236  {
237  wxFileName fname( full3Dpath );
238 
239  if( fname.FileExists() ) // Only check if file exists. If not, it will
240  { // use the same model in cache.
241  bool reload = false;
242  wxDateTime fmdate = fname.GetModificationTime();
243 
244  if( fmdate != mi->second->modTime )
245  {
246  unsigned char hashSum[20];
247  getSHA1( full3Dpath, hashSum );
248  mi->second->modTime = fmdate;
249 
250  if( !isSHA1Same( hashSum, mi->second->sha1sum ) )
251  {
252  mi->second->SetSHA1( hashSum );
253  reload = true;
254  }
255  }
256 
257  if( reload )
258  {
259  if( NULL != mi->second->sceneData )
260  {
261  S3D::DestroyNode( mi->second->sceneData );
262  mi->second->sceneData = NULL;
263  }
264 
265  if( NULL != mi->second->renderData )
266  S3D::Destroy3DModel( &mi->second->renderData );
267 
268  mi->second->sceneData = m_Plugins->Load3DModel( full3Dpath, mi->second->pluginInfo );
269  }
270  }
271 
272  if( NULL != aCachePtr )
273  *aCachePtr = mi->second;
274 
275  return mi->second->sceneData;
276  }
277 
278  // a cache item does not exist; search the Filename->Cachename map
279  return checkCache( full3Dpath, aCachePtr );
280 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
static std::mutex mutex3D_cache
Definition: 3d_cache.cpp:65
static bool isSHA1Same(const unsigned char *shaA, const unsigned char *shaB)
Definition: 3d_cache.cpp:69
SGLIB_API void Destroy3DModel(S3DMODEL **aModel)
Function Destroy3DModel frees memory used by an S3DMODEL structure and sets the pointer to the struct...
Definition: ifsg_api.cpp:536
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
SCENEGRAPH * Load3DModel(const wxString &aFileName, std::string &aPluginInfo)
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
#define NULL
bool getSHA1(const wxString &aFileName, unsigned char *aSHA1Sum)
Function getSHA1 calculates the SHA1 hash of the given file.
Definition: 3d_cache.cpp:361
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
Find or create cache entry for file name.
Definition: 3d_cache.cpp:289
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
wxString ResolvePath(const wxString &aFileName)
Function ResolvePath determines the full path of the given file name.
SGLIB_API void DestroyNode(SGNODE *aNode)
Function DestroyNode deletes the given SG* class node.
Definition: ifsg_api.cpp:210
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References checkCache(), S3D::Destroy3DModel(), S3D::DestroyNode(), getSHA1(), isSHA1Same(), S3D_PLUGIN_MANAGER::Load3DModel(), m_CacheMap, m_FNResolver, m_Plugins, MASK_3D_CACHE, mutex3D_cache, NULL, FILENAME_RESOLVER::ResolvePath(), and S3D_CACHE_ENTRY::sceneData.

Referenced by GetModel(), and Load().

◆ Load()

SCENEGRAPH * S3D_CACHE::Load ( const wxString &  aModelFile)

Function Load attempts to load the scene data for a model; it will consult the internal cache list and load from cache if possible before invoking the load() function of the available plugins.

Parameters
aModelFile[in] is the partial or full path to the model to be loaded
Returns
true if the model was successfully loaded, otherwise false. The model may fail to load if, for example, the plugin does not support rendering of the 3D model.

Definition at line 283 of file 3d_cache.cpp.

284 {
285  return load( aModelFile );
286 }
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:214

References load().

Referenced by export_vrml_module().

◆ loadCacheData()

bool S3D_CACHE::loadCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 417 of file 3d_cache.cpp.

418 {
419  wxString bname = aCacheItem->GetCacheBaseName();
420 
421  if( bname.empty() )
422  {
423  wxLogTrace( MASK_3D_CACHE,
424  " * [3D model] cannot load cached model; no file hash available" );
425 
426  return false;
427  }
428 
429  if( m_CacheDir.empty() )
430  {
431  wxLogTrace( MASK_3D_CACHE,
432  " * [3D model] cannot load cached model; config directory unknown" );
433 
434  return false;
435  }
436 
437  wxString fname = m_CacheDir + bname + wxT( ".3dc" );
438 
439  if( !wxFileName::FileExists( fname ) )
440  {
441  wxString errmsg = "cannot open file";
442  wxLogTrace( MASK_3D_CACHE, " * [3D model] %s '%s'", errmsg.GetData(), fname.GetData() );
443  return false;
444  }
445 
446  if( NULL != aCacheItem->sceneData )
447  S3D::DestroyNode( (SGNODE*) aCacheItem->sceneData );
448 
449  aCacheItem->sceneData = (SCENEGRAPH*)S3D::ReadCache( fname.ToUTF8(), m_Plugins, checkTag );
450 
451  if( NULL == aCacheItem->sceneData )
452  return false;
453 
454  return true;
455 }
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.
Definition: ifsg_api.cpp:321
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:181
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
#define NULL
wxString m_CacheDir
3D cache directory
Definition: 3d_cache.h:73
static bool checkTag(const char *aTag, void *aPluginMgrPtr)
Definition: 3d_cache.cpp:79
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
SGLIB_API void DestroyNode(SGNODE *aNode)
Function DestroyNode deletes the given SG* class node.
Definition: ifsg_api.cpp:210
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:67

References checkTag(), S3D::DestroyNode(), S3D_CACHE_ENTRY::GetCacheBaseName(), m_CacheDir, m_Plugins, MASK_3D_CACHE, NULL, S3D::ReadCache(), and S3D_CACHE_ENTRY::sceneData.

Referenced by checkCache().

◆ saveCacheData()

bool S3D_CACHE::saveCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 458 of file 3d_cache.cpp.

459 {
460  if( NULL == aCacheItem )
461  {
462  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * NULL passed for aCacheItem",
463  __FILE__, __FUNCTION__, __LINE__ );
464 
465  return false;
466  }
467 
468  if( NULL == aCacheItem->sceneData )
469  {
470  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * aCacheItem has no valid scene data",
471  __FILE__, __FUNCTION__, __LINE__ );
472 
473  return false;
474  }
475 
476  wxString bname = aCacheItem->GetCacheBaseName();
477 
478  if( bname.empty() )
479  {
480  wxLogTrace( MASK_3D_CACHE,
481  " * [3D model] cannot load cached model; no file hash available" );
482 
483  return false;
484  }
485 
486  if( m_CacheDir.empty() )
487  {
488  wxLogTrace( MASK_3D_CACHE,
489  " * [3D model] cannot load cached model; config directory unknown" );
490 
491  return false;
492  }
493 
494  wxString fname = m_CacheDir + bname + wxT( ".3dc" );
495 
496  if( wxFileName::Exists( fname ) )
497  {
498  if( !wxFileName::FileExists( fname ) )
499  {
500  wxLogTrace( MASK_3D_CACHE, " * [3D model] path exists but is not a regular file '%s'",
501  fname );
502 
503  return false;
504  }
505  }
506 
507  return S3D::WriteCache( fname.ToUTF8(), true, (SGNODE*)aCacheItem->sceneData,
508  aCacheItem->pluginInfo.c_str() );
509 }
std::string pluginInfo
Definition: 3d_cache.cpp:142
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:181
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
#define NULL
wxString m_CacheDir
3D cache directory
Definition: 3d_cache.h:73
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
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.
Definition: ifsg_api.cpp:232

References S3D_CACHE_ENTRY::GetCacheBaseName(), m_CacheDir, MASK_3D_CACHE, NULL, S3D_CACHE_ENTRY::pluginInfo, S3D_CACHE_ENTRY::sceneData, and S3D::WriteCache().

Referenced by checkCache().

◆ Set3DConfigDir()

bool S3D_CACHE::Set3DConfigDir ( const wxString &  aConfigDir)

Function Set3DConfigDir Sets the configuration directory to be used by the model manager for storing 3D model manager configuration data and the model cache.

The config directory may only be set once in the lifetime of the object.

Parameters
aConfigDiris the configuration directory to use for 3D model manager data
Returns
true on success

Definition at line 512 of file 3d_cache.cpp.

513 {
514  if( !m_ConfigDir.empty() )
515  return false;
516 
517  wxFileName cfgdir;
518 
519  if( aConfigDir.StartsWith( "${" ) || aConfigDir.StartsWith( "$(" ) )
520  cfgdir.Assign( ExpandEnvVarSubstitutions( aConfigDir ), "" );
521  else
522  cfgdir.Assign( aConfigDir, "" );
523 
524  cfgdir.Normalize();
525 
526  if( !cfgdir.DirExists() )
527  {
528  cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
529 
530  if( !cfgdir.DirExists() )
531  {
532  wxLogTrace( MASK_3D_CACHE,
533  "%s:%s:%d\n * failed to create 3D configuration directory '%s'",
534  __FILE__, __FUNCTION__, __LINE__, cfgdir.GetPath() );
535 
536  return false;
537  }
538  }
539 
540  m_ConfigDir = cfgdir.GetPath();
541 
542  // inform the file resolver of the config directory
544  {
545  wxLogTrace( MASK_3D_CACHE,
546  "%s:%s:%d\n * could not set 3D Config Directory on filename resolver\n"
547  " * config directory: '%s'",
548  __FILE__, __FUNCTION__, __LINE__, m_ConfigDir );
549  }
550 
551  // 3D cache data must go to a user's cache directory;
552  // unfortunately wxWidgets doesn't seem to provide
553  // functions to retrieve such a directory.
554  //
555  // 1. OSX: ~/Library/Caches/kicad/3d/
556  // 2. Linux: ${XDG_CACHE_HOME}/kicad/3d ~/.cache/kicad/3d/
557  // 3. MSWin: AppData\Local\kicad\3d
558  wxString cacheDir;
559 
560 #if defined(_WIN32)
561  wxStandardPaths::Get().UseAppInfo( wxStandardPaths::AppInfo_None );
562  cacheDir = wxStandardPaths::Get().GetUserLocalDataDir();
563  cacheDir.append( "\\kicad\\3d" );
564 #elif defined(__APPLE)
565  cacheDir = "${HOME}/Library/Caches/kicad/3d";
566 #else // assume Linux
567  cacheDir = ExpandEnvVarSubstitutions( "${XDG_CACHE_HOME}" );
568 
569  if( cacheDir.empty() || cacheDir == "${XDG_CACHE_HOME}" )
570  cacheDir = "${HOME}/.cache";
571 
572  cacheDir.append( "/kicad/3d" );
573 #endif
574 
575  cacheDir = ExpandEnvVarSubstitutions( cacheDir );
576  cfgdir.Assign( cacheDir, "" );
577 
578  if( !cfgdir.DirExists() )
579  {
580  cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
581 
582  if( !cfgdir.DirExists() )
583  {
584  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * failed to create 3D cache directory '%s'",
585  __FILE__, __FUNCTION__, __LINE__, cfgdir.GetPath() );
586 
587  return false;
588  }
589  }
590 
591  m_CacheDir = cfgdir.GetPathWithSep();
592  return true;
593 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:525
wxString m_CacheDir
3D cache directory
Definition: 3d_cache.h:73
wxString m_ConfigDir
base configuration path for 3D items
Definition: 3d_cache.h:76
bool Set3DConfigDir(const wxString &aConfigDir)
Function Set3DConfigDir sets the user's configuration directory for 3D models.

References ExpandEnvVarSubstitutions(), m_CacheDir, m_ConfigDir, m_FNResolver, MASK_3D_CACHE, and FILENAME_RESOLVER::Set3DConfigDir().

Referenced by Get3DConfigDir().

◆ SetProgramBase()

void S3D_CACHE::SetProgramBase ( PGM_BASE aBase)

Function SetProgramBase sets the filename resolver's pointer to the application's PGM_BASE instance; the pointer is used to extract the local env vars.

Definition at line 677 of file 3d_cache.cpp.

678 {
679  m_FNResolver->SetProgramBase( aBase );
680  return;
681 }
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
void SetProgramBase(PGM_BASE *aBase)
Function SetProgramBase sets a pointer to the application's PGM_BASE instance; the pointer is used to...

References m_FNResolver, and FILENAME_RESOLVER::SetProgramBase().

◆ SetProjectDir()

bool S3D_CACHE::SetProjectDir ( const wxString &  aProjDir)

Function SetProjectDir sets the current project's working directory; this affects the model search path.

Definition at line 651 of file 3d_cache.cpp.

652 {
653  bool hasChanged = false;
654 
655  if( m_FNResolver->SetProjectDir( aProjDir, &hasChanged ) && hasChanged )
656  {
657  m_CacheMap.clear();
658 
659  std::list< S3D_CACHE_ENTRY* >::iterator sL = m_CacheList.begin();
660  std::list< S3D_CACHE_ENTRY* >::iterator eL = m_CacheList.end();
661 
662  while( sL != eL )
663  {
664  delete *sL;
665  ++sL;
666  }
667 
668  m_CacheList.clear();
669 
670  return true;
671  }
672 
673  return false;
674 }
bool SetProjectDir(const wxString &aProjDir, bool *flgChanged=NULL)
Function SetProjectDir sets the current KiCad project directory as the first entry in the model path ...
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:64
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:58

References m_CacheList, m_CacheMap, m_FNResolver, and FILENAME_RESOLVER::SetProjectDir().

◆ Type()

KICAD_T S3D_CACHE::Type ( )
inlineoverridevirtual

Implements PROJECT::_ELEM.

Definition at line 118 of file 3d_cache.h.

119  {
120  return S3D_CACHE_T;
121  }

References S3D_CACHE_T.

Member Data Documentation

◆ m_CacheDir

wxString S3D_CACHE::m_CacheDir
private

3D cache directory

Definition at line 73 of file 3d_cache.h.

Referenced by checkCache(), loadCacheData(), saveCacheData(), and Set3DConfigDir().

◆ m_CacheList

std::list< S3D_CACHE_ENTRY* > S3D_CACHE::m_CacheList
private

cache entries

Definition at line 58 of file 3d_cache.h.

Referenced by checkCache(), FlushCache(), and SetProjectDir().

◆ m_CacheMap

std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString > S3D_CACHE::m_CacheMap
private

mapping of file names to cache names and data

Definition at line 61 of file 3d_cache.h.

Referenced by checkCache(), FlushCache(), GetModelHash(), load(), and SetProjectDir().

◆ m_ConfigDir

wxString S3D_CACHE::m_ConfigDir
private

base configuration path for 3D items

Definition at line 76 of file 3d_cache.h.

Referenced by Get3DConfigDir(), and Set3DConfigDir().

◆ m_DirtyCache

bool S3D_CACHE::m_DirtyCache
private

set true if the cache needs to be updated

Definition at line 70 of file 3d_cache.h.

Referenced by S3D_CACHE().

◆ m_FNResolver

FILENAME_RESOLVER* S3D_CACHE::m_FNResolver
private

object to resolve file names

Definition at line 64 of file 3d_cache.h.

Referenced by GetModelHash(), GetProjectDir(), GetResolver(), load(), S3D_CACHE(), Set3DConfigDir(), SetProgramBase(), SetProjectDir(), and ~S3D_CACHE().

◆ m_Plugins

S3D_PLUGIN_MANAGER* S3D_CACHE::m_Plugins
private

plugin manager

Definition at line 67 of file 3d_cache.h.

Referenced by checkCache(), ClosePlugins(), GetFileFilters(), load(), loadCacheData(), S3D_CACHE(), and ~S3D_CACHE().

◆ m_ProjDir

wxString S3D_CACHE::m_ProjDir
private

current KiCad project dir

Definition at line 79 of file 3d_cache.h.


The documentation for this class was generated from the following files: