#include "MeshObjWriter.h"

#include "File.h"
#include "TriMesh.h"
#include "AbstractFace.h"

#include <sstream>

std::string MeshObjWriter::serialize(TriMesh* mesh)
{
	std::stringstream output;
	for (const auto& node : mesh->getNodes())
	{
		const auto x = node->getPoint().getX();
		const auto y = node->getPoint().getY();
		const auto z = node->getPoint().getZ();
		output << "v "<< x << " " << y << " " << z << "\n";
	}

	for (const auto& face : mesh->getFaces())
	{
		auto ids = face->getNodeIds();
		output << "f " << 1 + ids[0] << " " << 1 + ids[1] << " " << 1 + ids[2] << "\n";
	}
	return output.str();
}

void MeshObjWriter::write(const Path& path, TriMesh* mesh)
{
	File file(path);
	file.writeText(serialize(mesh));
}