WIP on reading SketchUp files with materials

parent 85fbb26e
......@@ -19,6 +19,10 @@
#ifndef IW_ITA_GEO_ACOUSTIC_MATERIAL
#define IW_ITA_GEO_ACOUSTIC_MATERIAL
#ifdef THIRD_OCTAVE
#undef THIRD_OCTAVE
#endif
// ITAGeo includes
#include <ITAGeoDefinitions.h>
......
......@@ -85,7 +85,7 @@ namespace ITAGeo
protected:
std::string m_sModelName; //!< Model name
ITAGeo::CMaterialManager* m_pAssociatedMaterialManager; //!< Associated material manager (where to add materials during load)
ITAGeo::CMaterialManager* m_pMaterialManager; //!< Associated material manager (where to add materials during load)
/* @todo check if removal ok
//friend class ITAGeo::CSketchUpModel;
......
#include <ITAGeoHalfedgeMeshModel.h>
#include <ITAGeoAcousticMaterial.h>
#include <ITAException.h>
......@@ -21,6 +23,7 @@ typedef OpenMesh::PolyMesh_ArrayKernelT<> CITAMesh;
// STL
#include <iosfwd>
#include <map>
//! ITAMesh pointer wrapper
......@@ -28,17 +31,15 @@ struct CITAMeshPtr
{
CITAMesh* pTopLevelMesh; //!< Mesh model pointer
};
OpenMesh::MPropHandleT< std::map< int, std::string > > tTextureMap;
ITAGeo::CHalfedgeMeshModel::CHalfedgeMeshModel()
: m_prModelMeshData( new CITAMeshPtr )
{
m_prModelMeshData->pTopLevelMesh = new CITAMesh;
/* Add acoustic properties here?
m_prModelMeshData->pTopLevelMesh->add_property( tAcousticEdgeProp );
m_prModelMeshData->pTopLevelMesh->add_property( tAcousticFaceProp );
m_prModelMeshData->pTopLevelMesh->add_property( tAcousticVertexProp );
*/
AddAcousticFacePropertyToMesh( m_prModelMeshData->pTopLevelMesh );
m_prModelMeshData->pTopLevelMesh->request_face_texture_index();
m_prModelMeshData->pTopLevelMesh->add_property( tTextureMap, "TextureMapping" );
}
ITAGeo::CHalfedgeMeshModel::~CHalfedgeMeshModel()
......@@ -75,6 +76,24 @@ bool ITAGeo::CHalfedgeMeshModel::Load( const std::string& sSKPFilePath )
bool bSuccess = OpenMesh::IO::read_mesh( *( m_prModelMeshData->pTopLevelMesh ), oFile.GetName() );
// Enrich acoustic material
std::map< int, std::string > mTextures = m_prModelMeshData->pTopLevelMesh->property( tTextureMap );
CITAMesh* pMesh = static_cast< CITAMesh* >( m_prModelMeshData->pTopLevelMesh );
CITAMesh::FaceIter f_it = pMesh->faces_begin();
while( f_it != pMesh->faces_end() )
{
CITAMesh::FaceHandle hFace( *f_it++ );
if( pMesh->has_face_texture_index() )
{
int iTextureIndex = pMesh->texture_index( hFace );
std::string sMaterialName = mTextures[ iTextureIndex ];
if( m_pMaterialManager && m_pMaterialManager->HasMaterial( sMaterialName ) )
AssignFaceMaterial( pMesh, m_pMaterialManager->GetMaterial( sMaterialName ), hFace );
}
}
return bSuccess;
}
......@@ -165,7 +184,7 @@ bool ITAGeo::CHalfedgeMeshModel::SetFaceAcousticMaterial( int iFaceID, IAcoustic
CITAMesh::FaceHandle hFace( *f_it++ );
if( hFace.idx() == iFaceID )
{
return AddFaceMaterial( pMesh, pMaterial, hFace );
return AssignFaceMaterial( pMesh, pMaterial, hFace );
}
}
......@@ -197,7 +216,7 @@ const CITAMeshPtr* ITAGeo::CHalfedgeMeshModel::GetMesh() const
void ITAGeo::CHalfedgeMeshModel::GetBoundingBoxAxesAligned( VistaVector3D& v3Min, VistaVector3D& v3Max ) const
{
CITAMesh* pMesh = static_cast< CITAMesh* >( m_prModelMeshData->pTopLevelMesh );
if( pMesh->n_vertices() == 0 )
{
v3Max.SetToZeroVector();
......@@ -206,12 +225,12 @@ void ITAGeo::CHalfedgeMeshModel::GetBoundingBoxAxesAligned( VistaVector3D& v3Min
}
CITAMesh::VertexIter v_it = pMesh->vertices_begin();
CITAMesh::VertexHandle hFirstVertex( *v_it++ );
VistaVector3D v3FirstVertexPoint( pMesh->point( hFirstVertex ).data() );
v3Max = v3FirstVertexPoint;
v3Min = v3FirstVertexPoint;
while( v_it != pMesh->vertices_end() )
{
CITAMesh::VertexHandle hVertex( *v_it++ );
......
......@@ -21,6 +21,7 @@
#include <SketchUpAPI/model/drawing_element.h>
#include <SketchUpAPI/model/edge.h>
#include <SketchUpAPI/model/entities.h>
#include <SketchUpAPI/model/material.h>
#include <SketchUpAPI/model/model.h>
#include <SketchUpAPI/model/face.h>
#include <SketchUpAPI/model/group.h>
......@@ -34,6 +35,10 @@
#include <VistaMath/VistaGeometries.h>
#include <VistaTools/VistaFileSystemFile.h>
// STL
#include <list>
typedef OpenMesh::PolyMesh_ArrayKernelT<> CITAMesh;
CITAGeoHalfedgeSketchUpReader::CITAGeoHalfedgeSketchUpReader()
......@@ -48,6 +53,7 @@ CITAGeoHalfedgeSketchUpReader::~CITAGeoHalfedgeSketchUpReader()
bool CITAGeoHalfedgeSketchUpReader::read( const std::string& sFilePath, OpenMesh::IO::BaseImporter& oBaseImporter, OpenMesh::IO::Options& )
{
SUInitialize();
SUResult sur;
......@@ -97,10 +103,31 @@ bool CITAGeoHalfedgeSketchUpReader::read( const std::string& sFilePath, OpenMesh
// Iterate over faces and translate into MyMesh representation
SUPoint3D vSUPoint3D;
std::map< const void*, CITAMesh::VertexHandle > mTopLevelVertices;
std::map< std::string, int > mTextures;
int iTextureIndex = 0;
for( size_t n = 0; n < nNumFaces; n++ )
{
const SUFaceRef& rFace( vrFaces[ n ] );
// Material / textures
SUMaterialRef rMaterial = SU_INVALID;
if( ( sur = SUFaceGetFrontMaterial( rFace, &rMaterial ) ) != SU_ERROR_NONE )
return sur;
std::string sMaterialName;
SUStringRef rMaterialName = SU_INVALID;
SUStringCreate( &rMaterialName );
if( SUMaterialGetName( rMaterial, &rMaterialName ) == SU_ERROR_NONE )
sMaterialName = SUStringToStdString( rMaterialName );
if( mTextures.find( sMaterialName ) == mTextures.end() )
mTextures[ sMaterialName ] = iTextureIndex++;
// Geometry
size_t nNumEdges = 0;
sur = SUFaceGetNumEdges( rFace, &nNumEdges );
......@@ -137,9 +164,15 @@ bool CITAGeoHalfedgeSketchUpReader::read( const std::string& sFilePath, OpenMesh
vhVertices[ m ] = hVertex;
}
oBaseImporter.add_face( vhVertices );
CITAMesh::FaceHandle hFace = oBaseImporter.add_face( vhVertices );
oBaseImporter.set_face_texindex( hFace, mTextures[ sMaterialName ] );
}
// Add material information
for( auto a : mTextures )
oBaseImporter.add_texture_information( a.second, a.first );
// Release
if( ( sur = SUModelRelease( &rModel ) ) != SU_ERROR_NONE )
ITA_EXCEPT1( INVALID_PARAMETER, "Encountered SU error code " + std::to_string( long double( sur ) ) );
......
......@@ -11,7 +11,7 @@
void ITAGeo::CModelBase::SetMaterialManager( CMaterialManager* pMaterialManager )
{
m_pAssociatedMaterialManager = pMaterialManager;
m_pMaterialManager = pMaterialManager;
}
std::ostream& operator<<( std::ostream& oIn, const ITAGeo::CModelBase& oModel )
......
......@@ -25,7 +25,7 @@ void AddAcousticFacePropertyToMesh( CITAMesh* pMesh )
pMesh->add_property( tAcousticFaceProp );
}
bool AddFaceMaterial( CITAMesh* pMesh, ITAGeo::IAcousticMaterial* pMaterial, CITAMesh::FaceHandle hFace )
bool AssignFaceMaterial( CITAMesh* pMesh, const ITAGeo::IAcousticMaterial* pMaterial, CITAMesh::FaceHandle hFace )
{
assert( pMesh );
assert( pMaterial );
......
......@@ -43,7 +43,7 @@ typedef OpenMesh::PolyMesh_ArrayKernelT<> CITAMesh;
void AddAcousticFacePropertyToMesh( CITAMesh* pMesh );
//! Adds acoustic material to a mesh face
bool AddFaceMaterial( CITAMesh* pMesh, ITAGeo::IAcousticMaterial* pMaterial, CITAMesh::FaceHandle hFace );
bool AssignFaceMaterial( CITAMesh* pMesh, const ITAGeo::IAcousticMaterial* pMaterial, CITAMesh::FaceHandle hFace );
//! Converts a color to a one-dimensional acoustic material
bool SUColorToOneDimAcousticMaterial( ITAGeo::CScalarMaterial* pMaterial, const SUColor& tColor );
......
......@@ -131,7 +131,7 @@ bool ITAGeo::CSketchUpModel::Load( const std::string& sSKPFilePath )
// Top-level mesh (do not explode connected groups and component instances here)
CITAMesh* pTLMesh = m_pTopLevelMeshModel->GetMesh()->pTopLevelMesh;
SU_EXC( SUEntitiesToITAMesh( rModelEntities, pTLMesh, false, m_pAssociatedMaterialManager ) );
SU_EXC( SUEntitiesToITAMesh( rModelEntities, pTLMesh, false, m_pMaterialManager ) );
// Top-level groups (exploded)
......@@ -165,7 +165,7 @@ bool ITAGeo::CSketchUpModel::Load( const std::string& sSKPFilePath )
// Recursion (explode connected groups and component instances, and add to current group mesh)
CITAMesh* pGroupMesh = pHEMeshModel->GetMesh()->pTopLevelMesh;
SU_EXC( SUEntitiesToITAMesh( rGroupEntities, pGroupMesh, true, vtTransform, m_pAssociatedMaterialManager ) );
SU_EXC( SUEntitiesToITAMesh( rGroupEntities, pGroupMesh, true, vtTransform, m_pMaterialManager ) );
}
}
......
......@@ -20,6 +20,7 @@
*/
#include <ITAGeoHalfedgeMeshModel.h>
#include <ITAGeoMaterialManager.h>
#include <ITAException.h>
#include <ITAStopWatch.h>
......@@ -36,15 +37,19 @@ using namespace ITAGeo;
int main( int, char** )
{
string sSKPFileName = "ExportReceiverAndSources";
string sInExtension = "ac";
string sInFileName = "simple_room";
string sInExtension = "skp";
CMaterialDirectory oMD( "./" );
CHalfedgeMeshModel oHEModel;
if( oHEModel.Load( sSKPFileName + "." + sInExtension ) )
cout << "Sucessfully loaded '" << sSKPFileName << "' as an halfedge model" << endl;
oHEModel.SetMaterialManager( &oMD );
if( oHEModel.Load( sInFileName + "." + sInExtension ) )
cout << "Sucessfully loaded '" << sInFileName << "' as an halfedge model" << endl;
if( oHEModel.Store( sSKPFileName + ".skp" ) )
cout << "Sucessfully stored file '" << sSKPFileName << ".skp' from halfedge model" << endl;
if( oHEModel.Store( sInFileName + "_out.skp" ) )
cout << "Sucessfully stored file '" << sInFileName << ".skp' from halfedge model" << endl;
return 0;
}
......@@ -32,7 +32,7 @@ int main( int, char** )
{
std::string sBasePath = "./";
string sSuffix = "skp";
std::string sModelName = "GroupCompTest";
std::string sModelName = "SimpleRoom";
VistaFileSystemFile oFile( sBasePath + sModelName + "." + sSuffix );
CMaterialDirectory oMatDir( "." );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment