PlyExporter.cpp 15 KB

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