RawLoader.cpp 11 KB

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