Added support for VRML1 transforms (except for MatrixTransform)

This commit is contained in:
Cirilo Bernardo 2016-01-07 17:01:07 +11:00
parent 0bb4beb5d7
commit 1b6480e858
8 changed files with 419 additions and 4 deletions

View File

@ -37,7 +37,7 @@ IFSG_TRANSFORM::IFSG_TRANSFORM( bool create )
m_node = NULL;
if( !create )
return ;
return;
m_node = new SCENEGRAPH( NULL );

View File

@ -31,6 +31,7 @@ add_library( s3d_plugin_vrml MODULE
v1/vrml1_coords.cpp
v1/vrml1_switch.cpp
v1/vrml1_faceset.cpp
v1/vrml1_transform.cpp
)
target_link_libraries( s3d_plugin_vrml kicad_3dsg ${OPENGL_LIBRARIES} ${wxWidgets_LIBRARIES} )

View File

@ -30,6 +30,7 @@
#include "vrml1_coords.h"
#include "vrml1_switch.h"
#include "vrml1_faceset.h"
#include "vrml1_transform.h"
#include "plugins/3dapi/ifsg_all.h"
@ -455,6 +456,16 @@ bool WRL1BASE::ReadNode( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
break;
case WRL1_TRANSFORM:
case WRL1_TRANSLATION:
case WRL1_ROTATION:
case WRL1_SCALE:
if( !readTransform( proc, aParent, aNode ) )
return false;
break;
//
// items not implemented or for optional future implementation:
//
@ -618,6 +629,26 @@ bool WRL1BASE::readFaceSet( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
}
bool WRL1BASE::readTransform( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
{
if( NULL != aNode )
*aNode = NULL;
WRL1TRANSFORM* np = new WRL1TRANSFORM( 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

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

View File

@ -505,7 +505,9 @@ SGNODE* WRL1FACESET::TranslateToSG( SGNODE* aParent, bool calcNormals )
while( sI != eI )
{
lCPts.push_back( SGPOINT( pcoords[*sI].x, pcoords[*sI].y, pcoords[*sI].z ) );
glm::vec4 pt = glm::vec4( pcoords[*sI].x, pcoords[*sI].y, pcoords[*sI].z, 0.0 );
pt = m_current.txmatrix * pt;
lCPts.push_back( SGPOINT( pt.x, pt.y, pt.z ) );
++sI;
}

View File

@ -77,7 +77,7 @@ struct WRL1STATUS
// normal binding
WRL1_BINDING normbind;
// transform
glm::dmat4 txmatrix;
glm::mat4 txmatrix;
WRL1STATUS()
{
@ -92,7 +92,7 @@ struct WRL1STATUS
norm = NULL;
normbind = BIND_DEFAULT;
coord = NULL;
txmatrix = glm::scale( glm::dmat4( 1.0 ), glm::dvec3( 1.0 ) );
txmatrix = glm::scale( glm::mat4( 1.0 ), glm::vec3( 1.0 ) );
return;
}
};

View File

@ -0,0 +1,319 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015-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_transform.h"
#include "plugins/3dapi/ifsg_all.h"
WRL1TRANSFORM::WRL1TRANSFORM( NAMEREGISTER* aDictionary ) : WRL1NODE( aDictionary )
{
m_Type = WRL1_TRANSFORM;
return;
}
WRL1TRANSFORM::WRL1TRANSFORM( NAMEREGISTER* aDictionary, WRL1NODE* aParent ) :
WRL1NODE( aDictionary )
{
m_Type = WRL1_TRANSFORM;
m_Parent = aParent;
if( NULL != m_Parent )
m_Parent->AddChildNode( this );
return;
}
WRL1TRANSFORM::~WRL1TRANSFORM()
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 2 )
std::cerr << " * [INFO] Destroying Transform with " << m_Children.size();
std::cerr << " children, " << m_Refs.size() << " references and ";
std::cerr << m_BackPointers.size() << " backpointers\n";
#endif
return;
}
bool WRL1TRANSFORM::Read( WRLPROC& proc, WRL1BASE* aTopNode )
{
/*
* Structure of a Transform node:
*
* Transform {
* SFVec3f center 0 0 0
* SFRotation rotation 0 0 1 0
* SFVec3f scale 1 1 1
* SFRotation scaleOrientation 0 0 1 0
* SFVec3f translation 0 0 0
* }
*/
if( NULL == aTopNode )
{
#ifdef DEBUG_VRML1
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [BUG] aTopNode is NULL\n";
#endif
return false;
}
center.x = 0.0;
center.y = 0.0;
center.z = 0.0;
translation = center;
rotation.x = 0.0;
rotation.y = 0.0;
rotation.z = 1.0;
rotation.w = 0.0;
scaleOrientation = rotation;
scale.x = 1.0;
scale.y = 1.0;
scale.z = 1.0;
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:
// center
// rotation
// scale
// ScaleOrientation
// translation
proc.GetFilePosData( line, column );
if( !glob.compare( "center" ) )
{
if( !proc.ReadSFVec3f( center ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] invalid center at line " << line << ", column ";
std::cerr << column << "\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
std::cerr << " * [INFO] message: '" << proc.GetError() << "'\n";
#endif
return false;
}
}
else if( !glob.compare( "rotation" ) )
{
if( !proc.ReadSFRotation( rotation ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] invalid rotation at line " << line << ", column ";
std::cerr << column << "\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
std::cerr << " * [INFO] message: '" << proc.GetError() << "'\n";
#endif
return false;
}
}
else if( !glob.compare( "scaleFactor" ) )
{
if( !proc.ReadSFVec3f( scale ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] invalid scale at line " << line << ", column ";
std::cerr << column << "\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
std::cerr << " * [INFO] message: '" << proc.GetError() << "'\n";
#endif
return false;
}
}
else if( !glob.compare( "scaleOrientation" ) )
{
if( !proc.ReadSFRotation( scaleOrientation ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] invalid scaleOrientation at line " << line << ", column ";
std::cerr << column << "\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
std::cerr << " * [INFO] message: '" << proc.GetError() << "'\n";
#endif
return false;
}
}
else if( !glob.compare( "translation" ) )
{
if( !proc.ReadSFVec3f( translation ) )
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] invalid translation at line " << line << ", column ";
std::cerr << column << "\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
std::cerr << " * [INFO] message: '" << proc.GetError() << "'\n";
#endif
return false;
}
}
else
{
#if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
std::cerr << " * [INFO] bad Transform at line " << line << ", column ";
std::cerr << column << "\n";
std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
#endif
return false;
}
} // while( true ) -- reading contents of Transform{}
return true;
}
bool WRL1TRANSFORM::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 WRL1TRANSFORM::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;
}
SGNODE* WRL1TRANSFORM::TranslateToSG( SGNODE* aParent, bool calcNormals )
{
if( NULL == m_Parent )
return NULL;
if( WRL2_BASE == m_Parent->GetNodeType() )
return NULL;
WRL1STATUS* cp = m_Parent->GetCurrentSettings();
if( NULL == cp )
return NULL;
// rotation
float rX, rY, rZ, rW;
rX = rotation.x;
rY = rotation.y;
rZ = rotation.z;
rW = rotation.w;
glm::mat4 rM = glm::rotate( rW, glm::vec3( rX, rY, rZ ) );
// translation
float dX, dY, dZ;
dX = translation.x;
dY = translation.y;
dZ = translation.z;
glm::mat4 tM = glm::translate( glm::vec3( dX, dY, dZ ) );
// center
dX = center.x;
dY = center.y;
dZ = center.z;
glm::mat4 cM = glm::translate( glm::vec3( dX, dY, dZ ) );
glm::mat4 ncM = glm::translate( glm::vec3( -dX, -dY, -dZ ) );
// scale
glm::mat4 sM = glm::scale( glm::mat4( 1.0 ), glm::vec3( scale.x, scale.y, scale.z ) );
// scaleOrientation
rX = scaleOrientation.x;
rY = scaleOrientation.y;
rZ = scaleOrientation.z;
rW = scaleOrientation.w;
glm::mat4 srM = glm::rotate( rW, glm::vec3( rX, rY, rZ ) );
glm::mat4 nsrM = glm::rotate( -rW, glm::vec3( rX, rY, rZ ) );
// resultant transform:
// tx' = tM * cM * rM * srM * sM * nsrM * ncM
cp->txmatrix = cp->txmatrix * tM * cM * rM * srM * sM * nsrM * ncM;
return NULL;
}

View File

@ -0,0 +1,61 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015-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_transform.h
*/
#ifndef VRML1_TRANSFORM_H
#define VRML1_TRANSFORM_H
#include "vrml1_node.h"
class WRL1BASE;
class SGNODE;
/**
* Class WRL1TRANSFORM
*/
class WRL1TRANSFORM : public WRL1NODE
{
private:
WRLVEC3F center;
WRLVEC3F scale;
WRLVEC3F translation;
WRLROTATION rotation;
WRLROTATION scaleOrientation;
public:
WRL1TRANSFORM( NAMEREGISTER* aDictionary );
WRL1TRANSFORM( NAMEREGISTER* aDictionary, WRL1NODE* aNode );
virtual ~WRL1TRANSFORM();
// 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_TRANSFORM_H