// File: srajangarg[P]raytracer/src/Primitives.cpp
// Function: Triangle::intersect(Ray&) const
bool
Triangle::intersect(Ray & ray) const
{
  Vec3 e1, e2;  //Edge1, Edge2
  Vec3 P, Q, T;
  double det, inv_det, u, v, t;
  Ray new_ray = ray;
  new_ray.transform(worldToModel_);

  // dind vectors for two edges sharing V1
  e1 = verts[1] - verts[0];
  e2 = verts[2] - verts[0];

  // begin calculating determinant - also used to calculate u parameter
  P = new_ray.direction() ^ e2;
  // if determinant is near zero, ray lies in plane of triangle or ray is parallel to plane of triangle
  det = e1 * P;

  if(det > -EPS and det < EPS) return false;
  inv_det = 1.0 / det;

  // calculate distance from V1 to ray origin
  T = new_ray.start() - verts[0];

  // calculate u parameter and test bound
  u = T * P * inv_det;
  // the intersection lies outside of the triangle
  if(u < 0 or u > 1) return false;

  // prepare to test v parameter
  Q = T ^ e1;

  // Ccalculate V parameter and test bound
  v = new_ray.direction() * Q * inv_det;
  // the intersection lies outside of the triangle
  if(v < 0 or u + v  > 1) return false;

  t = e2 * Q * inv_det;

  if (t > EPS and ray.minT() > t)
  {
    ray.setMinT(t);
    return true;
  }

  return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6fc0
leaq -0x48(%rbp), %rdi
callq 0x6fc0
leaq -0x60(%rbp), %rdi
callq 0x6fc0
leaq -0x78(%rbp), %rdi
callq 0x6fc0
leaq -0x90(%rbp), %rdi
callq 0x6fc0
movq -0x18(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x3c940
movq -0x178(%rbp), %rsi
addq $0x88, %rsi
leaq -0xf8(%rbp), %rdi
callq 0x3e1a0
movq -0x178(%rbp), %rdx
movq %rdx, %rsi
addq $0x190, %rsi            # imm = 0x190
addq $0x18, %rsi
addq $0x190, %rdx            # imm = 0x190
leaq -0x110(%rbp), %rdi
callq 0x3bd80
leaq -0x30(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x72d0
movq -0x178(%rbp), %rdx
movq %rdx, %rsi
addq $0x190, %rsi            # imm = 0x190
addq $0x30, %rsi
addq $0x190, %rdx            # imm = 0x190
leaq -0x128(%rbp), %rdi
callq 0x3bd80
leaq -0x48(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x72d0
leaq -0xf8(%rbp), %rdi
callq 0x3e290
movq %rax, %rsi
leaq -0x140(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3e930
leaq -0x60(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x72d0
leaq -0x30(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3e2b0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movsd 0xb40c(%rip), %xmm1     # 0x48d08
ucomisd %xmm1, %xmm0
jbe 0x3d91d
movsd 0xb406(%rip), %xmm0     # 0x48d10
ucomisd -0x98(%rbp), %xmm0
jbe 0x3d91d
movb $0x0, -0x1(%rbp)
jmp 0x3da9e
movsd 0x9813(%rip), %xmm0     # 0x47138
divsd -0x98(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3e280
movq -0x178(%rbp), %rdx
movq %rax, %rsi
addq $0x190, %rdx            # imm = 0x190
leaq -0x158(%rbp), %rdi
callq 0x3bd80
leaq -0x90(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x72d0
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3e2b0
mulsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
xorps %xmm0, %xmm0
ucomisd -0xa8(%rbp), %xmm0
ja 0x3d9b4
movsd -0xa8(%rbp), %xmm0
movsd 0x978a(%rip), %xmm1     # 0x47138
ucomisd %xmm1, %xmm0
jbe 0x3d9bd
movb $0x0, -0x1(%rbp)
jmp 0x3da9e
leaq -0x170(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x3e930
leaq -0x78(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x72d0
leaq -0xf8(%rbp), %rdi
callq 0x3e290
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3e2b0
mulsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
xorps %xmm0, %xmm0
ucomisd -0xb0(%rbp), %xmm0
ja 0x3da37
movsd -0xa8(%rbp), %xmm0
addsd -0xb0(%rbp), %xmm0
movsd 0x9707(%rip), %xmm1     # 0x47138
ucomisd %xmm1, %xmm0
jbe 0x3da3d
movb $0x0, -0x1(%rbp)
jmp 0x3da9e
leaq -0x48(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x3e2b0
mulsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0xb8(%rbp), %xmm0
movsd 0xb2a6(%rip), %xmm1     # 0x48d10
ucomisd %xmm1, %xmm0
jbe 0x3da9a
movq -0x18(%rbp), %rdi
callq 0x3e310
ucomisd -0xb8(%rbp), %xmm0
jbe 0x3da9a
movq -0x18(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x3e330
movb $0x1, -0x1(%rbp)
jmp 0x3da9e
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)