PlyExporter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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. #if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_PLY_EXPORTER)
  34. #include "PlyExporter.h"
  35. #include <boost/scoped_ptr.hpp>
  36. #include <cmath>
  37. #include "Exceptional.h"
  38. #include "../include/assimp/scene.h"
  39. #include "../include/assimp/version.h"
  40. #include "../include/assimp/IOSystem.hpp"
  41. #include "../include/assimp/Exporter.hpp"
  42. #include "qnan.h"
  43. //using namespace Assimp;
  44. namespace Assimp {
  45. // ------------------------------------------------------------------------------------------------
  46. // Worker function for exporting a scene to PLY. Prototyped and registered in Exporter.cpp
  47. void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  48. {
  49. // invoke the exporter
  50. PlyExporter exporter(pFile, pScene);
  51. // we're still here - export successfully completed. Write the file.
  52. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  53. if(outfile == NULL) {
  54. throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
  55. }
  56. outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
  57. }
  58. void ExportScenePlyBinary(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  59. {
  60. // invoke the exporter
  61. PlyExporter exporter(pFile, pScene, true);
  62. // we're still here - export successfully completed. Write the file.
  63. boost::scoped_ptr<IOStream> outfile(pIOSystem->Open(pFile, "wb"));
  64. if (outfile == NULL) {
  65. throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
  66. }
  67. outfile->Write(exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()), 1);
  68. }
  69. #define PLY_EXPORT_HAS_NORMALS 0x1
  70. #define PLY_EXPORT_HAS_TANGENTS_BITANGENTS 0x2
  71. #define PLY_EXPORT_HAS_TEXCOORDS 0x4
  72. #define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS)
  73. // ------------------------------------------------------------------------------------------------
  74. PlyExporter::PlyExporter(const char* _filename, const aiScene* pScene, bool binary)
  75. : filename(_filename)
  76. , endl("\n")
  77. {
  78. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  79. const std::locale& l = std::locale("C");
  80. mOutput.imbue(l);
  81. unsigned int faces = 0u, vertices = 0u, components = 0u;
  82. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  83. const aiMesh& m = *pScene->mMeshes[i];
  84. faces += m.mNumFaces;
  85. vertices += m.mNumVertices;
  86. if (m.HasNormals()) {
  87. components |= PLY_EXPORT_HAS_NORMALS;
  88. }
  89. if (m.HasTangentsAndBitangents()) {
  90. components |= PLY_EXPORT_HAS_TANGENTS_BITANGENTS;
  91. }
  92. for (unsigned int t = 0; m.HasTextureCoords(t); ++t) {
  93. components |= PLY_EXPORT_HAS_TEXCOORDS << t;
  94. }
  95. for (unsigned int t = 0; m.HasVertexColors(t); ++t) {
  96. components |= PLY_EXPORT_HAS_COLORS << t;
  97. }
  98. }
  99. mOutput << "ply" << endl;
  100. if (binary) {
  101. #if (defined AI_BUILD_BIG_ENDIAN)
  102. mOutput << "format binary_big_endian 1.0" << endl;
  103. #else
  104. mOutput << "format binary_little_endian 1.0" << endl;
  105. #endif
  106. }
  107. else {
  108. mOutput << "format ascii 1.0" << endl;
  109. }
  110. mOutput << "comment Created by Open Asset Import Library - http://assimp.sf.net (v"
  111. << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.'
  112. << aiGetVersionRevision() << ")" << endl;
  113. mOutput << "element vertex " << vertices << endl;
  114. mOutput << "property float x" << endl;
  115. mOutput << "property float y" << endl;
  116. mOutput << "property float z" << endl;
  117. if(components & PLY_EXPORT_HAS_NORMALS) {
  118. mOutput << "property float nx" << endl;
  119. mOutput << "property float ny" << endl;
  120. mOutput << "property float nz" << endl;
  121. }
  122. // write texcoords first, just in case an importer does not support tangents
  123. // bitangents and just skips over the rest of the line upon encountering
  124. // unknown fields (Ply leaves pretty much every vertex component open,
  125. // but in reality most importers only know about vertex positions, normals
  126. // and texture coordinates).
  127. for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
  128. if (!c) {
  129. mOutput << "property float s" << endl;
  130. mOutput << "property float t" << endl;
  131. }
  132. else {
  133. mOutput << "property float s" << c << endl;
  134. mOutput << "property float t" << c << endl;
  135. }
  136. }
  137. for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
  138. if (!c) {
  139. mOutput << "property float r" << endl;
  140. mOutput << "property float g" << endl;
  141. mOutput << "property float b" << endl;
  142. mOutput << "property float a" << endl;
  143. }
  144. else {
  145. mOutput << "property float r" << c << endl;
  146. mOutput << "property float g" << c << endl;
  147. mOutput << "property float b" << c << endl;
  148. mOutput << "property float a" << c << endl;
  149. }
  150. }
  151. if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
  152. mOutput << "property float tx" << endl;
  153. mOutput << "property float ty" << endl;
  154. mOutput << "property float tz" << endl;
  155. mOutput << "property float bx" << endl;
  156. mOutput << "property float by" << endl;
  157. mOutput << "property float bz" << endl;
  158. }
  159. mOutput << "element face " << faces << endl;
  160. // uchar seems to be the most common type for the number of indices per polygon and int seems to be most common for the vertex indices.
  161. // For instance, MeshLab fails to load meshes in which both types are uint. Houdini seems to have problems as well.
  162. // Obviously, using uchar will not work for meshes with polygons with more than 255 indices, but how realistic is this case?
  163. mOutput << "property list uchar int vertex_index" << endl;
  164. mOutput << "end_header" << endl;
  165. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  166. if (binary) {
  167. WriteMeshVertsBinary(pScene->mMeshes[i], components);
  168. }
  169. else {
  170. WriteMeshVerts(pScene->mMeshes[i], components);
  171. }
  172. }
  173. for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) {
  174. if (binary) {
  175. WriteMeshIndicesBinary(pScene->mMeshes[i], ofs);
  176. }
  177. else {
  178. WriteMeshIndices(pScene->mMeshes[i], ofs);
  179. }
  180. ofs += pScene->mMeshes[i]->mNumVertices;
  181. }
  182. }
  183. // ------------------------------------------------------------------------------------------------
  184. PlyExporter::~PlyExporter() {
  185. // empty
  186. }
  187. // ------------------------------------------------------------------------------------------------
  188. void PlyExporter::WriteMeshVerts(const aiMesh* m, unsigned int components)
  189. {
  190. static const float inf = std::numeric_limits<float>::infinity();
  191. // If a component (for instance normal vectors) is present in at least one mesh in the scene,
  192. // then default values are written for meshes that do not contain this component.
  193. for (unsigned int i = 0; i < m->mNumVertices; ++i) {
  194. mOutput <<
  195. m->mVertices[i].x << " " <<
  196. m->mVertices[i].y << " " <<
  197. m->mVertices[i].z
  198. ;
  199. if(components & PLY_EXPORT_HAS_NORMALS) {
  200. if (m->HasNormals() && is_not_qnan(m->mNormals[i].x) && std::fabs(m->mNormals[i].x) != inf) {
  201. mOutput <<
  202. " " << m->mNormals[i].x <<
  203. " " << m->mNormals[i].y <<
  204. " " << m->mNormals[i].z;
  205. }
  206. else {
  207. mOutput << " 0.0 0.0 0.0";
  208. }
  209. }
  210. for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
  211. if (m->HasTextureCoords(c)) {
  212. mOutput <<
  213. " " << m->mTextureCoords[c][i].x <<
  214. " " << m->mTextureCoords[c][i].y;
  215. }
  216. else {
  217. mOutput << " -1.0 -1.0";
  218. }
  219. }
  220. for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
  221. if (m->HasVertexColors(c)) {
  222. mOutput <<
  223. " " << m->mColors[c][i].r <<
  224. " " << m->mColors[c][i].g <<
  225. " " << m->mColors[c][i].b <<
  226. " " << m->mColors[c][i].a;
  227. }
  228. else {
  229. mOutput << " -1.0 -1.0 -1.0 -1.0";
  230. }
  231. }
  232. if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
  233. if (m->HasTangentsAndBitangents()) {
  234. mOutput <<
  235. " " << m->mTangents[i].x <<
  236. " " << m->mTangents[i].y <<
  237. " " << m->mTangents[i].z <<
  238. " " << m->mBitangents[i].x <<
  239. " " << m->mBitangents[i].y <<
  240. " " << m->mBitangents[i].z
  241. ;
  242. }
  243. else {
  244. mOutput << " 0.0 0.0 0.0 0.0 0.0 0.0";
  245. }
  246. }
  247. mOutput << endl;
  248. }
  249. }
  250. // ------------------------------------------------------------------------------------------------
  251. void PlyExporter::WriteMeshVertsBinary(const aiMesh* m, unsigned int components)
  252. {
  253. // If a component (for instance normal vectors) is present in at least one mesh in the scene,
  254. // then default values are written for meshes that do not contain this component.
  255. aiVector3D defaultNormal(0, 0, 0);
  256. aiVector2D defaultUV(-1, -1);
  257. aiColor4D defaultColor(-1, -1, -1, -1);
  258. for (unsigned int i = 0; i < m->mNumVertices; ++i) {
  259. mOutput.write(reinterpret_cast<const char*>(&m->mVertices[i].x), 12);
  260. if (components & PLY_EXPORT_HAS_NORMALS) {
  261. if (m->HasNormals()) {
  262. mOutput.write(reinterpret_cast<const char*>(&m->mNormals[i].x), 12);
  263. }
  264. else {
  265. mOutput.write(reinterpret_cast<const char*>(&defaultNormal.x), 12);
  266. }
  267. }
  268. for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
  269. if (m->HasTextureCoords(c)) {
  270. mOutput.write(reinterpret_cast<const char*>(&m->mTextureCoords[c][i].x), 6);
  271. }
  272. else {
  273. mOutput.write(reinterpret_cast<const char*>(&defaultUV.x), 6);
  274. }
  275. }
  276. for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
  277. if (m->HasVertexColors(c)) {
  278. mOutput.write(reinterpret_cast<const char*>(&m->mColors[c][i].r), 16);
  279. }
  280. else {
  281. mOutput.write(reinterpret_cast<const char*>(&defaultColor.r), 16);
  282. }
  283. }
  284. if (components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
  285. if (m->HasTangentsAndBitangents()) {
  286. mOutput.write(reinterpret_cast<const char*>(&m->mTangents[i].x), 12);
  287. mOutput.write(reinterpret_cast<const char*>(&m->mBitangents[i].x), 12);
  288. }
  289. else {
  290. mOutput.write(reinterpret_cast<const char*>(&defaultNormal.x), 12);
  291. mOutput.write(reinterpret_cast<const char*>(&defaultNormal.x), 12);
  292. }
  293. }
  294. }
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. void PlyExporter::WriteMeshIndices(const aiMesh* m, unsigned int offset)
  298. {
  299. for (unsigned int i = 0; i < m->mNumFaces; ++i) {
  300. const aiFace& f = m->mFaces[i];
  301. mOutput << f.mNumIndices << " ";
  302. for(unsigned int c = 0; c < f.mNumIndices; ++c) {
  303. mOutput << (f.mIndices[c] + offset) << (c == f.mNumIndices-1 ? endl : " ");
  304. }
  305. }
  306. }
  307. // Generic method in case we want to use different data types for the indices or make this configurable.
  308. template<typename NumIndicesType, typename IndexType>
  309. void WriteMeshIndicesBinary_Generic(const aiMesh* m, unsigned int offset, std::ostringstream& output)
  310. {
  311. for (unsigned int i = 0; i < m->mNumFaces; ++i) {
  312. const aiFace& f = m->mFaces[i];
  313. NumIndicesType numIndices = static_cast<NumIndicesType>(f.mNumIndices);
  314. output.write(reinterpret_cast<const char*>(&numIndices), sizeof(NumIndicesType));
  315. for (unsigned int c = 0; c < f.mNumIndices; ++c) {
  316. IndexType index = f.mIndices[c] + offset;
  317. output.write(reinterpret_cast<const char*>(&index), sizeof(IndexType));
  318. }
  319. }
  320. }
  321. void PlyExporter::WriteMeshIndicesBinary(const aiMesh* m, unsigned int offset)
  322. {
  323. WriteMeshIndicesBinary_Generic<unsigned char, int>(m, offset, mOutput);
  324. }
  325. } // end of namespace Assimp
  326. #endif // !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_PLY_EXPORTER)