2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-03-02 22:53:49 +00:00
|
|
|
* Copyright (C) 2015-2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2020-12-16 18:19:28 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-02-23 06:46:22 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2015-12-09 07:30:48 +00:00
|
|
|
#include "3d_cache/sg/sg_index.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
SGINDEX::SGINDEX( SGNODE* aParent ) : SGNODE( aParent )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Parent = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] inappropriate parent to SGINDEX (type "
|
|
|
|
"'%d')" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
aParent->GetNodeType() );
|
2020-12-16 18:19:28 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGINDEX::~SGINDEX()
|
|
|
|
{
|
|
|
|
index.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGINDEX::SetParent( SGNODE* aParent, bool notify )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != m_Parent )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( aParent == m_Parent )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// handle the change in parents
|
|
|
|
if( notify )
|
|
|
|
m_Parent->unlinkChildNode( this );
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Parent = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aParent )
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only a SGFACESET may be parent to a SGINDEX and derived types
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Parent = aParent;
|
|
|
|
|
|
|
|
if( m_Parent )
|
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
SGNODE* SGINDEX::FindNode(const char *aNodeName, const SGNODE *aCaller) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNodeName || 0 == aNodeName[0] )
|
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !m_Name.compare( aNodeName ) )
|
|
|
|
return this;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void SGINDEX::unlinkChildNode( const SGNODE* aCaller ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
// Node should have no children or refs.
|
|
|
|
wxCHECK( false, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void SGINDEX::unlinkRefNode( const SGNODE* aCaller ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
// Node should have no children or refs.
|
|
|
|
wxCHECK( false, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
bool SGINDEX::AddRefNode( SGNODE* aNode ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
// Node should have no children or refs.
|
|
|
|
wxCHECK( false, false );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
bool SGINDEX::AddChildNode( SGNODE* aNode ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
// Node should have no children or refs.
|
|
|
|
wxCHECK( false, false );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGINDEX::GetIndices( size_t& nIndices, int*& aIndexList )
|
|
|
|
{
|
|
|
|
if( index.empty() )
|
|
|
|
{
|
|
|
|
nIndices = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
aIndexList = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nIndices = index.size();
|
|
|
|
aIndexList = & index[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGINDEX::SetIndices( size_t nIndices, int* aIndexList )
|
|
|
|
{
|
|
|
|
index.clear();
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( 0 == nIndices || nullptr == aIndexList )
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < nIndices; ++i )
|
|
|
|
index.push_back( aIndexList[i] );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGINDEX::AddIndex( int aIndex )
|
|
|
|
{
|
|
|
|
index.push_back( aIndex );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGINDEX::ReNameNodes( void )
|
|
|
|
{
|
|
|
|
m_written = false;
|
|
|
|
|
|
|
|
// rename this node
|
|
|
|
m_Name.clear();
|
|
|
|
GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGINDEX::WriteVRML( std::ostream& aFile, bool aReuseFlag )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( index.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( S3D::SGTYPE_COORDINDEX == m_SGtype )
|
|
|
|
return writeCoordIndex( aFile );
|
|
|
|
|
|
|
|
return writeColorIndex( aFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGINDEX::writeCoordIndex( std::ostream& aFile )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
size_t n = index.size();
|
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxCHECK_MSG( n % 3 == 0, false, wxT( "Coordinate index is not divisible by three (violates "
|
|
|
|
"triangle constraint)" ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
aFile << " coordIndex [\n ";
|
|
|
|
|
|
|
|
// indices to control formatting
|
|
|
|
int nv0 = 0;
|
|
|
|
int nv1 = 0;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < n; )
|
|
|
|
{
|
|
|
|
aFile << index[i];
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if( ++nv0 == 3 )
|
|
|
|
{
|
|
|
|
aFile << ",-1";
|
|
|
|
++nv1;
|
|
|
|
nv0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i < n )
|
|
|
|
{
|
|
|
|
aFile << ",";
|
|
|
|
|
|
|
|
if( nv1 == 8 )
|
|
|
|
{
|
|
|
|
nv1 = 0;
|
|
|
|
aFile << "\n ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aFile << "]\n";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGINDEX::writeColorIndex( std::ostream& aFile )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
aFile << " colorIndex [\n ";
|
|
|
|
return writeIndexList( aFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGINDEX::writeIndexList( std::ostream& aFile )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
// index to control formatting
|
|
|
|
int nv = 0;
|
|
|
|
size_t n = index.size();
|
|
|
|
|
|
|
|
for( size_t i = 0; i < n; )
|
|
|
|
{
|
|
|
|
aFile << index[i];
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if( i < n )
|
|
|
|
{
|
|
|
|
aFile << ",";
|
|
|
|
|
|
|
|
if( ++nv == 20 )
|
|
|
|
{
|
|
|
|
aFile << "\n ";
|
|
|
|
nv = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aFile << "]\n";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGINDEX::WriteCache( std::ostream& aFile, SGNODE* parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( m_Parent, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
SGNODE* np = m_Parent;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
while( nullptr != np->GetParent() )
|
2015-12-08 07:31:57 +00:00
|
|
|
np = np->GetParent();
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( np->WriteCache( aFile, nullptr ) )
|
2016-03-14 01:30:59 +00:00
|
|
|
{
|
|
|
|
m_written = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( parentNode == m_Parent, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !aFile.good() )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] bad stream" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aFile << "[" << GetName() << "]";
|
|
|
|
size_t npts = index.size();
|
|
|
|
aFile.write( (char*)&npts, sizeof(size_t) );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < npts; ++i )
|
|
|
|
aFile.write( (char*)&index[i], sizeof(int) );
|
|
|
|
|
|
|
|
if( aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
2016-03-14 01:30:59 +00:00
|
|
|
m_written = true;
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGINDEX::ReadCache( std::istream& aFile, SGNODE* parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( index.empty(), false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
size_t npts;
|
|
|
|
aFile.read( (char*)&npts, sizeof(size_t) );
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if( aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < npts; ++i )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
aFile.read( (char*) &tmp, sizeof( int ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
index.push_back( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|