VTKTriangleMeshExporter.cpp 6.14 KB
Newer Older
Alexander Stukowski's avatar
Alexander Stukowski committed
1 2
///////////////////////////////////////////////////////////////////////////////
//
3
//  Copyright (2018) Alexander Stukowski
Alexander Stukowski's avatar
Alexander Stukowski committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//
//  This file is part of OVITO (Open Visualization Tool).
//
//  OVITO is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  OVITO is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

#include <plugins/mesh/Mesh.h>
#include <plugins/mesh/surface/RenderableSurfaceMesh.h>
#include "VTKTriangleMeshExporter.h"

namespace Ovito { namespace Mesh {

IMPLEMENT_OVITO_CLASS(VTKTriangleMeshExporter);

/******************************************************************************
 * This is called once for every output file to be written and before
 * exportData() is called.
 *****************************************************************************/
34
bool VTKTriangleMeshExporter::openOutputFile(const QString& filePath, int numberOfFrames, AsyncOperation& operation)
Alexander Stukowski's avatar
Alexander Stukowski committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
	OVITO_ASSERT(!_outputFile.isOpen());
	OVITO_ASSERT(!_outputStream);

	_outputFile.setFileName(filePath);
	_outputStream.reset(new CompressedTextWriter(_outputFile, dataset()));

	return true;
}

/******************************************************************************
 * This is called once for every output file written after exportData()
 * has been called.
 *****************************************************************************/
void VTKTriangleMeshExporter::closeOutputFile(bool exportCompleted)
{
	_outputStream.reset();
	if(_outputFile.isOpen())
		_outputFile.close();

	if(!exportCompleted)
		_outputFile.remove();
}

/******************************************************************************
 * Exports a single animation frame to the current output file.
 *****************************************************************************/
62
bool VTKTriangleMeshExporter::exportFrame(int frameNumber, TimePoint time, const QString& filePath, AsyncOperation&& operation)
Alexander Stukowski's avatar
Alexander Stukowski committed
63
{
64 65 66 67 68 69 70 71 72 73 74 75 76 77
	// Evaluate pipeline.
	// Note: We are requesting the rendering state from the pipeline,
	// because we are interested in renderable triangle meshes.
	const PipelineFlowState& state = getPipelineDataToBeExported(time, operation, true);

	// Look up the RenderableSurfaceMesh to be exported in the pipeline state.
	DataObjectReference objectRef(&RenderableSurfaceMesh::OOClass(), dataObjectToExport().dataPath());
	const RenderableSurfaceMesh* meshObj = static_object_cast<RenderableSurfaceMesh>(state.getLeafObject(objectRef));
	if(!meshObj) {
		throwException(tr("The pipeline output does not contain the surface mesh to be exported (animation frame: %1; object key: %2). Available surface mesh keys: (%3)")
			.arg(frameNumber).arg(objectRef.dataPath()).arg(getAvailableDataObjectList(state, RenderableSurfaceMesh::OOClass())));
	}

	operation.setProgressText(tr("Writing file %1").arg(filePath));	
Alexander Stukowski's avatar
Alexander Stukowski committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

	const TriMesh& surfaceMesh = meshObj->surfaceMesh();
	const TriMesh& capPolygonsMesh = meshObj->capPolygonsMesh();
	textStream() << "# vtk DataFile Version 3.0\n";
	textStream() << "# Triangle surface mesh written by " << QCoreApplication::applicationName() << " " << QCoreApplication::applicationVersion() << "\n";
	textStream() << "ASCII\n";
	textStream() << "DATASET UNSTRUCTURED_GRID\n";
	textStream() << "POINTS " << (surfaceMesh.vertexCount() + capPolygonsMesh.vertexCount()) << " double\n";
	for(const Point3& p : surfaceMesh.vertices())
		textStream() << p.x() << " " << p.y() << " " << p.z() << "\n";
	for(const Point3& p : capPolygonsMesh.vertices())
		textStream() << p.x() << " " << p.y() << " " << p.z() << "\n";
	auto totalFaceCount = surfaceMesh.faceCount() + capPolygonsMesh.faceCount();
	textStream() << "\nCELLS " << totalFaceCount << " " << (totalFaceCount * 4) << "\n";
	for(const TriMeshFace& f : surfaceMesh.faces()) {
		textStream() << "3";
		for(size_t i = 0; i < 3; i++)
			textStream() << " " << f.vertex(i);
		textStream() << "\n";
	}
	for(const TriMeshFace& f : capPolygonsMesh.faces()) {
		textStream() << "3";
		for(size_t i = 0; i < 3; i++)
			textStream() << " " << (f.vertex(i) + surfaceMesh.vertexCount());
		textStream() << "\n";
	}
	textStream() << "\nCELL_TYPES " << totalFaceCount << "\n";
	for(size_t i = 0; i < totalFaceCount; i++)
		textStream() << "5\n";	// Triangle

	textStream() << "\nCELL_DATA " << totalFaceCount << "\n";
	textStream() << "SCALARS cap unsigned_char\n";
	textStream() << "LOOKUP_TABLE default\n";
	for(size_t i = 0; i < surfaceMesh.faceCount(); i++)
		textStream() << "0\n";
	for(size_t i = 0; i < capPolygonsMesh.faceCount(); i++)
		textStream() << "1\n";

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if(!meshObj->materialColors().empty()) {
		textStream() << "\nSCALARS material_index int\n";
		textStream() << "LOOKUP_TABLE default\n";
		for(size_t i = 0; i < surfaceMesh.faceCount(); i++)
			textStream() << surfaceMesh.face(i).materialIndex() << "\n";
		for(size_t i = 0; i < capPolygonsMesh.faceCount(); i++)
			textStream() << "0\n";

		textStream() << "\nCOLOR_SCALARS color 3\n";
		for(size_t i = 0; i < surfaceMesh.faceCount(); i++) {
			const ColorA& c = meshObj->materialColors()[surfaceMesh.face(i).materialIndex() % meshObj->materialColors().size()];
			textStream() << c.r() << " " << c.g() << " " << c.b() << "\n";
		}
		for(size_t i = 0; i < capPolygonsMesh.faceCount(); i++)
			textStream() << "1 1 1\n";			
	}

Alexander Stukowski's avatar
Alexander Stukowski committed
133 134 135 136 137 138 139 140
	textStream() << "\nPOINT_DATA " << (surfaceMesh.vertexCount() + capPolygonsMesh.vertexCount()) << "\n";
	textStream() << "SCALARS cap unsigned_char\n";
	textStream() << "LOOKUP_TABLE default\n";
	for(size_t i = 0; i < surfaceMesh.vertexCount(); i++)
		textStream() << "0\n";
	for(size_t i = 0; i < capPolygonsMesh.vertexCount(); i++)
		textStream() << "1\n";
		
141
	return !operation.isCanceled();
Alexander Stukowski's avatar
Alexander Stukowski committed
142 143 144 145
}

}	// End of namespace
}	// End of namespace