metadata.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file metadata.h
  35. * @brief Defines the data structures for holding node meta information.
  36. */
  37. #pragma once
  38. #ifndef AI_METADATA_H_INC
  39. #define AI_METADATA_H_INC
  40. #ifdef __GNUC__
  41. #pragma GCC system_header
  42. #endif
  43. #if defined(_MSC_VER) && (_MSC_VER <= 1500)
  44. #include "Compiler/pstdint.h"
  45. #else
  46. #include <stdint.h>
  47. #endif
  48. #include <assimp/quaternion.h>
  49. // -------------------------------------------------------------------------------
  50. /**
  51. * Enum used to distinguish data types
  52. */
  53. // -------------------------------------------------------------------------------
  54. typedef enum aiMetadataType {
  55. AI_BOOL = 0,
  56. AI_INT32 = 1,
  57. AI_UINT64 = 2,
  58. AI_FLOAT = 3,
  59. AI_DOUBLE = 4,
  60. AI_AISTRING = 5,
  61. AI_AIVECTOR3D = 6,
  62. AI_AIMETADATA = 7,
  63. AI_INT64 = 8,
  64. AI_UINT32 = 9,
  65. AI_META_MAX = 10,
  66. #ifndef SWIG
  67. FORCE_32BIT = INT_MAX
  68. #endif
  69. } aiMetadataType;
  70. // -------------------------------------------------------------------------------
  71. /**
  72. * Metadata entry
  73. *
  74. * The type field uniquely identifies the underlying type of the data field
  75. */
  76. // -------------------------------------------------------------------------------
  77. struct aiMetadataEntry {
  78. aiMetadataType mType;
  79. void *mData;
  80. #ifdef __cplusplus
  81. aiMetadataEntry() :
  82. mType(AI_META_MAX),
  83. mData( nullptr ) {
  84. // empty
  85. }
  86. #endif
  87. };
  88. #ifdef __cplusplus
  89. #include <string>
  90. struct aiMetadata;
  91. // -------------------------------------------------------------------------------
  92. /**
  93. * Helper functions to get the aiType enum entry for a type
  94. */
  95. // -------------------------------------------------------------------------------
  96. inline aiMetadataType GetAiType(const bool &) {
  97. return AI_BOOL;
  98. }
  99. inline aiMetadataType GetAiType(int32_t) {
  100. return AI_INT32;
  101. }
  102. inline aiMetadataType GetAiType(const uint64_t &) {
  103. return AI_UINT64;
  104. }
  105. inline aiMetadataType GetAiType(const float &) {
  106. return AI_FLOAT;
  107. }
  108. inline aiMetadataType GetAiType(const double &) {
  109. return AI_DOUBLE;
  110. }
  111. inline aiMetadataType GetAiType(const aiString &) {
  112. return AI_AISTRING;
  113. }
  114. inline aiMetadataType GetAiType(const aiVector3D &) {
  115. return AI_AIVECTOR3D;
  116. }
  117. inline aiMetadataType GetAiType(const aiMetadata &) {
  118. return AI_AIMETADATA;
  119. }
  120. inline aiMetadataType GetAiType(const int64_t &) {
  121. return AI_INT64;
  122. }
  123. inline aiMetadataType GetAiType(const uint32_t &) {
  124. return AI_UINT32;
  125. }
  126. #endif // __cplusplus
  127. // -------------------------------------------------------------------------------
  128. /**
  129. * Container for holding metadata.
  130. *
  131. * Metadata is a key-value store using string keys and values.
  132. */
  133. // -------------------------------------------------------------------------------
  134. struct aiMetadata {
  135. /** Length of the mKeys and mValues arrays, respectively */
  136. unsigned int mNumProperties;
  137. /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
  138. C_STRUCT aiString *mKeys;
  139. /** Arrays of values, may not be NULL. Entries in this array may be NULL if the
  140. * corresponding property key has no assigned value. */
  141. C_STRUCT aiMetadataEntry *mValues;
  142. #ifdef __cplusplus
  143. /**
  144. * @brief The default constructor, set all members to zero by default.
  145. */
  146. aiMetadata() AI_NO_EXCEPT
  147. : mNumProperties(0),
  148. mKeys(nullptr),
  149. mValues(nullptr) {
  150. // empty
  151. }
  152. aiMetadata(const aiMetadata &rhs) :
  153. mNumProperties(rhs.mNumProperties), mKeys(nullptr), mValues(nullptr) {
  154. mKeys = new aiString[mNumProperties];
  155. for (size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i) {
  156. mKeys[i] = rhs.mKeys[i];
  157. }
  158. mValues = new aiMetadataEntry[mNumProperties];
  159. for (size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i) {
  160. mValues[i].mType = rhs.mValues[i].mType;
  161. switch (rhs.mValues[i].mType) {
  162. case AI_BOOL:
  163. mValues[i].mData = new bool;
  164. ::memcpy(mValues[i].mData, rhs.mValues[i].mData, sizeof(bool));
  165. break;
  166. case AI_INT32: {
  167. int32_t v;
  168. ::memcpy(&v, rhs.mValues[i].mData, sizeof(int32_t));
  169. mValues[i].mData = new int32_t(v);
  170. } break;
  171. case AI_UINT64: {
  172. uint64_t v;
  173. ::memcpy(&v, rhs.mValues[i].mData, sizeof(uint64_t));
  174. mValues[i].mData = new uint64_t(v);
  175. } break;
  176. case AI_FLOAT: {
  177. float v;
  178. ::memcpy(&v, rhs.mValues[i].mData, sizeof(float));
  179. mValues[i].mData = new float(v);
  180. } break;
  181. case AI_DOUBLE: {
  182. double v;
  183. ::memcpy(&v, rhs.mValues[i].mData, sizeof(double));
  184. mValues[i].mData = new double(v);
  185. } break;
  186. case AI_AISTRING: {
  187. aiString v;
  188. rhs.Get<aiString>(static_cast<unsigned int>(i), v);
  189. mValues[i].mData = new aiString(v);
  190. } break;
  191. case AI_AIVECTOR3D: {
  192. aiVector3D v;
  193. rhs.Get<aiVector3D>(static_cast<unsigned int>(i), v);
  194. mValues[i].mData = new aiVector3D(v);
  195. } break;
  196. case AI_AIMETADATA: {
  197. aiMetadata v;
  198. rhs.Get<aiMetadata>(static_cast<unsigned int>(i), v);
  199. mValues[i].mData = new aiMetadata(v);
  200. } break;
  201. case AI_INT64: {
  202. int64_t v;
  203. ::memcpy(&v, rhs.mValues[i].mData, sizeof(int64_t));
  204. mValues[i].mData = new int64_t(v);
  205. } break;
  206. case AI_UINT32: {
  207. uint32_t v;
  208. ::memcpy(&v, rhs.mValues[i].mData, sizeof(uint32_t));
  209. mValues[i].mData = new uint32_t(v);
  210. } break;
  211. #ifndef SWIG
  212. case FORCE_32BIT:
  213. #endif
  214. default:
  215. break;
  216. }
  217. }
  218. }
  219. aiMetadata &operator=(aiMetadata rhs) {
  220. using std::swap;
  221. swap(mNumProperties, rhs.mNumProperties);
  222. swap(mKeys, rhs.mKeys);
  223. swap(mValues, rhs.mValues);
  224. return *this;
  225. }
  226. /**
  227. * @brief The destructor.
  228. */
  229. ~aiMetadata() {
  230. delete[] mKeys;
  231. mKeys = nullptr;
  232. if (mValues) {
  233. // Delete each metadata entry
  234. for (unsigned i = 0; i < mNumProperties; ++i) {
  235. void *data = mValues[i].mData;
  236. switch (mValues[i].mType) {
  237. case AI_BOOL:
  238. delete static_cast<bool *>(data);
  239. break;
  240. case AI_INT32:
  241. delete static_cast<int32_t *>(data);
  242. break;
  243. case AI_UINT64:
  244. delete static_cast<uint64_t *>(data);
  245. break;
  246. case AI_FLOAT:
  247. delete static_cast<float *>(data);
  248. break;
  249. case AI_DOUBLE:
  250. delete static_cast<double *>(data);
  251. break;
  252. case AI_AISTRING:
  253. delete static_cast<aiString *>(data);
  254. break;
  255. case AI_AIVECTOR3D:
  256. delete static_cast<aiVector3D *>(data);
  257. break;
  258. case AI_AIMETADATA:
  259. delete static_cast<aiMetadata *>(data);
  260. break;
  261. case AI_INT64:
  262. delete static_cast<int64_t *>(data);
  263. break;
  264. case AI_UINT32:
  265. delete static_cast<uint32_t *>(data);
  266. break;
  267. #ifndef SWIG
  268. case FORCE_32BIT:
  269. #endif
  270. default:
  271. break;
  272. }
  273. }
  274. // Delete the metadata array
  275. delete[] mValues;
  276. mValues = nullptr;
  277. }
  278. }
  279. /**
  280. * @brief Allocates property fields + keys.
  281. * @param numProperties Number of requested properties.
  282. */
  283. static inline aiMetadata *Alloc(unsigned int numProperties) {
  284. if (0 == numProperties) {
  285. return nullptr;
  286. }
  287. aiMetadata *data = new aiMetadata;
  288. data->mNumProperties = numProperties;
  289. data->mKeys = new aiString[data->mNumProperties]();
  290. data->mValues = new aiMetadataEntry[data->mNumProperties]();
  291. return data;
  292. }
  293. /**
  294. * @brief Deallocates property fields + keys.
  295. */
  296. static inline void Dealloc(aiMetadata *metadata) {
  297. delete metadata;
  298. }
  299. template <typename T>
  300. inline void Add(const std::string &key, const T &value) {
  301. aiString *new_keys = new aiString[mNumProperties + 1];
  302. aiMetadataEntry *new_values = new aiMetadataEntry[mNumProperties + 1];
  303. for (unsigned int i = 0; i < mNumProperties; ++i) {
  304. new_keys[i] = mKeys[i];
  305. new_values[i] = mValues[i];
  306. }
  307. delete[] mKeys;
  308. delete[] mValues;
  309. mKeys = new_keys;
  310. mValues = new_values;
  311. mNumProperties++;
  312. Set(mNumProperties - 1, key, value);
  313. }
  314. template <typename T>
  315. inline bool Set(unsigned index, const std::string &key, const T &value) {
  316. // In range assertion
  317. if (index >= mNumProperties) {
  318. return false;
  319. }
  320. // Ensure that we have a valid key.
  321. if (key.empty()) {
  322. return false;
  323. }
  324. // Set metadata key
  325. mKeys[index] = key;
  326. // Set metadata type
  327. mValues[index].mType = GetAiType(value);
  328. // Copy the given value to the dynamic storage
  329. if (nullptr != mValues[index].mData && AI_AIMETADATA != mValues[index].mType) {
  330. ::memcpy(mValues[index].mData, &value, sizeof(T));
  331. } else if (nullptr != mValues[index].mData && AI_AIMETADATA == mValues[index].mType) {
  332. *static_cast<T *>(mValues[index].mData) = value;
  333. } else {
  334. if (nullptr != mValues[index].mData) {
  335. delete static_cast<T *>(mValues[index].mData);
  336. mValues[index].mData = nullptr;
  337. }
  338. mValues[index].mData = new T(value);
  339. }
  340. return true;
  341. }
  342. template <typename T>
  343. inline bool Set(const std::string &key, const T &value) {
  344. if (key.empty()) {
  345. return false;
  346. }
  347. bool result = false;
  348. for (unsigned int i = 0; i < mNumProperties; ++i) {
  349. if (key == mKeys[i].C_Str()) {
  350. Set(i, key, value);
  351. result = true;
  352. break;
  353. }
  354. }
  355. return result;
  356. }
  357. template <typename T>
  358. inline bool Get(unsigned index, T &value) const {
  359. // In range assertion
  360. if (index >= mNumProperties) {
  361. return false;
  362. }
  363. // Return false if the output data type does
  364. // not match the found value's data type
  365. if (GetAiType(value) != mValues[index].mType) {
  366. return false;
  367. }
  368. // Otherwise, output the found value and
  369. // return true
  370. value = *static_cast<T *>(mValues[index].mData);
  371. return true;
  372. }
  373. template <typename T>
  374. inline bool Get(const aiString &key, T &value) const {
  375. // Search for the given key
  376. for (unsigned int i = 0; i < mNumProperties; ++i) {
  377. if (mKeys[i] == key) {
  378. return Get(i, value);
  379. }
  380. }
  381. return false;
  382. }
  383. template <typename T>
  384. inline bool Get(const std::string &key, T &value) const {
  385. return Get(aiString(key), value);
  386. }
  387. /// Return metadata entry for analyzing it by user.
  388. /// \param [in] pIndex - index of the entry.
  389. /// \param [out] pKey - pointer to the key value.
  390. /// \param [out] pEntry - pointer to the entry: type and value.
  391. /// \return false - if pIndex is out of range, else - true.
  392. inline bool Get(size_t index, const aiString *&key, const aiMetadataEntry *&entry) const {
  393. if (index >= mNumProperties) {
  394. return false;
  395. }
  396. key = &mKeys[index];
  397. entry = &mValues[index];
  398. return true;
  399. }
  400. /// Check whether there is a metadata entry for the given key.
  401. /// \param [in] Key - the key value value to check for.
  402. inline bool HasKey(const char *key) const {
  403. if (nullptr == key) {
  404. return false;
  405. }
  406. // Search for the given key
  407. for (unsigned int i = 0; i < mNumProperties; ++i) {
  408. if (0 == strncmp(mKeys[i].C_Str(), key, mKeys[i].length)) {
  409. return true;
  410. }
  411. }
  412. return false;
  413. }
  414. friend bool CompareKeys(const aiMetadata &lhs, const aiMetadata &rhs) {
  415. if (lhs.mNumProperties != rhs.mNumProperties) {
  416. return false;
  417. }
  418. for (unsigned int i = 0; i < lhs.mNumProperties; ++i) {
  419. if (lhs.mKeys[i] != rhs.mKeys[i]) {
  420. return false;
  421. }
  422. }
  423. return true;
  424. }
  425. friend bool CompareValues(const aiMetadata &lhs, const aiMetadata &rhs) {
  426. if (lhs.mNumProperties != rhs.mNumProperties) {
  427. return false;
  428. }
  429. for (unsigned int i = 0; i < lhs.mNumProperties; ++i) {
  430. if (lhs.mValues[i].mType != rhs.mValues[i].mType) {
  431. return false;
  432. }
  433. switch (lhs.mValues[i].mType) {
  434. case AI_BOOL: {
  435. if (*static_cast<bool *>(lhs.mValues[i].mData) != *static_cast<bool *>(rhs.mValues[i].mData)) {
  436. return false;
  437. }
  438. } break;
  439. case AI_INT32: {
  440. if (*static_cast<int32_t *>(lhs.mValues[i].mData) != *static_cast<int32_t *>(rhs.mValues[i].mData)) {
  441. return false;
  442. }
  443. } break;
  444. case AI_UINT64: {
  445. if (*static_cast<uint64_t *>(lhs.mValues[i].mData) != *static_cast<uint64_t *>(rhs.mValues[i].mData)) {
  446. return false;
  447. }
  448. } break;
  449. case AI_FLOAT: {
  450. if (*static_cast<float *>(lhs.mValues[i].mData) != *static_cast<float *>(rhs.mValues[i].mData)) {
  451. return false;
  452. }
  453. } break;
  454. case AI_DOUBLE: {
  455. if (*static_cast<double *>(lhs.mValues[i].mData) != *static_cast<double *>(rhs.mValues[i].mData)) {
  456. return false;
  457. }
  458. } break;
  459. case AI_AISTRING: {
  460. if (*static_cast<aiString *>(lhs.mValues[i].mData) != *static_cast<aiString *>(rhs.mValues[i].mData)) {
  461. return false;
  462. }
  463. } break;
  464. case AI_AIVECTOR3D: {
  465. if (*static_cast<aiVector3D *>(lhs.mValues[i].mData) != *static_cast<aiVector3D *>(rhs.mValues[i].mData)) {
  466. return false;
  467. }
  468. } break;
  469. case AI_AIMETADATA: {
  470. if (*static_cast<aiMetadata *>(lhs.mValues[i].mData) != *static_cast<aiMetadata *>(rhs.mValues[i].mData)) {
  471. return false;
  472. }
  473. } break;
  474. case AI_INT64: {
  475. if (*static_cast<int64_t *>(lhs.mValues[i].mData) != *static_cast<int64_t *>(rhs.mValues[i].mData)) {
  476. return false;
  477. }
  478. } break;
  479. case AI_UINT32: {
  480. if (*static_cast<uint32_t *>(lhs.mValues[i].mData) != *static_cast<uint32_t *>(rhs.mValues[i].mData)) {
  481. return false;
  482. }
  483. } break;
  484. #ifndef SWIG
  485. case FORCE_32BIT:
  486. #endif
  487. default:
  488. break;
  489. }
  490. }
  491. return true;
  492. }
  493. friend bool operator==(const aiMetadata &lhs, const aiMetadata &rhs) {
  494. return CompareKeys(lhs, rhs) && CompareValues(lhs, rhs);
  495. }
  496. friend bool operator!=(const aiMetadata &lhs, const aiMetadata &rhs) {
  497. return !(lhs == rhs);
  498. }
  499. #endif // __cplusplus
  500. };
  501. #endif // AI_METADATA_H_INC