FBXProperties.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**************************************************************************/
  2. /* FBXProperties.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. /*
  31. Open Asset Import Library (assimp)
  32. ----------------------------------------------------------------------
  33. Copyright (c) 2006-2019, assimp team
  34. All rights reserved.
  35. Redistribution and use of this software in source and binary forms,
  36. with or without modification, are permitted provided that the
  37. following conditions are met:
  38. * Redistributions of source code must retain the above
  39. copyright notice, this list of conditions and the
  40. following disclaimer.
  41. * Redistributions in binary form must reproduce the above
  42. copyright notice, this list of conditions and the
  43. following disclaimer in the documentation and/or other
  44. materials provided with the distribution.
  45. * Neither the name of the assimp team, nor the names of its
  46. contributors may be used to endorse or promote products
  47. derived from this software without specific prior
  48. written permission of the assimp team.
  49. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. ----------------------------------------------------------------------
  61. */
  62. /** @file FBXProperties.h
  63. * @brief FBX dynamic properties
  64. */
  65. #ifndef FBXPROPERTIES_H
  66. #define FBXPROPERTIES_H
  67. #include "FBXParser.h"
  68. #include <map>
  69. #include <memory>
  70. #include <string>
  71. #include <vector>
  72. namespace FBXDocParser {
  73. // Forward declarations
  74. class Element;
  75. /** Represents a dynamic property. Type info added by deriving classes,
  76. * see #TypedProperty.
  77. Example:
  78. @verbatim
  79. P: "ShininessExponent", "double", "Number", "",0.5
  80. @endvebatim
  81. */
  82. class Property {
  83. protected:
  84. Property();
  85. public:
  86. virtual ~Property();
  87. public:
  88. template <typename T>
  89. const T *As() const {
  90. return dynamic_cast<const T *>(this);
  91. }
  92. };
  93. template <typename T>
  94. class TypedProperty : public Property {
  95. public:
  96. explicit TypedProperty(const T &value) :
  97. value(value) {
  98. // empty
  99. }
  100. const T &Value() const {
  101. return value;
  102. }
  103. private:
  104. T value;
  105. };
  106. #define new_Property new Property
  107. typedef Property *PropertyPtr;
  108. typedef std::map<std::string, PropertyPtr> DirectPropertyMap;
  109. typedef std::map<std::string, PropertyPtr> PropertyMap;
  110. typedef std::map<std::string, ElementPtr> LazyPropertyMap;
  111. /**
  112. * Represents a property table as can be found in the newer FBX files (Properties60, Properties70)
  113. */
  114. class PropertyTable {
  115. public:
  116. // in-memory property table with no source element
  117. PropertyTable();
  118. PropertyTable(const PropertyTable *templateProps);
  119. PropertyTable(const ElementPtr element, const PropertyTable *templateProps);
  120. ~PropertyTable();
  121. PropertyPtr Get(const std::string &name) const;
  122. // PropertyTable's need not be coupled with FBX elements so this can be NULL
  123. ElementPtr GetElement() const {
  124. return element;
  125. }
  126. const PropertyMap &GetProperties() const {
  127. return props;
  128. }
  129. const LazyPropertyMap &GetLazyProperties() const {
  130. return lazyProps;
  131. }
  132. const PropertyTable *TemplateProps() const {
  133. return templateProps;
  134. }
  135. DirectPropertyMap GetUnparsedProperties() const;
  136. private:
  137. LazyPropertyMap lazyProps;
  138. mutable PropertyMap props;
  139. const PropertyTable *templateProps = nullptr;
  140. const ElementPtr element = nullptr;
  141. };
  142. // ------------------------------------------------------------------------------------------------
  143. template <typename T>
  144. inline T PropertyGet(const PropertyTable *in, const std::string &name, const T &defaultValue) {
  145. PropertyPtr prop = in->Get(name);
  146. if (nullptr == prop) {
  147. return defaultValue;
  148. }
  149. // strong typing, no need to be lenient
  150. const TypedProperty<T> *const tprop = prop->As<TypedProperty<T>>();
  151. if (nullptr == tprop) {
  152. return defaultValue;
  153. }
  154. return tprop->Value();
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. template <typename T>
  158. inline T PropertyGet(const PropertyTable *in, const std::string &name, bool &result, bool useTemplate = false) {
  159. PropertyPtr prop = in->Get(name);
  160. if (nullptr == prop) {
  161. if (!useTemplate) {
  162. result = false;
  163. return T();
  164. }
  165. const PropertyTable *templ = in->TemplateProps();
  166. if (nullptr == templ) {
  167. result = false;
  168. return T();
  169. }
  170. prop = templ->Get(name);
  171. if (nullptr == prop) {
  172. result = false;
  173. return T();
  174. }
  175. }
  176. // strong typing, no need to be lenient
  177. const TypedProperty<T> *const tprop = prop->As<TypedProperty<T>>();
  178. if (nullptr == tprop) {
  179. result = false;
  180. return T();
  181. }
  182. result = true;
  183. return tprop->Value();
  184. }
  185. } // namespace FBXDocParser
  186. #endif // FBXPROPERTIES_H