FBXProperties.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXProperties.h
  34. * @brief FBX dynamic properties
  35. */
  36. #ifndef INCLUDED_AI_FBX_PROPERTIES_H
  37. #define INCLUDED_AI_FBX_PROPERTIES_H
  38. #include "FBXCompileConfig.h"
  39. #include <memory>
  40. #include <string>
  41. namespace Assimp {
  42. namespace FBX {
  43. // Forward declarations
  44. class Element;
  45. /** Represents a dynamic property. Type info added by deriving classes,
  46. * see #TypedProperty.
  47. Example:
  48. @verbatim
  49. P: "ShininessExponent", "double", "Number", "",0.5
  50. @endvebatim
  51. */
  52. class Property {
  53. protected:
  54. Property();
  55. public:
  56. virtual ~Property();
  57. public:
  58. template <typename T>
  59. const T* As() const {
  60. return dynamic_cast<const T*>(this);
  61. }
  62. };
  63. template<typename T>
  64. class TypedProperty : public Property {
  65. public:
  66. explicit TypedProperty(const T& value)
  67. : value(value) {
  68. // empty
  69. }
  70. const T& Value() const {
  71. return value;
  72. }
  73. private:
  74. T value;
  75. };
  76. typedef std::fbx_unordered_map<std::string,std::shared_ptr<Property> > DirectPropertyMap;
  77. typedef std::fbx_unordered_map<std::string,const Property*> PropertyMap;
  78. typedef std::fbx_unordered_map<std::string,const Element*> LazyPropertyMap;
  79. /**
  80. * Represents a property table as can be found in the newer FBX files (Properties60, Properties70)
  81. */
  82. class PropertyTable {
  83. public:
  84. // in-memory property table with no source element
  85. PropertyTable();
  86. PropertyTable(const Element& element, std::shared_ptr<const PropertyTable> templateProps);
  87. ~PropertyTable();
  88. const Property* Get(const std::string& name) const;
  89. // PropertyTable's need not be coupled with FBX elements so this can be nullptr
  90. const Element* GetElement() const {
  91. return element;
  92. }
  93. const PropertyTable* TemplateProps() const {
  94. return templateProps.get();
  95. }
  96. DirectPropertyMap GetUnparsedProperties() const;
  97. private:
  98. LazyPropertyMap lazyProps;
  99. mutable PropertyMap props;
  100. const std::shared_ptr<const PropertyTable> templateProps;
  101. const Element* const element;
  102. };
  103. // ------------------------------------------------------------------------------------------------
  104. template <typename T>
  105. inline
  106. T PropertyGet(const PropertyTable& in, const std::string& name, const T& defaultValue) {
  107. const Property* const prop = in.Get(name);
  108. if( nullptr == prop) {
  109. return defaultValue;
  110. }
  111. // strong typing, no need to be lenient
  112. const TypedProperty<T>* const tprop = prop->As< TypedProperty<T> >();
  113. if( nullptr == tprop) {
  114. return defaultValue;
  115. }
  116. return tprop->Value();
  117. }
  118. // ------------------------------------------------------------------------------------------------
  119. template <typename T>
  120. inline
  121. T PropertyGet(const PropertyTable& in, const std::string& name, bool& result, bool useTemplate=false ) {
  122. const Property* prop = in.Get(name);
  123. if( nullptr == prop) {
  124. if ( ! useTemplate ) {
  125. result = false;
  126. return T();
  127. }
  128. const PropertyTable* templ = in.TemplateProps();
  129. if ( nullptr == templ ) {
  130. result = false;
  131. return T();
  132. }
  133. prop = templ->Get(name);
  134. if ( nullptr == prop ) {
  135. result = false;
  136. return T();
  137. }
  138. }
  139. // strong typing, no need to be lenient
  140. const TypedProperty<T>* const tprop = prop->As< TypedProperty<T> >();
  141. if( nullptr == tprop) {
  142. result = false;
  143. return T();
  144. }
  145. result = true;
  146. return tprop->Value();
  147. }
  148. } //! FBX
  149. } //! Assimp
  150. #endif // INCLUDED_AI_FBX_PROPERTIES_H