2015-05-19 03:48:29 +00:00
/*
Open Asset Import Library ( assimp )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2020-01-20 13:53:12 +00:00
Copyright ( c ) 2006 - 2020 , assimp team
2018-01-28 18:42:05 +00:00
2017-05-09 17:57:36 +00:00
2015-05-19 03:48:29 +00:00
All rights reserved .
2015-05-19 03:52:10 +00:00
Redistribution and use of this software in source and binary forms ,
with or without modification , are permitted provided that the
2015-05-19 03:48:29 +00:00
following conditions are met :
* Redistributions of source code must retain the above
copyright notice , this list of conditions and the
following disclaimer .
* Redistributions in binary form must reproduce the above
copyright notice , this list of conditions and the
following disclaimer in the documentation and / or other
materials provided with the distribution .
* Neither the name of the assimp team , nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team .
2015-05-19 03:52:10 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
" AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
2015-05-19 03:48:29 +00:00
LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
2015-05-19 03:48:29 +00:00
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
2015-05-19 03:52:10 +00:00
SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
2015-05-19 03:48:29 +00:00
LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
2015-05-19 03:52:10 +00:00
DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
2015-05-19 03:48:29 +00:00
OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
/** @file IFCLoad.cpp
* @ brief Implementation of the Industry Foundation Classes loader .
*/
# ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
# include <iterator>
# include <limits>
2016-04-05 22:17:21 +00:00
# include <tuple>
2015-05-19 03:48:29 +00:00
# ifndef ASSIMP_BUILD_NO_COMPRESSED_IFC
2020-03-01 12:15:45 +00:00
# ifdef ASSIMP_USE_HUNTER
# include <minizip/unzip.h>
# else
# include <unzip.h>
# endif
2015-05-19 03:48:29 +00:00
# endif
2018-11-25 11:00:51 +00:00
# include "../STEPParser/STEPFileReader.h"
2020-03-01 12:15:45 +00:00
# include "IFCLoader.h"
2015-05-19 03:48:29 +00:00
# include "IFCUtil.h"
2018-01-16 03:14:44 +00:00
# include <assimp/MemoryIOWrapper.h>
2020-03-01 12:15:45 +00:00
# include <assimp/importerdesc.h>
2016-06-06 20:04:29 +00:00
# include <assimp/scene.h>
# include <assimp/Importer.hpp>
2015-05-19 03:48:29 +00:00
namespace Assimp {
2020-03-01 12:15:45 +00:00
template < >
const char * LogFunctions < IFCImporter > : : Prefix ( ) {
static auto prefix = " IFC: " ;
return prefix ;
2015-05-19 03:48:29 +00:00
}
2020-03-01 12:15:45 +00:00
} // namespace Assimp
2015-05-19 03:48:29 +00:00
using namespace Assimp ;
using namespace Assimp : : Formatter ;
using namespace Assimp : : IFC ;
/* DO NOT REMOVE this comment block. The genentitylist.sh script
* just looks for names adhering to the IfcSomething naming scheme
* and includes all matches in the whitelist for code - generation . Thus ,
* all entity classes that are only indirectly referenced need to be
* mentioned explicitly .
IfcRepresentationMap
IfcProductRepresentation
IfcUnitAssignment
IfcClosedShell
IfcDoor
*/
namespace {
// forward declarations
2020-03-01 12:15:45 +00:00
void SetUnits ( ConversionData & conv ) ;
void SetCoordinateSpace ( ConversionData & conv ) ;
void ProcessSpatialStructures ( ConversionData & conv ) ;
void MakeTreeRelative ( ConversionData & conv ) ;
void ConvertUnit ( const : : Assimp : : STEP : : EXPRESS : : DataType & dt , ConversionData & conv ) ;
2015-05-19 03:48:29 +00:00
2020-03-01 12:15:45 +00:00
} // namespace
2015-05-19 03:48:29 +00:00
static const aiImporterDesc desc = {
2015-05-19 03:57:13 +00:00
" Industry Foundation Classes (IFC) Importer " ,
" " ,
" " ,
" " ,
aiImporterFlags_SupportBinaryFlavour ,
0 ,
0 ,
0 ,
0 ,
2017-02-27 19:11:59 +00:00
" ifc ifczip stp "
2015-05-19 03:48:29 +00:00
} ;
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
2020-03-01 12:15:45 +00:00
IFCImporter : : IFCImporter ( ) { }
2015-05-19 03:48:29 +00:00
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Destructor, private as well
2020-03-01 12:15:45 +00:00
IFCImporter : : ~ IFCImporter ( ) {
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Returns whether the class can handle the format of the given file.
2020-03-01 12:15:45 +00:00
bool IFCImporter : : CanRead ( const std : : string & pFile , IOSystem * pIOHandler , bool checkSig ) const {
const std : : string & extension = GetExtension ( pFile ) ;
if ( extension = = " ifc " | | extension = = " ifczip " ) {
2015-05-19 03:57:13 +00:00
return true ;
2020-03-01 12:15:45 +00:00
} else if ( ( ! extension . length ( ) | | checkSig ) & & pIOHandler ) {
2015-05-19 03:57:13 +00:00
// note: this is the common identification for STEP-encoded files, so
// it is only unambiguous as long as we don't support any further
// file formats with STEP as their encoding.
2020-03-01 12:15:45 +00:00
const char * tokens [ ] = { " ISO-10303-21 " } ;
const bool found ( SearchFileHeaderForToken ( pIOHandler , pFile , tokens , 1 ) ) ;
2018-02-27 17:30:36 +00:00
return found ;
2015-05-19 03:57:13 +00:00
}
return false ;
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
// List all extensions handled by this loader
2020-03-01 12:15:45 +00:00
const aiImporterDesc * IFCImporter : : GetInfo ( ) const {
2015-05-19 03:57:13 +00:00
return & desc ;
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
// Setup configuration properties for the loader
2020-03-01 12:15:45 +00:00
void IFCImporter : : SetupProperties ( const Importer * pImp ) {
settings . skipSpaceRepresentations = pImp - > GetPropertyBool ( AI_CONFIG_IMPORT_IFC_SKIP_SPACE_REPRESENTATIONS , true ) ;
settings . useCustomTriangulation = pImp - > GetPropertyBool ( AI_CONFIG_IMPORT_IFC_CUSTOM_TRIANGULATION , true ) ;
settings . conicSamplingAngle = std : : min ( std : : max ( ( float ) pImp - > GetPropertyFloat ( AI_CONFIG_IMPORT_IFC_SMOOTHING_ANGLE , AI_IMPORT_IFC_DEFAULT_SMOOTHING_ANGLE ) , 5.0f ) , 120.0f ) ;
settings . cylindricalTessellation = std : : min ( std : : max ( pImp - > GetPropertyInteger ( AI_CONFIG_IMPORT_IFC_CYLINDRICAL_TESSELLATION , AI_IMPORT_IFC_DEFAULT_CYLINDRICAL_TESSELLATION ) , 3 ) , 180 ) ;
settings . skipAnnotations = true ;
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Imports the given file into the given scene structure.
2020-03-01 12:15:45 +00:00
void IFCImporter : : InternReadFile ( const std : : string & pFile , aiScene * pScene , IOSystem * pIOHandler ) {
2016-04-05 21:23:53 +00:00
std : : shared_ptr < IOStream > stream ( pIOHandler - > Open ( pFile ) ) ;
2015-05-19 03:57:13 +00:00
if ( ! stream ) {
ThrowException ( " Could not open file for reading " ) ;
}
2015-05-19 03:48:29 +00:00
2015-05-19 03:57:13 +00:00
// if this is a ifczip file, decompress its contents first
2020-03-01 12:15:45 +00:00
if ( GetExtension ( pFile ) = = " ifczip " ) {
2015-05-19 03:48:29 +00:00
# ifndef ASSIMP_BUILD_NO_COMPRESSED_IFC
2020-03-01 12:15:45 +00:00
unzFile zip = unzOpen ( pFile . c_str ( ) ) ;
if ( zip = = NULL ) {
2015-05-19 03:57:13 +00:00
ThrowException ( " Could not open ifczip file for reading, unzip failed " ) ;
}
// chop 'zip' postfix
2020-03-01 12:15:45 +00:00
std : : string fileName = pFile . substr ( 0 , pFile . length ( ) - 3 ) ;
2015-05-19 03:57:13 +00:00
std : : string : : size_type s = pFile . find_last_of ( ' \\ ' ) ;
2020-03-01 12:15:45 +00:00
if ( s = = std : : string : : npos ) {
2015-05-19 03:57:13 +00:00
s = pFile . find_last_of ( ' / ' ) ;
}
2020-03-01 12:15:45 +00:00
if ( s ! = std : : string : : npos ) {
fileName = fileName . substr ( s + 1 ) ;
2015-05-19 03:57:13 +00:00
}
// search file (same name as the IFCZIP except for the file extension) and place file pointer there
2020-03-01 12:15:45 +00:00
if ( UNZ_OK = = unzGoToFirstFile ( zip ) ) {
2015-05-19 03:57:13 +00:00
do {
// get file size, etc.
unz_file_info fileInfo ;
char filename [ 256 ] ;
2020-03-01 12:15:45 +00:00
unzGetCurrentFileInfo ( zip , & fileInfo , filename , sizeof ( filename ) , 0 , 0 , 0 , 0 ) ;
2015-05-19 03:57:13 +00:00
if ( GetExtension ( filename ) ! = " ifc " ) {
continue ;
}
2020-03-01 12:15:45 +00:00
uint8_t * buff = new uint8_t [ fileInfo . uncompressed_size ] ;
2015-05-19 03:57:13 +00:00
LogInfo ( " Decompressing IFCZIP file " ) ;
2019-01-06 18:37:30 +00:00
unzOpenCurrentFile ( zip ) ;
size_t total = 0 ;
int read = 0 ;
do {
int bufferSize = fileInfo . uncompressed_size < INT16_MAX ? fileInfo . uncompressed_size : INT16_MAX ;
2020-03-01 12:15:45 +00:00
void * buffer = malloc ( bufferSize ) ;
2019-01-06 18:37:30 +00:00
read = unzReadCurrentFile ( zip , buffer , bufferSize ) ;
if ( read > 0 ) {
2020-03-01 12:15:45 +00:00
memcpy ( ( char * ) buff + total , buffer , read ) ;
2019-01-06 18:37:30 +00:00
total + = read ;
}
free ( buffer ) ;
} while ( read > 0 ) ;
2015-05-19 03:57:13 +00:00
size_t filesize = fileInfo . uncompressed_size ;
2020-03-01 12:15:45 +00:00
if ( total = = 0 | | size_t ( total ) ! = filesize ) {
2015-05-19 03:57:13 +00:00
delete [ ] buff ;
ThrowException ( " Failed to decompress IFC ZIP file " ) ;
}
2020-03-01 12:15:45 +00:00
unzCloseCurrentFile ( zip ) ;
stream . reset ( new MemoryIOStream ( buff , fileInfo . uncompressed_size , true ) ) ;
2015-05-19 03:57:13 +00:00
if ( unzGoToNextFile ( zip ) = = UNZ_END_OF_LIST_OF_FILE ) {
ThrowException ( " Found no IFC file member in IFCZIP file (1) " ) ;
}
2020-03-01 12:15:45 +00:00
break ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
} while ( true ) ;
} else {
2015-05-19 03:57:13 +00:00
ThrowException ( " Found no IFC file member in IFCZIP file (2) " ) ;
}
unzClose ( zip ) ;
2015-05-19 03:48:29 +00:00
# else
2015-05-19 03:57:13 +00:00
ThrowException ( " Could not open ifczip file for reading, assimp was built without ifczip support " ) ;
2015-05-19 03:48:29 +00:00
# endif
2015-05-19 03:57:13 +00:00
}
2016-04-05 21:23:53 +00:00
std : : unique_ptr < STEP : : DB > db ( STEP : : ReadFileHeader ( stream ) ) ;
2020-03-01 12:15:45 +00:00
const STEP : : HeaderInfo & head = static_cast < const STEP : : DB & > ( * db ) . GetHeader ( ) ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if ( ! head . fileSchema . size ( ) | | head . fileSchema . substr ( 0 , 3 ) ! = " IFC " ) {
2015-05-19 03:57:13 +00:00
ThrowException ( " Unrecognized file schema: " + head . fileSchema ) ;
}
if ( ! DefaultLogger : : isNullLogger ( ) ) {
LogDebug ( " File schema is \' " + head . fileSchema + ' \' ' ) ;
if ( head . timestamp . length ( ) ) {
LogDebug ( " Timestamp \' " + head . timestamp + ' \' ' ) ;
}
if ( head . app . length ( ) ) {
2020-03-01 12:15:45 +00:00
LogDebug ( " Application/Exporter identline is \' " + head . app + ' \' ' ) ;
2015-05-19 03:57:13 +00:00
}
}
// obtain a copy of the machine-generated IFC scheme
2018-01-13 09:27:45 +00:00
: : Assimp : : STEP : : EXPRESS : : ConversionSchema schema ;
Schema_2x3 : : GetSchema ( schema ) ;
2015-05-19 03:57:13 +00:00
// tell the reader which entity types to track with special care
2020-03-01 12:15:45 +00:00
static const char * const types_to_track [ ] = {
2015-05-19 03:57:13 +00:00
" ifcsite " , " ifcbuilding " , " ifcproject "
} ;
// tell the reader for which types we need to simulate STEPs reverse indices
2020-03-01 12:15:45 +00:00
static const char * const inverse_indices_to_track [ ] = {
2015-05-19 03:57:13 +00:00
" ifcrelcontainedinspatialstructure " , " ifcrelaggregates " , " ifcrelvoidselement " , " ifcreldefinesbyproperties " , " ifcpropertyset " , " ifcstyleditem "
} ;
// feed the IFC schema into the reader and pre-parse all lines
STEP : : ReadFile ( * db , schema , types_to_track , inverse_indices_to_track ) ;
2020-03-01 12:15:45 +00:00
const STEP : : LazyObject * proj = db - > GetObject ( " ifcproject " ) ;
2015-05-19 03:57:13 +00:00
if ( ! proj ) {
ThrowException ( " missing IfcProject entity " ) ;
}
2020-03-01 12:15:45 +00:00
ConversionData conv ( * db , proj - > To < Schema_2x3 : : IfcProject > ( ) , pScene , settings ) ;
2015-05-19 03:57:13 +00:00
SetUnits ( conv ) ;
SetCoordinateSpace ( conv ) ;
ProcessSpatialStructures ( conv ) ;
MakeTreeRelative ( conv ) ;
2020-03-01 12:15:45 +00:00
// NOTE - this is a stress test for the importer, but it works only
// in a build with no entities disabled. See
// scripts/IFCImporter/CPPGenerator.py
// for more information.
# ifdef ASSIMP_IFC_TEST
db - > EvaluateAll ( ) ;
# endif
2015-05-19 03:57:13 +00:00
// do final data copying
if ( conv . meshes . size ( ) ) {
pScene - > mNumMeshes = static_cast < unsigned int > ( conv . meshes . size ( ) ) ;
2020-03-01 12:15:45 +00:00
pScene - > mMeshes = new aiMesh * [ pScene - > mNumMeshes ] ( ) ;
std : : copy ( conv . meshes . begin ( ) , conv . meshes . end ( ) , pScene - > mMeshes ) ;
2015-05-19 03:57:13 +00:00
// needed to keep the d'tor from burning us
conv . meshes . clear ( ) ;
}
if ( conv . materials . size ( ) ) {
pScene - > mNumMaterials = static_cast < unsigned int > ( conv . materials . size ( ) ) ;
2020-03-01 12:15:45 +00:00
pScene - > mMaterials = new aiMaterial * [ pScene - > mNumMaterials ] ( ) ;
std : : copy ( conv . materials . begin ( ) , conv . materials . end ( ) , pScene - > mMaterials ) ;
2015-05-19 03:57:13 +00:00
// needed to keep the d'tor from burning us
conv . materials . clear ( ) ;
}
// apply world coordinate system (which includes the scaling to convert to meters and a -90 degrees rotation around x)
aiMatrix4x4 scale , rot ;
2020-03-01 12:15:45 +00:00
aiMatrix4x4 : : Scaling ( static_cast < aiVector3D > ( IfcVector3 ( conv . len_scale ) ) , scale ) ;
aiMatrix4x4 : : RotationX ( - AI_MATH_HALF_PI_F , rot ) ;
2015-05-19 03:57:13 +00:00
pScene - > mRootNode - > mTransformation = rot * scale * conv . wcs * pScene - > mRootNode - > mTransformation ;
// this must be last because objects are evaluated lazily as we process them
2020-03-01 12:15:45 +00:00
if ( ! DefaultLogger : : isNullLogger ( ) ) {
LogDebug ( ( Formatter : : format ( ) , " STEP: evaluated " , db - > GetEvaluatedObjectCount ( ) , " object records " ) ) ;
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:48:29 +00:00
}
namespace {
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ConvertUnit ( const Schema_2x3 : : IfcNamedUnit & unit , ConversionData & conv ) {
if ( const Schema_2x3 : : IfcSIUnit * const si = unit . ToPtr < Schema_2x3 : : IfcSIUnit > ( ) ) {
if ( si - > UnitType = = " LENGTHUNIT " ) {
2015-05-19 03:57:13 +00:00
conv . len_scale = si - > Prefix ? ConvertSIPrefix ( si - > Prefix ) : 1.f ;
IFCImporter : : LogDebug ( " got units used for lengths " ) ;
}
2020-03-01 12:15:45 +00:00
if ( si - > UnitType = = " PLANEANGLEUNIT " ) {
2015-05-19 03:57:13 +00:00
if ( si - > Name ! = " RADIAN " ) {
IFCImporter : : LogWarn ( " expected base unit for angles to be radian " ) ;
}
}
2020-03-01 12:15:45 +00:00
} else if ( const Schema_2x3 : : IfcConversionBasedUnit * const convu = unit . ToPtr < Schema_2x3 : : IfcConversionBasedUnit > ( ) ) {
if ( convu - > UnitType = = " PLANEANGLEUNIT " ) {
2015-05-19 03:57:13 +00:00
try {
2018-01-13 09:27:45 +00:00
conv . angle_scale = convu - > ConversionFactor - > ValueComponent - > To < : : Assimp : : STEP : : EXPRESS : : REAL > ( ) ;
2020-03-01 12:15:45 +00:00
ConvertUnit ( * convu - > ConversionFactor - > UnitComponent , conv ) ;
2015-05-19 03:57:13 +00:00
IFCImporter : : LogDebug ( " got units used for angles " ) ;
2020-03-01 12:15:45 +00:00
} catch ( std : : bad_cast & ) {
2015-05-19 03:57:13 +00:00
IFCImporter : : LogError ( " skipping unknown IfcConversionBasedUnit.ValueComponent entry - expected REAL " ) ;
}
}
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ConvertUnit ( const : : Assimp : : STEP : : EXPRESS : : DataType & dt , ConversionData & conv ) {
2015-05-19 03:57:13 +00:00
try {
2020-03-01 12:15:45 +00:00
const : : Assimp : : STEP : : EXPRESS : : ENTITY & e = dt . To < : : Assimp : : STEP : : EXPRESS : : ENTITY > ( ) ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
const Schema_2x3 : : IfcNamedUnit & unit = e . ResolveSelect < Schema_2x3 : : IfcNamedUnit > ( conv . db ) ;
if ( unit . UnitType ! = " LENGTHUNIT " & & unit . UnitType ! = " PLANEANGLEUNIT " ) {
2015-05-19 03:57:13 +00:00
return ;
}
2020-03-01 12:15:45 +00:00
ConvertUnit ( unit , conv ) ;
} catch ( std : : bad_cast & ) {
2015-05-19 03:57:13 +00:00
// not entity, somehow
IFCImporter : : LogError ( " skipping unknown IfcUnit entry - expected entity " ) ;
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void SetUnits ( ConversionData & conv ) {
2015-05-19 03:57:13 +00:00
// see if we can determine the coordinate space used to express.
2020-03-01 12:15:45 +00:00
for ( size_t i = 0 ; i < conv . proj . UnitsInContext - > Units . size ( ) ; + + i ) {
ConvertUnit ( * conv . proj . UnitsInContext - > Units [ i ] , conv ) ;
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void SetCoordinateSpace ( ConversionData & conv ) {
const Schema_2x3 : : IfcRepresentationContext * fav = NULL ;
for ( const Schema_2x3 : : IfcRepresentationContext & v : conv . proj . RepresentationContexts ) {
2015-05-19 03:57:13 +00:00
fav = & v ;
// Model should be the most suitable type of context, hence ignore the others
if ( v . ContextType & & v . ContextType . Get ( ) = = " Model " ) {
break ;
}
}
if ( fav ) {
2020-03-01 12:15:45 +00:00
if ( const Schema_2x3 : : IfcGeometricRepresentationContext * const geo = fav - > ToPtr < Schema_2x3 : : IfcGeometricRepresentationContext > ( ) ) {
2015-05-19 03:57:13 +00:00
ConvertAxisPlacement ( conv . wcs , * geo - > WorldCoordinateSystem , conv ) ;
IFCImporter : : LogDebug ( " got world coordinate system " ) ;
}
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ResolveObjectPlacement ( aiMatrix4x4 & m , const Schema_2x3 : : IfcObjectPlacement & place , ConversionData & conv ) {
if ( const Schema_2x3 : : IfcLocalPlacement * const local = place . ToPtr < Schema_2x3 : : IfcLocalPlacement > ( ) ) {
2015-05-19 03:57:13 +00:00
IfcMatrix4 tmp ;
ConvertAxisPlacement ( tmp , * local - > RelativePlacement , conv ) ;
m = static_cast < aiMatrix4x4 > ( tmp ) ;
if ( local - > PlacementRelTo ) {
2020-03-01 12:15:45 +00:00
aiMatrix4x4 tmpM ;
ResolveObjectPlacement ( tmpM , local - > PlacementRelTo . Get ( ) , conv ) ;
m = tmpM * m ;
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
IFCImporter : : LogWarn ( " skipping unknown IfcObjectPlacement entity, type is " + place . GetClassName ( ) ) ;
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
bool ProcessMappedItem ( const Schema_2x3 : : IfcMappedItem & mapped , aiNode * nd_src , std : : vector < aiNode * > & subnodes_src , unsigned int matid , ConversionData & conv ) {
2018-01-13 09:27:45 +00:00
// insert a custom node here, the carthesian transform operator is simply a conventional transformation matrix
2016-04-05 20:56:11 +00:00
std : : unique_ptr < aiNode > nd ( new aiNode ( ) ) ;
2015-05-19 03:57:13 +00:00
nd - > mName . Set ( " IfcMappedItem " ) ;
// handle the Cartesian operator
IfcMatrix4 m ;
ConvertTransformOperator ( m , * mapped . MappingTarget ) ;
IfcMatrix4 msrc ;
2020-03-01 12:15:45 +00:00
ConvertAxisPlacement ( msrc , * mapped . MappingSource - > MappingOrigin , conv ) ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
msrc = m * msrc ;
2015-05-19 03:57:13 +00:00
2018-04-18 09:12:40 +00:00
std : : set < unsigned int > meshes ;
2015-05-19 03:57:13 +00:00
const size_t old_openings = conv . collect_openings ? conv . collect_openings - > size ( ) : 0 ;
if ( conv . apply_openings ) {
IfcMatrix4 minv = msrc ;
minv . Inverse ( ) ;
2020-03-01 12:15:45 +00:00
for ( TempOpening & open : * conv . apply_openings ) {
2015-05-19 03:57:13 +00:00
open . Transform ( minv ) ;
}
}
2020-03-01 12:15:45 +00:00
unsigned int localmatid = ProcessMaterials ( mapped . GetID ( ) , matid , conv , false ) ;
const Schema_2x3 : : IfcRepresentation & repr = mapped . MappingSource - > MappedRepresentation ;
2015-05-19 03:57:13 +00:00
bool got = false ;
2020-03-01 12:15:45 +00:00
for ( const Schema_2x3 : : IfcRepresentationItem & item : repr . Items ) {
if ( ! ProcessRepresentationItem ( item , localmatid , meshes , conv ) ) {
2015-05-19 03:57:13 +00:00
IFCImporter : : LogWarn ( " skipping mapped entity of type " + item . GetClassName ( ) + " , no representations could be generated " ) ;
2020-03-01 12:15:45 +00:00
} else
got = true ;
2015-05-19 03:57:13 +00:00
}
if ( ! got ) {
return false ;
}
2020-03-01 12:15:45 +00:00
AssignAddedMeshes ( meshes , nd . get ( ) , conv ) ;
2015-05-19 03:57:13 +00:00
if ( conv . collect_openings ) {
// if this pass serves us only to collect opening geometry,
// make sure we transform the TempMesh's which we need to
// preserve as well.
2020-03-01 12:15:45 +00:00
if ( const size_t diff = conv . collect_openings - > size ( ) - old_openings ) {
for ( size_t i = 0 ; i < diff ; + + i ) {
( * conv . collect_openings ) [ old_openings + i ] . Transform ( msrc ) ;
2015-05-19 03:57:13 +00:00
}
}
}
2020-03-01 12:15:45 +00:00
nd - > mTransformation = nd_src - > mTransformation * static_cast < aiMatrix4x4 > ( msrc ) ;
2015-05-19 03:57:13 +00:00
subnodes_src . push_back ( nd . release ( ) ) ;
return true ;
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
struct RateRepresentationPredicate {
2020-03-01 12:15:45 +00:00
int Rate ( const Schema_2x3 : : IfcRepresentation * r ) const {
2015-05-19 03:57:13 +00:00
// the smaller, the better
2020-03-01 12:15:45 +00:00
if ( ! r - > RepresentationIdentifier ) {
2015-05-19 03:57:13 +00:00
// neutral choice if no extra information is specified
return 0 ;
}
2020-03-01 12:15:45 +00:00
const std : : string & name = r - > RepresentationIdentifier . Get ( ) ;
2015-05-19 03:57:13 +00:00
if ( name = = " MappedRepresentation " ) {
if ( ! r - > Items . empty ( ) ) {
// take the first item and base our choice on it
2020-03-01 12:15:45 +00:00
const Schema_2x3 : : IfcMappedItem * const m = r - > Items . front ( ) - > ToPtr < Schema_2x3 : : IfcMappedItem > ( ) ;
2015-05-19 03:57:13 +00:00
if ( m ) {
return Rate ( m - > MappingSource - > MappedRepresentation ) ;
}
}
return 100 ;
}
return Rate ( name ) ;
}
2020-03-01 12:15:45 +00:00
int Rate ( const std : : string & r ) const {
2015-05-19 03:57:13 +00:00
if ( r = = " SolidModel " ) {
return - 3 ;
}
// give strong preference to extruded geometry.
if ( r = = " SweptSolid " ) {
return - 10 ;
}
if ( r = = " Clipping " ) {
return - 5 ;
}
// 'Brep' is difficult to get right due to possible voids in the
// polygon boundaries, so take it only if we are forced to (i.e.
// if the only alternative is (non-clipping) boolean operations,
// which are not supported at all).
if ( r = = " Brep " ) {
return - 2 ;
}
// Curves, bounding boxes - those will most likely not be loaded
// as we can't make any use out of this data. So consider them
// last.
if ( r = = " BoundingBox " | | r = = " Curve2D " ) {
return 100 ;
}
return 0 ;
}
2020-03-01 12:15:45 +00:00
bool operator ( ) ( const Schema_2x3 : : IfcRepresentation * a , const Schema_2x3 : : IfcRepresentation * b ) const {
2015-05-19 03:57:13 +00:00
return Rate ( a ) < Rate ( b ) ;
}
2015-05-19 03:48:29 +00:00
} ;
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ProcessProductRepresentation ( const Schema_2x3 : : IfcProduct & el , aiNode * nd , std : : vector < aiNode * > & subnodes , ConversionData & conv ) {
if ( ! el . Representation ) {
2015-05-19 03:57:13 +00:00
return ;
}
// extract Color from metadata, if present
2020-03-01 12:15:45 +00:00
unsigned int matid = ProcessMaterials ( el . GetID ( ) , std : : numeric_limits < uint32_t > : : max ( ) , conv , false ) ;
2018-04-18 09:12:40 +00:00
std : : set < unsigned int > meshes ;
2015-05-19 03:57:13 +00:00
// we want only one representation type, so bring them in a suitable order (i.e try those
// that look as if we could read them quickly at first). This way of reading
// representation is relatively generic and allows the concrete implementations
// for the different representation types to make some sensible choices what
// to load and what not to load.
2020-03-01 12:15:45 +00:00
const STEP : : ListOf < STEP : : Lazy < Schema_2x3 : : IfcRepresentation > , 1 , 0 > & src = el . Representation . Get ( ) - > Representations ;
std : : vector < const Schema_2x3 : : IfcRepresentation * > repr_ordered ( src . size ( ) ) ;
std : : copy ( src . begin ( ) , src . end ( ) , repr_ordered . begin ( ) ) ;
std : : sort ( repr_ordered . begin ( ) , repr_ordered . end ( ) , RateRepresentationPredicate ( ) ) ;
for ( const Schema_2x3 : : IfcRepresentation * repr : repr_ordered ) {
2015-05-19 03:57:13 +00:00
bool res = false ;
2020-03-01 12:15:45 +00:00
for ( const Schema_2x3 : : IfcRepresentationItem & item : repr - > Items ) {
if ( const Schema_2x3 : : IfcMappedItem * const geo = item . ToPtr < Schema_2x3 : : IfcMappedItem > ( ) ) {
res = ProcessMappedItem ( * geo , nd , subnodes , matid , conv ) | | res ;
} else {
res = ProcessRepresentationItem ( item , matid , meshes , conv ) | | res ;
2015-05-19 03:57:13 +00:00
}
}
// if we got something meaningful at this point, skip any further representations
2020-03-01 12:15:45 +00:00
if ( res ) {
2015-05-19 03:57:13 +00:00
break ;
}
}
2020-03-01 12:15:45 +00:00
AssignAddedMeshes ( meshes , nd , conv ) ;
2015-05-19 03:48:29 +00:00
}
typedef std : : map < std : : string , std : : string > Metadata ;
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ProcessMetadata ( const Schema_2x3 : : ListOf < Schema_2x3 : : Lazy < Schema_2x3 : : IfcProperty > , 1 , 0 > & set , ConversionData & conv , Metadata & properties ,
const std : : string & prefix = " " ,
2018-03-20 22:38:08 +00:00
unsigned int nest = 0 ) {
2020-03-01 12:15:45 +00:00
for ( const Schema_2x3 : : IfcProperty & property : set ) {
const std : : string & key = prefix . length ( ) > 0 ? ( prefix + " . " + property . Name ) : property . Name ;
if ( const Schema_2x3 : : IfcPropertySingleValue * const singleValue = property . ToPtr < Schema_2x3 : : IfcPropertySingleValue > ( ) ) {
2015-05-19 03:57:13 +00:00
if ( singleValue - > NominalValue ) {
2020-03-01 12:15:45 +00:00
if ( const : : Assimp : : STEP : : EXPRESS : : STRING * str = singleValue - > NominalValue . Get ( ) - > ToPtr < : : Assimp : : STEP : : EXPRESS : : STRING > ( ) ) {
2015-05-19 03:57:13 +00:00
std : : string value = static_cast < std : : string > ( * str ) ;
2020-03-01 12:15:45 +00:00
properties [ key ] = value ;
} else if ( const : : Assimp : : STEP : : EXPRESS : : REAL * val1 = singleValue - > NominalValue . Get ( ) - > ToPtr < : : Assimp : : STEP : : EXPRESS : : REAL > ( ) ) {
float value = static_cast < float > ( * val1 ) ;
2015-05-19 03:57:13 +00:00
std : : stringstream s ;
s < < value ;
2020-03-01 12:15:45 +00:00
properties [ key ] = s . str ( ) ;
} else if ( const : : Assimp : : STEP : : EXPRESS : : INTEGER * val2 = singleValue - > NominalValue . Get ( ) - > ToPtr < : : Assimp : : STEP : : EXPRESS : : INTEGER > ( ) ) {
int64_t curValue = static_cast < int64_t > ( * val2 ) ;
2015-05-19 03:57:13 +00:00
std : : stringstream s ;
2020-03-01 12:15:45 +00:00
s < < curValue ;
properties [ key ] = s . str ( ) ;
2015-05-19 03:57:13 +00:00
}
}
2020-03-01 12:15:45 +00:00
} else if ( const Schema_2x3 : : IfcPropertyListValue * const listValue = property . ToPtr < Schema_2x3 : : IfcPropertyListValue > ( ) ) {
2015-05-19 03:57:13 +00:00
std : : stringstream ss ;
ss < < " [ " ;
2020-03-01 12:15:45 +00:00
unsigned index = 0 ;
for ( const Schema_2x3 : : IfcValue : : Out & v : listValue - > ListValues ) {
2015-05-19 03:57:13 +00:00
if ( ! v ) continue ;
2020-03-01 12:15:45 +00:00
if ( const : : Assimp : : STEP : : EXPRESS : : STRING * str = v - > ToPtr < : : Assimp : : STEP : : EXPRESS : : STRING > ( ) ) {
2015-05-19 03:57:13 +00:00
std : : string value = static_cast < std : : string > ( * str ) ;
ss < < " ' " < < value < < " ' " ;
2020-03-01 12:15:45 +00:00
} else if ( const : : Assimp : : STEP : : EXPRESS : : REAL * val1 = v - > ToPtr < : : Assimp : : STEP : : EXPRESS : : REAL > ( ) ) {
float value = static_cast < float > ( * val1 ) ;
2015-05-19 03:57:13 +00:00
ss < < value ;
2020-03-01 12:15:45 +00:00
} else if ( const : : Assimp : : STEP : : EXPRESS : : INTEGER * val2 = v - > ToPtr < : : Assimp : : STEP : : EXPRESS : : INTEGER > ( ) ) {
int64_t value = static_cast < int64_t > ( * val2 ) ;
2015-05-19 03:57:13 +00:00
ss < < value ;
}
2020-03-01 12:15:45 +00:00
if ( index + 1 < listValue - > ListValues . size ( ) ) {
2015-05-19 03:57:13 +00:00
ss < < " , " ;
}
index + + ;
}
ss < < " ] " ;
2020-03-01 12:15:45 +00:00
properties [ key ] = ss . str ( ) ;
} else if ( const Schema_2x3 : : IfcComplexProperty * const complexProp = property . ToPtr < Schema_2x3 : : IfcComplexProperty > ( ) ) {
if ( nest > 2 ) { // mostly arbitrary limit to prevent stack overflow vulnerabilities
2015-05-19 03:57:13 +00:00
IFCImporter : : LogError ( " maximum nesting level for IfcComplexProperty reached, skipping this property. " ) ;
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
ProcessMetadata ( complexProp - > HasProperties , conv , properties , key , nest + 1 ) ;
}
2020-03-01 12:15:45 +00:00
} else {
properties [ key ] = " " ;
2015-05-19 03:57:13 +00:00
}
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ProcessMetadata ( uint64_t relDefinesByPropertiesID , ConversionData & conv , Metadata & properties ) {
if ( const Schema_2x3 : : IfcRelDefinesByProperties * const pset = conv . db . GetObject ( relDefinesByPropertiesID ) - > ToPtr < Schema_2x3 : : IfcRelDefinesByProperties > ( ) ) {
if ( const Schema_2x3 : : IfcPropertySet * const set = conv . db . GetObject ( pset - > RelatingPropertyDefinition - > GetID ( ) ) - > ToPtr < Schema_2x3 : : IfcPropertySet > ( ) ) {
2015-05-19 03:57:13 +00:00
ProcessMetadata ( set - > HasProperties , conv , properties ) ;
}
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
aiNode * ProcessSpatialStructure ( aiNode * parent , const Schema_2x3 : : IfcProduct & el , ConversionData & conv ,
std : : vector < TempOpening > * collect_openings = nullptr ) {
const STEP : : DB : : RefMap & refs = conv . db . GetRefs ( ) ;
2015-05-19 03:57:13 +00:00
// skip over space and annotation nodes - usually, these have no meaning in Assimp's context
bool skipGeometry = false ;
2020-03-01 12:15:45 +00:00
if ( conv . settings . skipSpaceRepresentations ) {
if ( el . ToPtr < Schema_2x3 : : IfcSpace > ( ) ) {
2015-05-19 03:57:13 +00:00
IFCImporter : : LogDebug ( " skipping IfcSpace entity due to importer settings " ) ;
skipGeometry = true ;
}
}
2020-03-01 12:15:45 +00:00
if ( conv . settings . skipAnnotations ) {
if ( el . ToPtr < Schema_2x3 : : IfcAnnotation > ( ) ) {
2015-05-19 03:57:13 +00:00
IFCImporter : : LogDebug ( " skipping IfcAnnotation entity due to importer settings " ) ;
2018-12-30 09:36:23 +00:00
return nullptr ;
2015-05-19 03:57:13 +00:00
}
}
// add an output node for this spatial structure
2020-03-01 12:15:45 +00:00
aiNode * nd ( new aiNode ) ;
nd - > mName . Set ( el . GetClassName ( ) + " _ " + ( el . Name ? el . Name . Get ( ) : " Unnamed " ) + " _ " + el . GlobalId ) ;
2015-05-19 03:57:13 +00:00
nd - > mParent = parent ;
conv . already_processed . insert ( el . GetID ( ) ) ;
// check for node metadata
STEP : : DB : : RefMapRange children = refs . equal_range ( el . GetID ( ) ) ;
2020-03-01 12:15:45 +00:00
if ( children . first ! = refs . end ( ) ) {
2015-05-19 03:57:13 +00:00
Metadata properties ;
2020-03-01 12:15:45 +00:00
if ( children . first = = children . second ) {
2015-05-19 03:57:13 +00:00
// handles single property set
ProcessMetadata ( ( * children . first ) . second , conv , properties ) ;
2018-12-30 09:36:23 +00:00
} else {
2015-05-19 03:57:13 +00:00
// handles multiple property sets (currently all property sets are merged,
// which may not be the best solution in the long run)
2020-03-01 12:15:45 +00:00
for ( STEP : : DB : : RefMap : : const_iterator it = children . first ; it ! = children . second ; + + it ) {
2015-05-19 03:57:13 +00:00
ProcessMetadata ( ( * it ) . second , conv , properties ) ;
}
}
if ( ! properties . empty ( ) ) {
2020-03-01 12:15:45 +00:00
aiMetadata * data = aiMetadata : : Alloc ( static_cast < unsigned int > ( properties . size ( ) ) ) ;
unsigned int index ( 0 ) ;
for ( const Metadata : : value_type & kv : properties ) {
data - > Set ( index + + , kv . first , aiString ( kv . second ) ) ;
2016-11-22 20:06:14 +00:00
}
2015-05-19 03:57:13 +00:00
nd - > mMetaData = data ;
}
}
2020-03-01 12:15:45 +00:00
if ( el . ObjectPlacement ) {
ResolveObjectPlacement ( nd - > mTransformation , el . ObjectPlacement . Get ( ) , conv ) ;
2015-05-19 03:57:13 +00:00
}
std : : vector < TempOpening > openings ;
IfcMatrix4 myInv ;
bool didinv = false ;
// convert everything contained directly within this structure,
// this may result in more nodes.
2020-03-01 12:15:45 +00:00
std : : vector < aiNode * > subnodes ;
2015-05-19 03:57:13 +00:00
try {
// locate aggregates and 'contained-in-here'-elements of this spatial structure and add them in recursively
// on our way, collect openings in *this* element
STEP : : DB : : RefMapRange range = refs . equal_range ( el . GetID ( ) ) ;
2020-03-01 12:15:45 +00:00
for ( STEP : : DB : : RefMapRange range2 = range ; range2 . first ! = range . second ; + + range2 . first ) {
2015-05-19 03:57:13 +00:00
// skip over meshes that have already been processed before. This is strictly necessary
// because the reverse indices also include references contained in argument lists and
// therefore every element has a back-reference hold by its parent.
if ( conv . already_processed . find ( ( * range2 . first ) . second ) ! = conv . already_processed . end ( ) ) {
continue ;
}
2020-03-01 12:15:45 +00:00
const STEP : : LazyObject & obj = conv . db . MustGetObject ( ( * range2 . first ) . second ) ;
2015-05-19 03:57:13 +00:00
// handle regularly-contained elements
2020-03-01 12:15:45 +00:00
if ( const Schema_2x3 : : IfcRelContainedInSpatialStructure * const cont = obj - > ToPtr < Schema_2x3 : : IfcRelContainedInSpatialStructure > ( ) ) {
if ( cont - > RelatingStructure - > GetID ( ) ! = el . GetID ( ) ) {
2015-05-19 03:57:13 +00:00
continue ;
}
2020-03-01 12:15:45 +00:00
for ( const Schema_2x3 : : IfcProduct & pro : cont - > RelatedElements ) {
if ( pro . ToPtr < Schema_2x3 : : IfcOpeningElement > ( ) ) {
2015-05-19 03:57:13 +00:00
// IfcOpeningElement is handled below. Sadly we can't use it here as is:
// The docs say that opening elements are USUALLY attached to building storey,
// but we want them for the building elements to which they belong.
continue ;
}
2020-03-01 12:15:45 +00:00
aiNode * const ndnew = ProcessSpatialStructure ( nd , pro , conv , nullptr ) ;
if ( ndnew ) {
subnodes . push_back ( ndnew ) ;
2015-05-19 03:57:13 +00:00
}
}
}
// handle openings, which we collect in a list rather than adding them to the node graph
2020-03-01 12:15:45 +00:00
else if ( const Schema_2x3 : : IfcRelVoidsElement * const fills = obj - > ToPtr < Schema_2x3 : : IfcRelVoidsElement > ( ) ) {
if ( fills - > RelatingBuildingElement - > GetID ( ) = = el . GetID ( ) ) {
const Schema_2x3 : : IfcFeatureElementSubtraction & open = fills - > RelatedOpeningElement ;
2015-05-19 03:57:13 +00:00
// move opening elements to a separate node since they are semantically different than elements that are just 'contained'
2016-04-05 20:56:11 +00:00
std : : unique_ptr < aiNode > nd_aggr ( new aiNode ( ) ) ;
2015-05-19 03:57:13 +00:00
nd_aggr - > mName . Set ( " $RelVoidsElement " ) ;
2018-12-30 09:36:23 +00:00
nd_aggr - > mParent = nd ;
2015-05-19 03:57:13 +00:00
nd_aggr - > mTransformation = nd - > mTransformation ;
std : : vector < TempOpening > openings_local ;
2020-03-01 12:15:45 +00:00
aiNode * const ndnew = ProcessSpatialStructure ( nd_aggr . get ( ) , open , conv , & openings_local ) ;
2015-05-19 03:57:13 +00:00
if ( ndnew ) {
nd_aggr - > mNumChildren = 1 ;
2020-03-01 12:15:45 +00:00
nd_aggr - > mChildren = new aiNode * [ 1 ] ( ) ;
2015-05-19 03:57:13 +00:00
nd_aggr - > mChildren [ 0 ] = ndnew ;
2020-03-01 12:15:45 +00:00
if ( openings_local . size ( ) ) {
2015-05-19 03:57:13 +00:00
if ( ! didinv ) {
2020-03-01 12:15:45 +00:00
myInv = aiMatrix4x4 ( nd - > mTransformation ) . Inverse ( ) ;
2015-05-19 03:57:13 +00:00
didinv = true ;
}
// we need all openings to be in the local space of *this* node, so transform them
2020-03-01 12:15:45 +00:00
for ( TempOpening & op : openings_local ) {
op . Transform ( myInv * nd_aggr - > mChildren [ 0 ] - > mTransformation ) ;
2015-05-19 03:57:13 +00:00
openings . push_back ( op ) ;
}
}
2020-03-01 12:15:45 +00:00
subnodes . push_back ( nd_aggr . release ( ) ) ;
2015-05-19 03:57:13 +00:00
}
}
}
}
2020-03-01 12:15:45 +00:00
for ( ; range . first ! = range . second ; + + range . first ) {
2015-05-19 03:57:13 +00:00
// see note in loop above
if ( conv . already_processed . find ( ( * range . first ) . second ) ! = conv . already_processed . end ( ) ) {
continue ;
}
2020-03-01 12:15:45 +00:00
if ( const Schema_2x3 : : IfcRelAggregates * const aggr = conv . db . GetObject ( ( * range . first ) . second ) - > ToPtr < Schema_2x3 : : IfcRelAggregates > ( ) ) {
if ( aggr - > RelatingObject - > GetID ( ) ! = el . GetID ( ) ) {
2015-05-19 03:57:13 +00:00
continue ;
}
// move aggregate elements to a separate node since they are semantically different than elements that are just 'contained'
2016-04-05 20:56:11 +00:00
std : : unique_ptr < aiNode > nd_aggr ( new aiNode ( ) ) ;
2015-05-19 03:57:13 +00:00
nd_aggr - > mName . Set ( " $RelAggregates " ) ;
2018-12-30 09:36:23 +00:00
nd_aggr - > mParent = nd ;
2015-05-19 03:57:13 +00:00
nd_aggr - > mTransformation = nd - > mTransformation ;
2020-03-01 12:15:45 +00:00
nd_aggr - > mChildren = new aiNode * [ aggr - > RelatedObjects . size ( ) ] ( ) ;
for ( const Schema_2x3 : : IfcObjectDefinition & def : aggr - > RelatedObjects ) {
if ( const Schema_2x3 : : IfcProduct * const prod = def . ToPtr < Schema_2x3 : : IfcProduct > ( ) ) {
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
aiNode * const ndnew = ProcessSpatialStructure ( nd_aggr . get ( ) , * prod , conv , NULL ) ;
if ( ndnew ) {
2015-05-19 03:57:13 +00:00
nd_aggr - > mChildren [ nd_aggr - > mNumChildren + + ] = ndnew ;
}
}
}
2020-03-01 12:15:45 +00:00
subnodes . push_back ( nd_aggr . release ( ) ) ;
2015-05-19 03:57:13 +00:00
}
}
conv . collect_openings = collect_openings ;
2020-03-01 12:15:45 +00:00
if ( ! conv . collect_openings ) {
2015-05-19 03:57:13 +00:00
conv . apply_openings = & openings ;
}
if ( ! skipGeometry ) {
2020-03-01 12:15:45 +00:00
ProcessProductRepresentation ( el , nd , subnodes , conv ) ;
conv . apply_openings = conv . collect_openings = nullptr ;
2015-05-19 03:57:13 +00:00
}
if ( subnodes . size ( ) ) {
2020-03-01 12:15:45 +00:00
nd - > mChildren = new aiNode * [ subnodes . size ( ) ] ( ) ;
for ( aiNode * nd2 : subnodes ) {
2015-05-19 03:57:13 +00:00
nd - > mChildren [ nd - > mNumChildren + + ] = nd2 ;
2018-12-30 09:44:43 +00:00
nd2 - > mParent = nd ;
2015-05-19 03:57:13 +00:00
}
}
2020-03-01 12:15:45 +00:00
} catch ( . . . ) {
2015-05-19 03:57:13 +00:00
// it hurts, but I don't want to pull boost::ptr_vector into -noboost only for these few spots here
2020-03-01 12:15:45 +00:00
std : : for_each ( subnodes . begin ( ) , subnodes . end ( ) , delete_fun < aiNode > ( ) ) ;
2015-05-19 03:57:13 +00:00
throw ;
}
ai_assert ( conv . already_processed . find ( el . GetID ( ) ) ! = conv . already_processed . end ( ) ) ;
conv . already_processed . erase ( conv . already_processed . find ( el . GetID ( ) ) ) ;
2018-12-30 09:36:23 +00:00
return nd ;
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void ProcessSpatialStructures ( ConversionData & conv ) {
2015-05-19 03:57:13 +00:00
// XXX add support for multiple sites (i.e. IfcSpatialStructureElements with composition == COMPLEX)
// process all products in the file. it is reasonable to assume that a
// file that is relevant for us contains at least a site or a building.
2020-03-01 12:15:45 +00:00
const STEP : : DB : : ObjectMapByType & map = conv . db . GetObjectsByType ( ) ;
2015-05-19 03:57:13 +00:00
ai_assert ( map . find ( " ifcsite " ) ! = map . end ( ) ) ;
2020-03-01 12:15:45 +00:00
const STEP : : DB : : ObjectSet * range = & map . find ( " ifcsite " ) - > second ;
2015-05-19 03:57:13 +00:00
if ( range - > empty ( ) ) {
ai_assert ( map . find ( " ifcbuilding " ) ! = map . end ( ) ) ;
range = & map . find ( " ifcbuilding " ) - > second ;
if ( range - > empty ( ) ) {
// no site, no building - fail;
IFCImporter : : ThrowException ( " no root element found (expected IfcBuilding or preferably IfcSite) " ) ;
}
}
2020-03-01 12:15:45 +00:00
std : : vector < aiNode * > nodes ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
for ( const STEP : : LazyObject * lz : * range ) {
const Schema_2x3 : : IfcSpatialStructureElement * const prod = lz - > ToPtr < Schema_2x3 : : IfcSpatialStructureElement > ( ) ;
if ( ! prod ) {
2015-05-19 03:57:13 +00:00
continue ;
}
2020-03-01 12:15:45 +00:00
IFCImporter : : LogDebug ( " looking at spatial structure ` " + ( prod - > Name ? prod - > Name . Get ( ) : " unnamed " ) + " ` " + ( prod - > ObjectType ? " which is of type " + prod - > ObjectType . Get ( ) : " " ) ) ;
2015-05-19 03:57:13 +00:00
2017-06-01 07:47:50 +00:00
// the primary sites are referenced by an IFCRELAGGREGATES element which assigns them to the IFCPRODUCT
2020-03-01 12:15:45 +00:00
const STEP : : DB : : RefMap & refs = conv . db . GetRefs ( ) ;
2017-06-01 07:47:50 +00:00
STEP : : DB : : RefMapRange ref_range = refs . equal_range ( conv . proj . GetID ( ) ) ;
2020-03-01 12:15:45 +00:00
for ( ; ref_range . first ! = ref_range . second ; + + ref_range . first ) {
if ( const Schema_2x3 : : IfcRelAggregates * const aggr = conv . db . GetObject ( ( * ref_range . first ) . second ) - > ToPtr < Schema_2x3 : : IfcRelAggregates > ( ) ) {
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
for ( const Schema_2x3 : : IfcObjectDefinition & def : aggr - > RelatedObjects ) {
2015-05-19 03:57:13 +00:00
// comparing pointer values is not sufficient, we would need to cast them to the same type first
// as there is multiple inheritance in the game.
if ( def . GetID ( ) = = prod - > GetID ( ) ) {
IFCImporter : : LogDebug ( " selecting this spatial structure as root structure " ) ;
2017-06-01 07:47:50 +00:00
// got it, this is one primary site.
2020-03-01 12:15:45 +00:00
nodes . push_back ( ProcessSpatialStructure ( NULL , * prod , conv , NULL ) ) ;
2015-05-19 03:57:13 +00:00
}
}
}
}
}
2020-03-01 12:15:45 +00:00
size_t nb_nodes = nodes . size ( ) ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
if ( nb_nodes = = 0 ) {
IFCImporter : : LogWarn ( " failed to determine primary site element, taking all the IfcSite " ) ;
for ( const STEP : : LazyObject * lz : * range ) {
const Schema_2x3 : : IfcSpatialStructureElement * const prod = lz - > ToPtr < Schema_2x3 : : IfcSpatialStructureElement > ( ) ;
if ( ! prod ) {
continue ;
}
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
nodes . push_back ( ProcessSpatialStructure ( NULL , * prod , conv , NULL ) ) ;
}
2017-06-01 07:47:50 +00:00
2020-03-01 12:15:45 +00:00
nb_nodes = nodes . size ( ) ;
}
2017-06-01 07:47:50 +00:00
2020-03-01 12:15:45 +00:00
if ( nb_nodes = = 1 ) {
conv . out - > mRootNode = nodes [ 0 ] ;
} else if ( nb_nodes > 1 ) {
conv . out - > mRootNode = new aiNode ( " Root " ) ;
conv . out - > mRootNode - > mParent = NULL ;
conv . out - > mRootNode - > mNumChildren = static_cast < unsigned int > ( nb_nodes ) ;
conv . out - > mRootNode - > mChildren = new aiNode * [ conv . out - > mRootNode - > mNumChildren ] ;
2018-01-16 03:14:44 +00:00
2020-03-01 12:15:45 +00:00
for ( size_t i = 0 ; i < nb_nodes ; + + i ) {
aiNode * node = nodes [ i ] ;
2017-06-01 07:47:50 +00:00
2020-03-01 12:15:45 +00:00
node - > mParent = conv . out - > mRootNode ;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
conv . out - > mRootNode - > mChildren [ i ] = node ;
}
} else {
IFCImporter : : ThrowException ( " failed to determine primary site element " ) ;
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void MakeTreeRelative ( aiNode * start , const aiMatrix4x4 & combined ) {
2015-05-19 03:57:13 +00:00
// combined is the parent's absolute transformation matrix
const aiMatrix4x4 old = start - > mTransformation ;
2015-05-19 03:48:29 +00:00
2015-05-19 03:57:13 +00:00
if ( ! combined . IsIdentity ( ) ) {
start - > mTransformation = aiMatrix4x4 ( combined ) . Inverse ( ) * start - > mTransformation ;
}
2015-05-19 03:48:29 +00:00
2015-05-19 03:57:13 +00:00
// All nodes store absolute transformations right now, so we need to make them relative
for ( unsigned int i = 0 ; i < start - > mNumChildren ; + + i ) {
2020-03-01 12:15:45 +00:00
MakeTreeRelative ( start - > mChildren [ i ] , old ) ;
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:48:29 +00:00
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void MakeTreeRelative ( ConversionData & conv ) {
MakeTreeRelative ( conv . out - > mRootNode , IfcMatrix4 ( ) ) ;
2015-05-19 03:48:29 +00:00
}
2020-03-01 12:15:45 +00:00
} // namespace
2015-05-19 03:48:29 +00:00
# endif