瀏覽代碼

Added binary ply exporter.

Andreas Henne 10 年之前
父節點
當前提交
4550279629
共有 3 個文件被更改,包括 105 次插入7 次删除
  1. 4 0
      code/Exporter.cpp
  2. 97 6
      code/PlyExporter.cpp
  3. 4 1
      code/PlyExporter.h

+ 4 - 0
code/Exporter.cpp

@@ -78,6 +78,7 @@ void ExportSceneObj(const char*,IOSystem*, const aiScene*);
 void ExportSceneSTL(const char*,IOSystem*, const aiScene*);
 void ExportSceneSTL(const char*,IOSystem*, const aiScene*);
 void ExportSceneSTLBinary(const char*,IOSystem*, const aiScene*);
 void ExportSceneSTLBinary(const char*,IOSystem*, const aiScene*);
 void ExportScenePly(const char*,IOSystem*, const aiScene*);
 void ExportScenePly(const char*,IOSystem*, const aiScene*);
+void ExportScenePlyBinary(const char*, IOSystem*, const aiScene*);
 void ExportScene3DS(const char*, IOSystem*, const aiScene*);
 void ExportScene3DS(const char*, IOSystem*, const aiScene*);
 void ExportSceneAssbin(const char*, IOSystem*, const aiScene*);
 void ExportSceneAssbin(const char*, IOSystem*, const aiScene*);
 void ExportSceneAssxml(const char*, IOSystem*, const aiScene*);
 void ExportSceneAssxml(const char*, IOSystem*, const aiScene*);
@@ -113,6 +114,9 @@ Exporter::ExportFormatEntry gExporters[] =
 	Exporter::ExportFormatEntry( "ply", "Stanford Polygon Library", "ply" , &ExportScenePly, 
 	Exporter::ExportFormatEntry( "ply", "Stanford Polygon Library", "ply" , &ExportScenePly, 
 		aiProcess_PreTransformVertices
 		aiProcess_PreTransformVertices
 	),
 	),
+	Exporter::ExportFormatEntry("plyb", "Stanford Polygon Library Binary", "ply", &ExportScenePlyBinary,
+	aiProcess_PreTransformVertices
+	),
 #endif
 #endif
 
 
 #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
 #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER

+ 97 - 6
code/PlyExporter.cpp

@@ -56,7 +56,7 @@ void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene
 	PlyExporter exporter(pFile, pScene);
 	PlyExporter exporter(pFile, pScene);
 
 
 	// we're still here - export successfully completed. Write the file.
 	// we're still here - export successfully completed. Write the file.
-	boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
+    boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
 	if(outfile == NULL) {
 	if(outfile == NULL) {
 		throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
 		throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
 	}
 	}
@@ -64,6 +64,20 @@ void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene
 	outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
 	outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
 }
 }
 
 
+void ExportScenePlyBinary(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene)
+{
+	// invoke the exporter 
+    PlyExporter exporter(pFile, pScene, true);
+
+	// we're still here - export successfully completed. Write the file.
+	boost::scoped_ptr<IOStream> outfile(pIOSystem->Open(pFile, "wb"));
+	if (outfile == NULL) {
+		throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
+	}
+
+	outfile->Write(exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()), 1);
+}
+
 } // end of namespace Assimp
 } // end of namespace Assimp
 
 
 #define PLY_EXPORT_HAS_NORMALS 0x1
 #define PLY_EXPORT_HAS_NORMALS 0x1
@@ -72,7 +86,7 @@ void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene
 #define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS)
 #define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS)
 
 
 // ------------------------------------------------------------------------------------------------
 // ------------------------------------------------------------------------------------------------
-PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene)
+PlyExporter::PlyExporter(const char* _filename, const aiScene* pScene, bool binary)
 : filename(_filename)
 : filename(_filename)
 , pScene(pScene)
 , pScene(pScene)
 , endl("\n") 
 , endl("\n") 
@@ -102,7 +116,18 @@ PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene)
 	}
 	}
 
 
 	mOutput << "ply" << endl;
 	mOutput << "ply" << endl;
-	mOutput << "format ascii 1.0" << endl;
+	if (binary)
+	{
+#if (defined AI_BUILD_BIG_ENDIAN)
+		mOutput << "format binary_big_endian 1.0" << endl;
+#else
+		mOutput << "format binary_little_endian 1.0" << endl;
+#endif
+	}
+	else
+	{
+		mOutput << "format ascii 1.0" << endl;
+	}
 	mOutput << "comment Created by Open Asset Import Library - http://assimp.sf.net (v"
 	mOutput << "comment Created by Open Asset Import Library - http://assimp.sf.net (v"
 		<< aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' 
 		<< aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' 
 		<< aiGetVersionRevision() << ")" << endl;
 		<< aiGetVersionRevision() << ")" << endl;
@@ -160,13 +185,19 @@ PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene)
 
 
 	mOutput << "element face " << faces << endl;
 	mOutput << "element face " << faces << endl;
 	mOutput << "property list uint uint vertex_index" << endl;
 	mOutput << "property list uint uint vertex_index" << endl;
-	mOutput << "end_header" << endl;
+    mOutput << "end_header" << endl;
 
 
 	for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
 	for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
-		WriteMeshVerts(pScene->mMeshes[i],components);
+		if (binary)
+			WriteMeshVertsBinary(pScene->mMeshes[i], components);
+		else
+			WriteMeshVerts(pScene->mMeshes[i], components);
 	}
 	}
 	for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) {
 	for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) {
-		WriteMeshIndices(pScene->mMeshes[i],ofs);
+		if (binary)
+			WriteMeshIndicesBinary(pScene->mMeshes[i], ofs);
+		else
+			WriteMeshIndices(pScene->mMeshes[i], ofs);
 		ofs += pScene->mMeshes[i]->mNumVertices;
 		ofs += pScene->mMeshes[i]->mNumVertices;
 	}
 	}
 }
 }
@@ -236,6 +267,54 @@ void PlyExporter :: WriteMeshVerts(const aiMesh* m, unsigned int components)
 	}
 	}
 }
 }
 
 
+// ------------------------------------------------------------------------------------------------
+void PlyExporter::WriteMeshVertsBinary(const aiMesh* m, unsigned int components)
+{
+	aiVector3D defaultNormal(0, 0, 0);
+	aiVector2D defaultUV(-1, -1);
+	aiColor4D defaultColor(-1, -1, -1, -1);
+	for (unsigned int i = 0; i < m->mNumVertices; ++i) {
+		mOutput.write(reinterpret_cast<const char*>(&m->mVertices[i].x), 12);
+		if (components & PLY_EXPORT_HAS_NORMALS) {
+			if (m->HasNormals()) {
+				mOutput.write(reinterpret_cast<const char*>(&m->mNormals[i].x), 12);
+			}
+			else {
+				mOutput.write(reinterpret_cast<const char*>(&defaultNormal.x), 12);
+			}
+		}
+
+		for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
+			if (m->HasTextureCoords(c)) {
+				mOutput.write(reinterpret_cast<const char*>(&m->mTextureCoords[c][i].x), 6);
+			}
+			else {
+				mOutput.write(reinterpret_cast<const char*>(&defaultUV.x), 6);
+			}
+		}
+
+		for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
+			if (m->HasVertexColors(c)) {
+				mOutput.write(reinterpret_cast<const char*>(&m->mColors[c][i].r), 16);
+			}
+			else {
+				mOutput.write(reinterpret_cast<const char*>(&defaultColor.r), 16);
+			}
+		}
+
+		if (components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
+			if (m->HasTangentsAndBitangents()) {
+				mOutput.write(reinterpret_cast<const char*>(&m->mTangents[i].x), 12);
+				mOutput.write(reinterpret_cast<const char*>(&m->mBitangents[i].x), 12);
+			}
+			else {
+				mOutput.write(reinterpret_cast<const char*>(&defaultNormal.x), 12);
+				mOutput.write(reinterpret_cast<const char*>(&defaultNormal.x), 12);
+			}
+		}
+	}
+}
+
 // ------------------------------------------------------------------------------------------------
 // ------------------------------------------------------------------------------------------------
 void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset)
 void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset)
 {
 {
@@ -248,4 +327,16 @@ void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset)
 	}
 	}
 }
 }
 
 
+void PlyExporter::WriteMeshIndicesBinary(const aiMesh* m, unsigned int offset)
+{
+	for (unsigned int i = 0; i < m->mNumFaces; ++i) {
+		const aiFace& f = m->mFaces[i];
+		mOutput.write(reinterpret_cast<const char*>(&f.mNumIndices), 4);
+		for (unsigned int c = 0; c < f.mNumIndices; ++c) {
+			unsigned int index = f.mIndices[c] + offset;
+			mOutput.write(reinterpret_cast<const char*>(&index), 4);
+		}
+	}
+}
+
 #endif
 #endif

+ 4 - 1
code/PlyExporter.h

@@ -59,7 +59,7 @@ class PlyExporter
 {
 {
 public:
 public:
 	/// Constructor for a specific scene to export
 	/// Constructor for a specific scene to export
-	PlyExporter(const char* filename, const aiScene* pScene);
+	PlyExporter(const char* filename, const aiScene* pScene, bool binary = false);
 
 
 public:
 public:
 
 
@@ -71,6 +71,9 @@ private:
 	void WriteMeshVerts(const aiMesh* m, unsigned int components);
 	void WriteMeshVerts(const aiMesh* m, unsigned int components);
 	void WriteMeshIndices(const aiMesh* m, unsigned int ofs);
 	void WriteMeshIndices(const aiMesh* m, unsigned int ofs);
 
 
+	void WriteMeshVertsBinary(const aiMesh* m, unsigned int components);
+	void WriteMeshIndicesBinary(const aiMesh* m, unsigned int offset);
+
 private:
 private:
 
 
 	const std::string filename;
 	const std::string filename;