// File: /workspace/github_cpp_makefile_star2_O3/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), %rcx
testb $0x5, 0xa0(%rsp,%rcx)
jne 0x4a7a
leaq 0x70(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
movq -0x18(%rax), %rax
testb $0x2, 0xa0(%rsp,%rax)
jne 0x4854
leaq (%rsp,%rax), %rdi
addq $0x80, %rdi
leaq 0x20(%rsp), %rbp
leaq 0x48(%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 0x22f0
movq 0x60(%rsp), %rsi
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r14, %rdx
callq 0x5510
movq %r15, %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0x2290
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4638
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movl $0x2, %edx
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
leaq 0x2b2e(%rip), %rcx      # 0x7179
callq 0x2200
testl %eax, %eax
je 0x47a2
movl $0x2, %edx
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
leaq 0x2b11(%rip), %rcx      # 0x717c
callq 0x2200
testl %eax, %eax
jne 0x4817
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 0x2ada(%rip), %rsi      # 0x717f
xorl %edx, %edx
callq 0x22c0
cmpq $-0x1, %rax
je 0x475d
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 0x4776
movl 0xc(%rsp), %eax
decl %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x471e
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x46b6
leaq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0x584e
jmp 0x46b6
movl 0xc(%rsp), %eax
decl %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4750
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x475d
leaq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0x584e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x472d
movq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x554e
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4817
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x4817
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 0x2300
addq $0x4, %r13
cmpq $0xc, %r13
jne 0x47c6
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0x4805
movl 0x18(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rbp)
leaq 0x20(%rsp), %rbp
jmp 0x4817
movq %rbp, %rdi
leaq 0x10(%rsp), %rdx
callq 0x56e4
leaq 0x20(%rsp), %rbp
movq %r15, %rdi
movq 0x57af(%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 0x45dd
movq 0x577d(%rip), %rdi      # 0x9fd8
leaq 0x291f(%rip), %rsi      # 0x7181
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 0x5747(%rip), %rdi      # 0x9fd8
callq 0x2120
movq %rax, %rbx
leaq 0x28e7(%rip), %rsi      # 0x7187
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, 0x48(%rsp)
movl %ebx, 0x50(%rsp)
movq 0x40(%rsp), %rdi
callq 0x4b6a
movss 0x27fa(%rip), %xmm0     # 0x7124
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, 0x48(%rsp,%rcx)
incl %ebx
cmpl $0x3, %ebx
jne 0x492a
movq 0x40(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
imulq %r14, %rcx
testl %ecx, %ecx
jle 0x49d2
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 0x48(%rsp,%r9), %xmm0
movss %xmm0, (%rsi,%r9)
incl %edi
cmpl $0x3, %edi
jne 0x4992
incq %rdx
cmpq %rcx, %rdx
jne 0x4988
leaq 0x288(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x4b6a
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
imulq %r14, %rcx
testl %ecx, %ecx
jle 0x4a5e
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 0x4a21
incq %rdx
cmpq %rcx, %rdx
jne 0x4a17
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x4a7a
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 0x4ad6
movq %rax, %rbx
jmp 0x4b32
movq %rax, %rbx
jmp 0x4b3f
jmp 0x4acf
jmp 0x4adb
jmp 0x4acf
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4b16
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x4b16
jmp 0x4ad6
movq %rax, %rbx
jmp 0x4b16
jmp 0x4adb
movq %rax, %rbx
jmp 0x4af5
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4af5
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq 0x54d4(%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 0x4b32
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x80(%rsp), %rdi
callq 0x2060
movq 0x58(%rsp), %rdi
callq 0x557a
movq 0x40(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x4b62
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x22d0