NDOLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /** @file NDOLoader.cpp
  35. * Implementation of the NDO importer class.
  36. */
  37. #ifndef ASSIMP_BUILD_NO_NDO_IMPORTER
  38. #include "NDOLoader.h"
  39. #include <assimp/DefaultLogger.hpp>
  40. #include <assimp/IOSystem.hpp>
  41. #include <assimp/scene.h>
  42. #include <assimp/importerdesc.h>
  43. #include <assimp/StreamReader.h>
  44. #include <map>
  45. #include <limits>
  46. using namespace Assimp;
  47. static constexpr aiImporterDesc desc = {
  48. "Nendo Mesh Importer",
  49. "",
  50. "",
  51. "http://www.izware.com/nendo/index.htm",
  52. aiImporterFlags_SupportBinaryFlavour,
  53. 0,
  54. 0,
  55. 0,
  56. 0,
  57. "ndo"
  58. };
  59. // ------------------------------------------------------------------------------------------------
  60. // Returns whether the class can handle the format of the given file.
  61. bool NDOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const
  62. {
  63. static const char* tokens[] = {"nendo"};
  64. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,AI_COUNT_OF(tokens),5);
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Build a string of all file extensions supported
  68. const aiImporterDesc* NDOImporter::GetInfo () const
  69. {
  70. return &desc;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Setup configuration properties for the loader
  74. void NDOImporter::SetupProperties(const Importer* /*pImp*/)
  75. {
  76. // nothing to be done for the moment
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Helper function to process edges and vertices for a face
  80. void ProcessFaceEdgesAndVertices(const NDOImporter::Object& obj,
  81. unsigned int start_edge, unsigned int key,
  82. std::vector<aiVector3D>& vertices, std::vector<unsigned int>& indices)
  83. {
  84. unsigned int cur_edge = start_edge;
  85. do {
  86. unsigned int next_edge, next_vert;
  87. if (key == obj.edges[cur_edge].edge[3]) {
  88. next_edge = obj.edges[cur_edge].edge[5];
  89. next_vert = obj.edges[cur_edge].edge[1];
  90. }
  91. else {
  92. next_edge = obj.edges[cur_edge].edge[4];
  93. next_vert = obj.edges[cur_edge].edge[0];
  94. }
  95. indices.push_back( static_cast<unsigned int>(vertices.size()) );
  96. if (next_vert < obj.vertices.size()) {
  97. vertices.push_back(obj.vertices[ next_vert ].val);
  98. }
  99. else {
  100. ASSIMP_LOG_WARN("NDOImporter: next_vert is out of bounds, skipping invalid access.");
  101. break;
  102. }
  103. cur_edge = next_edge;
  104. } while (cur_edge != start_edge);
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. // Imports the given file into the given scene structure.
  108. void NDOImporter::InternReadFile( const std::string& pFile,
  109. aiScene* pScene, IOSystem* pIOHandler)
  110. {
  111. auto file = pIOHandler->Open( pFile, "rb");
  112. if (!file) {
  113. throw DeadlyImportError("Nendo: Could not open ", pFile);
  114. }
  115. StreamReaderBE reader(file);
  116. // first 9 bytes are nendo file format ("nendo 1.n")
  117. const char* head = (const char*)reader.GetPtr();
  118. reader.IncPtr(9);
  119. if (strncmp("nendo ",head,6)) {
  120. throw DeadlyImportError("Not a Nendo file; magic signature missing");
  121. }
  122. // check if this is a supported version. if not, continue, too -- users,
  123. // please don't complain if it doesn't work then ...
  124. unsigned int file_format = 12;
  125. if (!strncmp("1.0",head+6,3)) {
  126. file_format = 10;
  127. ASSIMP_LOG_INFO("NDO file format is 1.0");
  128. }
  129. else if (!strncmp("1.1",head+6,3)) {
  130. file_format = 11;
  131. ASSIMP_LOG_INFO("NDO file format is 1.1");
  132. }
  133. else if (!strncmp("1.2",head+6,3)) {
  134. file_format = 12;
  135. ASSIMP_LOG_INFO("NDO file format is 1.2");
  136. }
  137. else {
  138. char buff[4] = {0};
  139. memcpy(buff, head+6, 3);
  140. ASSIMP_LOG_WARN( "Unrecognized nendo file format version, continuing happily ... :", buff);
  141. }
  142. reader.IncPtr(2); /* skip flags */
  143. if (file_format >= 12) {
  144. reader.IncPtr(2);
  145. }
  146. unsigned int temp = reader.GetU1();
  147. std::vector<Object> objects(temp); /* buffer to store all the loaded objects in */
  148. // read all objects
  149. for (unsigned int o = 0; o < objects.size(); ++o) {
  150. // if (file_format < 12) {
  151. if (!reader.GetI1()) {
  152. continue; /* skip over empty object */
  153. }
  154. // reader.GetI2();
  155. // }
  156. Object& obj = objects[o];
  157. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  158. head = (const char*)reader.GetPtr();
  159. if (std::numeric_limits<unsigned int>::max() - 76 < temp) {
  160. throw DeadlyImportError("Invalid name length");
  161. }
  162. reader.IncPtr(temp + 76); /* skip unknown stuff */
  163. obj.name = std::string(head, temp);
  164. // read edge table
  165. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  166. obj.edges.reserve(temp);
  167. for (unsigned int e = 0; e < temp; ++e) {
  168. obj.edges.emplace_back();
  169. Edge& edge = obj.edges.back();
  170. for (unsigned int i = 0; i< 8; ++i) {
  171. edge.edge[i] = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  172. }
  173. edge.hard = file_format >= 11 ? reader.GetU1() : 0;
  174. for (unsigned int i = 0; i< 8; ++i) {
  175. edge.color[i] = reader.GetU1();
  176. }
  177. }
  178. // read face table
  179. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  180. obj.faces.reserve(temp);
  181. for (unsigned int e = 0; e < temp; ++e) {
  182. obj.faces.emplace_back();
  183. Face& face = obj.faces.back();
  184. face.elem = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  185. }
  186. // read vertex table
  187. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  188. obj.vertices.reserve(temp);
  189. for (unsigned int e = 0; e < temp; ++e) {
  190. obj.vertices.emplace_back();
  191. Vertex& v = obj.vertices.back();
  192. v.num = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  193. v.val.x = reader.GetF4();
  194. v.val.y = reader.GetF4();
  195. v.val.z = reader.GetF4();
  196. }
  197. // read UVs
  198. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  199. for (unsigned int e = 0; e < temp; ++e) {
  200. file_format >= 12 ? reader.GetU4() : reader.GetU2();
  201. }
  202. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  203. for (unsigned int e = 0; e < temp; ++e) {
  204. file_format >= 12 ? reader.GetU4() : reader.GetU2();
  205. }
  206. if (reader.GetU1()) {
  207. const unsigned int x = reader.GetU2(), y = reader.GetU2();
  208. temp = 0;
  209. while (temp < x*y) {
  210. unsigned int repeat = reader.GetU1();
  211. reader.GetU1();
  212. reader.GetU1();
  213. reader.GetU1();
  214. temp += repeat;
  215. }
  216. }
  217. }
  218. // construct a dummy node graph and add all named objects as child nodes
  219. aiNode* root = pScene->mRootNode = new aiNode("$NDODummyRoot");
  220. aiNode** cc = root->mChildren = new aiNode* [ root->mNumChildren = static_cast<unsigned int>( objects.size()) ] ();
  221. pScene->mMeshes = new aiMesh* [ root->mNumChildren] ();
  222. std::vector<aiVector3D> vertices;
  223. std::vector<unsigned int> indices;
  224. for(const Object& obj : objects) {
  225. aiNode* nd = *cc++ = new aiNode(obj.name);
  226. nd->mParent = root;
  227. // translated from a python dict() - a vector might be sufficient as well
  228. typedef std::map<unsigned int, unsigned int> FaceTable;
  229. FaceTable face_table;
  230. unsigned int n = 0;
  231. for(const Edge& edge : obj.edges) {
  232. face_table[edge.edge[2]] = n;
  233. face_table[edge.edge[3]] = n;
  234. ++n;
  235. }
  236. aiMesh* mesh = new aiMesh();
  237. mesh->mNumFaces=static_cast<unsigned int>(face_table.size());
  238. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  239. vertices.clear();
  240. vertices.reserve(4 * face_table.size()); // arbitrarily chosen
  241. for(FaceTable::value_type& v : face_table) {
  242. indices.clear();
  243. aiFace& f = *faces++;
  244. ProcessFaceEdgesAndVertices(obj, v.second, v.first, vertices, indices);
  245. f.mIndices = new unsigned int[f.mNumIndices = static_cast<unsigned int>(indices.size())];
  246. std::copy(indices.begin(),indices.end(),f.mIndices);
  247. }
  248. mesh->mVertices = new aiVector3D[mesh->mNumVertices = static_cast<unsigned int>(vertices.size())];
  249. std::copy(vertices.begin(),vertices.end(),mesh->mVertices);
  250. if (mesh->mNumVertices) {
  251. pScene->mMeshes[pScene->mNumMeshes] = mesh;
  252. (nd->mMeshes = new unsigned int[nd->mNumMeshes=1])[0]=pScene->mNumMeshes++;
  253. }else
  254. delete mesh;
  255. }
  256. }
  257. #endif