// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x88(%rsi), %r14
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x1e3bb
cmpb $0x1, 0x188(%r15)
jne 0x1dea7
leaq 0x190(%r15), %rax
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
xorl %ecx, %ecx
pushq $0x2
popq %rdx
cmpq $0x3, %rcx
je 0x1df35
leaq 0x1(%rcx), %rsi
cmpq $0x3, %rsi
movl $0x0, %edi
cmovneq %rsi, %rdi
imulq $0x18, %rdi, %rdi
movsd (%rax,%rdi), %xmm4
movsd 0x8(%rax,%rdi), %xmm6
subsd %xmm0, %xmm4
subsd %xmm1, %xmm6
movsd 0x10(%rax,%rdi), %xmm3
subsd %xmm2, %xmm3
leal -0x1(%rcx), %edi
cmpq $0x1, %rsi
cmoveq %rdx, %rdi
imulq $0x18, %rdi, %rdi
movsd (%rax,%rdi), %xmm7
movsd 0x8(%rax,%rdi), %xmm5
subsd %xmm0, %xmm7
subsd %xmm1, %xmm5
movsd 0x10(%rax,%rdi), %xmm8
subsd %xmm2, %xmm8
movapd %xmm5, %xmm9
mulsd %xmm3, %xmm9
mulsd %xmm7, %xmm3
mulsd %xmm6, %xmm7
mulsd %xmm8, %xmm6
subsd %xmm9, %xmm6
mulsd %xmm4, %xmm8
subsd %xmm8, %xmm3
mulsd %xmm4, %xmm5
subsd %xmm7, %xmm5
mulsd %xmm3, %xmm3
mulsd %xmm6, %xmm6
addsd %xmm3, %xmm6
mulsd %xmm5, %xmm5
addsd %xmm6, %xmm5
sqrtsd %xmm5, %xmm3
movsd %xmm3, 0xa0(%rsp,%rcx,8)
movq %rsi, %rcx
jmp 0x1dde6
movsd 0x1a8(%r15), %xmm5
movsd 0x190(%r15), %xmm0
subsd %xmm0, %xmm5
movsd 0x1c8(%r15), %xmm1
movupd 0x1b0(%r15), %xmm2
movupd 0x198(%r15), %xmm3
subpd %xmm3, %xmm2
subsd %xmm3, %xmm1
movsd 0x1d0(%r15), %xmm4
movhpd 0x1c0(%r15), %xmm4      # xmm4 = xmm4[0],mem[0]
shufpd $0x1, %xmm0, %xmm3      # xmm3 = xmm3[1],xmm0[0]
subpd %xmm3, %xmm4
movapd %xmm2, %xmm0
shufpd $0x1, %xmm5, %xmm0      # xmm0 = xmm0[1],xmm5[0]
mulsd %xmm1, %xmm5
unpcklpd %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0]
mulpd %xmm0, %xmm1
movapd %xmm2, %xmm0
mulpd %xmm4, %xmm0
subpd %xmm1, %xmm0
movapd %xmm0, (%rsp)
unpckhpd %xmm4, %xmm4            # xmm4 = xmm4[1,1]
mulsd %xmm2, %xmm4
subsd %xmm4, %xmm5
movapd %xmm5, 0x10(%rsp)
jmp 0x1df88
addq $0x150, %r15            # imm = 0x150
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1df7d
movsd 0xa0(%rsp,%rax,8), %xmm0
movupd -0x10(%r15), %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
mulsd (%r15), %xmm0
addpd %xmm2, %xmm4
addsd %xmm0, %xmm3
incq %rax
addq $0x18, %r15
jmp 0x1df46
movapd %xmm3, 0x10(%rsp)
movapd %xmm4, (%rsp)
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e4d4
leaq 0x60(%rsp), %rdx
movaps (%rsp), %xmm0
movaps %xmm0, (%rdx)
movaps 0x10(%rsp), %xmm0
movsd %xmm0, 0x10(%rdx)
andq $0x0, 0x18(%rdx)
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1e40c
movaps (%r14), %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, (%rdi)
movsd 0x10(%r14), %xmm0
movsd %xmm0, 0x10(%rdi)
callq 0xa1b2
movups (%rax), %xmm0
movups %xmm0, (%rbx)
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x10(%rbx)
movq %rbx, %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r14
popq %r15
retq
nop