2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2015-12-11 06:37:42 +00:00
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2016-01-14 02:49:40 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <cstdio>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/utils.h>
|
|
|
|
#include <wx/stdpaths.h>
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
#include <boost/uuid/sha1.hpp>
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtx/transform.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
#include "3d_cache.h"
|
|
|
|
#include "3d_info.h"
|
|
|
|
#include "sg/scenegraph.h"
|
|
|
|
#include "3d_filename_resolver.h"
|
|
|
|
#include "3d_plugin_manager.h"
|
2015-12-09 07:30:48 +00:00
|
|
|
#include "plugins/3dapi/ifsg_api.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define CACHE_CONFIG_NAME wxT( "cache.cfg" )
|
|
|
|
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
static const wxString sha1ToWXString( const unsigned char* aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
unsigned char uc;
|
|
|
|
unsigned char tmp;
|
2016-01-14 02:49:40 +00:00
|
|
|
char sha1[41];
|
2015-12-08 07:31:57 +00:00
|
|
|
int j = 0;
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
for( int i = 0; i < 20; ++i )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
uc = aSHA1Sum[i];
|
2015-12-08 07:31:57 +00:00
|
|
|
tmp = uc / 16;
|
|
|
|
|
|
|
|
if( tmp > 9 )
|
|
|
|
tmp += 87;
|
|
|
|
else
|
|
|
|
tmp += 48;
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
sha1[j++] = tmp;
|
2015-12-08 07:31:57 +00:00
|
|
|
tmp = uc % 16;
|
|
|
|
|
|
|
|
if( tmp > 9 )
|
|
|
|
tmp += 87;
|
|
|
|
else
|
|
|
|
tmp += 48;
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
sha1[j++] = tmp;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
sha1[j] = 0;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
return wxString::FromUTF8Unchecked( sha1 );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
static bool sha1Matches( const unsigned char* sha1a, const unsigned char* sha1b )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
for( int i = 0; i < 20; ++i )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
if( sha1a[i] != sha1b[i] )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
static bool isSHA1null( const unsigned char* aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
if( NULL == aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-01-14 02:49:40 +00:00
|
|
|
std::cerr << " * [BUG] NULL passed for aSHA1Sum\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
for( int i = 0; i < 20; ++i )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
if( 0 != aSHA1Sum[i] )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class S3D_CACHE_ENTRY
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// prohibit assignment and default copy constructor
|
|
|
|
S3D_CACHE_ENTRY( const S3D_CACHE_ENTRY& source );
|
|
|
|
S3D_CACHE_ENTRY& operator=( const S3D_CACHE_ENTRY& source );
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
wxString m_CacheBaseName; // base name of cache file (a SHA1 digest)
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
S3D_CACHE_ENTRY();
|
|
|
|
~S3D_CACHE_ENTRY();
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
void SetSHA1( const unsigned char* aSHA1Sum );
|
2015-12-08 07:31:57 +00:00
|
|
|
const wxString GetCacheBaseName( void );
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
unsigned char sha1sum[20];
|
2015-12-08 07:31:57 +00:00
|
|
|
SCENEGRAPH* sceneData;
|
2015-12-16 09:01:44 +00:00
|
|
|
S3DMODEL* renderData;
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
S3D_CACHE_ENTRY::S3D_CACHE_ENTRY()
|
|
|
|
{
|
|
|
|
sceneData = NULL;
|
2015-12-16 09:01:44 +00:00
|
|
|
renderData = NULL;
|
2016-01-14 02:49:40 +00:00
|
|
|
memset( sha1sum, 0, 20 );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
S3D_CACHE_ENTRY::~S3D_CACHE_ENTRY()
|
|
|
|
{
|
|
|
|
if( NULL != sceneData )
|
|
|
|
delete sceneData;
|
2015-12-16 09:01:44 +00:00
|
|
|
|
|
|
|
if( NULL != renderData )
|
|
|
|
S3D::Destroy3DModel( &renderData );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
void S3D_CACHE_ENTRY::SetSHA1( const unsigned char* aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
if( NULL == aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-01-14 02:49:40 +00:00
|
|
|
std::cerr << " * [BUG] NULL passed for aSHA1Sum\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
memcpy( sha1sum, aSHA1Sum, 20 );
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const wxString S3D_CACHE_ENTRY::GetCacheBaseName( void )
|
|
|
|
{
|
|
|
|
if( m_CacheBaseName.empty() )
|
2016-01-14 02:49:40 +00:00
|
|
|
m_CacheBaseName = sha1ToWXString( sha1sum );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return m_CacheBaseName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
S3D_CACHE::S3D_CACHE()
|
|
|
|
{
|
|
|
|
m_DirtyCache = false;
|
|
|
|
m_FNResolver = new S3D_FILENAME_RESOLVER;
|
|
|
|
m_Plugins = new S3D_PLUGIN_MANAGER;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
S3D_CACHE::~S3D_CACHE()
|
|
|
|
{
|
|
|
|
FlushCache();
|
|
|
|
|
|
|
|
if( m_FNResolver )
|
|
|
|
delete m_FNResolver;
|
|
|
|
|
|
|
|
if( m_Plugins )
|
|
|
|
delete m_Plugins;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
SCENEGRAPH* S3D_CACHE::load( const wxString& aModelFile, S3D_CACHE_ENTRY** aCachePtr )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2015-12-16 09:01:44 +00:00
|
|
|
if( aCachePtr )
|
|
|
|
*aCachePtr = NULL;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
wxString full3Dpath = m_FNResolver->ResolvePath( aModelFile );
|
|
|
|
|
|
|
|
if( full3Dpath.empty() )
|
|
|
|
{
|
|
|
|
// the model cannot be found; we cannot proceed
|
|
|
|
std::cout << " * [3D model] could not find model '";
|
|
|
|
std::cout << aModelFile.ToUTF8() << "'\n";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check cache if file is already loaded
|
|
|
|
std::map< wxString, S3D_CACHE_ENTRY*, S3D::rsort_wxString >::iterator mi;
|
|
|
|
mi = m_CacheMap.find( full3Dpath );
|
|
|
|
|
|
|
|
if( mi != m_CacheMap.end() )
|
2015-12-16 09:01:44 +00:00
|
|
|
{
|
|
|
|
if( NULL != aCachePtr )
|
|
|
|
*aCachePtr = mi->second;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return mi->second->sceneData;
|
2015-12-16 09:01:44 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
// a cache item does not exist; search the Filename->Cachename map
|
2015-12-16 09:01:44 +00:00
|
|
|
return checkCache( full3Dpath, aCachePtr );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
SCENEGRAPH* S3D_CACHE::Load( const wxString& aModelFile )
|
|
|
|
{
|
|
|
|
return load( aModelFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCENEGRAPH* S3D_CACHE::checkCache( const wxString& aFileName, S3D_CACHE_ENTRY** aCachePtr )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2015-12-16 09:01:44 +00:00
|
|
|
if( aCachePtr )
|
|
|
|
*aCachePtr = NULL;
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
unsigned char sha1sum[20];
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
if( !getSHA1( aFileName, sha1sum ) || m_CacheDir.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
// just in case we can't get an hash digest (for example, on access issues)
|
2015-12-08 07:31:57 +00:00
|
|
|
// or we do not have a configured cache file directory, we create an
|
|
|
|
// entry to prevent further attempts at loading the file
|
|
|
|
S3D_CACHE_ENTRY* ep = new S3D_CACHE_ENTRY;
|
|
|
|
m_CacheList.push_back( ep );
|
|
|
|
|
|
|
|
if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* >
|
|
|
|
( aFileName, ep ) ).second == false )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * [BUG] duplicate entry in map file; key = ";
|
|
|
|
std::cerr << aFileName.ToUTF8() << "\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
m_CacheList.pop_back();
|
|
|
|
delete ep;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-16 09:01:44 +00:00
|
|
|
if( aCachePtr )
|
|
|
|
*aCachePtr = ep;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
S3D_CACHE_ENTRY* ep = new S3D_CACHE_ENTRY;
|
|
|
|
m_CacheList.push_back( ep );
|
|
|
|
|
|
|
|
if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* >
|
|
|
|
( aFileName, ep ) ).second == false )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * [BUG] duplicate entry in map file; key = ";
|
|
|
|
std::cerr << aFileName.ToUTF8() << "\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
m_CacheList.pop_back();
|
|
|
|
delete ep;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
if( aCachePtr )
|
|
|
|
*aCachePtr = ep;
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
ep->SetSHA1( sha1sum );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
wxString bname = ep->GetCacheBaseName();
|
|
|
|
wxString cachename = m_CacheDir + bname + wxT( ".3dc" );
|
|
|
|
|
|
|
|
if( wxFileName::FileExists( cachename ) )
|
|
|
|
{
|
|
|
|
loadCacheData( ep );
|
|
|
|
return ep->sceneData;
|
|
|
|
}
|
|
|
|
|
|
|
|
ep->sceneData = m_Plugins->Load3DModel( aFileName );
|
|
|
|
|
2015-12-14 09:15:44 +00:00
|
|
|
if( NULL != ep->sceneData )
|
|
|
|
saveCacheData( ep );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return ep->sceneData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
bool S3D_CACHE::getSHA1( const wxString& aFileName, unsigned char* aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( aFileName.empty() )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * [BUG] empty filename\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
if( NULL == aSHA1Sum )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * [BUG] NULL pointer passed for aMD5Sum\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fp = fopen( aFileName.ToUTF8(), "rb" );
|
2016-01-14 02:49:40 +00:00
|
|
|
boost::uuids::detail::sha1 dblock;
|
|
|
|
unsigned char block[4096];
|
|
|
|
size_t bsize = 0;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
while( ( bsize = fread( &block, 1, 4096, fp ) ) > 0 )
|
|
|
|
dblock.process_bytes( block, bsize );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
fclose( fp );
|
2016-01-14 02:49:40 +00:00
|
|
|
unsigned int digest[5];
|
|
|
|
dblock.get_digest( digest );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-14 02:49:40 +00:00
|
|
|
// ensure MSB order
|
|
|
|
for( int i = 0; i < 5; ++i )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-14 02:49:40 +00:00
|
|
|
int idx = i << 2;
|
|
|
|
unsigned int tmp = digest[i];
|
|
|
|
aSHA1Sum[idx+3] = tmp & 0xff;
|
|
|
|
tmp >>= 8;
|
|
|
|
aSHA1Sum[idx+2] = tmp & 0xff;
|
|
|
|
tmp >>= 8;
|
|
|
|
aSHA1Sum[idx+1] = tmp & 0xff;
|
|
|
|
tmp >>= 8;
|
|
|
|
aSHA1Sum[idx] = tmp & 0xff;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_CACHE::loadCacheData( S3D_CACHE_ENTRY* aCacheItem )
|
|
|
|
{
|
|
|
|
wxString bname = aCacheItem->GetCacheBaseName();
|
|
|
|
|
|
|
|
if( bname.empty() )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << " * [3D model] cannot load cached model; no md5 hash available\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_CacheDir.empty() )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "cannot load cached model; config directory unknown" );
|
|
|
|
std::cerr << " * [3D model] " << errmsg.ToUTF8() << "\n";
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString fname = m_CacheDir + bname + wxT( ".3dc" );
|
|
|
|
|
2015-12-14 09:15:44 +00:00
|
|
|
if( !wxFileName::FileExists( fname ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "cannot open file" );
|
|
|
|
std::cerr << " * [3D model] " << errmsg.ToUTF8() << " '";
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << fname.ToUTF8() << "'\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:15:44 +00:00
|
|
|
if( NULL != aCacheItem->sceneData )
|
|
|
|
S3D::DestroyNode( (SGNODE*) aCacheItem->sceneData );
|
|
|
|
|
|
|
|
aCacheItem->sceneData = (SCENEGRAPH*)S3D::ReadCache( fname );
|
|
|
|
|
|
|
|
if( NULL == aCacheItem->sceneData )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_CACHE::saveCacheData( S3D_CACHE_ENTRY* aCacheItem )
|
|
|
|
{
|
|
|
|
if( NULL == aCacheItem )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * NULL passed for aCacheItem\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( NULL == aCacheItem->sceneData )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * aCacheItem has no valid scene data\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString bname = aCacheItem->GetCacheBaseName();
|
|
|
|
|
|
|
|
if( bname.empty() )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2016-01-14 02:49:40 +00:00
|
|
|
std::cerr << " * [3D model] cannot load cached model; no hash available\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_CacheDir.empty() )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "cannot load cached model; config directory unknown" );
|
|
|
|
std::cerr << " * [3D model] " << errmsg.ToUTF8() << "\n";
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString fname = m_CacheDir + bname + wxT( ".3dc" );
|
|
|
|
|
|
|
|
if( wxFileName::Exists( fname ) )
|
|
|
|
{
|
|
|
|
// determine if the file is a regular file
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
if( stat( fname.ToUTF8(), &info ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !S_ISREG( info.st_mode ) )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "path exists but is not a regular file" );
|
|
|
|
std::cerr << " * [3D model] " << errmsg.ToUTF8() << " '";
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << fname.ToUTF8() << "'\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the file already exists on disk; just exit
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:15:44 +00:00
|
|
|
return S3D::WriteCache( fname, true, (SGNODE*)aCacheItem->sceneData );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_CACHE::Set3DConfigDir( const wxString& aConfigDir )
|
|
|
|
{
|
|
|
|
if( !m_ConfigDir.empty() )
|
|
|
|
return false;
|
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
wxFileName cfgdir( aConfigDir, wxT( "" ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
cfgdir.Normalize();
|
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
if( !cfgdir.DirExists() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2015-12-14 07:41:15 +00:00
|
|
|
cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
if( !cfgdir.DirExists() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "failed to create 3D configuration directory" );
|
|
|
|
std::cerr << " * " << errmsg.ToUTF8() << "\n";
|
|
|
|
errmsg = _( "config directory" );
|
|
|
|
std::cerr << " * " << errmsg.ToUTF8() << " '";
|
2015-12-14 07:41:15 +00:00
|
|
|
std::cerr << cfgdir.GetPath().ToUTF8() << "'\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
return false;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2015-12-14 07:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ConfigDir = cfgdir.GetPath();
|
|
|
|
|
|
|
|
// inform the file resolver of the config directory
|
|
|
|
if( !m_FNResolver->Set3DConfigDir( m_ConfigDir ) )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-14 07:41:15 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * could not set 3D Config Directory on filename resolver\n";
|
|
|
|
std::cerr << " * config directory: '" << m_ConfigDir.ToUTF8() << "'\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
2015-12-14 07:41:15 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
cfgdir.AppendDir( wxT( "cache" ) );
|
|
|
|
|
|
|
|
if( !cfgdir.DirExists() )
|
|
|
|
{
|
|
|
|
cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !cfgdir.DirExists() )
|
|
|
|
{
|
2015-12-14 07:41:15 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "failed to create 3D cache directory" );
|
|
|
|
std::cerr << " * " << errmsg.ToUTF8() << "\n";
|
|
|
|
errmsg = _( "cache directory" );
|
|
|
|
std::cerr << " * " << errmsg.ToUTF8() << " '";
|
2015-12-14 07:41:15 +00:00
|
|
|
std::cerr << cfgdir.GetPath().ToUTF8() << "'\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
return false;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
m_CacheDir = cfgdir.GetPathWithSep();
|
|
|
|
return true;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString S3D_CACHE::Get3DConfigDir( bool createDefault )
|
|
|
|
{
|
|
|
|
if( !m_ConfigDir.empty() || !createDefault )
|
|
|
|
return m_ConfigDir;
|
|
|
|
|
|
|
|
// note: duplicated from common/common.cpp GetKicadConfigPath() to avoid
|
|
|
|
// code coupling; ideally the instantiating code should call
|
|
|
|
// Set3DConfigDir() to set the directory rather than relying on this
|
|
|
|
// directory remaining the same in future KiCad releases.
|
|
|
|
wxFileName cfgpath;
|
|
|
|
|
|
|
|
// From the wxWidgets wxStandardPaths::GetUserConfigDir() help:
|
|
|
|
// Unix: ~ (the home directory)
|
|
|
|
// Windows: "C:\Documents and Settings\username\Application Data"
|
|
|
|
// Mac: ~/Library/Preferences
|
|
|
|
cfgpath.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
|
|
|
|
|
|
|
|
#if !defined( __WINDOWS__ ) && !defined( __WXMAC__ )
|
|
|
|
wxString envstr;
|
|
|
|
|
|
|
|
if( !wxGetEnv( wxT( "XDG_CONFIG_HOME" ), &envstr ) || envstr.IsEmpty() )
|
|
|
|
{
|
|
|
|
// XDG_CONFIG_HOME is not set, so use the fallback
|
|
|
|
cfgpath.AppendDir( wxT( ".config" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Override the assignment above with XDG_CONFIG_HOME
|
|
|
|
cfgpath.AssignDir( envstr );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cfgpath.AppendDir( wxT( "kicad" ) );
|
|
|
|
cfgpath.AppendDir( wxT( "3d" ) );
|
|
|
|
|
|
|
|
if( !cfgpath.DirExists() )
|
|
|
|
{
|
|
|
|
cfgpath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !cfgpath.DirExists() )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "failed to create 3D configuration directory" );
|
2015-12-08 07:31:57 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
std::cerr << " * " << errmsg.ToUTF8() << "\n";
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return wxT( "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Set3DConfigDir( cfgpath.GetPath() ) )
|
|
|
|
return m_ConfigDir;
|
|
|
|
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_CACHE::SetProjectDir( const wxString& aProjDir )
|
|
|
|
{
|
|
|
|
bool hasChanged = false;
|
|
|
|
|
|
|
|
if( m_FNResolver->SetProjectDir( aProjDir, &hasChanged ) && hasChanged )
|
|
|
|
{
|
|
|
|
m_CacheMap.clear();
|
|
|
|
|
|
|
|
std::list< S3D_CACHE_ENTRY* >::iterator sL = m_CacheList.begin();
|
|
|
|
std::list< S3D_CACHE_ENTRY* >::iterator eL = m_CacheList.end();
|
|
|
|
|
|
|
|
while( sL != eL )
|
|
|
|
{
|
|
|
|
delete *sL;
|
|
|
|
++sL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_CacheList.clear();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString S3D_CACHE::GetProjectDir( void )
|
|
|
|
{
|
|
|
|
return m_FNResolver->GetProjectDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
S3D_FILENAME_RESOLVER* S3D_CACHE::GetResolver( void )
|
|
|
|
{
|
|
|
|
return m_FNResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::list< wxString > const* S3D_CACHE::GetFileFilters( void ) const
|
|
|
|
{
|
|
|
|
return m_Plugins->GetFileFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D_CACHE::FlushCache( void )
|
|
|
|
{
|
|
|
|
std::list< S3D_CACHE_ENTRY* >::iterator sCL = m_CacheList.begin();
|
|
|
|
std::list< S3D_CACHE_ENTRY* >::iterator eCL = m_CacheList.end();
|
|
|
|
|
|
|
|
while( sCL != eCL )
|
|
|
|
{
|
|
|
|
delete *sCL;
|
|
|
|
++sCL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_CacheList.clear();
|
|
|
|
ClosePlugins();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D_CACHE::ClosePlugins( void )
|
|
|
|
{
|
|
|
|
if( NULL != m_Plugins )
|
|
|
|
m_Plugins->ClosePlugins();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
|
2015-12-16 22:21:30 +00:00
|
|
|
S3DMODEL* S3D_CACHE::GetModel( const wxString& aModelFileName )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2015-12-16 09:01:44 +00:00
|
|
|
S3D_CACHE_ENTRY* cp = NULL;
|
|
|
|
SCENEGRAPH* sp = load( aModelFileName, &cp );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !sp )
|
|
|
|
return NULL;
|
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
if( !cp )
|
|
|
|
{
|
2016-01-12 06:09:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-16 09:01:44 +00:00
|
|
|
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
std::cerr << " * [BUG] model loaded with no associated S3D_CACHE_ENTRY\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
if( cp->renderData )
|
|
|
|
return cp->renderData;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2015-12-16 22:21:30 +00:00
|
|
|
S3DMODEL* mp = S3D::GetModel( sp );
|
2015-12-16 09:01:44 +00:00
|
|
|
cp->renderData = mp;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2015-12-16 09:01:44 +00:00
|
|
|
return mp;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
wxString S3D_CACHE::GetModelHash( const wxString& aModelFileName )
|
|
|
|
{
|
|
|
|
wxString full3Dpath = m_FNResolver->ResolvePath( aModelFileName );
|
|
|
|
|
|
|
|
if( full3Dpath.empty() || !wxFileName::FileExists( full3Dpath ) )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
// check cache if file is already loaded
|
|
|
|
std::map< wxString, S3D_CACHE_ENTRY*, S3D::rsort_wxString >::iterator mi;
|
|
|
|
mi = m_CacheMap.find( full3Dpath );
|
|
|
|
|
|
|
|
if( mi != m_CacheMap.end() )
|
|
|
|
return mi->second->GetCacheBaseName();
|
|
|
|
|
|
|
|
// a cache item does not exist; search the Filename->Cachename map
|
|
|
|
S3D_CACHE_ENTRY* cp = NULL;
|
|
|
|
checkCache( full3Dpath, &cp );
|
|
|
|
|
|
|
|
if( NULL != cp )
|
|
|
|
return cp->GetCacheBaseName();
|
|
|
|
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|