RawLoader.cpp 11 KB

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