2016-02-05 04:32:52 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2022-01-12 23:55:18 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-02-05 04:32:52 +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 00:20:34 +00:00
|
|
|
#include <sstream>
|
2016-02-05 04:32:52 +00:00
|
|
|
#include <cmath>
|
2016-02-23 00:20:34 +00:00
|
|
|
#include <wx/log.h>
|
2016-02-05 04:32:52 +00:00
|
|
|
#include <wx/xml/xml.h>
|
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#include "x3d_ops.h"
|
|
|
|
#include "x3d_ifaceset.h"
|
2016-02-05 05:08:30 +00:00
|
|
|
#include "x3d_coords.h"
|
|
|
|
#include "plugins/3dapi/ifsg_all.h"
|
|
|
|
#include "wrlfacet.h"
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
X3DIFACESET::X3DIFACESET() : X3DNODE()
|
|
|
|
{
|
|
|
|
m_Type = X3D_INDEXED_FACE_SET;
|
2021-07-18 14:06:48 +00:00
|
|
|
coord = nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
X3DIFACESET::X3DIFACESET( X3DNODE* aParent ) : X3DNODE()
|
|
|
|
{
|
|
|
|
m_Type = X3D_INDEXED_FACE_SET;
|
2021-07-18 14:06:48 +00:00
|
|
|
coord = nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
init();
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != aParent )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3DNODES ptype = aParent->GetNodeType();
|
|
|
|
|
|
|
|
if( X3D_SHAPE == ptype )
|
|
|
|
m_Parent = aParent;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != m_Parent )
|
2016-02-05 04:32:52 +00:00
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
X3DIFACESET::~X3DIFACESET()
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying IndexedFaceSet." ) );
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void X3DIFACESET::init()
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
coord = nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
ccw = true;
|
2019-12-12 21:51:21 +00:00
|
|
|
creaseAngle = 0.733f; // approx 42 degrees; this is larger than VRML spec.
|
|
|
|
creaseLimit = 0.74317f; // cos( 0.733 )
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void X3DIFACESET::readFields( wxXmlNode* aNode )
|
|
|
|
{
|
|
|
|
// DEF
|
|
|
|
// ccw
|
|
|
|
// creaseAngle
|
|
|
|
// coordIndex
|
|
|
|
|
|
|
|
wxXmlAttribute* prop;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
2017-09-22 17:11:11 +00:00
|
|
|
const wxString& pname = prop->GetName();
|
2016-02-05 04:32:52 +00:00
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
if( pname == wxT( "DEF" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
m_Name = prop->GetValue();
|
|
|
|
m_Dict->AddName( m_Name, this );
|
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "ccw" ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
{
|
2016-02-05 04:32:52 +00:00
|
|
|
X3D::ParseSFBool( prop->GetValue(), ccw );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "creaseAngle" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3D::ParseSFFloat( prop->GetValue(), creaseAngle );
|
|
|
|
|
|
|
|
if( creaseAngle < 0.0f )
|
|
|
|
creaseAngle = 0.0f;
|
2019-12-12 21:51:21 +00:00
|
|
|
else if( creaseAngle > static_cast<float>( M_PI * 0.34f ) )
|
|
|
|
creaseAngle = static_cast<float>( M_PI / 3.0f );
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
creaseLimit = cosf( creaseAngle );
|
|
|
|
}
|
2022-02-05 13:25:43 +00:00
|
|
|
else if( pname == wxT( "coordIndex" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
wxStringTokenizer indices( prop->GetValue() );
|
|
|
|
|
|
|
|
while( indices.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
long index = 0;
|
|
|
|
indices.GetNextToken().ToLong( &index );
|
|
|
|
coordIndex.push_back( (int) index );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DIFACESET::Read( wxXmlNode* aNode, X3DNODE* aTopNode, X3D_DICT& aDict )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == aTopNode || nullptr == aNode )
|
2016-02-05 04:32:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Dict = &aDict;
|
|
|
|
readFields( aNode );
|
|
|
|
bool ok = false;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( wxXmlNode* child = aNode->GetChildren(); child != nullptr; child = child->GetNext() )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
if( child->GetName() == wxT( "Coordinate" ) )
|
2016-02-05 04:32:52 +00:00
|
|
|
ok = X3D::ReadCoordinates( child, this, aDict );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( false == ok )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !SetParent( aTopNode ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DIFACESET::SetParent( X3DNODE* aParent, bool doUnlink )
|
|
|
|
{
|
|
|
|
if( aParent == m_Parent )
|
|
|
|
return true;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != aParent )
|
2016-02-05 04:32:52 +00:00
|
|
|
{
|
|
|
|
X3DNODES nt = aParent->GetNodeType();
|
|
|
|
|
|
|
|
if( nt != X3D_SHAPE )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != m_Parent && doUnlink )
|
2016-02-05 04:32:52 +00:00
|
|
|
m_Parent->unlinkChildNode( this );
|
|
|
|
|
|
|
|
m_Parent = aParent;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != m_Parent )
|
2016-02-05 04:32:52 +00:00
|
|
|
m_Parent->AddChildNode( this );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DIFACESET::AddChildNode( X3DNODE* aNode )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == aNode )
|
2016-02-05 05:08:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aNode->GetNodeType() != X3D_COORDINATE )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aNode == coord )
|
|
|
|
return true;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != coord )
|
2016-02-05 05:08:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Children.push_back( aNode );
|
|
|
|
coord = aNode;
|
|
|
|
|
|
|
|
if( aNode->GetParent() != this )
|
|
|
|
aNode->SetParent( this );
|
|
|
|
|
|
|
|
return true;
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool X3DIFACESET::AddRefNode( X3DNODE* aNode )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == aNode )
|
2016-02-05 05:08:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aNode->GetNodeType() != X3D_COORDINATE )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aNode == coord )
|
|
|
|
return true;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != coord )
|
2016-02-05 05:08:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Refs.push_back( aNode );
|
2016-02-05 22:38:03 +00:00
|
|
|
aNode->addNodeRef( this );
|
2016-02-05 05:08:30 +00:00
|
|
|
coord = aNode;
|
|
|
|
return true;
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGNODE* X3DIFACESET::TranslateToSG( SGNODE* aParent )
|
|
|
|
{
|
2016-02-05 05:08:30 +00:00
|
|
|
S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
|
|
|
|
|
2022-01-12 23:55:18 +00:00
|
|
|
if( nullptr != aParent && ptype != S3D::SGTYPE_SHAPE )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceVrmlPlugin,
|
|
|
|
wxT( " * [BUG] IndexedFaceSet does not have a valid Shape parent "
|
|
|
|
"(parent ID: %d)" ), ptype );
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-05 05:08:30 +00:00
|
|
|
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin,
|
2021-07-28 01:06:24 +00:00
|
|
|
wxT( " * [INFO] Translating IndexedFaceSet with %zu children, %zu references, "
|
|
|
|
"%zu back pointers, and %zu coordinate indices." ),
|
2021-07-24 17:27:35 +00:00
|
|
|
m_Children.size(), m_Refs.size(), m_BackPointers.size(), coordIndex.size() );
|
2016-02-05 05:08:30 +00:00
|
|
|
|
|
|
|
if( m_sgNode )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != aParent )
|
2016-02-05 05:08:30 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( m_sgNode )
|
2016-02-05 05:08:30 +00:00
|
|
|
&& !S3D::AddSGNodeChild( aParent, m_sgNode ) )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
}
|
|
|
|
else if( aParent != S3D::GetSGNodeParent( m_sgNode )
|
|
|
|
&& !S3D::AddSGNodeRef( aParent, m_sgNode ) )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_sgNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t vsize = coordIndex.size();
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == coord || vsize < 3 )
|
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
|
|
|
|
WRLVEC3F* pcoords;
|
|
|
|
size_t coordsize;
|
|
|
|
((X3DCOORDS*) coord)->GetCoords( pcoords, coordsize );
|
|
|
|
|
|
|
|
if( coordsize < 3 )
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
|
|
|
|
// check that all indices are valid
|
|
|
|
for( size_t idx = 0; idx < vsize; ++idx )
|
|
|
|
{
|
|
|
|
if( coordIndex[idx] < 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( coordIndex[idx] >= (int)coordsize )
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SHAPE lShape;
|
2021-07-18 14:06:48 +00:00
|
|
|
FACET* fp = nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
size_t iCoord;
|
|
|
|
int idx; // coordinate index
|
|
|
|
|
|
|
|
// no per-vertex colors; we can save a few CPU cycles
|
|
|
|
for( iCoord = 0; iCoord < vsize; ++iCoord )
|
|
|
|
{
|
|
|
|
idx = coordIndex[iCoord];
|
|
|
|
|
|
|
|
if( idx < 0 )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != fp )
|
2016-02-05 05:08:30 +00:00
|
|
|
{
|
|
|
|
if( fp->HasMinPoints() )
|
2021-07-18 14:06:48 +00:00
|
|
|
fp = nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
else
|
|
|
|
fp->Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the coordinate is bad then skip it
|
|
|
|
if( idx >= (int)coordsize )
|
|
|
|
continue;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == fp )
|
2016-02-05 05:08:30 +00:00
|
|
|
fp = lShape.NewFacet();
|
|
|
|
|
|
|
|
// push the vertex value and index
|
|
|
|
fp->AddVertex( pcoords[idx], idx );
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
SGNODE* np = nullptr;
|
2016-02-05 05:08:30 +00:00
|
|
|
|
|
|
|
if( ccw )
|
2021-07-18 14:06:48 +00:00
|
|
|
np = lShape.CalcShape( aParent, nullptr, WRL1_ORDER::ORD_CCW, creaseLimit, true );
|
2016-02-05 05:08:30 +00:00
|
|
|
else
|
2021-07-18 14:06:48 +00:00
|
|
|
np = lShape.CalcShape( aParent, nullptr, WRL1_ORDER::ORD_CLOCKWISE, creaseLimit, true );
|
2016-02-05 05:08:30 +00:00
|
|
|
|
|
|
|
return np;
|
2016-02-05 04:32:52 +00:00
|
|
|
}
|