// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x50, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movups (%rsi), %xmm0
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movups 0x10(%rsi), %xmm0
movaps %xmm0, 0x10(%r15)
movups 0x20(%rsi), %xmm0
movaps %xmm0, 0x20(%r15)
movups 0x30(%rsi), %xmm0
movaps %xmm0, 0x30(%r15)
leaq 0x88(%rdi), %rsi
movq %r15, %rdi
callq 0x1e2ca
movsd 0x1a8(%r14), %xmm3
movsd 0x190(%r14), %xmm12
movsd 0x198(%r14), %xmm11
subsd %xmm12, %xmm3
movsd 0x1b0(%r14), %xmm5
subsd %xmm11, %xmm5
movsd 0x1a0(%r14), %xmm13
movsd 0x1c0(%r14), %xmm0
subsd %xmm12, %xmm0
movsd 0x1c8(%r14), %xmm2
subsd %xmm11, %xmm2
movsd 0x1d0(%r14), %xmm1
subsd %xmm13, %xmm1
movsd 0x18(%r15), %xmm9
movsd 0x20(%r15), %xmm6
movsd 0x28(%r15), %xmm7
movapd %xmm2, %xmm4
mulsd %xmm7, %xmm4
movapd %xmm6, %xmm15
mulsd %xmm1, %xmm15
subsd %xmm4, %xmm15
movapd %xmm1, %xmm8
mulsd %xmm9, %xmm8
movapd %xmm7, %xmm4
mulsd %xmm0, %xmm4
subsd %xmm8, %xmm4
movapd %xmm0, %xmm8
movsd %xmm6, (%rsp)
mulsd %xmm6, %xmm8
movsd %xmm9, 0x8(%rsp)
mulsd %xmm2, %xmm9
subsd %xmm8, %xmm9
movapd %xmm5, %xmm8
mulsd %xmm4, %xmm8
movapd %xmm3, %xmm10
mulsd %xmm15, %xmm10
addsd %xmm8, %xmm10
movsd 0x1b8(%r14), %xmm14
subsd %xmm13, %xmm14
movapd %xmm14, %xmm8
mulsd %xmm9, %xmm8
addsd %xmm10, %xmm8
movapd 0x3e21(%rip), %xmm10    # 0x21a60
andpd %xmm8, %xmm10
movsd 0x56fc(%rip), %xmm6     # 0x23348
ucomisd %xmm10, %xmm6
jbe 0x1dc5f
xorl %eax, %eax
addq $0x50, %rsp
popq %rbx
popq %r14
popq %r15
retq
movsd 0x3499(%rip), %xmm6     # 0x21100
divsd %xmm8, %xmm6
movsd 0x10(%rsp), %xmm10
movsd 0x18(%rsp), %xmm8
subsd %xmm12, %xmm10
subsd %xmm11, %xmm8
movsd 0x20(%rsp), %xmm11
subsd %xmm13, %xmm11
mulsd %xmm8, %xmm4
mulsd %xmm10, %xmm15
addsd %xmm4, %xmm15
mulsd %xmm11, %xmm9
addsd %xmm15, %xmm9
mulsd %xmm6, %xmm9
xorl %eax, %eax
xorpd %xmm4, %xmm4
ucomisd %xmm9, %xmm4
ja 0x1dc55
ucomisd 0x343c(%rip), %xmm9     # 0x21100
ja 0x1dc55
movapd %xmm6, %xmm12
movapd %xmm5, %xmm6
mulsd %xmm11, %xmm6
movapd %xmm8, %xmm4
mulsd %xmm14, %xmm4
subsd %xmm6, %xmm4
mulsd %xmm10, %xmm14
mulsd %xmm3, %xmm11
subsd %xmm14, %xmm11
mulsd %xmm8, %xmm3
mulsd %xmm5, %xmm10
subsd %xmm3, %xmm10
movsd (%rsp), %xmm5
mulsd %xmm11, %xmm5
movsd 0x8(%rsp), %xmm3
mulsd %xmm4, %xmm3
addsd %xmm5, %xmm3
mulsd %xmm10, %xmm7
addsd %xmm3, %xmm7
mulsd %xmm12, %xmm7
xorl %eax, %eax
xorpd %xmm3, %xmm3
ucomisd %xmm7, %xmm3
ja 0x1dc55
addsd %xmm7, %xmm9
ucomisd 0x33bc(%rip), %xmm9     # 0x21100
ja 0x1dc55
movapd %xmm12, %xmm6
mulsd %xmm11, %xmm2
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm10, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm1, %xmm6
xorl %eax, %eax
ucomisd 0x55d5(%rip), %xmm6     # 0x23348
jbe 0x1dc55
movsd 0x30(%rbx), %xmm0
ucomisd %xmm6, %xmm0
jbe 0x1dc55
movsd %xmm6, 0x30(%rbx)
movb $0x1, %al
jmp 0x1dc55