Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

225 Zeilen
6.8 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 OFFLoader.cpp
  35. * @brief Implementation of the OFF importer class
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
  39. // internal headers
  40. #include "OFFLoader.h"
  41. #include "ParsingUtils.h"
  42. #include "fast_atof.h"
  43. using namespace Assimp;
  44. static const aiImporterDesc desc = {
  45. "OFF Importer",
  46. "",
  47. "",
  48. "",
  49. aiImporterFlags_SupportBinaryFlavour,
  50. 0,
  51. 0,
  52. 0,
  53. 0,
  54. "off"
  55. };
  56. // ------------------------------------------------------------------------------------------------
  57. // Constructor to be privately used by Importer
  58. OFFImporter::OFFImporter()
  59. {}
  60. // ------------------------------------------------------------------------------------------------
  61. // Destructor, private as well
  62. OFFImporter::~OFFImporter()
  63. {}
  64. // ------------------------------------------------------------------------------------------------
  65. // Returns whether the class can handle the format of the given file.
  66. bool OFFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  67. {
  68. const std::string extension = GetExtension(pFile);
  69. if (extension == "off")
  70. return true;
  71. else if (!extension.length() || checkSig)
  72. {
  73. if (!pIOHandler)return true;
  74. const char* tokens[] = {"off"};
  75. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  76. }
  77. return false;
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. const aiImporterDesc* OFFImporter::GetInfo () const
  81. {
  82. return &desc;
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // Imports the given file into the given scene structure.
  86. void OFFImporter::InternReadFile( const std::string& pFile,
  87. aiScene* pScene, IOSystem* pIOHandler)
  88. {
  89. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  90. // Check whether we can read from the file
  91. if( file.get() == NULL) {
  92. throw DeadlyImportError( "Failed to open OFF file " + pFile + ".");
  93. }
  94. // allocate storage and copy the contents of the file to a memory buffer
  95. std::vector<char> mBuffer2;
  96. TextFileToBuffer(file.get(),mBuffer2);
  97. const char* buffer = &mBuffer2[0];
  98. char line[4096];
  99. GetNextLine(buffer,line);
  100. if ('O' == line[0]) {
  101. GetNextLine(buffer,line); // skip the 'OFF' line
  102. }
  103. const char* sz = line; SkipSpaces(&sz);
  104. const unsigned int numVertices = strtoul10(sz,&sz);SkipSpaces(&sz);
  105. const unsigned int numFaces = strtoul10(sz,&sz);
  106. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = 1 ];
  107. aiMesh* mesh = pScene->mMeshes[0] = new aiMesh();
  108. aiFace* faces = mesh->mFaces = new aiFace [mesh->mNumFaces = numFaces];
  109. std::vector<aiVector3D> tempPositions(numVertices);
  110. // now read all vertex lines
  111. for (unsigned int i = 0; i< numVertices;++i)
  112. {
  113. if(!GetNextLine(buffer,line))
  114. {
  115. DefaultLogger::get()->error("OFF: The number of verts in the header is incorrect");
  116. break;
  117. }
  118. aiVector3D& v = tempPositions[i];
  119. sz = line; SkipSpaces(&sz);
  120. sz = fast_atoreal_move<float>(sz,(float&)v.x); SkipSpaces(&sz);
  121. sz = fast_atoreal_move<float>(sz,(float&)v.y); SkipSpaces(&sz);
  122. fast_atoreal_move<float>(sz,(float&)v.z);
  123. }
  124. // First find out how many vertices we'll need
  125. const char* old = buffer;
  126. for (unsigned int i = 0; i< mesh->mNumFaces;++i)
  127. {
  128. if(!GetNextLine(buffer,line))
  129. {
  130. DefaultLogger::get()->error("OFF: The number of faces in the header is incorrect");
  131. break;
  132. }
  133. sz = line;SkipSpaces(&sz);
  134. if(!(faces->mNumIndices = strtoul10(sz,&sz)) || faces->mNumIndices > 9)
  135. {
  136. DefaultLogger::get()->error("OFF: Faces with zero indices aren't allowed");
  137. --mesh->mNumFaces;
  138. continue;
  139. }
  140. mesh->mNumVertices += faces->mNumIndices;
  141. ++faces;
  142. }
  143. if (!mesh->mNumVertices)
  144. throw DeadlyImportError("OFF: There are no valid faces");
  145. // allocate storage for the output vertices
  146. aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  147. // second: now parse all face indices
  148. buffer = old;faces = mesh->mFaces;
  149. for (unsigned int i = 0, p = 0; i< mesh->mNumFaces;)
  150. {
  151. if(!GetNextLine(buffer,line))break;
  152. unsigned int idx;
  153. sz = line;SkipSpaces(&sz);
  154. if(!(idx = strtoul10(sz,&sz)) || idx > 9)
  155. continue;
  156. faces->mIndices = new unsigned int [faces->mNumIndices];
  157. for (unsigned int m = 0; m < faces->mNumIndices;++m)
  158. {
  159. SkipSpaces(&sz);
  160. if ((idx = strtoul10(sz,&sz)) >= numVertices)
  161. {
  162. DefaultLogger::get()->error("OFF: Vertex index is out of range");
  163. idx = numVertices-1;
  164. }
  165. faces->mIndices[m] = p++;
  166. *verts++ = tempPositions[idx];
  167. }
  168. ++i;
  169. ++faces;
  170. }
  171. // generate the output node graph
  172. pScene->mRootNode = new aiNode();
  173. pScene->mRootNode->mName.Set("<OFFRoot>");
  174. pScene->mRootNode->mMeshes = new unsigned int [pScene->mRootNode->mNumMeshes = 1];
  175. pScene->mRootNode->mMeshes[0] = 0;
  176. // generate a default material
  177. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = 1];
  178. aiMaterial* pcMat = new aiMaterial();
  179. aiColor4D clr(0.6f,0.6f,0.6f,1.0f);
  180. pcMat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  181. pScene->mMaterials[0] = pcMat;
  182. const int twosided =1;
  183. pcMat->AddProperty(&twosided,1,AI_MATKEY_TWOSIDED);
  184. }
  185. #endif // !! ASSIMP_BUILD_NO_OFF_IMPORTER