kicad/3d-viewer/3d_read_mesh.cpp

585 lines
15 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2011 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
*/
/**
* @file 3d_read_mesh.cpp
*/
2007-05-06 16:03:28 +00:00
#include <fctsys.h>
#include <common.h>
#include <macros.h>
#include <kicad_string.h>
#include <appl_wxstruct.h>
2007-05-06 16:03:28 +00:00
#include <3d_viewer.h>
2007-05-06 16:03:28 +00:00
int S3D_MASTER::ReadData()
2007-05-06 16:03:28 +00:00
{
char line[1024], * text;
wxFileName fn;
wxString FullFilename;
FILE* file;
int LineNum = 0;
2008-01-01 07:53:29 +00:00
if( m_Shape3DName.IsEmpty() )
{
return 1;
}
wxString shape3DNname = m_Shape3DName;
#ifdef __WINDOWS__
shape3DNname.Replace( wxT("/"), wxT("\\") );
#else
shape3DNname.Replace( wxT("\\"), wxT("/") );
#endif
if( wxFileName::FileExists( shape3DNname ) )
2011-09-17 15:31:21 +00:00
{
FullFilename = shape3DNname;
2011-09-17 15:31:21 +00:00
}
else
{
fn = shape3DNname;
FullFilename = wxGetApp().FindLibraryPath( fn );
if( FullFilename.IsEmpty() )
{
2009-12-02 13:06:55 +00:00
wxLogDebug( wxT( "3D part library <%s> could not be found." ),
GetChars( fn.GetFullPath() ) );
return -1;
}
}
file = wxFopen( FullFilename, wxT( "rt" ) );
2007-05-06 16:03:28 +00:00
2008-01-01 07:53:29 +00:00
if( file == NULL )
{
return -1;
}
// Switch the locale to standard C (needed to print floating point numbers like 1.3)
SetLocaleTo_C_standard();
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
while( GetLine( file, line, &LineNum, 512 ) )
{
text = strtok( line, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( stricmp( text, "DEF" ) == 0 )
{
while( GetLine( file, line, &LineNum, 512 ) )
{
text = strtok( line, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( text == NULL )
continue;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( *text == '}' )
break;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( stricmp( text, "children" ) == 0 )
{
ReadChildren( file, &LineNum );
}
}
}
}
fclose( file );
SetLocaleTo_Default(); // revert to the current locale
2008-01-01 07:53:29 +00:00
return 0;
2007-05-06 16:03:28 +00:00
}
2008-01-01 07:53:29 +00:00
int S3D_MASTER::ReadMaterial( FILE* file, int* LineNum )
2007-05-06 16:03:28 +00:00
{
2008-01-01 07:53:29 +00:00
char line[512], * text, * command;
wxString mat_name;
2008-12-06 08:21:54 +00:00
S3D_MATERIAL* material = NULL;
2008-01-01 07:53:29 +00:00
command = strtok( NULL, " \t\n\r" );
text = strtok( NULL, " \t\n\r" );
mat_name = FROM_UTF8( text );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( stricmp( command, "USE" ) == 0 )
{
for( material = m_Materials; material; material = material->Next() )
2008-01-01 07:53:29 +00:00
{
if( material->m_Name == mat_name )
{
material->SetMaterial();
return 1;
}
}
printf( "ReadMaterial error: material not found\n" );
return 0;
}
if( stricmp( command, "DEF" ) == 0 )
{
2008-12-06 08:21:54 +00:00
material = new S3D_MATERIAL( this, mat_name );
2008-01-01 07:53:29 +00:00
Insert( material );
2008-01-01 07:53:29 +00:00
while( GetLine( file, line, LineNum, 512 ) )
{
text = strtok( line, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( text == NULL )
continue;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( text[0] == '}' )
{
material->SetMaterial();
return 0;
}
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( stricmp( text, "diffuseColor" ) == 0 )
{
text = strtok( NULL, " \t\n\r" );
material->m_DiffuseColor.x = atof( text );
text = strtok( NULL, " \t\n\r" );
material->m_DiffuseColor.y = atof( text );
text = strtok( NULL, " \t\n\r" );
material->m_DiffuseColor.z = atof( text );
}
else if( stricmp( text, "emissiveColor" ) == 0 )
{
text = strtok( NULL, " \t\n\r" );
material->m_EmissiveColor.x = atof( text );
text = strtok( NULL, " \t\n\r" );
material->m_EmissiveColor.y = atof( text );
text = strtok( NULL, " \t\n\r" );
material->m_EmissiveColor.z = atof( text );
}
else if( strnicmp( text, "specularColor", 13 ) == 0 )
{
text = strtok( NULL, " \t\n\r" );
material->m_SpecularColor.x = atof( text );
text = strtok( NULL, " \t\n\r" );
material->m_SpecularColor.y = atof( text );
text = strtok( NULL, " \t\n\r" );
material->m_SpecularColor.z = atof( text );
}
else if( strnicmp( text, "ambientIntensity", 16 ) == 0 )
{
text = strtok( NULL, " \t\n\r" );
material->m_AmbientIntensity = atof( text );
}
else if( strnicmp( text, "transparency", 12 ) == 0 )
{
text = strtok( NULL, " \t\n\r" );
material->m_Transparency = atof( text );
}
else if( strnicmp( text, "shininess", 9 ) == 0 )
{
text = strtok( NULL, " \t\n\r" );
material->m_Shininess = atof( text );
}
}
}
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
return -1;
2007-05-06 16:03:28 +00:00
}
2008-12-06 08:21:54 +00:00
int S3D_MASTER::ReadChildren( FILE* file, int* LineNum )
2007-05-06 16:03:28 +00:00
{
2008-01-01 07:53:29 +00:00
char line[1024], * text;
while( GetLine( file, line, LineNum, 512 ) )
{
text = strtok( line, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( *text == ']' )
return 0;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( *text == ',' )
continue;
if( stricmp( text, "Shape" ) == 0 )
{
ReadShape( file, LineNum );
}
else
{
printf( "ReadChildren error line %d <%s> \n", *LineNum, text );
break;
}
}
return 1;
2007-05-06 16:03:28 +00:00
}
2008-01-01 07:53:29 +00:00
2008-12-06 08:21:54 +00:00
int S3D_MASTER::ReadShape( FILE* file, int* LineNum )
2007-05-06 16:03:28 +00:00
{
2008-01-01 07:53:29 +00:00
char line[1024], * text;
int err = 1;
while( GetLine( file, line, LineNum, 512 ) )
{
text = strtok( line, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( *text == '}' )
{
err = 0;
break;
}
if( stricmp( text, "appearance" ) == 0 )
{
ReadAppearance( file, LineNum );
}
else if( stricmp( text, "geometry" ) == 0 )
{
ReadGeometry( file, LineNum );
}
else
{
printf( "ReadShape error line %d <%s> \n", *LineNum, text );
break;
}
}
return err;
2007-05-06 16:03:28 +00:00
}
2008-01-01 07:53:29 +00:00
2008-12-06 08:21:54 +00:00
int S3D_MASTER::ReadAppearance( FILE* file, int* LineNum )
2007-05-06 16:03:28 +00:00
{
2008-01-01 07:53:29 +00:00
char line[1024], * text;
int err = 1;
while( GetLine( file, line, LineNum, 512 ) )
{
text = strtok( line, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( *text == '}' )
{
err = 0; break;
}
if( stricmp( text, "material" ) == 0 )
{
ReadMaterial( file, LineNum );
}
else
{
printf( "ReadAppearance error line %d <%s> \n", *LineNum, text );
break;
}
}
return err;
2007-05-06 16:03:28 +00:00
}
2008-01-01 07:53:29 +00:00
2007-05-06 16:03:28 +00:00
#define BUFSIZE 2000
/**
* Function ReadCoordList
* reads 3D coordinate lists like:
2008-01-01 07:53:29 +00:00
* coord Coordinate { point [
* -5.24489 6.57640e-3 -9.42129e-2,
* -5.11821 6.57421e-3 0.542654,
* -3.45868 0.256565 1.32000 ] }
* or:
* normal Normal { vector [
* 0.995171 -6.08102e-6 9.81541e-2,
* 0.923880 -4.09802e-6 0.382683,
* 0.707107 -9.38186e-7 0.707107]
* }
*
2008-01-01 07:53:29 +00:00
* text_buffer contains the first line of this node :
* "coord Coordinate { point ["
2007-05-06 16:03:28 +00:00
*/
void ReadCoordsList( FILE* file, char* text_buffer, std::vector< double >& aList, int* LineNum )
2007-05-06 16:03:28 +00:00
{
unsigned int ii = 0, jj = 0;
2008-01-01 07:53:29 +00:00
char* text;
2011-09-17 15:31:21 +00:00
bool HasData = false;
bool StartData = false;
bool EndNode = false;
2008-01-01 07:53:29 +00:00
char string_num[512];
text = text_buffer;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
while( !EndNode )
{
if( *text == 0 ) // Needs data !
{
text = text_buffer;
GetLine( file, text_buffer, LineNum, 512 );
}
while( !EndNode && *text )
{
switch( *text )
{
case '[':
2011-09-17 15:31:21 +00:00
StartData = true;
jj = 0;
string_num[jj] = 0;
2008-01-01 07:53:29 +00:00
break;
case '}':
2011-09-17 15:31:21 +00:00
EndNode = true;
2008-01-01 07:53:29 +00:00
break;
case ']':
case '\t':
case ' ':
case ',':
jj = 0;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( !StartData || !HasData )
break;
2011-09-17 15:31:21 +00:00
aList.push_back( atof( string_num ) );
2008-01-01 07:53:29 +00:00
string_num[jj] = 0;
ii++;
2011-09-17 15:31:21 +00:00
HasData = false;
2008-01-01 07:53:29 +00:00
if( *text == ']' )
{
2011-09-17 15:31:21 +00:00
StartData = false;
2008-01-01 07:53:29 +00:00
}
2008-01-01 07:53:29 +00:00
break;
default:
if( !StartData )
break;
2011-09-17 15:31:21 +00:00
if( jj >= sizeof( string_num ) )
2008-01-01 07:53:29 +00:00
break;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
string_num[jj] = *text;
jj++;
string_num[jj] = 0;
2011-09-17 15:31:21 +00:00
HasData = true;
2008-01-01 07:53:29 +00:00
break;
}
text++;
}
}
2007-05-06 16:03:28 +00:00
}
2008-01-01 07:53:29 +00:00
2008-12-06 08:21:54 +00:00
int S3D_MASTER::ReadGeometry( FILE* file, int* LineNum )
2007-05-06 16:03:28 +00:00
{
2008-01-01 07:53:29 +00:00
char line[1024], buffer[1024], * text;
int err = 1;
std::vector< double > points;
std::vector< double > list;
2008-01-01 07:53:29 +00:00
while( GetLine( file, line, LineNum, 512 ) )
{
strcpy( buffer, line );
text = strtok( buffer, " \t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( *text == '}' )
{
err = 0;
break;
2008-01-01 07:53:29 +00:00
}
if( stricmp( text, "normalPerVertex" ) == 0 )
{
text = strtok( NULL, " ,\t\n\r" );
2011-09-17 15:31:21 +00:00
if( stricmp( text, "true" ) == 0 )
2008-01-01 07:53:29 +00:00
{
}
else
{
}
continue;
}
if( stricmp( text, "colorPerVertex" ) == 0 )
{
text = strtok( NULL, " ,\t\n\r" );
2011-09-17 15:31:21 +00:00
if( stricmp( text, "true" ) == 0 )
2008-01-01 07:53:29 +00:00
{
}
else
{
}
continue;
}
2007-05-06 16:03:28 +00:00
2008-01-01 07:53:29 +00:00
if( stricmp( text, "normal" ) == 0 )
{
ReadCoordsList( file, line, list, LineNum );
list.clear();
2008-01-01 07:53:29 +00:00
continue;
}
2008-01-01 07:53:29 +00:00
if( stricmp( text, "normalIndex" ) == 0 )
{
while( GetLine( file, line, LineNum, 512 ) )
{
text = strtok( line, " ,\t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
while( text )
{
if( *text == ']' )
break;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
text = strtok( NULL, " ,\t\n\r" );
}
if( text && (*text == ']') )
break;
}
continue;
}
if( stricmp( text, "color" ) == 0 )
{
ReadCoordsList( file, line, list, LineNum );
list.clear();
2008-01-01 07:53:29 +00:00
continue;
}
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( stricmp( text, "colorIndex" ) == 0 )
{
while( GetLine( file, line, LineNum, 512 ) )
{
text = strtok( line, " ,\t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
while( text )
{
if( *text == ']' )
break;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
text = strtok( NULL, " ,\t\n\r" );
}
if( text && (*text == ']') )
break;
}
continue;
}
if( stricmp( text, "coord" ) == 0 )
{
ReadCoordsList( file, line, points, LineNum );
2008-01-01 07:53:29 +00:00
}
else if( stricmp( text, "coordIndex" ) == 0 )
{
if( points.size() < 3 || points.size() % 3 != 0 )
{
wxLogError( wxT( "3D geometry read error <%s> at line %d." ),
GetChars( FROM_UTF8( text ) ), *LineNum );
err = 1;
break;
}
std::vector< int > coordIndex;
std::vector< S3D_Vertex > vertices;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
while( GetLine( file, line, LineNum, 512 ) )
{
int jj;
2008-01-01 07:53:29 +00:00
text = strtok( line, " ,\t\n\r" );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
while( text )
{
if( *text == ']' )
break;
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
jj = atoi( text );
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
if( jj < 0 )
{
for( jj = 0; jj < (int) coordIndex.size(); jj++ )
2008-01-01 07:53:29 +00:00
{
int kk = coordIndex[jj] * 3;
2011-11-26 20:02:59 +00:00
if( (kk < 0) || ((kk + 3) > (int)points.size()) )
{
wxLogError( wxT( "3D geometry index read error <%s> at line %d." ),
GetChars( FROM_UTF8( text ) ), *LineNum );
err = 1;
break;
}
S3D_Vertex vertex;
vertex.x = points[kk];
vertex.y = points[kk + 1];
vertex.z = points[kk + 2];
vertices.push_back( vertex );
2008-01-01 07:53:29 +00:00
}
Set_Object_Coords( vertices );
Set_Object_Data( vertices );
vertices.clear();
coordIndex.clear();
2008-01-01 07:53:29 +00:00
}
else
{
coordIndex.push_back( jj );
2008-01-01 07:53:29 +00:00
}
2011-09-17 15:31:21 +00:00
2008-01-01 07:53:29 +00:00
text = strtok( NULL, " ,\t\n\r" );
}
if( text && (*text == ']') )
break;
}
}
else
{
wxLogError( wxT( "3D geometry read error <%s> at line %d." ),
GetChars( FROM_UTF8( text ) ), *LineNum );
err = 1;
2008-01-01 07:53:29 +00:00
break;
}
}
return err;
}
2007-05-06 16:03:28 +00:00
2011-09-17 15:31:21 +00:00
int Struct3D_Shape::ReadData( FILE* file, int* LineNum )
2007-05-06 16:03:28 +00:00
{
2008-01-01 07:53:29 +00:00
char line[512];
2007-05-06 16:03:28 +00:00
2008-01-01 07:53:29 +00:00
while( GetLine( file, line, LineNum, 512 ) )
{
}
2007-05-06 16:03:28 +00:00
2008-01-01 07:53:29 +00:00
return -1;
2007-05-06 16:03:28 +00:00
}