#include <fstream>
#include "AneuMeshLoader.h"

void AneuMeshLoader::loadMesh(const std::string &path) {
    std::ifstream file(path);
    loadNodes(file);
    loadFiniteElements(file);
    loadBoundaryElements(file);
    if (file) std::cout << "Mesh was successfully loaded!" << std::endl;
    else throw FileNotExistException();
}

void AneuMeshLoader::loadNodes(std::ifstream &file) {
    int amountOfNodes, dimension;
    file >> amountOfNodes >> dimension;
    for (unsigned int i = 0; i < amountOfNodes; ++i) {
        double x, y, z;
        file >> x >> y >> z;
        nodes.emplace_back(i + 1, x, y, z, true);
    }
}

void AneuMeshLoader::loadFiniteElements(std::ifstream &file) {
    int amountOfElements, amountOfNodesInElement;
    file >> amountOfElements >> amountOfNodesInElement;
    for (unsigned int i = 0; i < amountOfElements; ++i) {
        unsigned int areaID;
        file >> areaID;
        std::vector<unsigned int> nodesID(amountOfNodesInElement);
        for (int j = 0; j < amountOfNodesInElement; ++j) {
            file >> nodesID[j];
        }
        finiteElements.emplace_back(i + 1, areaID, nodesID);
    }
}

void AneuMeshLoader::loadBoundaryElements(std::ifstream &file) {
    int amountOfBoundaryElements, amountOfNodesInBoundaryElement;
    file >> amountOfBoundaryElements >> amountOfNodesInBoundaryElement;
    for (unsigned int i = 0; i < amountOfBoundaryElements; ++i) {
        unsigned int boundaryID;
        file >> boundaryID;
        std::vector<unsigned int> nodesID(amountOfNodesInBoundaryElement);
        for (int j = 0; j < amountOfNodesInBoundaryElement; ++j) {
            file >> nodesID[j];
        }
        boundaryElements.emplace_back(i + 1, boundaryID, nodesID);
    }
}
