Added ShapeHints to VRML1 parser

This commit is contained in:
Cirilo Bernardo 2016-01-07 18:03:07 +11:00
parent 1b6480e858
commit 0377be722f
8 changed files with 456 additions and 15 deletions

View File

@ -4,7 +4,7 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/v2
)
add_definitions( -DDEBUG_VRML1=2 -DDEBUG_VRML2=2 )
add_definitions( -DDEBUG_VRML1=3 -DDEBUG_VRML2=3 )
add_library( s3d_plugin_vrml MODULE
vrml.cpp
@ -32,6 +32,7 @@ add_library( s3d_plugin_vrml MODULE
v1/vrml1_switch.cpp
v1/vrml1_faceset.cpp
v1/vrml1_transform.cpp
v1/vrml1_shapehints.cpp
)
target_link_libraries( s3d_plugin_vrml kicad_3dsg ${OPENGL_LIBRARIES} ${wxWidgets_LIBRARIES} )

View File

@ -31,6 +31,7 @@
#include "vrml1_switch.h"
#include "vrml1_faceset.h"
#include "vrml1_transform.h"
#include "vrml1_shapehints.h"
#include "plugins/3dapi/ifsg_all.h"
@ -466,6 +467,13 @@ bool WRL1BASE::ReadNode( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
break;
case WRL1_SHAPEHINTS:
if( !readShapeHints( proc, aParent, aNode ) )
return false;
break;
//
// items not implemented or for optional future implementation:
//
@ -649,6 +657,26 @@ bool WRL1BASE::readTransform( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode
}
bool WRL1BASE::readShapeHints( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
if( NULL != aNode )
*aNode = NULL;
WRL1SHAPEHINTS* np = new WRL1SHAPEHINTS( m_dictionary, aParent );
if( !np->Read( proc, this ) )
{
delete np;
return false;
}
if( NULL != aNode )
*aNode = (WRL1NODE*) np;
return true;
}
SGNODE* WRL1BASE::TranslateToSG( SGNODE* aParent, bool calcNormals )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 2 )

View File

@ -58,6 +58,7 @@ private:
bool readCoords( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode );
bool readFaceSet( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode );
bool readTransform( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode );
bool readShapeHints( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode );
std::map< std::string, WRL1INLINE* > m_inlineModels;

View File

@ -328,9 +328,29 @@ SGNODE* WRL1FACESET::TranslateToSG( SGNODE* aParent, bool calcNormals )
// assuming convex polygons, create triangles for the SG node
for( idx = 3; idx < vsize; )
{
lCIdx.push_back( i1 );
lCIdx.push_back( i2 );
lCIdx.push_back( i3 );
switch( m_current.order )
{
case ORD_CCW:
lCIdx.push_back( i1 );
lCIdx.push_back( i2 );
lCIdx.push_back( i3 );
break;
case ORD_CLOCKWISE:
lCIdx.push_back( i1 );
lCIdx.push_back( i3 );
lCIdx.push_back( i2 );
break;
default:
lCIdx.push_back( i1 );
lCIdx.push_back( i2 );
lCIdx.push_back( i3 );
lCIdx.push_back( i1 );
lCIdx.push_back( i3 );
lCIdx.push_back( i2 );
break;
}
++nfaces;
i2 = i3;
@ -423,12 +443,41 @@ SGNODE* WRL1FACESET::TranslateToSG( SGNODE* aParent, bool calcNormals )
for( idx = 3; idx < vsize; )
{
lCIdx.push_back( i1 );
lCIdx.push_back( i2 );
lCIdx.push_back( i3 );
lColors.push_back( pc1 );
lColors.push_back( pc2 );
lColors.push_back( pc3 );
switch( m_current.order )
{
case ORD_CCW:
lCIdx.push_back( i1 );
lCIdx.push_back( i2 );
lCIdx.push_back( i3 );
lColors.push_back( pc1 );
lColors.push_back( pc2 );
lColors.push_back( pc3 );
break;
case ORD_CLOCKWISE:
lCIdx.push_back( i1 );
lCIdx.push_back( i3 );
lCIdx.push_back( i2 );
lColors.push_back( pc1 );
lColors.push_back( pc3 );
lColors.push_back( pc2 );
break;
default:
lCIdx.push_back( i1 );
lCIdx.push_back( i2 );
lCIdx.push_back( i3 );
lCIdx.push_back( i1 );
lCIdx.push_back( i3 );
lCIdx.push_back( i2 );
lColors.push_back( pc1 );
lColors.push_back( pc2 );
lColors.push_back( pc3 );
lColors.push_back( pc1 );
lColors.push_back( pc3 );
lColors.push_back( pc2 );
break;
}
++nfaces;
i2 = i3;
@ -511,12 +560,43 @@ SGNODE* WRL1FACESET::TranslateToSG( SGNODE* aParent, bool calcNormals )
++sI;
}
for( size_t i = 0; i < lCPts.size(); i += 3 )
switch( m_current.order )
{
SGVECTOR sv = S3D::CalcTriNorm( lCPts[i], lCPts[i+1], lCPts[i+2] );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
case ORD_CCW:
for( size_t i = 0; i < lCPts.size(); i += 3 )
{
SGVECTOR sv = S3D::CalcTriNorm( lCPts[i], lCPts[i+1], lCPts[i+2] );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
}
break;
case ORD_CLOCKWISE:
for( size_t i = 0; i < lCPts.size(); i += 3 )
{
SGVECTOR sv = S3D::CalcTriNorm( lCPts[i], lCPts[i+2], lCPts[i+1] );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
}
break;
default:
for( size_t i = 0; i < lCPts.size(); i += 6 )
{
SGVECTOR sv = S3D::CalcTriNorm( lCPts[i], lCPts[i+1], lCPts[i+2] );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
lCNorm.push_back( sv );
}
break;
}
} while( 0 );

View File

@ -78,6 +78,8 @@ struct WRL1STATUS
WRL1_BINDING normbind;
// transform
glm::mat4 txmatrix;
// winding order of vertices
WRL1_ORDER order;
WRL1STATUS()
{
@ -93,6 +95,7 @@ struct WRL1STATUS
normbind = BIND_DEFAULT;
coord = NULL;
txmatrix = glm::scale( glm::mat4( 1.0 ), glm::vec3( 1.0 ) );
order = ORD_UNKNOWN;
return;
}
};

View File

@ -0,0 +1,263 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
*
* 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 "vrml1_base.h"
#include "vrml1_shapehints.h"
#include "plugins/3dapi/ifsg_all.h"
WRL1SHAPEHINTS::WRL1SHAPEHINTS( NAMEREGISTER* aDictionary ) : WRL1NODE( aDictionary )
{
m_order = ORD_UNKNOWN;
m_Type = WRL1_SHAPEHINTS;
return;
}
WRL1SHAPEHINTS::WRL1SHAPEHINTS( NAMEREGISTER* aDictionary, WRL1NODE* aParent ) :
WRL1NODE( aDictionary )
{
m_order = ORD_UNKNOWN;
m_Type = WRL1_SHAPEHINTS;
m_Parent = aParent;
if( NULL != m_Parent )
m_Parent->AddChildNode( this );
return;
}
WRL1SHAPEHINTS::~WRL1SHAPEHINTS()
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 2 )
std::cerr << " * [INFO] Destroying ShapeHints node\n";
#endif
return;
}
bool WRL1SHAPEHINTS::AddRefNode( WRL1NODE* aNode )
{
// this node may not own or reference any other node
#ifdef DEBUG_VRML1
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [BUG] AddRefNode is not applicable\n";
#endif
return false;
}
bool WRL1SHAPEHINTS::AddChildNode( WRL1NODE* aNode )
{
// this node may not own or reference any other node
#ifdef DEBUG_VRML1
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [BUG] AddChildNode is not applicable\n";
#endif
return false;
}
bool WRL1SHAPEHINTS::Read( WRLPROC& proc, WRL1BASE* aTopNode )
{
if( NULL == aTopNode )
{
#ifdef DEBUG_VRML1
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [BUG] aTopNode is NULL\n";
#endif
return false;
}
size_t line, column;
proc.GetFilePosData( line, column );
char tok = proc.Peek();
if( proc.eof() )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] bad file format; unexpected eof at line ";
std::cerr << line << ", column " << column << "\n";
#endif
return false;
}
if( '{' != tok )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << proc.GetError() << "\n";
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] bad file format; expecting '{' but got '" << tok;
std::cerr << "' at line " << line << ", column " << column << "\n";
#endif
return false;
}
proc.Pop();
std::string glob;
while( true )
{
if( proc.Peek() == '}' )
{
proc.Pop();
break;
}
if( !proc.ReadName( glob ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << proc.GetError() << "\n";
#endif
return false;
}
// expecting one of:
// vertexOrdering
// shapeType
// faceType
// creaseAngle
if( !glob.compare( "vertexOrdering" ) )
{
if( !proc.ReadName( glob ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << proc.GetError() << "\n";
#endif
return false;
}
if( !glob.compare( "UNKNOWN_ORDERING" ) )
m_order = ORD_UNKNOWN;
else if( !glob.compare( "CLOCKWISE" ) )
m_order = ORD_CLOCKWISE;
else if( !glob.compare( "COUNTERCLOCKWISE" ) )
m_order = ORD_CCW;
else
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] bad ShapeHints at line " << line << ", column ";
std::cerr << column << " (invalid value '" << glob << "')\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
#endif
return false;
}
}
else if( !glob.compare( "shapeType" ) )
{
if( !proc.ReadName( glob ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << proc.GetError() << "\n";
#endif
return false;
}
// expected values:
// UNKNOWN_SHAPE_TYPE
// SOLID
}
else if( !glob.compare( "faceType" ) )
{
if( !proc.ReadName( glob ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << proc.GetError() << "\n";
#endif
return false;
}
// expected values:
// UNKNOWN_FACE_TYPE
// CONVEX
}
else if( !glob.compare( "creaseAngle" ) )
{
float tmp;
if( !proc.ReadSFFloat( tmp ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << proc.GetError() << "\n";
#endif
return false;
}
}
else
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] bad ShapeHints at line " << line << ", column ";
std::cerr << column << " (unexpected keyword '" << glob << "')\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
#endif
return false;
}
} // while( true ) -- reading contents of ShapeHints{}
return true;
}
SGNODE* WRL1SHAPEHINTS::TranslateToSG( SGNODE* aParent, bool calcNormals )
{
// note: this is not fully implemented since it is unlikely we shall
// ever make use of the fields shapeType, faceType, and creaseAngle
if( m_Parent )
{
WRL1STATUS* cp = m_Parent->GetCurrentSettings();
if( NULL != cp )
cp->order = m_order;
}
return NULL;
}

View File

@ -0,0 +1,58 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
*
* 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
*/
/**
* @file vrml1_shapehints.h
*/
#ifndef VRML1_SHAPEHINTS_H
#define VRML1_SHAPEHINTS_H
#include <vector>
#include "vrml1_node.h"
class WRL1BASE;
class SGNODE;
/**
* Class WRL1SHAPEHINTS
*/
class WRL1SHAPEHINTS : public WRL1NODE
{
private:
WRL1_ORDER m_order;
public:
WRL1SHAPEHINTS( NAMEREGISTER* aDictionary );
WRL1SHAPEHINTS( NAMEREGISTER* aDictionary, WRL1NODE* aParent );
virtual ~WRL1SHAPEHINTS();
// functions inherited from WRL1NODE
bool Read( WRLPROC& proc, WRL1BASE* aTopNode );
bool AddRefNode( WRL1NODE* aNode );
bool AddChildNode( WRL1NODE* aNode );
SGNODE* TranslateToSG( SGNODE* aParent, bool calcNormals );
};
#endif // VRML1_SHAPEHINTS_H

View File

@ -101,6 +101,13 @@ enum WRL1_BINDING
BIND_END
};
enum WRL1_ORDER
{
ORD_UNKNOWN = 0,
ORD_CLOCKWISE,
ORD_CCW
};
// VRML2 Node Types
// These are used to look up node names and to quickly
// determine what routine to invoke to read a section of