kicad/plugins/3d/vrml/v2/vrml2_box.cpp

337 lines
9.8 KiB
C++
Raw Normal View History

2016-01-03 06:58:23 +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>
2021-07-18 14:06:48 +00:00
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
2016-01-03 06:58:23 +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-03 06:58:23 +00:00
#include "vrml2_base.h"
#include "vrml2_box.h"
#include "plugins/3dapi/ifsg_all.h"
WRL2BOX::WRL2BOX() : WRL2NODE()
{
2021-03-10 23:49:09 +00:00
m_Type = WRL2NODES::WRL2_BOX;
2016-01-03 06:58:23 +00:00
size.x = 2.0;
size.y = 2.0;
size.z = 2.0;
}
WRL2BOX::WRL2BOX( WRL2NODE* aParent ) : WRL2NODE()
{
2021-03-10 23:49:09 +00:00
m_Type = WRL2NODES::WRL2_BOX;
2016-01-03 06:58:23 +00:00
m_Parent = aParent;
size.x = 2.0;
size.y = 2.0;
size.z = 2.0;
2021-07-18 14:06:48 +00:00
if( nullptr != m_Parent )
2016-01-03 06:58:23 +00:00
m_Parent->AddChildNode( this );
}
WRL2BOX::~WRL2BOX()
{
2022-02-05 13:25:43 +00:00
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying Box node." ) );
2016-01-03 06:58:23 +00:00
}
bool WRL2BOX::isDangling( void )
{
// this node is dangling unless it has a parent of type WRL2_SHAPE
2021-07-18 14:06:48 +00:00
if( nullptr == m_Parent || m_Parent->GetNodeType() != WRL2NODES::WRL2_SHAPE )
2016-01-03 06:58:23 +00:00
return true;
return false;
}
bool WRL2BOX::Read( WRLPROC& proc, WRL2BASE* aTopNode )
{
char tok = proc.Peek();
if( proc.eof() )
{
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-03 06:58:23 +00:00
return false;
}
if( '{' != tok )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] bad file format; expecting '{' but got '%s' %s." ),
__FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
2016-01-03 06:58:23 +00:00
return false;
}
proc.Pop();
std::string glob;
if( proc.Peek() == '}' )
{
proc.Pop();
return true;
}
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-03 06:58:23 +00:00
return false;
}
// expecting 'size'
if( !glob.compare( "size" ) )
{
if( !proc.ReadSFVec3f( size ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
" * [INFO] invalid size %s\n"
" * [INFO] file: '%s'\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName(), proc.GetError() );
2016-01-03 06:58:23 +00:00
return false;
}
2016-02-22 09:00:23 +00:00
// for legacy KiCad support we interpret units as 0.1 inch
size *= 2.54;
2016-01-03 06:58:23 +00:00
}
else
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
" * [INFO] invalid Box %s\n"
" * [INFO] file: '%s'\n" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName() );
2016-01-03 06:58:23 +00:00
return false;
}
if( size.x < 1e-6 || size.y < 1e-6 || size.z < 1e-6 )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
" * [INFO] invalid Box size %s\n"
" * [INFO] file: '%s'\n" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName() );
/// @note If this box is bad, should false be returned here?
2016-01-03 06:58:23 +00:00
}
if( proc.Peek() == '}' )
{
proc.Pop();
return true;
}
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
" * [INFO] invalid size %s (no closing brace).\n"
" * [INFO] file: '%s'\n" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(), proc.GetFileName() );
2016-01-03 06:58:23 +00:00
return false;
}
bool WRL2BOX::AddRefNode( WRL2NODE* aNode )
{
// this node may not own or reference any other node
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( false, false, wxT( "AddRefNode is not applicable." ) );
2016-01-03 06:58:23 +00:00
}
bool WRL2BOX::AddChildNode( WRL2NODE* aNode )
{
// this node may not own or reference any other node
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( false, false, wxT( "AddChildNode is not applicable." ) );
2016-01-03 06:58:23 +00:00
}
SGNODE* WRL2BOX::TranslateToSG( SGNODE* aParent )
2016-01-03 06:58:23 +00:00
{
S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_SHAPE ), nullptr,
wxString::Format( wxT( "Box does not have a Shape parent (parent ID: %s)" ),
ptype ) );
2016-01-03 06:58:23 +00:00
// do not render a bad box
if( size.x < 1e-6 || size.y < 1e-6 || size.z < 1e-6 )
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-03 06:58:23 +00:00
if( m_sgNode )
{
2021-07-18 14:06:48 +00:00
if( nullptr != aParent )
2016-01-03 06:58:23 +00:00
{
2021-07-18 14:06:48 +00:00
if( nullptr == S3D::GetSGNodeParent( m_sgNode )
2016-01-04 07:10:15 +00:00
&& !S3D::AddSGNodeChild( aParent, m_sgNode ) )
{
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-04 07:10:15 +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-01-04 07:10:15 +00:00
}
2016-01-03 06:58:23 +00:00
}
return m_sgNode;
}
// create the vertices, triangle indices, and normals
float x = size.x / 2.0;
float y = size.y / 2.0;
float z = size.z / 2.0;
std::vector< SGPOINT > vertices;
std::vector< SGVECTOR > norms;
std::vector< int > idx;
int base = 0;
2021-07-18 14:06:48 +00:00
2016-01-03 06:58:23 +00:00
// top
vertices.emplace_back( -x, -y, z );
vertices.emplace_back( x, -y, z );
vertices.emplace_back( x, y, z );
vertices.emplace_back( -x, y, z );
norms.emplace_back( 0.0, 0.0, 1.0 );
norms.emplace_back( 0.0, 0.0, 1.0 );
norms.emplace_back( 0.0, 0.0, 1.0 );
norms.emplace_back( 0.0, 0.0, 1.0 );
2016-01-03 06:58:23 +00:00
idx.push_back( base );
idx.push_back( base + 1 );
idx.push_back( base + 2 );
idx.push_back( base );
idx.push_back( base + 2 );
idx.push_back( base + 3 );
base += 4;
2021-07-18 14:06:48 +00:00
2016-01-03 06:58:23 +00:00
// bottom
vertices.emplace_back( -x, -y, -z );
vertices.emplace_back( x, -y, -z );
vertices.emplace_back( x, y, -z );
vertices.emplace_back( -x, y, -z );
norms.emplace_back( 0.0, 0.0, -1.0 );
norms.emplace_back( 0.0, 0.0, -1.0 );
norms.emplace_back( 0.0, 0.0, -1.0 );
norms.emplace_back( 0.0, 0.0, -1.0 );
2016-01-03 06:58:23 +00:00
idx.push_back( base );
idx.push_back( base + 2 );
idx.push_back( base + 1 );
idx.push_back( base );
idx.push_back( base + 3 );
idx.push_back( base + 2 );
base += 4;
2021-07-18 14:06:48 +00:00
2016-01-03 06:58:23 +00:00
// front
vertices.emplace_back( -x, -y, z );
vertices.emplace_back( -x, -y, -z );
vertices.emplace_back( x, -y, -z );
vertices.emplace_back( x, -y, z );
norms.emplace_back( 0.0, -1.0, 0.0 );
norms.emplace_back( 0.0, -1.0, 0.0 );
norms.emplace_back( 0.0, -1.0, 0.0 );
norms.emplace_back( 0.0, -1.0, 0.0 );
2016-01-03 06:58:23 +00:00
idx.push_back( base );
idx.push_back( base + 1 );
idx.push_back( base + 2 );
idx.push_back( base );
idx.push_back( base + 2 );
idx.push_back( base + 3 );
base += 4;
2021-07-18 14:06:48 +00:00
2016-01-03 06:58:23 +00:00
// back
vertices.emplace_back( -x, y, z );
vertices.emplace_back( -x, y, -z );
vertices.emplace_back( x, y, -z );
vertices.emplace_back( x, y, z );
norms.emplace_back( 0.0, 1.0, 0.0 );
norms.emplace_back( 0.0, 1.0, 0.0 );
norms.emplace_back( 0.0, 1.0, 0.0 );
norms.emplace_back( 0.0, 1.0, 0.0 );
2016-01-03 06:58:23 +00:00
idx.push_back( base );
idx.push_back( base + 2 );
idx.push_back( base + 1 );
idx.push_back( base );
idx.push_back( base + 3 );
idx.push_back( base + 2 );
base += 4;
2021-07-18 14:06:48 +00:00
2016-01-03 06:58:23 +00:00
// left
vertices.emplace_back( -x, -y, -z );
vertices.emplace_back( -x, -y, z );
vertices.emplace_back( -x, y, z );
vertices.emplace_back( -x, y, -z );
norms.emplace_back( -1.0, 0.0, 0.0 );
norms.emplace_back( -1.0, 0.0, 0.0 );
norms.emplace_back( -1.0, 0.0, 0.0 );
norms.emplace_back( -1.0, 0.0, 0.0 );
2016-01-03 06:58:23 +00:00
idx.push_back( base );
idx.push_back( base + 1 );
idx.push_back( base + 2 );
idx.push_back( base );
idx.push_back( base + 2 );
idx.push_back( base + 3 );
base += 4;
2021-07-18 14:06:48 +00:00
2016-01-03 06:58:23 +00:00
// right
vertices.emplace_back( x, -y, -z );
vertices.emplace_back( x, -y, z );
vertices.emplace_back( x, y, z );
vertices.emplace_back( x, y, -z );
norms.emplace_back( 1.0, 0.0, 0.0 );
norms.emplace_back( 1.0, 0.0, 0.0 );
norms.emplace_back( 1.0, 0.0, 0.0 );
norms.emplace_back( 1.0, 0.0, 0.0 );
2016-01-03 06:58:23 +00:00
idx.push_back( base );
idx.push_back( base + 2 );
idx.push_back( base + 1 );
idx.push_back( base );
idx.push_back( base + 3 );
idx.push_back( base + 2 );
IFSG_FACESET fsNode( aParent );
IFSG_COORDS cpNode( fsNode );
cpNode.SetCoordsList( vertices.size(), &vertices[0] );
IFSG_COORDINDEX ciNode( fsNode );
ciNode.SetIndices( idx.size(), &idx[0] );
IFSG_NORMALS nmNode( fsNode );
nmNode.SetNormalList( norms.size(), &norms[0] );
m_sgNode = fsNode.GetRawPtr();
return m_sgNode;
}