#include <fstream>
#include <cstdlib>
#include <iomanip>
#include "Vector3D.hpp"
#include "stl.hpp"

STL::STL(string name) : ofstream(name+".stl")
{
	solid= name;
	*this << "solid " << name << std::endl;
}

void STL::Tri(const Vector3D& a, const Vector3D& b, const Vector3D& c)
{
	Vector3D n= (b-a) % (c-a);
	n= unit(n);
	*this << "facet normal "   
		<< std::scientific 
		<< std::setw(10)
		<< std::setprecision(8) 
		<< n.x() << " " << n.y() << " " << n.z() << std::endl
    	<< "\touter loop\n"
        <<"\t\tvertex " << a.x() << " " << a.y() << " " << a.z() << std::endl
        <<"\t\tvertex " << b.x() << " " << b.y() << " " << b.z() << std::endl
        <<"\t\tvertex " << c.x() << " " << c.y() << " " << c.z() << std::endl
    	<< "\tendloop\n"
		<< "endfacet\n";
}

STL::~STL(void) 
{
	*this << "endsolid " << solid << std::endl;
}

