kicad/plugins/3d/vrml/x3d/x3d_ifaceset.cpp

328 lines
7.8 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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>
#include <sstream>
#include <cmath>
#include <wx/log.h>
#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"
X3DIFACESET::X3DIFACESET() : X3DNODE()
{
m_Type = X3D_INDEXED_FACE_SET;
2021-07-18 14:06:48 +00:00
coord = nullptr;
init();
}
X3DIFACESET::X3DIFACESET( X3DNODE* aParent ) : X3DNODE()
{
m_Type = X3D_INDEXED_FACE_SET;
2021-07-18 14:06:48 +00:00
coord = nullptr;
init();
2021-07-18 14:06:48 +00:00
if( nullptr != aParent )
{
X3DNODES ptype = aParent->GetNodeType();
if( X3D_SHAPE == ptype )
m_Parent = aParent;
}
2021-07-18 14:06:48 +00:00
if( nullptr != m_Parent )
m_Parent->AddChildNode( this );
}
X3DIFACESET::~X3DIFACESET()
{
2022-02-05 13:25:43 +00:00
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying IndexedFaceSet." ) );
}
void X3DIFACESET::init()
{
2021-07-18 14:06:48 +00:00
coord = nullptr;
ccw = true;
creaseAngle = 0.733f; // approx 42 degrees; this is larger than VRML spec.
creaseLimit = 0.74317f; // cos( 0.733 )
}
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() )
{
const wxString& pname = prop->GetName();
2022-02-05 13:25:43 +00:00
if( pname == wxT( "DEF" ) )
{
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
{
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" ) )
{
X3D::ParseSFFloat( prop->GetValue(), creaseAngle );
if( creaseAngle < 0.0f )
creaseAngle = 0.0f;
else if( creaseAngle > static_cast<float>( M_PI * 0.34f ) )
creaseAngle = static_cast<float>( M_PI / 3.0f );
creaseLimit = cosf( creaseAngle );
}
2022-02-05 13:25:43 +00:00
else if( pname == wxT( "coordIndex" ) )
{
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 )
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() )
{
2022-02-05 13:25:43 +00:00
if( child->GetName() == wxT( "Coordinate" ) )
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 )
{
X3DNODES nt = aParent->GetNodeType();
if( nt != X3D_SHAPE )
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != m_Parent && doUnlink )
m_Parent->unlinkChildNode( this );
m_Parent = aParent;
2021-07-18 14:06:48 +00:00
if( nullptr != m_Parent )
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;
}
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 );
aNode->addNodeRef( this );
2016-02-05 05:08:30 +00:00
coord = aNode;
return true;
}
SGNODE* X3DIFACESET::TranslateToSG( SGNODE* aParent )
{
2016-02-05 05:08:30 +00:00
S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
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,
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;
}