Merge pull request #21 from assimp/assimp-metadata

Assimp metadata implementation. This adds aiNode::mMetaData, the <assimp/metadata.h> header containing the aiMetaData structure. Also adds support for Ifc reading metadata to test out the new feature.
pull/22/merge
Alexander Gessler 2013-03-29 12:08:29 -07:00
commit eaee34b028
10 changed files with 8222 additions and 7620 deletions

View File

@ -34,6 +34,7 @@ SET( PUBLIC_HEADERS
${HEADER_PATH}/quaternion.h
${HEADER_PATH}/quaternion.inl
${HEADER_PATH}/scene.h
${HEADER_PATH}/metadata.h
${HEADER_PATH}/texture.h
${HEADER_PATH}/types.h
${HEADER_PATH}/vector2.h

View File

@ -251,7 +251,7 @@ void IFCImporter::InternReadFile( const std::string& pFile,
// tell the reader for which types we need to simulate STEPs reverse indices
static const char* const inverse_indices_to_track[] = {
"ifcrelcontainedinspatialstructure", "ifcrelaggregates", "ifcrelvoidselement", "ifcstyleditem"
"ifcrelcontainedinspatialstructure", "ifcrelaggregates", "ifcrelvoidselement", "ifcreldefinesbyproperties", "ifcpropertyset", "ifcstyleditem"
};
// feed the IFC schema into the reader and pre-parse all lines
@ -513,7 +513,7 @@ struct RateRepresentationPredicate {
return -3;
}
// give strong preference to extruded geometry
// give strong preference to extruded geometry.
if (r == "SweptSolid") {
return -10;
}
@ -584,6 +584,86 @@ void ProcessProductRepresentation(const IfcProduct& el, aiNode* nd, std::vector<
AssignAddedMeshes(meshes,nd,conv);
}
typedef std::map<std::string, std::string> Metadata;
// ------------------------------------------------------------------------------------------------
void ProcessMetadata(const ListOf< Lazy< IfcProperty >, 1, 0 >& set, ConversionData& conv, Metadata& properties,
const std::string& prefix = "",
unsigned int nest = 0)
{
BOOST_FOREACH(const IfcProperty& property, set) {
const std::string& key = prefix.length() > 0 ? (prefix + "." + property.Name) : property.Name;
if (const IfcPropertySingleValue* const singleValue = property.ToPtr<IfcPropertySingleValue>()) {
if (singleValue->NominalValue) {
if (const EXPRESS::STRING* str = singleValue->NominalValue.Get()->ToPtr<EXPRESS::STRING>()) {
std::string value = static_cast<std::string>(*str);
properties[key]=value;
}
else if (const EXPRESS::REAL* val = singleValue->NominalValue.Get()->ToPtr<EXPRESS::REAL>()) {
float value = static_cast<float>(*val);
std::stringstream s;
s << value;
properties[key]=s.str();
}
else if (const EXPRESS::INTEGER* val = singleValue->NominalValue.Get()->ToPtr<EXPRESS::INTEGER>()) {
int64_t value = static_cast<int64_t>(*val);
std::stringstream s;
s << value;
properties[key]=s.str();
}
}
}
else if (const IfcPropertyListValue* const listValue = property.ToPtr<IfcPropertyListValue>()) {
std::stringstream ss;
ss << "[";
unsigned index=0;
BOOST_FOREACH(const IfcValue::Out& v, listValue->ListValues) {
if (!v) continue;
if (const EXPRESS::STRING* str = v->ToPtr<EXPRESS::STRING>()) {
std::string value = static_cast<std::string>(*str);
ss << "'" << value << "'";
}
else if (const EXPRESS::REAL* val = v->ToPtr<EXPRESS::REAL>()) {
float value = static_cast<float>(*val);
ss << value;
}
else if (const EXPRESS::INTEGER* val = v->ToPtr<EXPRESS::INTEGER>()) {
int64_t value = static_cast<int64_t>(*val);
ss << value;
}
if (index+1<listValue->ListValues.size()) {
ss << ",";
}
index++;
}
ss << "]";
properties[key]=ss.str();
}
else if (const IfcComplexProperty* const complexProp = property.ToPtr<IfcComplexProperty>()) {
if(nest > 2) { // mostly arbitrary limit to prevent stack overflow vulnerabilities
IFCImporter::LogError("maximum nesting level for IfcComplexProperty reached, skipping this property.");
}
else {
ProcessMetadata(complexProp->HasProperties, conv, properties, key, nest + 1);
}
}
else {
properties[key]="";
}
}
}
// ------------------------------------------------------------------------------------------------
void ProcessMetadata(uint64_t relDefinesByPropertiesID, ConversionData& conv, Metadata& properties)
{
if (const IfcRelDefinesByProperties* const pset = conv.db.GetObject(relDefinesByPropertiesID)->ToPtr<IfcRelDefinesByProperties>()) {
if (const IfcPropertySet* const set = conv.db.GetObject(pset->RelatingPropertyDefinition->GetID())->ToPtr<IfcPropertySet>()) {
ProcessMetadata(set->HasProperties, conv, properties);
}
}
}
// ------------------------------------------------------------------------------------------------
aiNode* ProcessSpatialStructure(aiNode* parent, const IfcProduct& el, ConversionData& conv, std::vector<TempOpening>* collect_openings = NULL)
{
@ -609,6 +689,42 @@ aiNode* ProcessSpatialStructure(aiNode* parent, const IfcProduct& el, Conversion
nd->mName.Set(el.GetClassName()+"_"+(el.Name?el.Name.Get():"Unnamed")+"_"+el.GlobalId);
nd->mParent = parent;
conv.already_processed.insert(el.GetID());
// check for node metadata
STEP::DB::RefMapRange children = refs.equal_range(el.GetID());
if (children.first!=refs.end()) {
Metadata properties;
if (children.first==children.second) {
// handles single property set
ProcessMetadata((*children.first).second, conv, properties);
}
else {
// handles multiple property sets (currently all property sets are merged,
// which may not be the best solution in the long run)
for (STEP::DB::RefMap::const_iterator it=children.first; it!=children.second; ++it) {
ProcessMetadata((*it).second, conv, properties);
}
}
if (!properties.empty()) {
aiMetadata* data = new aiMetadata();
data->mNumProperties = properties.size();
data->mKeys = new aiString*[data->mNumProperties]();
data->mValues = new aiString*[data->mNumProperties]();
unsigned int i = 0;
BOOST_FOREACH(const Metadata::value_type& kv, properties) {
data->mKeys[i] = new aiString(kv.first);
if (kv.second.length() > 0) {
data->mValues[i] = new aiString(kv.second);
}
++i;
}
nd->mMetaData = data;
}
}
if(el.ObjectPlacement) {
ResolveObjectPlacement(nd->mTransformation,el.ObjectPlacement.Get(),conv);
}
@ -627,15 +743,24 @@ aiNode* ProcessSpatialStructure(aiNode* parent, const IfcProduct& el, Conversion
STEP::DB::RefMapRange range = refs.equal_range(el.GetID());
for(STEP::DB::RefMapRange range2 = range; range2.first != range.second; ++range2.first) {
// 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;
}
const STEP::LazyObject& obj = conv.db.MustGetObject((*range2.first).second);
// handle regularly-contained elements
if(const IfcRelContainedInSpatialStructure* const cont = obj->ToPtr<IfcRelContainedInSpatialStructure>()) {
if(cont->RelatingStructure->GetID() != el.GetID()) {
continue;
}
BOOST_FOREACH(const IfcProduct& pro, cont->RelatedElements) {
if(const IfcOpeningElement* const open = pro.ToPtr<IfcOpeningElement>()) {
// IfcOpeningElement is handled below. Sadly we can't use it here as is:
// The docs say that opening elements are USUALLY attached to building storeys
// but we want them for the building elements to which they belong to.
// 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;
}
@ -686,7 +811,14 @@ aiNode* ProcessSpatialStructure(aiNode* parent, const IfcProduct& el, Conversion
}
for(;range.first != range.second; ++range.first) {
// see note in loop above
if (conv.already_processed.find((*range.first).second) != conv.already_processed.end()) {
continue;
}
if(const IfcRelAggregates* const aggr = conv.db.GetObject((*range.first).second)->ToPtr<IfcRelAggregates>()) {
if(aggr->RelatingObject->GetID() != el.GetID()) {
continue;
}
// move aggregate elements to a separate node since they are semantically different than elements that are just 'contained'
std::auto_ptr<aiNode> nd_aggr(new aiNode());
@ -732,6 +864,8 @@ aiNode* ProcessSpatialStructure(aiNode* parent, const IfcProduct& el, Conversion
throw;
}
ai_assert(conv.already_processed.find(el.GetID()) != conv.already_processed.end());
conv.already_processed.erase(conv.already_processed.find(el.GetID()));
return nd.release();
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -208,6 +208,8 @@ struct ConversionData
// for later processing by a parent, which is a wall.
std::vector<TempOpening>* apply_openings;
std::vector<TempOpening>* collect_openings;
std::set<uint64_t> already_processed;
};

View File

@ -525,7 +525,7 @@ STEP::LazyObject::LazyObject(DB& db, uint64_t id,uint64_t /*line*/, const char*
--skip_depth;
}
if (skip_depth == 1 && *a=='#') {
if (skip_depth >= 1 && *a=='#') {
const char* tmp;
const int64_t num = static_cast<int64_t>( strtoul10_64(a+1,&tmp) );
db.MarkRef(num,id);

View File

@ -1421,6 +1421,10 @@ IFC support is new and considered experimental. Please report any bugs you may e
- The implementation knows only about IFC2X3 and applies this rule set to all models it encounters,
regardless of their actual version. Loading of older or newer files may fail with parsing errors.
@subsection ifc_metadata Metadata
IFC file properties (IfcPropertySet) are kept as per-node metadata, see aiNode::mMetaData.
<hr>
@section ogre Ogre
*ATTENTION*: The Ogre-Loader is currently under development, many things have changed after this documentation was written, but they are not final enough to rewrite the documentation. So things may have changed by now!

View File

@ -0,0 +1,120 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2012, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the 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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file metadata.h
* @brief Defines the data structures for holding node meta information.
*/
#ifndef __AI_METADATA_H_INC__
#define __AI_METADATA_H_INC__
#ifdef __cplusplus
extern "C" {
#endif
// -------------------------------------------------------------------------------
/**
* Container for holding metadata.
*
* Metadata is a key-value store using string keys and values.
*/
// -------------------------------------------------------------------------------
struct aiMetadata
{
/** Length of the mKeys and mValues arrays, respectively */
unsigned int mNumProperties;
/** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
C_STRUCT aiString** mKeys;
/** Arrays of values, may not be NULL. Entries in this array may be NULL if the
* corresponding property key has no assigned value. */
C_STRUCT aiString** mValues;
#ifdef __cplusplus
/** Constructor */
aiMetadata()
{
// set all members to zero by default
mKeys = NULL;
mValues = NULL;
mNumProperties = 0;
}
/** Destructor */
~aiMetadata()
{
if (mKeys && mValues) {
for (unsigned i=0; i<mNumProperties; ++i) {
if (mKeys[i]) {
delete mKeys[i];
}
if (mValues[i]) {
delete mValues[i];
}
}
delete [] mKeys;
delete [] mValues;
}
}
inline bool Get(const aiString& key, aiString& value)
{
for (unsigned i=0; i<mNumProperties; ++i) {
if (mKeys[i] && *mKeys[i]==key) {
value=*mValues[i];
return true;
}
}
return false;
}
#endif // __cplusplus
};
#ifdef __cplusplus
} //extern "C" {
#endif
#endif // __AI_METADATA_H_INC__

View File

@ -52,11 +52,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "camera.h"
#include "material.h"
#include "anim.h"
#include "metadata.h"
#ifdef __cplusplus
extern "C" {
#endif
// -------------------------------------------------------------------------------
/** A node in the imported hierarchy.
*
@ -71,16 +73,24 @@ struct aiNode
/** The name of the node.
*
* The name might be empty (length of zero) but all nodes which
* need to be accessed afterwards by bones or anims are usually named.
* Multiple nodes may have the same name, but nodes which are accessed
* by bones (see #aiBone and #aiMesh::mBones) *must* be unique.
* need to be referenced by either bones or animations are named.
* Multiple nodes may have the same name, except for nodes which are referenced
* by bones (see #aiBone and #aiMesh::mBones). Their names *must* be unique.
*
* Cameras and lights are assigned to a specific node name - if there
* are multiple nodes with this name, they're assigned to each of them.
* Cameras and lights reference a specific node by name - if there
* are multiple nodes with this name, they are assigned to each of them.
* <br>
* There are no limitations regarding the characters contained in
* this text. You should be able to handle stuff like whitespace, tabs,
* linefeeds, quotation marks, ampersands, ... .
* There are no limitations with regard to the characters contained in
* the name string as it is usually taken directly from the source file.
*
* Implementations should be able to handle tokens such as whitespace, tabs,
* line feeds, quotation marks, ampersands etc.
*
* Sometimes assimp introduces new nodes not present in the source file
* into the hierarchy (usually out of necessity because sometimes the
* source hierarchy format is simply not compatible). Their names are
* surrounded by @verbatim <> @endverbatim e.g.
* @verbatim<DummyRootNode> @endverbatim.
*/
C_STRUCT aiString mName;
@ -102,24 +112,39 @@ struct aiNode
/** The meshes of this node. Each entry is an index into the mesh */
unsigned int* mMeshes;
/** Metadata associated with this node or NULL if there is no metadata.
* Whether any metadata is generated depends on the source file format. See the
* @link importer_notes @endlink page for more information on every source file
* format. Importers that don't document any metadata don't write any.
*/
C_STRUCT aiMetadata* mMetaData;
#ifdef __cplusplus
/** Constructor */
aiNode()
{
// set all members to zero by default
mParent = NULL;
mNumChildren = 0; mChildren = NULL;
mNumMeshes = 0; mMeshes = NULL;
: mName()
, mParent()
, mNumChildren()
, mChildren()
, mNumMeshes()
, mMeshes()
, mMetaData()
{
}
/** Construction from a specific name */
aiNode(const std::string& name)
{
// set all members to zero by default
mParent = NULL;
mNumChildren = 0; mChildren = NULL;
mNumMeshes = 0; mMeshes = NULL;
mName = name;
: mName(name)
, mParent()
, mNumChildren()
, mChildren()
, mNumMeshes()
, mMeshes()
, mMetaData()
{
}
/** Destructor */
@ -134,8 +159,10 @@ struct aiNode
}
delete [] mChildren;
delete [] mMeshes;
delete mMetaData;
}
/** Searches for a node with a specific name, beginning at this
* nodes. Normally you will call this method on the root node
* of the scene.
@ -143,22 +170,45 @@ struct aiNode
* @param name Name to search for
* @return NULL or a valid Node if the search was successful.
*/
inline const aiNode* FindNode(const aiString& name) const
{
return FindNode(name.data);
}
inline aiNode* FindNode(const aiString& name)
{
return FindNode(name.data);
}
/** @override
*/
inline aiNode* FindNode(const char* name)
inline const aiNode* FindNode(const char* name) const
{
if (!::strcmp( mName.data,name))return this;
for (unsigned int i = 0; i < mNumChildren;++i)
{
aiNode* p = mChildren[i]->FindNode(name);
if (p)return p;
const aiNode* const p = mChildren[i]->FindNode(name);
if (p) {
return p;
}
}
// there is definitely no sub node with this name
// there is definitely no sub-node with this name
return NULL;
}
inline aiNode* FindNode(const char* name)
{
if (!::strcmp( mName.data,name))return this;
for (unsigned int i = 0; i < mNumChildren;++i)
{
aiNode* const p = mChildren[i]->FindNode(name);
if (p) {
return p;
}
}
// there is definitely no sub-node with this name
return NULL;
}

View File

@ -101,3 +101,10 @@ IfcUnit
IfcUnitAssignment
IfcVector
IfcIShapeProfileDef
IfcPropertyListValue
IfcRelDefinesByProperties
IfcPropertySet
IfcPropertySingleValue
IfcProperty
IfcComplexProperty
IfcElementQuantity