// File: srajangarg[P]raytracer/src/Primitives.cpp
// Function: Triangle::calculateNormal(Vec3 const&) const
Vec3
Triangle::calculateNormal(Vec3 const & position) const
{ 
  Vec3 pos = worldToModel_ * position;
  Vec3 norm(0, 0, 0);

  if (smooth_normals) {

    // get the areas of each of the subtriangles, as divided by pos
    double a[3];
    for (int i = 0; i < 3; ++i)
      a[i] = ((verts[(i+1)%3] - pos) ^ (verts[(i+2)%3] - pos)).length();

    // weigh the normals according to these areas
    for (int i = 0; i < 3; ++i)
      norm += a[i] * norms[i];

  } else {

    // simple normal to the triangle
    norm = (verts[1] - verts[0]) ^ (verts[2] - verts[0]);
  }

  return Vec3(worldToModel_.transpose() * Vec4(norm, 0), 3).normalize();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x208(%rbp)
movq %rdi, %rax
movq %rax, -0x200(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x1f8(%rbp)
addq $0x88, %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x3e350
leaq -0x48(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x147b0
movq -0x1f8(%rbp), %rax
testb $0x1, 0x188(%rax)
je 0x3dc3d
movl $0x0, -0x64(%rbp)
cmpl $0x3, -0x64(%rbp)
jge 0x3dbd2
movq -0x1f8(%rbp), %rsi
addq $0x190, %rsi            # imm = 0x190
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movslq %edx, %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq -0x98(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x3bd80
movq -0x1f8(%rbp), %rsi
addq $0x190, %rsi            # imm = 0x190
movl -0x64(%rbp), %eax
addl $0x2, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movslq %edx, %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq -0xb0(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x3bd80
leaq -0x80(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x3e930
leaq -0x80(%rbp), %rdi
callq 0x14e30
movslq -0x64(%rbp), %rax
movsd %xmm0, -0x60(%rbp,%rax,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3db24
movl $0x0, -0xb4(%rbp)
cmpl $0x3, -0xb4(%rbp)
jge 0x3dc3b
movq -0x1f8(%rbp), %rsi
movslq -0xb4(%rbp), %rax
movsd -0x60(%rbp,%rax,8), %xmm0
addq $0x140, %rsi            # imm = 0x140
movslq -0xb4(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq -0xd0(%rbp), %rdi
callq 0x3ea20
leaq -0x48(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3ea60
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x3dbdc
jmp 0x3dcb7
movq -0x1f8(%rbp), %rdx
movq %rdx, %rsi
addq $0x190, %rsi            # imm = 0x190
addq $0x18, %rsi
addq $0x190, %rdx            # imm = 0x190
leaq -0x100(%rbp), %rdi
callq 0x3bd80
movq -0x1f8(%rbp), %rdx
movq %rdx, %rsi
addq $0x190, %rsi            # imm = 0x190
addq $0x30, %rsi
addq $0x190, %rdx            # imm = 0x190
leaq -0x118(%rbp), %rdi
callq 0x3bd80
leaq -0xe8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
leaq -0x118(%rbp), %rdx
callq 0x3e930
leaq -0x48(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x72d0
movq -0x1f8(%rbp), %rsi
addq $0x88, %rsi
leaq -0x1d0(%rbp), %rdi
callq 0x3e560
leaq -0x1f0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
xorps %xmm0, %xmm0
callq 0x3e7e0
leaq -0x150(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
leaq -0x1f0(%rbp), %rdx
callq 0x3e3b0
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl $0x3, %edx
callq 0x3e830
leaq -0x130(%rbp), %rdi
callq 0x14e00
movq -0x208(%rbp), %rdi
movq %rax, %rsi
callq 0x71a0
movq -0x200(%rbp), %rax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
nopw %cs:(%rax,%rax)