// File: /workspace/github_cpp_makefile_star2_O1/SpiritLooper[P]RDV_OBJRender/model.cpp
// Function: Model::Model(char const*)
Model::Model(const char *filename) : verts_(), faces_() {
    std::ifstream in;
    in.open (filename, std::ifstream::in);
    if (in.fail()) return;
    std::string line;
    while (!in.eof()) {
        std::getline(in, line);
        std::istringstream iss(line.c_str());
        char trash;
        if (!line.compare(0, 2, "v ")) {
            iss >> trash;
            Vec3f v;
            for (int i=0;i<3;i++) iss >> v.raw[i];
            verts_.push_back(v);
        } else if (!line.compare(0, 2, "f ")) {
            std::vector<int> f;
          
            int itrash, idx;
            iss >> trash;
            if ( line.find("/") == std::string::npos ){
                while (iss >> idx ) {
                    idx--; // in wavefront obj all indices start at 1, not zero
                    f.push_back(idx);
                }
            } else {
                while (iss >> idx >> trash >> itrash >> trash >> itrash) {
                    idx--; // in wavefront obj all indices start at 1, not zero
                    f.push_back(idx);
                }
            }
            faces_.push_back(f);
        }
    }
    std::cout << "# v# " << verts_.size() << " f# "  << faces_.size() << std::endl;

    // Centrage du model sur l'origine 
    Vec3f min, max, ecart;
    get_bbox(min,max);
    for (int i = 0; i < 3; i++)
    {
        ecart[i] = (min[i] + max[i]) / 2.;
    }
    

    for (int i = 0; i < (int)verts_.size(); i++)
    {
        for (int j = 0; j < 3; j++)
        {
            verts_[i][j] =  verts_[i][j] - ecart[j];
        }   
    }

    get_bbox(min,max);

    // On cherche quel axe déborde le plus à partir de l'origine
    float rapport = std::max( max[0] , std::max( max[1] , max[2] ) );


    // Reduction à l'échelle du model selon l'axe qui nécessite le plus gros rapport de réduction
    for (int i = 0; i < (int)verts_.size(); i++)
    {
        for (int j = 0; j < 3; j++)
        {
            verts_[i][j] = verts_[i][j] / rapport;    
        }
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rsi, %rbx
leaq 0x18(%rdi), %rax
movq %rax, 0x58(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq %rdi, 0x40(%rsp)
movups %xmm0, (%rdi)
leaq 0x80(%rsp), %rdi
callq 0x21c0
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x2100
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xa0(%rsp,%rax)
jne 0x4aae
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0xa0(%rsp,%rax)
jne 0x4888
leaq (%rsp,%rax), %rdi
addq $0x80, %rdi
leaq 0x20(%rsp), %rbp
leaq 0x4c(%rsp), %r14
leaq 0x288(%rsp), %r15
leaq 0x3f(%rsp), %r12
leaq 0xc(%rsp), %rbx
movl $0xa, %esi
callq 0x2190
movsbl %al, %edx
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x2300
movq 0x60(%rsp), %rsi
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r14, %rdx
callq 0x5572
movq %r15, %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0x2290
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x466c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movl $0x2, %edx
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
leaq 0x2ac2(%rip), %rcx      # 0x7141
callq 0x2200
testl %eax, %eax
je 0x47d6
movl $0x2, %edx
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
leaq 0x2aa5(%rip), %rcx      # 0x7144
callq 0x2200
testl %eax, %eax
jne 0x484b
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x21f0
movl $0x1, %ecx
leaq 0x60(%rsp), %rdi
leaq 0x2a6e(%rip), %rsi      # 0x7147
xorl %edx, %edx
callq 0x22c0
cmpq $-0x1, %rax
je 0x4791
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x21f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x20f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x21f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x20f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x47aa
movl 0xc(%rsp), %eax
decl %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4752
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x46ea
leaq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0x58a4
jmp 0x46ea
movl 0xc(%rsp), %eax
decl %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4784
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x4791
leaq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0x58a4
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x4761
movq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x55b0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x484b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x484b
movq %r15, %rdi
movq %r12, %rsi
callq 0x21f0
movq 0x40(%rsp), %rbp
movq $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
xorl %r13d, %r13d
leaq (%rsp,%r13), %rsi
addq $0x10, %rsi
movq %r15, %rdi
callq 0x2310
addq $0x4, %r13
cmpq $0xc, %r13
jne 0x47fa
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0x4839
movl 0x18(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rbp)
leaq 0x20(%rsp), %rbp
jmp 0x484b
movq %rbp, %rdi
leaq 0x10(%rsp), %rdx
callq 0x573a
leaq 0x20(%rsp), %rbp
movq %r15, %rdi
movq 0x577b(%rip), %rsi      # 0x9fd0
callq 0x2220
leaq 0x300(%rsp), %rdi
callq 0x2090
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rdi
addq $0x80, %rdi
testb $0x2, 0x20(%rdi)
je 0x4611
movq 0x5749(%rip), %rdi      # 0x9fd8
leaq 0x28b3(%rip), %rsi      # 0x7149
movl $0x5, %edx
callq 0x21d0
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x2, %rsi
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r14, %rsi
movq 0x5713(%rip), %rdi      # 0x9fd8
callq 0x2120
movq %rax, %rbx
leaq 0x287b(%rip), %rsi      # 0x714f
movl $0x4, %edx
movq %rax, %rdi
callq 0x21d0
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rsi
subq 0x18(%rax), %rsi
sarq $0x3, %rsi
imulq %r14, %rsi
movq %rbx, %rdi
callq 0x2120
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
xorl %ebx, %ebx
leaq 0x288(%rsp), %rsi
movq %rbx, (%rsi)
movl %ebx, 0x8(%rsi)
leaq 0x10(%rsp), %rdx
movq %rbx, (%rdx)
movl %ebx, 0x8(%rdx)
movq %rbx, 0x4c(%rsp)
movl %ebx, 0x54(%rsp)
movq 0x40(%rsp), %rdi
callq 0x4b9e
movss 0x278a(%rip), %xmm0     # 0x70e8
xorl %eax, %eax
cmpl $0x1, %ebx
setne %al
testl %ebx, %ebx
leaq 0x4(,%rax,4), %rax
movl $0x0, %ecx
cmovneq %rax, %rcx
movss 0x288(%rsp,%rcx), %xmm1
addss 0x10(%rsp,%rcx), %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x4c(%rsp,%rcx)
incl %ebx
cmpl $0x3, %ebx
jne 0x495e
movq 0x40(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
imulq %r14, %rcx
testl %ecx, %ecx
jle 0x4a06
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
leaq (%rax,%rsi,4), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
cmpl $0x1, %edi
setne %r8b
testl %edi, %edi
leaq 0x4(,%r8,4), %r8
movl $0x0, %r9d
cmovneq %r8, %r9
movss (%rsi,%r9), %xmm0
subss 0x4c(%rsp,%r9), %xmm0
movss %xmm0, (%rsi,%r9)
incl %edi
cmpl $0x3, %edi
jne 0x49c6
incq %rdx
cmpq %rcx, %rdx
jne 0x49bc
leaq 0x288(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x4b9e
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
imulq %r14, %rcx
testl %ecx, %ecx
jle 0x4a92
movss 0x18(%rsp), %xmm0
maxss 0x14(%rsp), %xmm0
maxss 0x10(%rsp), %xmm0
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
leaq (%rax,%rsi,4), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
cmpl $0x1, %edi
setne %r8b
testl %edi, %edi
leaq 0x4(,%r8,4), %r8
movl $0x0, %r9d
cmovneq %r8, %r9
movss (%rsi,%r9), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rsi,%r9)
incl %edi
cmpl $0x3, %edi
jne 0x4a55
incq %rdx
cmpq %rcx, %rdx
jne 0x4a4b
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x4aae
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x80(%rsp), %rdi
callq 0x2060
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4b0a
movq %rax, %rbx
jmp 0x4b66
movq %rax, %rbx
jmp 0x4b73
jmp 0x4b03
jmp 0x4b0f
jmp 0x4b03
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4b4a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x4b4a
jmp 0x4b0a
movq %rax, %rbx
jmp 0x4b4a
jmp 0x4b0f
movq %rax, %rbx
jmp 0x4b29
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4b29
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq 0x54a0(%rip), %rsi      # 0x9fd0
leaq 0x288(%rsp), %rdi
callq 0x2220
leaq 0x300(%rsp), %rdi
callq 0x2090
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x4b66
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x80(%rsp), %rdi
callq 0x2060
movq 0x58(%rsp), %rdi
callq 0x55dc
movq 0x40(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x4b96
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x22d0