From 8d78fbcb90991ba54ab8596cbb2579dbae04ff44 Mon Sep 17 00:00:00 2001 From: Ashwin Bhat Date: Thu, 3 Oct 2024 12:33:37 -0700 Subject: [PATCH] Move Datalibrary into seperate h/cpp --- source/MaterialXCore/Datalibrary.cpp | 70 +++++++++++++++++++ source/MaterialXCore/Datalibrary.h | 41 +++++++++++ source/MaterialXCore/Document.cpp | 55 --------------- source/MaterialXCore/Document.h | 27 ------- source/MaterialXCore/Node.cpp | 1 + .../ColorManagementSystem.h | 1 + source/MaterialXGenShader/UnitSystem.h | 1 + 7 files changed, 114 insertions(+), 82 deletions(-) create mode 100644 source/MaterialXCore/Datalibrary.cpp create mode 100644 source/MaterialXCore/Datalibrary.h diff --git a/source/MaterialXCore/Datalibrary.cpp b/source/MaterialXCore/Datalibrary.cpp new file mode 100644 index 0000000000..1b86d5a179 --- /dev/null +++ b/source/MaterialXCore/Datalibrary.cpp @@ -0,0 +1,70 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +#include +#include + +MATERIALX_NAMESPACE_BEGIN + +const DataLibraryPtr standardDataLibrary = DataLibrary::create(); + +DataLibraryPtr DataLibrary::create() +{ + return std::make_shared(); +} + +// use loadDocuments to build this vector +void DataLibrary::loadDataLibrary(vector& librarydocuments) +{ + _datalibrary = createDocument(); + + for (auto library : librarydocuments) + { + for (auto child : library->getChildren()) + { + if (child->getCategory().empty()) + { + throw Exception("Trying to import child without a category: " + child->getName()); + } + + const string childName = child->getQualifiedName(child->getName()); + + // Check for duplicate elements. + ConstElementPtr previous = _datalibrary->getChild(childName); + if (previous) + { + continue; + } + + // Create the imported element. + ElementPtr childCopy = _datalibrary->addChildOfCategory(child->getCategory(), childName); + childCopy->copyContentFrom(child); + if (!childCopy->hasFilePrefix() && library->hasFilePrefix()) + { + childCopy->setFilePrefix(library->getFilePrefix()); + } + if (!childCopy->hasGeomPrefix() && library->hasGeomPrefix()) + { + childCopy->setGeomPrefix(library->getGeomPrefix()); + } + if (!childCopy->hasColorSpace() && library->hasColorSpace()) + { + childCopy->setColorSpace(library->getColorSpace()); + } + if (!childCopy->hasNamespace() && library->hasNamespace()) + { + childCopy->setNamespace(library->getNamespace()); + } + if (!childCopy->hasSourceUri() && library->hasSourceUri()) + { + childCopy->setSourceUri(library->getSourceUri()); + } + } + } + +} + + +MATERIALX_NAMESPACE_END diff --git a/source/MaterialXCore/Datalibrary.h b/source/MaterialXCore/Datalibrary.h new file mode 100644 index 0000000000..224dc73205 --- /dev/null +++ b/source/MaterialXCore/Datalibrary.h @@ -0,0 +1,41 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +#ifndef MATERIALX_DATALIBRARY +#define MATERIALX_DATALIBRARY + +/// @file +/// The top-level DataLibrary class + +#include + +MATERIALX_NAMESPACE_BEGIN + +class DataLibrary; +using DataLibraryPtr = shared_ptr; +using ConstDataLibraryPtr = shared_ptr; + +class MX_CORE_API DataLibrary +{ + public: + ConstDocumentPtr dataLibrary() + { + return _datalibrary; + } + + void loadDataLibrary(vector& documents); + + static DataLibraryPtr create(); + + private: + // Shared node library used across documents. + DocumentPtr _datalibrary; +}; + +extern MX_CORE_API const DataLibraryPtr standardDataLibrary; + +MATERIALX_NAMESPACE_END + +#endif diff --git a/source/MaterialXCore/Document.cpp b/source/MaterialXCore/Document.cpp index 155deffc23..3f75ea29b3 100644 --- a/source/MaterialXCore/Document.cpp +++ b/source/MaterialXCore/Document.cpp @@ -12,61 +12,6 @@ MATERIALX_NAMESPACE_BEGIN const string Document::CMS_ATTRIBUTE = "cms"; const string Document::CMS_CONFIG_ATTRIBUTE = "cmsconfig"; -const DataLibraryPtr standardDataLibrary = DataLibrary::create(); - -// use loadDocuments to build this vector -void DataLibrary::loadDataLibrary(vector& librarydocuments) -{ - _datalibrary = createDocument(); - - for (auto library : librarydocuments) - { - for (auto child : library->getChildren()) - { - if (child->getCategory().empty()) - { - throw Exception("Trying to import child without a category: " + child->getName()); - } - - const string childName = child->getQualifiedName(child->getName()); - - // Check for duplicate elements. - ConstElementPtr previous = _datalibrary->getChild(childName); - if (previous) - { - continue; - } - - // Create the imported element. - ElementPtr childCopy = _datalibrary->addChildOfCategory(child->getCategory(), childName); - childCopy->copyContentFrom(child); - if (!childCopy->hasFilePrefix() && library->hasFilePrefix()) - { - childCopy->setFilePrefix(library->getFilePrefix()); - } - if (!childCopy->hasGeomPrefix() && library->hasGeomPrefix()) - { - childCopy->setGeomPrefix(library->getGeomPrefix()); - } - if (!childCopy->hasColorSpace() && library->hasColorSpace()) - { - childCopy->setColorSpace(library->getColorSpace()); - } - if (!childCopy->hasNamespace() && library->hasNamespace()) - { - childCopy->setNamespace(library->getNamespace()); - } - if (!childCopy->hasSourceUri() && library->hasSourceUri()) - { - childCopy->setSourceUri(library->getSourceUri()); - } - } - } - -} - - - // // Document factory function // diff --git a/source/MaterialXCore/Document.h b/source/MaterialXCore/Document.h index 86b14686bf..6943c20a4b 100644 --- a/source/MaterialXCore/Document.h +++ b/source/MaterialXCore/Document.h @@ -23,10 +23,6 @@ using DocumentPtr = shared_ptr; /// A shared pointer to a const Document using ConstDocumentPtr = shared_ptr; -class DataLibrary; -using DataLibraryPtr = shared_ptr; -using ConstDataLibraryPtr = shared_ptr; - /// @class Document /// A MaterialX document, which represents the top-level element in the /// MaterialX ownership hierarchy. @@ -693,33 +689,10 @@ class MX_CORE_API Document : public GraphElement }; - -class MX_CORE_API DataLibrary -{ - public: - ConstDocumentPtr dataLibrary() - { - return _datalibrary; - } - - void loadDataLibrary(vector& documents); - - static DataLibraryPtr create() - { - return std::make_shared(); - } - - private: - // Shared node library used across documents. - DocumentPtr _datalibrary; -}; - /// Create a new Document. /// @relates Document MX_CORE_API DocumentPtr createDocument(); -extern MX_CORE_API const DataLibraryPtr standardDataLibrary; - MATERIALX_NAMESPACE_END #endif diff --git a/source/MaterialXCore/Node.cpp b/source/MaterialXCore/Node.cpp index bc76ee5080..1f031ac0a6 100644 --- a/source/MaterialXCore/Node.cpp +++ b/source/MaterialXCore/Node.cpp @@ -7,6 +7,7 @@ #include #include +#include #include diff --git a/source/MaterialXGenShader/ColorManagementSystem.h b/source/MaterialXGenShader/ColorManagementSystem.h index 62a1f2b64b..5d4c556468 100644 --- a/source/MaterialXGenShader/ColorManagementSystem.h +++ b/source/MaterialXGenShader/ColorManagementSystem.h @@ -16,6 +16,7 @@ #include #include +#include MATERIALX_NAMESPACE_BEGIN diff --git a/source/MaterialXGenShader/UnitSystem.h b/source/MaterialXGenShader/UnitSystem.h index 60a562afd0..96d43895f9 100644 --- a/source/MaterialXGenShader/UnitSystem.h +++ b/source/MaterialXGenShader/UnitSystem.h @@ -17,6 +17,7 @@ #include #include +#include MATERIALX_NAMESPACE_BEGIN