#include "MeshBuilder.h"

#include "Node.h"
#include "Edge.h"
#include "TriFace.h"

std::unique_ptr<TriMesh> MeshBuilder::buildTriMesh(const VecPoints& locations, const EdgeIds& edgeIds, const FaceIds& faceIds)
{
    auto mesh = std::make_unique<TriMesh>();

    VecNodes nodes(locations.size());
    for (std::size_t idx=0; idx<locations.size(); idx++)
    {
        nodes[idx] = Node::Create(locations[idx], idx);
    }

    VecEdges edges(edgeIds.size());
    for (std::size_t idx=0; idx<edgeIds.size(); idx++)
    {
        edges[idx] = Edge::Create(nodes[edgeIds[idx].first].get(), nodes[edgeIds[idx].second].get(), idx);
    }

    VecFaces faces(faceIds.size());
    for (std::size_t idx=0; idx<faceIds.size(); idx++)
    {
        faces[idx] = TriFace::Create(
                edges[faceIds[idx][0]].get(),
                edges[faceIds[idx][1]].get(),
                edges[faceIds[idx][2]].get(),
                idx);
    }

    mesh->populate(nodes, edges, faces);
    return mesh;
}

std::unique_ptr<LineMesh> MeshBuilder::buildLineMesh(const VecPoints& locations, const EdgeIds& edgeIds)
{
    auto mesh = std::make_unique<LineMesh>();

    VecNodes nodes(locations.size());
    for (std::size_t idx=0; idx<locations.size(); idx++)
    {
        nodes[idx] = Node::Create(locations[idx], idx);
    }

    VecEdges edges(edgeIds.size());
    for (std::size_t idx=0; idx<edgeIds.size(); idx++)
    {
        edges[idx] = Edge::Create(nodes[edgeIds[idx].first].get(), nodes[edgeIds[idx].second].get(), idx);
    }

    mesh->populate(nodes, edges);
    return mesh;
}
