<commit_before>#include "model_loader.hpp"

#include <algorithm>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#define DATA_PER_VERTEX 10 // 3 coords + 3 normal + 4 color
#define MAX_COORDINATE 2

model_obj::model_obj ()
: _is_loaded (false)
, _scale_factor (1.0f)
, _current_rgba{ { 1, 1, 1, 1 } } {
}

void model_obj::release () {
    _is_loaded    = false;
    _scale_factor = 1.0f;
    _vertices.clear ();
    _normals.clear ();
    _faces.clear ();
    _faces_normals.clear ();
    _faces_colors.clear ();
    _interleaved_faces.clear ();
    _current_rgba = { { 1, 1, 1, 1 } };
}

int model_obj::data_per_vertex () {
    return DATA_PER_VERTEX;
}

void model_obj::calculate_normals (const std::vector<float>& vertices,
std::vector<float>& normals) {
    // calculate Vector1 and Vector2
    float norm[3], va[3], vb[3], vr[3], val;
    va[0] = vertices[0] - vertices[3];
    va[1] = vertices[1] - vertices[4];
    va[2] = vertices[2] - vertices[5];

    vb[0] = vertices[0] - vertices[6];
    vb[1] = vertices[1] - vertices[7];
    vb[2] = vertices[2] - vertices[8];

    // cross product
    vr[0] = va[1] * vb[2] - vb[1] * va[2];
    vr[1] = vb[0] * va[2] - va[0] * vb[2];
    vr[2] = va[0] * vb[1] - vb[0] * va[1];

    // normalization factor
    val = sqrt (vr[0] * vr[0] + vr[1] * vr[1] + vr[2] * vr[2]);

    norm[0] = vr[0] / val;
    norm[1] = vr[1] / val;
    norm[2] = vr[2] / val;

    // push back the norms for all 3 vertices
    normals.push_back (norm[0]);
    normals.push_back (norm[1]);
    normals.push_back (norm[2]);

    normals.push_back (norm[0]);
    normals.push_back (norm[1]);
    normals.push_back (norm[2]);

    normals.push_back (norm[0]);
    normals.push_back (norm[1]);
    normals.push_back (norm[2]);
}

float model_obj::calculate_scale () {
    // ensure that every vertex fits into range -1 to 1
    float max_val = 0.0f;
    for (float val : _vertices) {
        float abs_val = std::fabs (val);
        if (max_val < abs_val) {
            max_val = abs_val;
        }
    }
    return (MAX_COORDINATE / max_val);
}

void model_obj::normalize_vertices () {
    float scale_factor = calculate_scale ();

    for (float& val : _vertices) {
        val = val * _scale_factor;
    }

    _scale_factor = scale_factor;<commit_msg>fix model scaling<commit_after>#include "model_loader.hpp"

#include <algorithm>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#define DATA_PER_VERTEX 10 // 3 coords + 3 normal + 4 color
#define MAX_COORDINATE 1

model_obj::model_obj ()
: _is_loaded (false)
, _scale_factor (1.0f)
, _current_rgba{ { 1, 1, 1, 1 } } {
}

void model_obj::release () {
    _is_loaded    = false;
    _scale_factor = 1.0f;
    _vertices.clear ();
    _normals.clear ();
    _faces.clear ();
    _faces_normals.clear ();
    _faces_colors.clear ();
    _interleaved_faces.clear ();
    _current_rgba = { { 1, 1, 1, 1 } };
}

int model_obj::data_per_vertex () {
    return DATA_PER_VERTEX;
}

void model_obj::calculate_normals (const std::vector<float>& vertices,
std::vector<float>& normals) {
    // calculate Vector1 and Vector2
    float norm[3], va[3], vb[3], vr[3], val;
    va[0] = vertices[0] - vertices[3];
    va[1] = vertices[1] - vertices[4];
    va[2] = vertices[2] - vertices[5];

    vb[0] = vertices[0] - vertices[6];
    vb[1] = vertices[1] - vertices[7];
    vb[2] = vertices[2] - vertices[8];

    // cross product
    vr[0] = va[1] * vb[2] - vb[1] * va[2];
    vr[1] = vb[0] * va[2] - va[0] * vb[2];
    vr[2] = va[0] * vb[1] - vb[0] * va[1];

    // normalization factor
    val = sqrt (vr[0] * vr[0] + vr[1] * vr[1] + vr[2] * vr[2]);

    norm[0] = vr[0] / val;
    norm[1] = vr[1] / val;
    norm[2] = vr[2] / val;

    // push back the norms for all 3 vertices
    normals.push_back (norm[0]);
    normals.push_back (norm[1]);
    normals.push_back (norm[2]);

    normals.push_back (norm[0]);
    normals.push_back (norm[1]);
    normals.push_back (norm[2]);

    normals.push_back (norm[0]);
    normals.push_back (norm[1]);
    normals.push_back (norm[2]);
}

float model_obj::calculate_scale () {
    // ensure that every vertex fits into range -1 to 1
    float max_val = 0.0f;
    for (float val : _faces) {
        float abs_val = std::fabs (val);
        if (max_val < abs_val) {
            max_val = abs_val;
        }
    }
    return (MAX_COORDINATE / max_val);
}

void model_obj::normalize_vertices () {
    float scale_factor = calculate_scale ();

    for (float& val : _faces) {
        val = val * scale_factor;
    }

    _scale_factor = scale_factor;
{'fraction_non_alphanumeric': 0.1681171725748249, 'fraction_numerical': 0.03523668011037996, 'mean_word_length': 2.969671440606571, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, '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': '1417890', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1858, 'n_words': 407}