<commit_before>// (c) 2015 <NAME>, AGPLv3

#include "meshobject.h"
#include <string>
#include <QVector3D>

using namespace std;

char* QStringToChar(QString str)
{
    string s = str.toStdString();
    char *cstr = new char[s.length() + 1];
    strcpy(cstr, s.c_str());
    return cstr;
}

MeshObject::MeshObject()
{
    stl = new stl_file;
    stl_initialize(stl);
    selected = true;
    saved = true;    
    file = "new";
    references = 0;
    size = 0;
    hidden = false;
}

MeshObject::MeshObject(stl_file* item, QString name)
{
    stl = item;
    selected = true;
    saved = false;
    references = 0;
    size = 0;
    hidden = false;
    file = name;
    stl_calculate_volume(stl);
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    this->updateGeometry();
}

MeshObject::MeshObject(const MeshObject& m) : QGLFunctions()
{
    references = 0;
    hidden = m.hidden;
    file = m.file;
    saved = m.saved;
    selected = m.selected;
    stl = new stl_file;
    stl_initialize(stl);
    stl->fp = m.stl->fp;
    stl->stats = m.stl->stats;
    stl->M = m.stl->M;
    stl->error = m.stl->error;
    stl_reallocate(stl);
    for(int i=0; i<stl->stats.number_of_facets;i++){
        stl->facet_start[i] = m.stl->facet_start[i];
        stl->neighbors_start[i] = m.stl->neighbors_start[i];
    }
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    this->updateGeometry();
}

MeshObject::~MeshObject(){
    stl_close(stl);
    delete(stl);
    glDeleteBuffers(1, &vbo);
}

bool MeshObject::loadGeometry(QString fileName)
{
    char* filename = QStringToChar(fileName);
    stl_open(stl, filename);
    if(stl_get_error(stl)){
       stl_clear_error(stl);
       return false;
    }
    delete []filename;
    stl_repair(stl,0,1,0,0,0,0,0,0,0,0,0,0,0,0);
    stl_calculate_volume(stl);
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    this->updateGeometry();
    file = fileName;
    return true;
}

void MeshObject::setSplitName(int index)
{
    QString add = "_part_"+QString::number(index)+".stl";
    file = file.section(".",0,0);
    file += add;
}

void MeshObject::setDuplicatedName()
{
    QString add = "_dulicated.stl";
    file = file.section(".",0,0);
    file += add;
}

void MeshObject::mergedFilename()
{
    QString add = "_merged.stl";
    file = file.section(".",0,0);
    file += add;
}

bool MeshObject::hasValidName()
{
    if(file.size() < 5)return false;
    else if(file == "untitled")return false;
    else if(file == "split")return false;
    else return true;
}

stl_file* MeshObject::getStlPointer()
{
    return stl;
}

bool MeshObject::isSaved()
{
    return saved;
}

void MeshObject::saveAs(QString fileName, int type)
{
    char* filename = QStringToChar(fileName);
    if(type == 1){
        stl_write_ascii(stl, filename, "ADMeshSTLmodel");
    }else if(type == 2){
        stl_write_binary(stl, filename, "ADMeshSTLmodel");
    }
    delete []filename;
    file = fileName;
    saved = true;
}

void MeshObject::save()
{
    char* filename = QStringToChar(file);
    if(stl->stats.type == binary){
        stl_write_binary(stl, filename, "ADMeshSTLmodel");
    }else if(stl->stats.type == ascii){
        stl_write_ascii(stl, filename, "ADMeshSTLmodel");
    }
    delete []filename;
    saved = true;
}

void MeshObject::exportSTL(QString fileName, int type)
{
    stl_check_facets_exact(stl);
    stl_generate_shared_vertices(stl);<commit_msg>Allow simple save for newly created objects (e.g. after split)<commit_after>// (c) 2015 <NAME>, AGPLv3

#include "meshobject.h"
#include <string>
#include <QVector3D>

using namespace std;

char* QStringToChar(QString str)
{
    string s = str.toStdString();
    char *cstr = new char[s.length() + 1];
    strcpy(cstr, s.c_str());
    return cstr;
}

MeshObject::MeshObject()
{
    stl = new stl_file;
    stl_initialize(stl);
    selected = true;
    saved = true;    
    file = "new";
    references = 0;
    size = 0;
    hidden = false;
}

MeshObject::MeshObject(stl_file* item, QString name)
{
    stl = item;
    selected = true;
    saved = false;
    references = 0;
    size = 0;
    hidden = false;
    file = name;
    stl_calculate_volume(stl);
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    this->updateGeometry();
}

MeshObject::MeshObject(const MeshObject& m) : QGLFunctions()
{
    references = 0;
    hidden = m.hidden;
    file = m.file;
    saved = m.saved;
    selected = m.selected;
    stl = new stl_file;
    stl_initialize(stl);
    stl->fp = m.stl->fp;
    stl->stats = m.stl->stats;
    stl->M = m.stl->M;
    stl->error = m.stl->error;
    stl_reallocate(stl);
    for(int i=0; i<stl->stats.number_of_facets;i++){
        stl->facet_start[i] = m.stl->facet_start[i];
        stl->neighbors_start[i] = m.stl->neighbors_start[i];
    }
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    this->updateGeometry();
}

MeshObject::~MeshObject(){
    stl_close(stl);
    delete(stl);
    glDeleteBuffers(1, &vbo);
}

bool MeshObject::loadGeometry(QString fileName)
{
    char* filename = QStringToChar(fileName);
    stl_open(stl, filename);
    if(stl_get_error(stl)){
       stl_clear_error(stl);
       return false;
    }
    delete []filename;
    stl_repair(stl,0,1,0,0,0,0,0,0,0,0,0,0,0,0);
    stl_calculate_volume(stl);
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    this->updateGeometry();
    file = fileName;
    return true;
}

void MeshObject::setSplitName(int index)
{
    QString add = "_part_"+QString::number(index)+".stl";
    file = file.section(".",0,0);
    file += add;
}

void MeshObject::setDuplicatedName()
{
    QString add = "_duplicated.stl";
    file = file.section(".",0,0);
    file += add;
    saved = false;
}

void MeshObject::mergedFilename()
{
    QString add = "_merged.stl";
    file = file.section(".",0,0);
    file += add;
    saved = false;
}

bool MeshObject::hasValidName()
{
    if(file.size() < 5)return false;
    else if(file == "untitled")return false;
    else if(file == "split")return false;
    else return true;
}

stl_file* MeshObject::getStlPointer()
{
    return stl;
}

bool MeshObject::isSaved()
{
    return saved;
}

void MeshObject::saveAs(QString fileName, int type)
{
    char* filename = QStringToChar(fileName);
    if(type == 1){
        stl_write_ascii(stl, filename, "ADMeshSTLmodel");
    }else if(type == 2){
        stl_write_binary(stl, filename, "ADMeshSTLmodel");
    }
    delete []filename;
    file = fileName;
    saved = true;
}

void MeshObject::save()
{
    char* filename = QStringToChar(file);    
    if(stl->stats.type == ascii){
        stl_write_ascii(stl, filename, "ADMeshSTLmodel");
    }else{
        stl_write_binary(stl, filename, "ADMeshSTLmodel");
    }
    delete []filename;
    saved = true;
}

void MeshObject::exportSTL(QString fileName, int type)
{
    stl_check_facets_exact(stl);
    stl_generate_shared_vertices(stl);
{'fraction_non_alphanumeric': 0.1536118363794604, 'fraction_numerical': 0.011604293588627792, 'mean_word_length': 3.274643521388717, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1594619', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2621, 'n_words': 490}