kicad/plugins/3d/vrml/v1/vrml1_base.cpp

570 lines
13 KiB
C++
Raw Normal View History

2016-01-05 05:38:53 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
2021-07-18 14:06:48 +00:00
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
2016-01-05 05:38:53 +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>
#include <sstream>
#include <wx/log.h>
2016-01-05 05:38:53 +00:00
#include "vrml1_base.h"
2016-01-08 05:23:42 +00:00
#include "vrml1_group.h"
2016-01-05 05:38:53 +00:00
#include "vrml1_separator.h"
2016-01-05 07:55:54 +00:00
#include "vrml1_material.h"
#include "vrml1_matbinding.h"
#include "vrml1_coords.h"
#include "vrml1_switch.h"
#include "vrml1_faceset.h"
#include "vrml1_transform.h"
2016-01-07 07:03:07 +00:00
#include "vrml1_shapehints.h"
2016-01-05 05:38:53 +00:00
#include "plugins/3dapi/ifsg_all.h"
2021-07-18 14:06:48 +00:00
WRL1BASE::WRL1BASE() : WRL1NODE( nullptr )
2016-01-05 05:38:53 +00:00
{
2021-03-11 00:02:39 +00:00
m_Type = WRL1NODES::WRL1_BASE;
2016-01-05 05:38:53 +00:00
m_dictionary = new NAMEREGISTER;
return;
}
WRL1BASE::~WRL1BASE()
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying virtual base node." ) );
2016-01-05 05:38:53 +00:00
2016-01-07 04:43:32 +00:00
cancelDict();
2016-01-05 05:38:53 +00:00
}
bool WRL1BASE::SetParent( WRL1NODE* aParent, bool /* doUnlink */ )
2016-01-05 05:38:53 +00:00
{
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( false, false, wxT( "Attempt to set parent on WRL1BASE node." ) );
2016-01-05 05:38:53 +00:00
}
std::string WRL1BASE::GetName( void )
{
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( false, std::string( "" ),
wxT( "Attempt to extract name from virtual base node." ) );
2016-01-05 05:38:53 +00:00
}
bool WRL1BASE::SetName( const std::string& aName )
{
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( false, false, wxT( "Attempt to set name on virtual base node." ) );
2016-01-05 05:38:53 +00:00
}
bool WRL1BASE::Read( WRLPROC& proc )
{
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( proc.GetVRMLType() == WRLVERSION::VRML_V1, false,
wxT( "No open file or file is not a VRML1 file" ) );
2016-01-05 05:38:53 +00:00
// Note: according to the VRML1 specification, a file may contain
// only one grouping node at the top level. The following code
// supports non-conformant VRML1 files by processing all top level
// nodes as if the vrml1_base were the equivalent of a vrml1_separator
while( proc.Peek() )
2016-01-05 07:55:54 +00:00
{
2021-07-18 14:06:48 +00:00
if( !ReadNode( proc, this, nullptr ) )
2016-01-05 07:55:54 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n * [INFO] bad file format; unexpected eof %s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
2016-01-05 07:55:54 +00:00
return false;
2016-01-05 07:55:54 +00:00
}
}
2016-01-08 05:23:42 +00:00
if( !proc.eof() )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetError() );
2016-01-05 07:55:54 +00:00
return false;
2016-01-05 07:55:54 +00:00
}
return true;
2016-01-05 05:38:53 +00:00
}
bool WRL1BASE::implementUse( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-05 05:38:53 +00:00
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( aParent, false, wxT( "Invoked with invalid parent." ) );
2016-01-05 05:38:53 +00:00
std::string glob;
if( !proc.ReadName( glob ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s" ), proc.GetError() );
2016-01-05 05:38:53 +00:00
return false;
}
WRL1NODE* ref = aParent->FindNode( glob );
// return 'true' - the file may be defective but it may still be somewhat OK
2021-07-18 14:06:48 +00:00
if( nullptr == ref )
2016-01-05 05:38:53 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n * [INFO] node '%s' not found." ),
__FILE__, __FUNCTION__, __LINE__, glob );
2016-01-05 05:38:53 +00:00
return true;
}
if( !aParent->AddRefNode( ref ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n * [INFO] failed to add node '%s' (%s) to parent of type %s." ),
__FILE__, __FUNCTION__, __LINE__, glob,
ref->GetNodeTypeName( ref->GetNodeType() ),
aParent->GetNodeTypeName( aParent->GetNodeType() ) );
2016-01-05 05:38:53 +00:00
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-05 05:38:53 +00:00
*aNode = ref;
return true;
}
bool WRL1BASE::implementDef( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-05 05:38:53 +00:00
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( nullptr != aParent, false, wxT( "Invalid parent pointer." ) );
2016-01-05 05:38:53 +00:00
std::string glob;
2021-07-18 14:06:48 +00:00
WRL1NODE* lnode = nullptr;
2016-01-05 05:38:53 +00:00
if( !proc.ReadName( glob ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetError() );
2016-01-05 05:38:53 +00:00
return false;
}
if( ReadNode( proc, aParent, &lnode ) )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-05 05:38:53 +00:00
*aNode = lnode;
if( lnode && !lnode->SetName( glob ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n * [INFO] bad formatting (invalid name) %s." ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
2016-01-05 05:38:53 +00:00
return false;
}
if( !m_dictionary )
return false;
m_dictionary->AddName( glob, lnode );
return true;
}
return false;
}
bool WRL1BASE::ReadNode( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
// This function reads a node and stores a pointer to it in aNode.
// A value 'true' is returned if a node is successfully read or,
// if the node is not supported, successfully discarded. Callers
// must always check the value of aNode when the function returns
// 'true' since it will be NULL if the node type is not supported.
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-05 05:38:53 +00:00
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( aParent, false, wxT( "Invalid parent pointer." ) );
2016-01-05 05:38:53 +00:00
std::string glob;
WRL1NODES ntype;
if( !proc.ReadName( glob ) )
{
if( !proc.eof() )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetError() );
2016-01-05 05:38:53 +00:00
}
return false;
}
// Process node name:
// the names encountered at this point should be one of the
// built-in node names or one of:
// DEF, USE
if( !glob.compare( "USE" ) )
{
if( !implementUse( proc, aParent, aNode ) )
return false;
return true;
}
if( !glob.compare( "DEF" ) )
{
if( !implementDef( proc, aParent, aNode ) )
return false;
return true;
}
ntype = getNodeTypeID( glob );
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Processing node '%s' ID: %d." ), glob, ntype );
2016-01-05 05:38:53 +00:00
switch( ntype )
{
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_GROUP:
2016-01-08 05:23:42 +00:00
if( !readGroup( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_SEPARATOR:
2016-01-05 05:38:53 +00:00
if( !readSeparator( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_SWITCH:
2016-01-05 07:55:54 +00:00
if( !readSwitch( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_MATERIAL:
2016-01-05 07:55:54 +00:00
if( !readMaterial( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_MATERIALBINDING:
2016-01-05 07:55:54 +00:00
if( !readMatBinding( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_COORDINATE3:
if( !readCoords( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_INDEXEDFACESET:
if( !readFaceSet( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_TRANSFORM:
case WRL1NODES::WRL1_TRANSLATION:
case WRL1NODES::WRL1_ROTATION:
case WRL1NODES::WRL1_SCALE:
if( !readTransform( proc, aParent, aNode ) )
return false;
break;
2021-03-11 00:02:39 +00:00
case WRL1NODES::WRL1_SHAPEHINTS:
2016-01-07 07:03:07 +00:00
if( !readShapeHints( proc, aParent, aNode ) )
return false;
break;
2016-01-05 05:38:53 +00:00
//
// items not implemented or for optional future implementation:
//
default:
if( !proc.DiscardNode() )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n * [INFO] could not discard node %s." ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
2016-01-05 05:38:53 +00:00
return false;
}
else
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( " * [INFO] discarded node '%s' %s (currently unsupported)." ),
glob, proc.GetFilePosition() );
}
2016-01-05 05:38:53 +00:00
break;
}
return true;
}
bool WRL1BASE::Read( WRLPROC& proc, WRL1BASE* aTopNode )
{
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( false, false, wxT( "This method must never be invoked on a WRL1BASE object" ) );
2016-01-05 05:38:53 +00:00
}
2016-01-08 05:23:42 +00:00
bool WRL1BASE::readGroup( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-08 05:23:42 +00:00
WRL1GROUP* np = new WRL1GROUP( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-08 05:23:42 +00:00
*aNode = (WRL1NODE*) np;
return true;
}
2016-01-05 05:38:53 +00:00
bool WRL1BASE::readSeparator( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-05 05:38:53 +00:00
WRL1SEPARATOR* np = new WRL1SEPARATOR( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-05 05:38:53 +00:00
*aNode = (WRL1NODE*) np;
return true;
}
2016-01-05 07:55:54 +00:00
bool WRL1BASE::readSwitch( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
WRL1SWITCH* np = new WRL1SWITCH( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-05 07:55:54 +00:00
*aNode = (WRL1NODE*) np;
return true;
}
bool WRL1BASE::readMaterial( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-05 07:55:54 +00:00
WRL1MATERIAL* np = new WRL1MATERIAL( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-05 07:55:54 +00:00
*aNode = (WRL1NODE*) np;
return true;
}
bool WRL1BASE::readMatBinding( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-05 07:55:54 +00:00
WRL1MATBINDING* np = new WRL1MATBINDING( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-05 07:55:54 +00:00
*aNode = (WRL1NODE*) np;
return true;
}
bool WRL1BASE::readCoords( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
WRL1COORDS* np = new WRL1COORDS( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = (WRL1NODE*) np;
return true;
}
bool WRL1BASE::readFaceSet( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
WRL1FACESET* np = new WRL1FACESET( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = (WRL1NODE*) np;
return true;
}
bool WRL1BASE::readTransform( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
WRL1TRANSFORM* np = new WRL1TRANSFORM( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = (WRL1NODE*) np;
return true;
}
2016-01-07 07:03:07 +00:00
bool WRL1BASE::readShapeHints( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
*aNode = nullptr;
2016-01-07 07:03:07 +00:00
WRL1SHAPEHINTS* np = new WRL1SHAPEHINTS( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
2021-07-18 14:06:48 +00:00
if( nullptr != aNode )
2016-01-07 07:03:07 +00:00
*aNode = (WRL1NODE*) np;
return true;
}
2016-01-08 05:23:42 +00:00
SGNODE* WRL1BASE::TranslateToSG( SGNODE* aParent, WRL1STATUS* /*sp*/ )
2016-01-05 05:38:53 +00:00
{
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Translating VRML1 Base with %zu items." ),
2021-07-24 17:27:35 +00:00
m_Items.size() );
2016-01-05 07:55:54 +00:00
2016-01-05 05:38:53 +00:00
if( m_Items.empty() )
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-05 05:38:53 +00:00
if( m_Items.size() == 1 )
2021-07-24 17:27:35 +00:00
return ( *m_Items.begin() )->TranslateToSG( nullptr, nullptr );
// Note: according to the VRML1 specification, a file may contain
// only one grouping node at the top level. The following code
// supports non-conformant VRML1 files.
m_current.Init();
IFSG_TRANSFORM txNode( true );
bool hasContent = false;
std::list< WRL1NODE* >::iterator sI = m_Items.begin();
std::list< WRL1NODE* >::iterator eI = m_Items.end();
SGNODE* node = txNode.GetRawPtr();
while( sI != eI )
2016-01-05 07:55:54 +00:00
{
2021-07-18 14:06:48 +00:00
if( nullptr != (*sI)->TranslateToSG( node, &m_current ) )
hasContent = true;
++sI;
}
2016-01-05 05:38:53 +00:00
if( !hasContent )
{
txNode.Destroy();
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-05 07:55:54 +00:00
}
return node;
2016-01-05 05:38:53 +00:00
}