// File: 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
movq %rsp, %rbp
subq $0x5a0, %rsp            # imm = 0x5A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x470(%rbp)
movq %rdi, %rax
movq %rax, -0x488(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x480(%rbp)
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0xa390
movaps -0x480(%rbp), %xmm0
movq -0x470(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movq %rdi, -0x468(%rbp)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
callq 0xa3b0
leaq -0x218(%rbp), %rdi
callq 0x2270
jmp 0x836d
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0x2150
jmp 0x8384
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x489(%rbp)
jmp 0x83a4
movb -0x489(%rbp), %al
testb $0x1, %al
jne 0x83b0
jmp 0x83ed
movl $0x1, -0x228(%rbp)
jmp 0x8cb8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x8ce5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x8cd9
leaq -0x248(%rbp), %rdi
callq 0x22e0
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x23c0
movb %al, -0x48a(%rbp)
jmp 0x8419
movb -0x48a(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x842a
jmp 0x8810
leaq -0x218(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x2070
jmp 0x843f
leaq -0x248(%rbp), %rdi
callq 0x20f0
movq %rax, -0x4a0(%rbp)
leaq -0x3e9(%rbp), %rdi
movq %rdi, -0x498(%rbp)
callq 0x2390
movq -0x4a0(%rbp), %rsi
movq -0x498(%rbp), %rdx
leaq -0x3e8(%rbp), %rdi
callq 0xa3d0
jmp 0x8481
leaq -0x3c8(%rbp), %rdi
leaq -0x3e8(%rbp), %rsi
movl $0x8, %edx
callq 0x2350
jmp 0x849b
leaq -0x3e8(%rbp), %rdi
callq 0x2140
leaq -0x3e9(%rbp), %rdi
callq 0x2240
leaq 0x6cbf(%rip), %rcx      # 0xf179
xorl %eax, %eax
movl %eax, %esi
leaq -0x248(%rbp), %rdi
movl $0x2, %edx
callq 0x22d0
movl %eax, -0x4a4(%rbp)
jmp 0x84d7
movl -0x4a4(%rbp), %eax
cmpl $0x0, %eax
jne 0x85db
leaq -0x3c8(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x22c0
jmp 0x84fb
leaq -0x3f8(%rbp), %rdi
callq 0x5b90
jmp 0x8509
movl $0x0, -0x3fc(%rbp)
cmpl $0x3, -0x3fc(%rbp)
jge 0x85c1
movslq -0x3fc(%rbp), %rax
leaq -0x3f8(%rbp,%rax,4), %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x2080
jmp 0x853d
jmp 0x853f
movl -0x3fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3fc(%rbp)
jmp 0x8513
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x8ccd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x8599
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x2140
leaq -0x3e9(%rbp), %rdi
callq 0x2240
jmp 0x8ccd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x87ff
movq -0x488(%rbp), %rdi
leaq -0x3f8(%rbp), %rsi
callq 0xa480
jmp 0x85d6
jmp 0x87ee
leaq 0x6b9a(%rip), %rcx      # 0xf17c
xorl %eax, %eax
movl %eax, %esi
leaq -0x248(%rbp), %rdi
movl $0x2, %edx
callq 0x22d0
movl %eax, -0x4a8(%rbp)
jmp 0x85ff
movl -0x4a8(%rbp), %eax
cmpl $0x0, %eax
jne 0x87ec
leaq -0x418(%rbp), %rdi
callq 0xa4f0
leaq -0x3c8(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x22c0
jmp 0x862f
leaq -0x248(%rbp), %rdi
leaq 0x6b42(%rip), %rsi      # 0xf17f
xorl %eax, %eax
movl %eax, %edx
callq 0x23e0
cmpq $-0x1, %rax
jne 0x86e5
jmp 0x8652
leaq -0x3c8(%rbp), %rdi
leaq -0x420(%rbp), %rsi
callq 0x2130
movq %rax, -0x4b0(%rbp)
jmp 0x866e
movq -0x4b0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x22b0
movb %al, -0x4b1(%rbp)
jmp 0x868c
movb -0x4b1(%rbp), %al
testb $0x1, %al
jne 0x8698
jmp 0x86e0
movl -0x420(%rbp), %eax
decl %eax
movl %eax, -0x420(%rbp)
leaq -0x418(%rbp), %rdi
leaq -0x420(%rbp), %rsi
callq 0xa510
jmp 0x86bb
jmp 0x8652
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x418(%rbp), %rdi
callq 0xa5f0
jmp 0x87ff
jmp 0x87c7
jmp 0x86e7
leaq -0x3c8(%rbp), %rdi
leaq -0x420(%rbp), %rsi
callq 0x2130
movq %rax, -0x4c0(%rbp)
jmp 0x8703
movq -0x4c0(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x22c0
movq %rax, -0x4c8(%rbp)
jmp 0x871f
movq -0x4c8(%rbp), %rdi
leaq -0x41c(%rbp), %rsi
callq 0x2130
movq %rax, -0x4d0(%rbp)
jmp 0x873b
movq -0x4d0(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x22c0
movq %rax, -0x4d8(%rbp)
jmp 0x8757
movq -0x4d8(%rbp), %rdi
leaq -0x41c(%rbp), %rsi
callq 0x2130
movq %rax, -0x4e0(%rbp)
jmp 0x8773
movq -0x4e0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x22b0
movb %al, -0x4e1(%rbp)
jmp 0x8791
movb -0x4e1(%rbp), %al
testb $0x1, %al
jne 0x879d
jmp 0x87c5
movl -0x420(%rbp), %eax
decl %eax
movl %eax, -0x420(%rbp)
leaq -0x418(%rbp), %rdi
leaq -0x420(%rbp), %rsi
callq 0xa510
jmp 0x87c0
jmp 0x86e7
jmp 0x87c7
movq -0x488(%rbp), %rdi
addq $0x18, %rdi
leaq -0x418(%rbp), %rsi
callq 0xa580
jmp 0x87e0
leaq -0x418(%rbp), %rdi
callq 0xa5f0
jmp 0x87ee
leaq -0x3c8(%rbp), %rdi
callq 0x2110
jmp 0x83f9
leaq -0x3c8(%rbp), %rdi
callq 0x2110
jmp 0x8ccd
movq 0xd7c1(%rip), %rdi      # 0x15fd8
leaq 0x6963(%rip), %rsi      # 0xf181
callq 0x21f0
movq %rax, -0x4f0(%rbp)
jmp 0x882c
movq -0x488(%rbp), %rdi
callq 0x78d0
movq -0x4f0(%rbp), %rdi
movq %rax, %rsi
callq 0x2090
movq %rax, -0x4f8(%rbp)
jmp 0x8850
movq -0x4f8(%rbp), %rdi
leaq 0x6929(%rip), %rsi      # 0xf187
callq 0x21f0
movq %rax, -0x500(%rbp)
jmp 0x886c
movq -0x488(%rbp), %rdi
addq $0x18, %rdi
callq 0xa650
movq -0x500(%rbp), %rdi
movq %rax, %rsi
callq 0x2090
movq %rax, -0x508(%rbp)
jmp 0x8894
movq -0x508(%rbp), %rdi
movq 0xd726(%rip), %rsi      # 0x15fc8
callq 0x2220
jmp 0x88a9
leaq -0x42c(%rbp), %rdi
callq 0x5b90
jmp 0x88b7
leaq -0x438(%rbp), %rdi
callq 0x5b90
jmp 0x88c5
leaq -0x444(%rbp), %rdi
callq 0x5b90
jmp 0x88d3
movq -0x488(%rbp), %rdi
leaq -0x42c(%rbp), %rsi
leaq -0x438(%rbp), %rdx
callq 0x8d10
jmp 0x88ef
movl $0x0, -0x448(%rbp)
cmpl $0x3, -0x448(%rbp)
jge 0x89c7
movslq -0x448(%rbp), %rsi
leaq -0x42c(%rbp), %rdi
callq 0x5c40
movq %rax, -0x510(%rbp)
jmp 0x8922
movq -0x510(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x51c(%rbp)
movslq -0x448(%rbp), %rsi
leaq -0x438(%rbp), %rdi
callq 0x5c40
movq %rax, -0x518(%rbp)
jmp 0x8951
movss -0x51c(%rbp), %xmm0
movq -0x518(%rbp), %rax
movss (%rax), %xmm1
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x6784(%rip), %xmm1     # 0xf0f8
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x52c(%rbp)
movslq -0x448(%rbp), %rsi
leaq -0x444(%rbp), %rdi
callq 0x5c40
movq %rax, -0x528(%rbp)
jmp 0x89a0
movq -0x528(%rbp), %rax
movss -0x52c(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x448(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x448(%rbp)
jmp 0x88f9
movl $0x0, -0x44c(%rbp)
movq -0x488(%rbp), %rdi
movl -0x44c(%rbp), %eax
movl %eax, -0x530(%rbp)
callq 0x78d0
movq %rax, %rcx
movl -0x530(%rbp), %eax
cmpl %ecx, %eax
jge 0x8af2
movl $0x0, -0x450(%rbp)
cmpl $0x3, -0x450(%rbp)
jge 0x8adc
movq -0x488(%rbp), %rdi
movslq -0x44c(%rbp), %rsi
callq 0x6ff0
movq %rax, %rdi
movslq -0x450(%rbp), %rsi
callq 0x5c40
movq %rax, -0x538(%rbp)
jmp 0x8a3c
movq -0x538(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x544(%rbp)
movslq -0x450(%rbp), %rsi
leaq -0x444(%rbp), %rdi
callq 0x5c40
movq %rax, -0x540(%rbp)
jmp 0x8a6b
movq -0x488(%rbp), %rdi
movss -0x544(%rbp), %xmm0
movq -0x540(%rbp), %rax
movss (%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x554(%rbp)
movslq -0x44c(%rbp), %rsi
callq 0x6ff0
movq %rax, %rdi
movslq -0x450(%rbp), %rsi
callq 0x5c40
movq %rax, -0x550(%rbp)
jmp 0x8ab5
movq -0x550(%rbp), %rax
movss -0x554(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x450(%rbp)
jmp 0x8a04
jmp 0x8ade
movl -0x44c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44c(%rbp)
jmp 0x89d1
movq -0x488(%rbp), %rdi
leaq -0x42c(%rbp), %rsi
leaq -0x438(%rbp), %rdx
callq 0x8d10
jmp 0x8b0e
xorl %eax, %eax
movl %eax, %esi
leaq -0x438(%rbp), %rdi
callq 0x5c40
movq %rax, -0x560(%rbp)
jmp 0x8b27
leaq -0x438(%rbp), %rdi
movl $0x1, %esi
callq 0x5c40
movq %rax, -0x568(%rbp)
jmp 0x8b41
leaq -0x438(%rbp), %rdi
movl $0x2, %esi
callq 0x5c40
movq %rax, -0x570(%rbp)
jmp 0x8b5b
movq -0x570(%rbp), %rsi
movq -0x568(%rbp), %rdi
callq 0x6f70
movq %rax, -0x578(%rbp)
jmp 0x8b77
movq -0x578(%rbp), %rsi
movq -0x560(%rbp), %rdi
callq 0x6f70
movq %rax, -0x580(%rbp)
jmp 0x8b93
movq -0x580(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x454(%rbp)
movl $0x0, -0x458(%rbp)
movq -0x488(%rbp), %rdi
movl -0x458(%rbp), %eax
movl %eax, -0x584(%rbp)
callq 0x78d0
movq %rax, %rcx
movl -0x584(%rbp), %eax
cmpl %ecx, %eax
jge 0x8ca2
movl $0x0, -0x45c(%rbp)
cmpl $0x3, -0x45c(%rbp)
jge 0x8c8c
movq -0x488(%rbp), %rdi
movslq -0x458(%rbp), %rsi
callq 0x6ff0
movq %rax, %rdi
movslq -0x45c(%rbp), %rsi
callq 0x5c40
movq %rax, -0x590(%rbp)
jmp 0x8c1b
movq -0x488(%rbp), %rdi
movq -0x590(%rbp), %rax
movss (%rax), %xmm0
movss -0x454(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x59c(%rbp)
movslq -0x458(%rbp), %rsi
callq 0x6ff0
movq %rax, %rdi
movslq -0x45c(%rbp), %rsi
callq 0x5c40
movq %rax, -0x598(%rbp)
jmp 0x8c65
movq -0x598(%rbp), %rax
movss -0x59c(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
jmp 0x8be3
jmp 0x8c8e
movl -0x458(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x458(%rbp)
jmp 0x8bb0
leaq -0x248(%rbp), %rdi
callq 0x2140
movl $0x0, -0x228(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x20a0
addq $0x5a0, %rsp            # imm = 0x5A0
popq %rbp
retq
leaq -0x248(%rbp), %rdi
callq 0x2140
leaq -0x218(%rbp), %rdi
callq 0x20a0
movq -0x468(%rbp), %rdi
callq 0xa680
movq -0x470(%rbp), %rdi
callq 0x6f10
movq -0x220(%rbp), %rdi
callq 0x2380
nopl (%rax)