#include "glm.hpp"
#include "math.hpp"

using namespace glm;
ifvec3::ifvec3(const llvec3 &pos, u32 density) {
    const auto d = IDX_LENGTH2 * density;
    raw = glm::divide(pos, glm::uvec3(d));
    offset = glm::vec3(rem(pos.x, d), rem(pos.y, d), rem(pos.z, d));
    if(density > 1) center();
}
llvec3 ifvec3::raw_as_long() const {
    return llvec3(raw) * llvec3(IDX_LENGTH2);
}
llvec3 ifvec3::as_voxel(int density) const {
    return raw_as_long() * llvec3(density) + llvec3(offset * vec3(density));
}
void ifvec3::center() {
    const auto diff = glm::divide(glm::llvec3(offset), uvec3(IDX_LENGTH2));
    raw += diff;
    offset -= diff * static_cast<long>(IDX_LENGTH2);
}
double ifvec3::dist(const ifvec3& p) const {
    return glm::length(glm::dvec3(raw - p.raw)) + glm::length(offset - p.offset);
}
ifvec3 ifvec3::divide(u32 m) const {
    return ifvec3(glm::divide(raw, glm::ucvec3(m)), glm::divide(offset, glm::uvec3(m)), false);
}