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 189 of file 3d_cache.cpp.

190 {
191  m_DirtyCache = false;
194 
195  return;
196 }
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 199 of file 3d_cache.cpp.

200 {
201  FlushCache();
202 
203  if( m_FNResolver )
204  delete m_FNResolver;
205 
206  if( m_Plugins )
207  delete m_Plugins;
208 
209  return;
210 }
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:701
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 288 of file 3d_cache.cpp.

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

723 {
724  if( NULL != m_Plugins )
726 
727  return;
728 }
#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 701 of file 3d_cache.cpp.

702 {
703  std::list< S3D_CACHE_ENTRY* >::iterator sCL = m_CacheList.begin();
704  std::list< S3D_CACHE_ENTRY* >::iterator eCL = m_CacheList.end();
705 
706  while( sCL != eCL )
707  {
708  delete *sCL;
709  ++sCL;
710  }
711 
712  m_CacheList.clear();
713  m_CacheMap.clear();
714 
715  if( closePlugins )
716  ClosePlugins();
717 
718  return;
719 }
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:722

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 595 of file 3d_cache.cpp.

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

696 {
697  return m_Plugins->GetFileFilters();
698 }
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 731 of file 3d_cache.cpp.

732 {
733  S3D_CACHE_ENTRY* cp = NULL;
734  SCENEGRAPH* sp = load( aModelFileName, &cp );
735 
736  if( !sp )
737  return NULL;
738 
739  if( !cp )
740  {
741  wxLogTrace( MASK_3D_CACHE,
742  "%s:%s:%d\n * [BUG] model loaded with no associated S3D_CACHE_ENTRY",
743  __FILE__, __FUNCTION__, __LINE__ );
744 
745  return NULL;
746  }
747 
748  if( cp->renderData )
749  return cp->renderData;
750 
751  S3DMODEL* mp = S3D::GetModel( sp );
752  cp->renderData = mp;
753 
754  return mp;
755 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:62
#define NULL
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:213
S3DMODEL * renderData
Definition: 3d_cache.cpp:143
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
Definition: 3d_cache.cpp:123
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 758 of file 3d_cache.cpp.

759 {
760  wxString full3Dpath = m_FNResolver->ResolvePath( aModelFileName );
761 
762  if( full3Dpath.empty() || !wxFileName::FileExists( full3Dpath ) )
763  return wxEmptyString;
764 
765  // check cache if file is already loaded
766  std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
767  mi = m_CacheMap.find( full3Dpath );
768 
769  if( mi != m_CacheMap.end() )
770  return mi->second->GetCacheBaseName();
771 
772  // a cache item does not exist; search the Filename->Cachename map
773  S3D_CACHE_ENTRY* cp = NULL;
774  checkCache( full3Dpath, &cp );
775 
776  if( NULL != cp )
777  return cp->GetCacheBaseName();
778 
779  return wxEmptyString;
780 }
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:180
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:288
wxString ResolvePath(const wxString &aFileName)
Function ResolvePath determines the full path of the given file name.
Definition: 3d_cache.cpp:123

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 683 of file 3d_cache.cpp.

684 {
685  return m_FNResolver->GetProjectDir();
686 }
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 689 of file 3d_cache.cpp.

690 {
691  return m_FNResolver;
692 }
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 360 of file 3d_cache.cpp.

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

214 {
215  if( aCachePtr )
216  *aCachePtr = NULL;
217 
218  wxString full3Dpath = m_FNResolver->ResolvePath( aModelFile );
219 
220  if( full3Dpath.empty() )
221  {
222  // the model cannot be found; we cannot proceed
223  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [3D model] could not find model '%s'\n",
224  __FILE__, __FUNCTION__, __LINE__, aModelFile );
225  return NULL;
226  }
227 
228  // check cache if file is already loaded
229  std::lock_guard<std::mutex> lock( mutex3D_cache );
230 
231  std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
232  mi = m_CacheMap.find( full3Dpath );
233 
234  if( mi != m_CacheMap.end() )
235  {
236  wxFileName fname( full3Dpath );
237 
238  if( fname.FileExists() ) // Only check if file exists. If not, it will
239  { // use the same model in cache.
240  bool reload = false;
241  wxDateTime fmdate = fname.GetModificationTime();
242 
243  if( fmdate != mi->second->modTime )
244  {
245  unsigned char hashSum[20];
246  getSHA1( full3Dpath, hashSum );
247  mi->second->modTime = fmdate;
248 
249  if( !isSHA1Same( hashSum, mi->second->sha1sum ) )
250  {
251  mi->second->SetSHA1( hashSum );
252  reload = true;
253  }
254  }
255 
256  if( reload )
257  {
258  if( NULL != mi->second->sceneData )
259  {
260  S3D::DestroyNode( mi->second->sceneData );
261  mi->second->sceneData = NULL;
262  }
263 
264  if( NULL != mi->second->renderData )
265  S3D::Destroy3DModel( &mi->second->renderData );
266 
267  mi->second->sceneData = m_Plugins->Load3DModel( full3Dpath, mi->second->pluginInfo );
268  }
269  }
270 
271  if( NULL != aCachePtr )
272  *aCachePtr = mi->second;
273 
274  return mi->second->sceneData;
275  }
276 
277  // a cache item does not exist; search the Filename->Cachename map
278  return checkCache( full3Dpath, aCachePtr );
279 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:62
static std::mutex mutex3D_cache
Definition: 3d_cache.cpp:64
static bool isSHA1Same(const unsigned char *shaA, const unsigned char *shaB)
Definition: 3d_cache.cpp:68
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:360
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
Find or create cache entry for file name.
Definition: 3d_cache.cpp:288
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:142
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 282 of file 3d_cache.cpp.

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

References load().

Referenced by export_vrml_module().

◆ loadCacheData()

bool S3D_CACHE::loadCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 416 of file 3d_cache.cpp.

417 {
418  wxString bname = aCacheItem->GetCacheBaseName();
419 
420  if( bname.empty() )
421  {
422  wxLogTrace( MASK_3D_CACHE,
423  " * [3D model] cannot load cached model; no file hash available" );
424 
425  return false;
426  }
427 
428  if( m_CacheDir.empty() )
429  {
430  wxLogTrace( MASK_3D_CACHE,
431  " * [3D model] cannot load cached model; config directory unknown" );
432 
433  return false;
434  }
435 
436  wxString fname = m_CacheDir + bname + wxT( ".3dc" );
437 
438  if( !wxFileName::FileExists( fname ) )
439  {
440  wxString errmsg = "cannot open file";
441  wxLogTrace( MASK_3D_CACHE, " * [3D model] %s '%s'", errmsg.GetData(), fname.GetData() );
442  return false;
443  }
444 
445  if( NULL != aCacheItem->sceneData )
446  S3D::DestroyNode( (SGNODE*) aCacheItem->sceneData );
447 
448  aCacheItem->sceneData = (SCENEGRAPH*)S3D::ReadCache( fname.ToUTF8(), m_Plugins, checkTag );
449 
450  if( NULL == aCacheItem->sceneData )
451  return false;
452 
453  return true;
454 }
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:62
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:180
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:78
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:142
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 457 of file 3d_cache.cpp.

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

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

677 {
678  m_FNResolver->SetProgramBase( aBase );
679  return;
680 }
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 650 of file 3d_cache.cpp.

651 {
652  bool hasChanged = false;
653 
654  if( m_FNResolver->SetProjectDir( aProjDir, &hasChanged ) && hasChanged )
655  {
656  m_CacheMap.clear();
657 
658  std::list< S3D_CACHE_ENTRY* >::iterator sL = m_CacheList.begin();
659  std::list< S3D_CACHE_ENTRY* >::iterator eL = m_CacheList.end();
660 
661  while( sL != eL )
662  {
663  delete *sL;
664  ++sL;
665  }
666 
667  m_CacheList.clear();
668 
669  return true;
670  }
671 
672  return false;
673 }
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: