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

433 lines
12 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>
2021-07-18 14:06:48 +00:00
* Copyright (C) 2021 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 <wx/log.h>
#include "vrml1_base.h"
#include "vrml1_faceset.h"
#include "vrml1_coords.h"
#include "vrml1_material.h"
#include "wrlfacet.h"
#include "plugins/3dapi/ifsg_all.h"
WRL1FACESET::WRL1FACESET( NAMEREGISTER* aDictionary ) : WRL1NODE( aDictionary )
{
2021-03-11 00:02:39 +00:00
m_Type = WRL1NODES::WRL1_INDEXEDFACESET;
}
WRL1FACESET::WRL1FACESET( NAMEREGISTER* aDictionary, WRL1NODE* aParent ) :
WRL1NODE( aDictionary )
{
2021-03-11 00:02:39 +00:00
m_Type = WRL1NODES::WRL1_INDEXEDFACESET;
m_Parent = aParent;
2021-07-18 14:06:48 +00:00
if( nullptr != m_Parent )
m_Parent->AddChildNode( this );
}
WRL1FACESET::~WRL1FACESET()
{
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying IndexedFaceSet with %zu children, "
"%zu references, and %zu back pointers." ),
2021-07-24 17:27:35 +00:00
m_Children.size(), m_Refs.size(), m_BackPointers.size() );
}
bool WRL1FACESET::AddRefNode( WRL1NODE* 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." ) );
}
bool WRL1FACESET::AddChildNode( WRL1NODE* 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." ) );
}
bool WRL1FACESET::Read( WRLPROC& proc, WRL1BASE* 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() );
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() );
return false;
}
proc.Pop();
std::string glob;
while( true )
{
if( proc.Peek() == '}' )
{
proc.Pop();
break;
}
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() );
return false;
}
// expecting one of:
// coordIndex[]
// materialIndex[]
if( !glob.compare( "coordIndex" ) )
{
if( !proc.ReadMFInt( coordIndex ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] invalid coordIndex %s.\n"
" * [INFO] file: '%s'\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName(), proc.GetError() );
return false;
}
}
else if( !glob.compare( "materialIndex" ) )
{
if( !proc.ReadMFInt( matIndex ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] invalid materialIndex %s.\n"
" * [INFO] file: '%s'\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName(), proc.GetError() );
return false;
}
}
else if( !glob.compare( "normalIndex" ) )
{
if( !proc.ReadMFInt( normIndex ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] invalid normalIndex %s\n"
" * [INFO] file: '%s'\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName(), proc.GetError() );
return false;
}
}
else if( !glob.compare( "textureCoordIndex" ) )
{
if( !proc.ReadMFInt( texIndex ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] invalid textureCoordIndex %s.\n"
" * [INFO] file: '%s'\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName(), proc.GetError() );
return false;
}
}
else
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] invalid IndexedFaceSet %s.\n"
" * [INFO] file: '%s'" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName() );
return false;
}
} // while( true ) -- reading contents of IndexedFaceSet{}
return true;
}
2016-01-08 05:23:42 +00:00
SGNODE* WRL1FACESET::TranslateToSG( SGNODE* aParent, WRL1STATUS* sp )
{
2016-01-08 05:23:42 +00:00
// note: m_sgNode is unused because we cannot manage everything
// with a single reused transform due to the fact that VRML1
// may use a MatrixTransformation entity which is impossible to
2021-07-18 14:06:48 +00:00
// decompose into Rotate,Scale,Transform via an analytic expression.
2016-01-08 05:23:42 +00:00
if( !m_Parent )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, " * [INFO] bad model: no parent node." );
2021-07-18 14:06:48 +00:00
return nullptr;
}
2016-01-08 05:23:42 +00:00
else
{
2021-07-18 14:06:48 +00:00
if( nullptr == sp )
2016-01-08 05:23:42 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, " * [INFO] bad model: no base data given." );
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-08 05:23:42 +00:00
}
}
2016-01-08 05:23:42 +00:00
m_current = *sp;
2021-07-24 17:27:35 +00:00
if( nullptr == m_current.coord )
2016-01-08 05:23:42 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, " * [INFO] bad model: no vertex set." );
return nullptr;
}
2016-01-08 05:23:42 +00:00
2021-07-24 17:27:35 +00:00
if( nullptr == m_current.mat )
{
wxLogTrace( traceVrmlPlugin, " * [INFO] bad model: no material set." );
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-08 05:23:42 +00:00
}
WRLVEC3F* pcoords;
size_t coordsize;
m_current.coord->GetCoords( pcoords, coordsize );
size_t vsize = coordIndex.size();
if( coordsize < 3 || vsize < 3 )
2016-01-08 05:23:42 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( " * [INFO] bad model: coordsize = %zu, indexsize = %zu" ),
2021-07-24 17:27:35 +00:00
coordsize, vsize );
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-08 05:23:42 +00:00
}
// 1. create the vertex/normals/colors lists
2021-07-18 14:06:48 +00:00
SGNODE* sgcolor = nullptr;
WRL1_BINDING mbind = m_current.matbind;
size_t matSize = matIndex.size();
switch( mbind )
{
2021-03-10 23:52:21 +00:00
case WRL1_BINDING::BIND_PER_FACE:
case WRL1_BINDING::BIND_PER_VERTEX:
case WRL1_BINDING::BIND_PER_VERTEX_INDEXED:
break;
2021-03-10 23:52:21 +00:00
case WRL1_BINDING::BIND_PER_FACE_INDEXED:
if( matIndex.empty() )
2016-01-08 05:23:42 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( " * [INFO] bad model: per face indexed but no indices" ) );
// support bad models by temporarily switching bindings
2021-03-10 23:52:21 +00:00
mbind = WRL1_BINDING::BIND_OVERALL;
sgcolor = m_current.mat->GetAppearance( 0 );
2016-01-08 05:23:42 +00:00
}
break;
default:
// use the first appearance definition
sgcolor = m_current.mat->GetAppearance( 0 );
break;
}
// copy the data into FACET structures
SHAPE lShape;
2021-07-18 14:06:48 +00:00
FACET* fp = nullptr;
size_t iCoord;
int idx; // coordinate index
size_t cidx = 0; // color index
SGCOLOR pc1;
2021-03-10 23:52:21 +00:00
if( mbind == WRL1_BINDING::BIND_OVERALL || mbind == WRL1_BINDING::BIND_DEFAULT )
{
// 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 )
{
if( fp->HasMinPoints() )
2021-07-18 14:06:48 +00:00
fp = nullptr;
else
fp->Init();
}
continue;
}
// if the coordinate is bad then skip it
if( idx >= (int)coordsize )
continue;
2016-01-08 05:23:42 +00:00
2021-07-18 14:06:48 +00:00
if( nullptr == fp )
fp = lShape.NewFacet();
2016-01-08 05:23:42 +00:00
// push the vertex value and index
WRLVEC3F vf;
glm::vec4 pt = glm::vec4( pcoords[idx].x, pcoords[idx].y, pcoords[idx].z, 1.0 );
pt = m_current.txmatrix * pt;
vf.x = pt.x;
vf.y = pt.y;
vf.z = pt.z;
fp->AddVertex( vf, idx );
}
}
else
{
for( iCoord = 0; iCoord < vsize; ++iCoord )
{
idx = coordIndex[iCoord];
if( idx < 0 )
{
2021-07-18 14:06:48 +00:00
if( nullptr != fp )
{
if( fp->HasMinPoints() )
2021-07-18 14:06:48 +00:00
fp = nullptr;
else
fp->Init();
}
2021-03-10 23:52:21 +00:00
if( mbind == WRL1_BINDING::BIND_PER_FACE
|| mbind == WRL1_BINDING::BIND_PER_FACE_INDEXED )
++cidx;
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 )
fp = lShape.NewFacet();
// push the vertex value and index
WRLVEC3F vf;
glm::vec4 pt = glm::vec4( pcoords[idx].x, pcoords[idx].y, pcoords[idx].z, 1.0 );
pt = m_current.txmatrix * pt;
vf.x = pt.x;
vf.y = pt.y;
vf.z = pt.z;
fp->AddVertex( vf, idx );
// push the color if appropriate
switch( mbind )
2016-01-07 07:03:07 +00:00
{
2021-03-10 23:52:21 +00:00
case WRL1_BINDING::BIND_PER_FACE:
2016-01-07 07:03:07 +00:00
if( !fp->HasColors() )
{
m_current.mat->GetColor( &pc1, cidx );
fp->AddColor( pc1 );
}
2016-01-07 07:03:07 +00:00
break;
2021-03-10 23:52:21 +00:00
case WRL1_BINDING::BIND_PER_VERTEX:
m_current.mat->GetColor( &pc1, idx );
fp->AddColor( pc1 );
2016-01-08 05:23:42 +00:00
break;
2021-03-10 23:52:21 +00:00
case WRL1_BINDING::BIND_PER_FACE_INDEXED:
2021-07-18 14:06:48 +00:00
if( !fp->HasColors() )
{
if( cidx >= matSize )
m_current.mat->GetColor( &pc1, matIndex.back() );
else
m_current.mat->GetColor( &pc1, matIndex[cidx] );
fp->AddColor( pc1 );
}
break;
2021-03-10 23:52:21 +00:00
case WRL1_BINDING::BIND_PER_VERTEX_INDEXED:
if( matIndex.empty() )
{
m_current.mat->GetColor( &pc1, idx );
}
else
2016-01-08 05:23:42 +00:00
{
if( iCoord >= matSize )
m_current.mat->GetColor( &pc1, matIndex.back() );
else
m_current.mat->GetColor( &pc1, matIndex[iCoord] );
2016-01-08 05:23:42 +00:00
}
fp->AddColor( pc1 );
2016-01-08 05:23:42 +00:00
break;
2016-01-07 07:03:07 +00:00
default:
break;
2016-01-07 07:03:07 +00:00
}
}
}
// extract the final data set
2016-02-02 00:23:15 +00:00
SGNODE* np = lShape.CalcShape( aParent, sgcolor, m_current.order, m_current.creaseLimit );
return np;
}