Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_STL_EXPORTER)
  35. #include "STLExporter.h"
  36. #include "../include/assimp/version.h"
  37. using namespace Assimp;
  38. namespace Assimp {
  39. // ------------------------------------------------------------------------------------------------
  40. // Worker function for exporting a scene to Stereolithograpy. Prototyped and registered in Exporter.cpp
  41. void ExportSceneSTL(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
  42. {
  43. // invoke the exporter
  44. STLExporter exporter(pFile, pScene);
  45. // we're still here - export successfully completed. Write the file.
  46. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  47. if(outfile == NULL) {
  48. throw DeadlyExportError("could not open output .stl file: " + std::string(pFile));
  49. }
  50. outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
  51. }
  52. void ExportSceneSTLBinary(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
  53. {
  54. // invoke the exporter
  55. STLExporter exporter(pFile, pScene, true);
  56. // we're still here - export successfully completed. Write the file.
  57. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  58. if(outfile == NULL) {
  59. throw DeadlyExportError("could not open output .stl file: " + std::string(pFile));
  60. }
  61. outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
  62. }
  63. } // end of namespace Assimp
  64. // ------------------------------------------------------------------------------------------------
  65. STLExporter :: STLExporter(const char* _filename, const aiScene* pScene, bool binary)
  66. : filename(_filename)
  67. , pScene(pScene)
  68. , endl("\n")
  69. {
  70. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  71. const std::locale& l = std::locale("C");
  72. mOutput.imbue(l);
  73. if (binary) {
  74. char buf[80] = {0} ;
  75. buf[0] = 'A'; buf[1] = 's'; buf[2] = 's'; buf[3] = 'i'; buf[4] = 'm'; buf[5] = 'p';
  76. buf[6] = 'S'; buf[7] = 'c'; buf[8] = 'e'; buf[9] = 'n'; buf[10] = 'e';
  77. mOutput.write(buf, 80);
  78. unsigned int meshnum = 0;
  79. for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  80. for (unsigned int j = 0; j < pScene->mMeshes[i]->mNumFaces; ++j) {
  81. meshnum++;
  82. }
  83. }
  84. AI_SWAP4(meshnum);
  85. mOutput.write((char *)&meshnum, 4);
  86. for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  87. WriteMeshBinary(pScene->mMeshes[i]);
  88. }
  89. } else {
  90. const std::string& name = "AssimpScene";
  91. mOutput << "solid " << name << endl;
  92. for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  93. WriteMesh(pScene->mMeshes[i]);
  94. }
  95. mOutput << "endsolid " << name << endl;
  96. }
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. void STLExporter :: WriteMesh(const aiMesh* m)
  100. {
  101. for (unsigned int i = 0; i < m->mNumFaces; ++i) {
  102. const aiFace& f = m->mFaces[i];
  103. // we need per-face normals. We specified aiProcess_GenNormals as pre-requisite for this exporter,
  104. // but nonetheless we have to expect per-vertex normals.
  105. aiVector3D nor;
  106. if (m->mNormals) {
  107. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  108. nor += m->mNormals[f.mIndices[a]];
  109. }
  110. nor.Normalize();
  111. }
  112. mOutput << " facet normal " << nor.x << " " << nor.y << " " << nor.z << endl;
  113. mOutput << " outer loop" << endl;
  114. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  115. const aiVector3D& v = m->mVertices[f.mIndices[a]];
  116. mOutput << " vertex " << v.x << " " << v.y << " " << v.z << endl;
  117. }
  118. mOutput << " endloop" << endl;
  119. mOutput << " endfacet" << endl << endl;
  120. }
  121. }
  122. void STLExporter :: WriteMeshBinary(const aiMesh* m)
  123. {
  124. for (unsigned int i = 0; i < m->mNumFaces; ++i) {
  125. const aiFace& f = m->mFaces[i];
  126. // we need per-face normals. We specified aiProcess_GenNormals as pre-requisite for this exporter,
  127. // but nonetheless we have to expect per-vertex normals.
  128. aiVector3D nor;
  129. if (m->mNormals) {
  130. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  131. nor += m->mNormals[f.mIndices[a]];
  132. }
  133. nor.Normalize();
  134. }
  135. float nx = nor.x, ny = nor.y, nz = nor.z;
  136. AI_SWAP4(nx); AI_SWAP4(ny); AI_SWAP4(nz);
  137. mOutput.write((char *)&nx, 4); mOutput.write((char *)&ny, 4); mOutput.write((char *)&nz, 4);
  138. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  139. const aiVector3D& v = m->mVertices[f.mIndices[a]];
  140. float vx = v.x, vy = v.y, vz = v.z;
  141. AI_SWAP4(vx); AI_SWAP4(vy); AI_SWAP4(vz);
  142. mOutput.write((char *)&vx, 4); mOutput.write((char *)&vy, 4); mOutput.write((char *)&vz, 4);
  143. }
  144. char dummy[2] = {0};
  145. mOutput.write(dummy, 2);
  146. }
  147. }
  148. #endif