X3DImporter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 X3DImporter.cpp
  34. /// \brief X3D-format files importer for Assimp: main algorithm implementation.
  35. /// \date 2015-2016
  36. /// \author [email protected]
  37. #ifndef ASSIMP_BUILD_NO_X3D_IMPORTER
  38. #include "X3DImporter.hpp"
  39. #include <assimp/StringUtils.h>
  40. // Header files, Assimp.
  41. #include <assimp/DefaultIOSystem.h>
  42. #include <assimp/fast_atof.h>
  43. // Header files, stdlib.
  44. #include <iterator>
  45. #include <memory>
  46. #include <string>
  47. namespace Assimp {
  48. /// Constant which holds the importer description
  49. const aiImporterDesc X3DImporter::Description = {
  50. "Extensible 3D(X3D) Importer",
  51. "smalcom",
  52. "",
  53. "See documentation in source code. Chapter: Limitations.",
  54. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
  55. 0,
  56. 0,
  57. 0,
  58. 0,
  59. "x3d x3db"
  60. };
  61. struct WordIterator {
  62. using iterator_category = std::input_iterator_tag;
  63. using value_type = const char *;
  64. using difference_type = ptrdiff_t;
  65. using pointer = value_type *;
  66. using reference = value_type &;
  67. static const char *whitespace;
  68. const char *mStart, *mEnd;
  69. WordIterator(const char *start, const char *end) :
  70. mStart(start),
  71. mEnd(end) {
  72. mStart = start + ::strspn(start, whitespace);
  73. if (mStart >= mEnd) {
  74. mStart = 0;
  75. }
  76. }
  77. WordIterator() :
  78. mStart(0),
  79. mEnd(0) {}
  80. WordIterator(const WordIterator &other) :
  81. mStart(other.mStart),
  82. mEnd(other.mEnd) {}
  83. WordIterator &operator=(const WordIterator &other) {
  84. mStart = other.mStart;
  85. mEnd = other.mEnd;
  86. return *this;
  87. }
  88. bool operator==(const WordIterator &other) const { return mStart == other.mStart; }
  89. bool operator!=(const WordIterator &other) const { return mStart != other.mStart; }
  90. WordIterator &operator++() {
  91. mStart += strcspn(mStart, whitespace);
  92. mStart += strspn(mStart, whitespace);
  93. if (mStart >= mEnd) {
  94. mStart = 0;
  95. }
  96. return *this;
  97. }
  98. WordIterator operator++(int) {
  99. WordIterator result(*this);
  100. ++(*this);
  101. return result;
  102. }
  103. const char *operator*() const { return mStart; }
  104. };
  105. const char *WordIterator::whitespace = ", \t\r\n";
  106. X3DImporter::X3DImporter() :
  107. mNodeElementCur(nullptr) {
  108. // empty
  109. }
  110. X3DImporter::~X3DImporter() {
  111. // Clear() is accounting if data already is deleted. So, just check again if all data is deleted.
  112. Clear();
  113. }
  114. void X3DImporter::Clear() {
  115. mNodeElementCur = nullptr;
  116. // Delete all elements
  117. if (!NodeElement_List.empty()) {
  118. for (std::list<X3DNodeElementBase *>::iterator it = NodeElement_List.begin(); it != NodeElement_List.end(); ++it) {
  119. delete *it;
  120. }
  121. NodeElement_List.clear();
  122. }
  123. }
  124. void X3DImporter::ParseFile(const std::string &file, IOSystem *pIOHandler) {
  125. ai_assert(nullptr != pIOHandler);
  126. static const std::string mode = "rb";
  127. std::unique_ptr<IOStream> fileStream(pIOHandler->Open(file, mode));
  128. if (!fileStream.get()) {
  129. throw DeadlyImportError("Failed to open file " + file + ".");
  130. }
  131. }
  132. bool X3DImporter::CanRead( const std::string &pFile, IOSystem * /*pIOHandler*/, bool checkSig ) const {
  133. if (checkSig) {
  134. std::string::size_type pos = pFile.find_last_of(".x3d");
  135. if (pos != std::string::npos) {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. void X3DImporter::GetExtensionList( std::set<std::string> &extensionList ) {
  142. extensionList.insert("x3d");
  143. }
  144. void X3DImporter::InternReadFile( const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler ) {
  145. std::shared_ptr<IOStream> stream(pIOHandler->Open(pFile, "rb"));
  146. if (!stream) {
  147. throw DeadlyImportError("Could not open file for reading");
  148. }
  149. pScene->mRootNode = new aiNode(pFile);
  150. }
  151. const aiImporterDesc *X3DImporter::GetInfo() const {
  152. return &Description;
  153. }
  154. }
  155. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER