RawLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, 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 RawLoader.cpp
  35. * @brief Implementation of the RAW importer class
  36. */
  37. #ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
  38. // internal headers
  39. #include "RawLoader.h"
  40. #include <assimp/ParsingUtils.h>
  41. #include <assimp/fast_atof.h>
  42. #include <assimp/importerdesc.h>
  43. #include <assimp/scene.h>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <assimp/IOSystem.hpp>
  46. #include <memory>
  47. namespace Assimp {
  48. static constexpr aiImporterDesc desc = {
  49. "Raw Importer",
  50. "",
  51. "",
  52. "",
  53. aiImporterFlags_SupportTextFlavour,
  54. 0,
  55. 0,
  56. 0,
  57. 0,
  58. "raw"
  59. };
  60. // ------------------------------------------------------------------------------------------------
  61. // Returns whether the class can handle the format of the given file.
  62. bool RAWImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const {
  63. return SimpleExtensionCheck(filename, "raw");
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. const aiImporterDesc *RAWImporter::GetInfo() const {
  67. return &desc;
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Imports the given file into the given scene structure.
  71. void RAWImporter::InternReadFile(const std::string &pFile,
  72. aiScene *pScene, IOSystem *pIOHandler) {
  73. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
  74. // Check whether we can read from the file
  75. if (file == nullptr) {
  76. throw DeadlyImportError("Failed to open RAW file ", pFile, ".");
  77. }
  78. // allocate storage and copy the contents of the file to a memory buffer
  79. // (terminate it with zero)
  80. std::vector<char> mBuffer2;
  81. TextFileToBuffer(file.get(), mBuffer2);
  82. const char *buffer = &mBuffer2[0];
  83. // list of groups loaded from the file
  84. std::vector<GroupInformation> outGroups(1, GroupInformation("<default>"));
  85. std::vector<GroupInformation>::iterator curGroup = outGroups.begin();
  86. // now read all lines
  87. char line[4096];
  88. const char *end = &line[4096];
  89. while (GetNextLine(buffer, line)) {
  90. // if the line starts with a non-numeric identifier, it marks
  91. // the beginning of a new group
  92. const char *sz = line;
  93. SkipSpaces(&sz, end);
  94. if (IsLineEnd(*sz)) continue;
  95. if (!IsNumeric(*sz)) {
  96. const char *sz2 = sz;
  97. while (!IsSpaceOrNewLine(*sz2))
  98. ++sz2;
  99. const unsigned int length = (unsigned int)(sz2 - sz);
  100. // find an existing group with this name
  101. for (std::vector<GroupInformation>::iterator it = outGroups.begin(), endIt = outGroups.end();
  102. it != endIt; ++it) {
  103. if (length == (*it).name.length() && !::strcmp(sz, (*it).name.c_str())) {
  104. curGroup = it;
  105. sz2 = nullptr;
  106. break;
  107. }
  108. }
  109. if (sz2) {
  110. outGroups.emplace_back(std::string(sz, length));
  111. curGroup = outGroups.end() - 1;
  112. }
  113. } else {
  114. // there can be maximally 12 floats plus an extra texture file name
  115. float data[12];
  116. unsigned int num;
  117. for (num = 0; num < 12; ++num) {
  118. if (!SkipSpaces(&sz, end) || !IsNumeric(*sz)) break;
  119. sz = fast_atoreal_move<float>(sz, data[num]);
  120. }
  121. if (num != 12 && num != 9) {
  122. ASSIMP_LOG_ERROR("A line may have either 9 or 12 floats and an optional texture");
  123. continue;
  124. }
  125. MeshInformation *output = nullptr;
  126. const char *sz2 = sz;
  127. unsigned int length;
  128. if (!IsLineEnd(*sz)) {
  129. while (!IsSpaceOrNewLine(*sz2))
  130. ++sz2;
  131. length = (unsigned int)(sz2 - sz);
  132. } else if (9 == num) {
  133. sz = "%default%";
  134. length = 9;
  135. } else {
  136. sz = "";
  137. length = 0;
  138. }
  139. // search in the list of meshes whether we have one with this texture
  140. for (auto &mesh : (*curGroup).meshes) {
  141. if (length == mesh.name.length() && (length ? !::strcmp(sz, mesh.name.c_str()) : true)) {
  142. output = &mesh;
  143. break;
  144. }
  145. }
  146. // if we don't have the mesh, create it
  147. if (!output) {
  148. (*curGroup).meshes.emplace_back(std::string(sz, length));
  149. output = &((*curGroup).meshes.back());
  150. }
  151. if (12 == num) {
  152. aiColor4D v(data[0], data[1], data[2], 1.0f);
  153. output->colors.push_back(v);
  154. output->colors.push_back(v);
  155. output->colors.push_back(v);
  156. output->vertices.emplace_back(data[3], data[4], data[5]);
  157. output->vertices.emplace_back(data[6], data[7], data[8]);
  158. output->vertices.emplace_back(data[9], data[10], data[11]);
  159. } else {
  160. output->vertices.emplace_back(data[0], data[1], data[2]);
  161. output->vertices.emplace_back(data[3], data[4], data[5]);
  162. output->vertices.emplace_back(data[6], data[7], data[8]);
  163. }
  164. }
  165. }
  166. pScene->mRootNode = new aiNode();
  167. pScene->mRootNode->mName.Set("<RawRoot>");
  168. // count the number of valid groups
  169. // (meshes can't be empty)
  170. for (auto &outGroup : outGroups) {
  171. if (!outGroup.meshes.empty()) {
  172. ++pScene->mRootNode->mNumChildren;
  173. pScene->mNumMeshes += (unsigned int)outGroup.meshes.size();
  174. }
  175. }
  176. if (!pScene->mNumMeshes) {
  177. throw DeadlyImportError("RAW: No meshes loaded. The file seems to be corrupt or empty.");
  178. }
  179. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
  180. aiNode **cc;
  181. if (1 == pScene->mRootNode->mNumChildren) {
  182. cc = &pScene->mRootNode;
  183. pScene->mRootNode->mNumChildren = 0;
  184. } else {
  185. cc = new aiNode *[pScene->mRootNode->mNumChildren];
  186. memset(cc, 0, sizeof(aiNode *) * pScene->mRootNode->mNumChildren);
  187. pScene->mRootNode->mChildren = cc;
  188. }
  189. pScene->mNumMaterials = pScene->mNumMeshes;
  190. aiMaterial **mats = pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
  191. unsigned int meshIdx = 0;
  192. for (auto &outGroup : outGroups) {
  193. if (outGroup.meshes.empty()) continue;
  194. aiNode *node;
  195. if (pScene->mRootNode->mNumChildren) {
  196. node = *cc = new aiNode();
  197. node->mParent = pScene->mRootNode;
  198. } else
  199. node = *cc;
  200. node->mName.Set(outGroup.name);
  201. // add all meshes
  202. node->mNumMeshes = (unsigned int)outGroup.meshes.size();
  203. unsigned int *pi = node->mMeshes = new unsigned int[node->mNumMeshes];
  204. for (std::vector<MeshInformation>::iterator it2 = outGroup.meshes.begin(),
  205. end2 = outGroup.meshes.end();
  206. it2 != end2; ++it2) {
  207. ai_assert(!(*it2).vertices.empty());
  208. // allocate the mesh
  209. *pi++ = meshIdx;
  210. aiMesh *mesh = pScene->mMeshes[meshIdx] = new aiMesh();
  211. mesh->mMaterialIndex = meshIdx++;
  212. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  213. // allocate storage for the vertex components and copy them
  214. mesh->mNumVertices = (unsigned int)(*it2).vertices.size();
  215. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  216. ::memcpy(mesh->mVertices, &(*it2).vertices[0], sizeof(aiVector3D) * mesh->mNumVertices);
  217. if ((*it2).colors.size()) {
  218. ai_assert((*it2).colors.size() == mesh->mNumVertices);
  219. mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
  220. ::memcpy(mesh->mColors[0], &(*it2).colors[0], sizeof(aiColor4D) * mesh->mNumVertices);
  221. }
  222. // generate triangles
  223. ai_assert(0 == mesh->mNumVertices % 3);
  224. aiFace *fc = mesh->mFaces = new aiFace[mesh->mNumFaces = mesh->mNumVertices / 3];
  225. aiFace *const fcEnd = fc + mesh->mNumFaces;
  226. unsigned int n = 0;
  227. while (fc != fcEnd) {
  228. aiFace &f = *fc++;
  229. f.mIndices = new unsigned int[f.mNumIndices = 3];
  230. for (unsigned int m = 0; m < 3; ++m)
  231. f.mIndices[m] = n++;
  232. }
  233. // generate a material for the mesh
  234. aiMaterial *mat = new aiMaterial();
  235. aiColor4D clr(1.0f, 1.0f, 1.0f, 1.0f);
  236. if ("%default%" == (*it2).name) // a gray default material
  237. {
  238. clr.r = clr.g = clr.b = 0.6f;
  239. } else if ((*it2).name.length() > 0) // a texture
  240. {
  241. aiString s;
  242. s.Set((*it2).name);
  243. mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
  244. }
  245. mat->AddProperty<aiColor4D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
  246. *mats++ = mat;
  247. }
  248. }
  249. }
  250. } // namespace Assimp
  251. #endif // !! ASSIMP_BUILD_NO_RAW_IMPORTER