#include "mesh2pov.h"
#include "povmesh.h"

#include <QDebug>

Stl2POV::Stl2POV()
{

}

void Stl2POV::convert(QIODevice& in, QIODevice& out) const
{
    POVMesh2 mesh;

    QByteArray header = in.read(80);
    unsigned int numtriangles;
    in.read((char*)&numtriangles, 4);

    float norm [3];
    float v[9]; //9*4=36 byte
    short attr;

    int vertices[3];

    for (unsigned int i = 0; i < numtriangles; ++i)
    {

        in.read((char*) norm, 12);

        in.read((char*) v, 36); //9*4=36 byte
        in.read((char*) &attr, 2);

        for (int vi = 0; vi < 3; vi++)
        {
            float* vf = &(v[vi*3]);
            vertices[vi] = mesh.addVertice(vf[0], vf[1], vf[2]);
        }

        mesh.addFace(vertices[0], vertices[1], vertices[2]);

    }

    mesh.setCenter();
    mesh.normalize();

    QTextStream to (&out);

    to << mesh;

    qDebug() << "fin";
}
