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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2016-02-23 06:46:22 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2015-12-09 07:30:48 +00:00
|
|
|
#include "3d_cache/sg/sg_coords.h"
|
|
|
|
#include "3d_cache/sg/sg_helpers.h"
|
2015-12-15 07:53:03 +00:00
|
|
|
#include "3d_cache/sg/sg_normals.h"
|
|
|
|
#include "3d_cache/sg/sg_faceset.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
SGCOORDS::SGCOORDS( SGNODE* aParent ) : SGNODE( aParent )
|
|
|
|
{
|
|
|
|
m_SGtype = S3D::SGTYPE_COORDS;
|
|
|
|
|
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;
|
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG,
|
|
|
|
wxT( "%s:%s:%d * [BUG] inappropriate parent to SGCOORDS (type %s)" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aParent->GetNodeType() );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2020-12-16 18:19:28 +00:00
|
|
|
else if( nullptr != aParent && S3D::SGTYPE_FACESET == aParent->GetNodeType() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGCOORDS::~SGCOORDS()
|
|
|
|
{
|
|
|
|
coords.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGCOORDS::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 SGCOORDS
|
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* SGCOORDS::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 SGCOORDS::unlinkChildNode( const SGNODE* aCaller ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aCaller, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void SGCOORDS::unlinkRefNode( const SGNODE* aCaller ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aCaller, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
bool SGCOORDS::AddRefNode( SGNODE* aNode ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, 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 SGCOORDS::AddChildNode( SGNODE* aNode ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, false );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SGCOORDS::GetCoordsList( size_t& aListSize, SGPOINT*& aCoordsList )
|
|
|
|
{
|
|
|
|
if( coords.empty() )
|
|
|
|
{
|
|
|
|
aListSize = 0;
|
2020-12-16 18:19:28 +00:00
|
|
|
aCoordsList = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aListSize = coords.size();
|
|
|
|
aCoordsList = &coords[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGCOORDS::SetCoordsList( size_t aListSize, const SGPOINT* aCoordsList )
|
|
|
|
{
|
|
|
|
coords.clear();
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( 0 == aListSize || nullptr == aCoordsList )
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < aListSize; ++i )
|
|
|
|
coords.push_back( aCoordsList[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGCOORDS::AddCoord( double aXValue, double aYValue, double aZValue )
|
|
|
|
{
|
2019-12-05 15:41:21 +00:00
|
|
|
coords.emplace_back( aXValue, aYValue, aZValue );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGCOORDS::AddCoord( const SGPOINT& aPoint )
|
|
|
|
{
|
|
|
|
coords.push_back( aPoint );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGCOORDS::ReNameNodes( void )
|
|
|
|
{
|
|
|
|
m_written = false;
|
|
|
|
|
|
|
|
// rename this node
|
|
|
|
m_Name.clear();
|
|
|
|
GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGCOORDS::WriteVRML( std::ostream& aFile, bool aReuseFlag )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( coords.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aReuseFlag )
|
|
|
|
{
|
|
|
|
if( !m_written )
|
|
|
|
{
|
|
|
|
aFile << " coord DEF " << GetName() << " Coordinate { point [\n ";
|
|
|
|
m_written = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aFile << " coord USE " << GetName() << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aFile << " coord Coordinate { point [\n ";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string tmp;
|
|
|
|
size_t n = coords.size();
|
|
|
|
bool nline = false;
|
2016-01-17 21:35:29 +00:00
|
|
|
SGPOINT pt;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
for( size_t i = 0; i < n; )
|
|
|
|
{
|
2016-01-17 21:35:29 +00:00
|
|
|
// ensure VRML output has 1U = 0.1 inch as per legacy kicad expectations
|
2016-01-17 23:54:24 +00:00
|
|
|
pt = coords[i];
|
2016-01-17 21:35:29 +00:00
|
|
|
pt.x /= 2.54;
|
2016-01-17 23:54:24 +00:00
|
|
|
pt.y /= 2.54;
|
|
|
|
pt.z /= 2.54;
|
2016-01-17 21:35:29 +00:00
|
|
|
S3D::FormatPoint( tmp, pt );
|
2015-12-08 07:31:57 +00:00
|
|
|
aFile << tmp ;
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if( i < n )
|
|
|
|
{
|
|
|
|
aFile << ",";
|
|
|
|
|
|
|
|
if( nline )
|
|
|
|
{
|
|
|
|
aFile << "\n ";
|
|
|
|
nline = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nline = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aFile << "] }\n";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
bool SGCOORDS::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" ),
|
|
|
|
__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 = coords.size();
|
|
|
|
aFile.write( (char*)&npts, sizeof(size_t) );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < npts; ++i )
|
|
|
|
S3D::WritePoint( aFile, coords[i] );
|
|
|
|
|
|
|
|
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 SGCOORDS::ReadCache( std::istream& aFile, SGNODE* parentNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( coords.empty(), false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
size_t npts;
|
2020-12-16 18:19:28 +00:00
|
|
|
aFile.read( (char*) &npts, sizeof( size_t ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
SGPOINT tmp;
|
|
|
|
|
|
|
|
if( aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < npts; ++i )
|
|
|
|
{
|
|
|
|
if( !S3D::ReadPoint( aFile, tmp ) || aFile.fail() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
coords.push_back( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-15 07:53:03 +00:00
|
|
|
|
|
|
|
|
2015-12-17 02:55:35 +00:00
|
|
|
bool SGCOORDS::CalcNormals( SGFACESET* callingNode, SGNODE** aPtr )
|
2015-12-15 07:53:03 +00:00
|
|
|
{
|
2015-12-16 03:25:46 +00:00
|
|
|
if( aPtr )
|
2020-12-16 18:19:28 +00:00
|
|
|
*aPtr = nullptr;
|
2015-12-16 03:25:46 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == m_Parent || nullptr == callingNode )
|
2015-12-15 07:53:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// the parent and all references must have indices; collect all
|
|
|
|
// indices into one std::vector<>
|
|
|
|
std::vector< int > ilist;
|
2020-12-16 18:19:28 +00:00
|
|
|
SGNORMALS* np = nullptr;
|
2015-12-15 07:53:03 +00:00
|
|
|
|
2015-12-17 02:55:35 +00:00
|
|
|
if( callingNode == m_Parent )
|
2015-12-15 07:53:03 +00:00
|
|
|
{
|
2015-12-17 02:55:35 +00:00
|
|
|
((SGFACESET*)m_Parent)->GatherCoordIndices( ilist );
|
|
|
|
|
|
|
|
std::list< SGNODE* >::iterator sB = m_BackPointers.begin();
|
|
|
|
std::list< SGNODE* >::iterator eB = m_BackPointers.end();
|
|
|
|
|
|
|
|
while( sB != eB )
|
|
|
|
{
|
|
|
|
SGFACESET* fp = (SGFACESET*)(*sB);
|
|
|
|
fp->GatherCoordIndices( ilist );
|
|
|
|
++sB;
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
np = ( (SGFACESET*) m_Parent )->m_Normals;
|
2015-12-17 02:55:35 +00:00
|
|
|
|
|
|
|
if( !np )
|
|
|
|
np = new SGNORMALS( m_Parent );
|
|
|
|
|
2015-12-15 07:53:03 +00:00
|
|
|
}
|
2015-12-17 02:55:35 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
callingNode->GatherCoordIndices( ilist );
|
|
|
|
np = callingNode->m_Normals;
|
2015-12-15 07:53:03 +00:00
|
|
|
|
2015-12-17 02:55:35 +00:00
|
|
|
if( !np )
|
|
|
|
np = new SGNORMALS( callingNode );
|
2015-12-15 07:53:03 +00:00
|
|
|
|
2015-12-17 02:55:35 +00:00
|
|
|
}
|
2015-12-15 07:53:03 +00:00
|
|
|
|
|
|
|
if( S3D::CalcTriangleNormals( coords, ilist, np->norms ) )
|
2015-12-16 03:25:46 +00:00
|
|
|
{
|
|
|
|
if( aPtr )
|
|
|
|
*aPtr = np;
|
|
|
|
|
2015-12-15 07:53:03 +00:00
|
|
|
return true;
|
2015-12-16 03:25:46 +00:00
|
|
|
}
|
2015-12-15 07:53:03 +00:00
|
|
|
|
|
|
|
delete np;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|