scene.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <assimp/scene.h>
  35. #include "ScenePrivate.h"
  36. aiScene::aiScene() :
  37. mFlags(0),
  38. mRootNode(nullptr),
  39. mNumMeshes(0),
  40. mMeshes(nullptr),
  41. mNumMaterials(0),
  42. mMaterials(nullptr),
  43. mNumAnimations(0),
  44. mAnimations(nullptr),
  45. mNumTextures(0),
  46. mTextures(nullptr),
  47. mNumLights(0),
  48. mLights(nullptr),
  49. mNumCameras(0),
  50. mCameras(nullptr),
  51. mMetaData(nullptr),
  52. mName(),
  53. mNumSkeletons(0),
  54. mSkeletons(nullptr),
  55. mPrivate(new Assimp::ScenePrivateData()) {
  56. // empty
  57. }
  58. aiScene::~aiScene() {
  59. // delete all sub-objects recursively
  60. delete mRootNode;
  61. // To make sure we won't crash if the data is invalid it's
  62. // much better to check whether both mNumXXX and mXXX are
  63. // valid instead of relying on just one of them.
  64. if (mNumMeshes && mMeshes) {
  65. for (unsigned int a = 0; a < mNumMeshes; ++a) {
  66. delete mMeshes[a];
  67. }
  68. }
  69. delete[] mMeshes;
  70. if (mNumMaterials && mMaterials) {
  71. for (unsigned int a = 0; a < mNumMaterials; ++a) {
  72. delete mMaterials[a];
  73. }
  74. }
  75. delete[] mMaterials;
  76. if (mNumAnimations && mAnimations) {
  77. for (unsigned int a = 0; a < mNumAnimations; ++a) {
  78. delete mAnimations[a];
  79. }
  80. }
  81. delete[] mAnimations;
  82. if (mNumTextures && mTextures) {
  83. for (unsigned int a = 0; a < mNumTextures; ++a) {
  84. delete mTextures[a];
  85. }
  86. }
  87. delete[] mTextures;
  88. if (mNumLights && mLights) {
  89. for (unsigned int a = 0; a < mNumLights; ++a) {
  90. delete mLights[a];
  91. }
  92. }
  93. delete[] mLights;
  94. if (mNumCameras && mCameras) {
  95. for (unsigned int a = 0; a < mNumCameras; ++a) {
  96. delete mCameras[a];
  97. }
  98. }
  99. delete[] mCameras;
  100. aiMetadata::Dealloc(mMetaData);
  101. delete[] mSkeletons;
  102. delete static_cast<Assimp::ScenePrivateData *>(mPrivate);
  103. }
  104. aiNode::aiNode() :
  105. mName(""),
  106. mParent(nullptr),
  107. mNumChildren(0),
  108. mChildren(nullptr),
  109. mNumMeshes(0),
  110. mMeshes(nullptr),
  111. mMetaData(nullptr) {
  112. // empty
  113. }
  114. aiNode::aiNode(const std::string &name) :
  115. mName(name),
  116. mParent(nullptr),
  117. mNumChildren(0),
  118. mChildren(nullptr),
  119. mNumMeshes(0),
  120. mMeshes(nullptr),
  121. mMetaData(nullptr) {
  122. // empty
  123. }
  124. /** Destructor */
  125. aiNode::~aiNode() {
  126. // delete all children recursively
  127. // to make sure we won't crash if the data is invalid ...
  128. if (mNumChildren && mChildren) {
  129. for (unsigned int a = 0; a < mNumChildren; a++)
  130. delete mChildren[a];
  131. }
  132. delete[] mChildren;
  133. delete[] mMeshes;
  134. delete mMetaData;
  135. }
  136. const aiNode *aiNode::FindNode(const char *name) const {
  137. if (nullptr == name) {
  138. return nullptr;
  139. }
  140. if (!::strcmp(mName.data, name)) {
  141. return this;
  142. }
  143. for (unsigned int i = 0; i < mNumChildren; ++i) {
  144. const aiNode *const p = mChildren[i]->FindNode(name);
  145. if (p) {
  146. return p;
  147. }
  148. }
  149. // there is definitely no sub-node with this name
  150. return nullptr;
  151. }
  152. aiNode *aiNode::FindNode(const char *name) {
  153. if (!::strcmp(mName.data, name)) return this;
  154. for (unsigned int i = 0; i < mNumChildren; ++i) {
  155. aiNode *const p = mChildren[i]->FindNode(name);
  156. if (p) {
  157. return p;
  158. }
  159. }
  160. // there is definitely no sub-node with this name
  161. return nullptr;
  162. }
  163. void aiNode::addChildren(unsigned int numChildren, aiNode **children) {
  164. if (nullptr == children || 0 == numChildren) {
  165. return;
  166. }
  167. for (unsigned int i = 0; i < numChildren; i++) {
  168. aiNode *child = children[i];
  169. if (nullptr != child) {
  170. child->mParent = this;
  171. }
  172. }
  173. if (mNumChildren > 0) {
  174. aiNode **tmp = new aiNode *[mNumChildren];
  175. ::memcpy(tmp, mChildren, sizeof(aiNode *) * mNumChildren);
  176. delete[] mChildren;
  177. mChildren = new aiNode *[mNumChildren + numChildren];
  178. ::memcpy(mChildren, tmp, sizeof(aiNode *) * mNumChildren);
  179. ::memcpy(&mChildren[mNumChildren], children, sizeof(aiNode *) * numChildren);
  180. mNumChildren += numChildren;
  181. delete[] tmp;
  182. } else {
  183. mChildren = new aiNode *[numChildren];
  184. for (unsigned int i = 0; i < numChildren; i++) {
  185. mChildren[i] = children[i];
  186. }
  187. mNumChildren = numChildren;
  188. }
  189. }