// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::Intersection(gaps::R3Ray const&, gaps::R3MeshFace*, gaps::R3MeshIntersection*) const
R3MeshType R3Mesh::
Intersection(const R3Ray& ray, R3MeshFace *f, R3MeshIntersection *intersection) const
{
  // Initialize pick variables
  R3MeshType type = R3_MESH_NULL_TYPE;
  if (intersection) {
    intersection->type = R3_MESH_NULL_TYPE;
    intersection->vertex = NULL;
    intersection->edge = NULL;
    intersection->face = NULL;
    intersection->t = RN_INFINITY;
  }

  // Check if degenerate face
  R3Vector normal = FaceNormal(f);
  if (RNIsZero(normal.Dot(normal))) return R3_MESH_NULL_TYPE;

  // Check if face intersects ray
  RNScalar t;
  R3Point p;
  R3Plane plane = FacePlane(f);
  if (R3Intersects(ray, plane, &p, &t) || R3Intersects(ray, -plane, &p, &t)) {
    // Check if face bbox contains p
    if (R3Contains(FaceBBox(f), p)) {
#if 1
      // Get convenient variables
      R3Point& p0 = f->vertex[0]->position;
      R3Point& p1 = f->vertex[1]->position;
      R3Point& p2 = f->vertex[2]->position;

      // Check side of first edge
      R3Vector e0 = p1 - p0;
      e0.Normalize();
      R3Vector n0 = normal % e0;
      R3Plane s0(p0, n0);
      RNScalar b0 = R3SignedDistance(s0, p);
      if (RNIsNegative(b0)) return R3_MESH_NULL_TYPE;

      // Check side of second edge
      R3Vector e1 = p2 - p1;
      e1.Normalize();
      R3Vector n1 = normal % e1;
      R3Plane s1(p1, n1);
      RNScalar b1 = R3SignedDistance(s1, p);
      if (RNIsNegative(b1)) return R3_MESH_NULL_TYPE;

      // Check side of third edge
      R3Vector e2 = p0 - p2;
      e2.Normalize();
      R3Vector n2 = normal % e2;
      R3Plane s2(p2, n2);
      RNScalar b2 = R3SignedDistance(s2, p);
      if (RNIsNegative(b2)) return R3_MESH_NULL_TYPE;

      // Fill in interesection info
      // Could easily return intersection type, etc. but too lazy now
      type = R3_MESH_FACE_TYPE;
      if (intersection) {
        intersection->type = type;
        intersection->point = p;
        intersection->vertex = NULL;
        intersection->edge = NULL;
        intersection->face = f;
        intersection->t = t;
      }
#else
      // This does not always work -- don't know why
      // Compute interpolation parameters for intersection point (from Graphics Gems I, page 393)
      RNScalar a, b;
      RNDimension dim = FaceNormal(f).MaxDimension();
      RNDimension dim1 = (dim + 1) % 3;
      RNDimension dim2 = (dim + 2) % 3;
      R3Point& p0 = f->vertex[0]->position;
      R3Point& p1 = f->vertex[1]->position;
      R3Point& p2 = f->vertex[2]->position;
      RNScalar u0 = p[dim1] - p0[dim1];
      RNScalar v0 = p[dim2] - p0[dim2];
      RNScalar u1 = p1[dim1] - p0[dim1];
      RNScalar u2 = p2[dim1] - p0[dim1];
      RNScalar v1 = p1[dim2] - p0[dim2];
      RNScalar v2 = p2[dim2] - p0[dim2];
      if (RNIsZero(u1)) {
        if (RNIsZero(u2)) {
          a = RN_INFINITY;
          b = RN_INFINITY;
        }
        else {
          b = u0/u2;
          if (RNIsLess(b, 0.0) || RNIsGreater(b, 1.0) || RNIsZero(v1)) a = b = RN_INFINITY;
          else a = (v0 - b*v2) / v1;
        }
      }
      else {
        RNScalar denom = v2*u1 - u2*v1;
        if (RNIsZero(denom)) a = b = RN_INFINITY;
        else {
          b = (v0*u1 - u0*v1) / denom;
          if (RNIsLess(b, 0.0) || RNIsGreater(b, 1.0) || RNIsZero(u1)) a = b = RN_INFINITY;
          else a = (u0 - b*u2) / u1;
        }
      }

      // Check if intersection point is inside triangle
      if (RNIsPositiveOrZero(a, 0.001) && RNIsPositiveOrZero(b, 0.001) && RNIsLess(a+b, 1.0, 0.001)) {
        if (RNIsZero(a, 0.01)) {
          if (RNIsZero(b, 0.01)) {
            type = R3_MESH_VERTEX_TYPE;
            if (intersection) {
              intersection->type = type;
              intersection->point = p;
              intersection->vertex = f->vertex[0];
              intersection->edge = f->edge[0];
              intersection->face = f;
              intersection->t = t;
            }
          }
          else if (RNIsEqual(b, 1.0, 0.01)) {
            type = R3_MESH_VERTEX_TYPE;
            if (intersection) {
              intersection->type = type;
              intersection->point = p;
              intersection->vertex = f->vertex[2];
              intersection->edge = f->edge[2];
              intersection->face = f;
              intersection->t = t;
            }
          }
          else {
            type = R3_MESH_EDGE_TYPE;
            if (intersection) {
              intersection->type = type;
              intersection->point = p;
              intersection->vertex = NULL;
              intersection->edge = f->edge[2];
              intersection->face = f;
              intersection->t = t;
            }
          }
        }
        else if (RNIsEqual(a, 1.0, 0.01)) {
          type = R3_MESH_VERTEX_TYPE;
          if (intersection) {
            intersection->type = type;
            intersection->point = p;
            intersection->vertex = f->vertex[1];
            intersection->edge = f->edge[1];
            intersection->face = f;
            intersection->t = t;
          }
        }                      
        else {
          if (RNIsZero(b, 0.01)) {
            type = R3_MESH_EDGE_TYPE;
            if (intersection) {
              intersection->type = type;
              intersection->point = p;
              intersection->vertex = NULL;
              intersection->edge = f->edge[0];
              intersection->face = f;
              intersection->t = t;
            }
          }
          else if (RNIsEqual(a+b, 1.0, 0.01)) {
            type = R3_MESH_EDGE_TYPE;
            if (intersection) {
              intersection->type = type;
              intersection->point = p;
              intersection->vertex = NULL;
              intersection->edge = f->edge[1];
              intersection->face = f;
              intersection->t = t;
            }
          }
          else {
            type = R3_MESH_FACE_TYPE;
            if (intersection) {
              intersection->type = type;
              intersection->point = p;
              intersection->vertex = NULL;
              intersection->edge = NULL;
              intersection->face = f;
              intersection->t = t;
            }
          }
        }
      }
#endif
    }
  }

  // Return intersection type
  return type;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
testq %rcx, %rcx
je 0x1a2d1
movl $0x0, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq $0x0, 0x18(%rbx)
leaq 0xcb8e0(%rip), %rax     # 0xe5ba8
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rbx)
testb $0x4, 0xa8(%r14)
jne 0x1a2ea
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r14), %r13
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3d4ce
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x3d756
leaq 0xcb888(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x87d04(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jb 0x1a332
xorl %eax, %eax
ucomisd %xmm0, %xmm1
jae 0x1a5f0
leaq 0x10(%rsp), %rdi
callq 0x3ca24
testb $0x4, 0xa8(%r14)
jne 0x1a355
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2c8(%rax)
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3b51a
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x435e3
testl %eax, %eax
jne 0x1a3d3
leaq 0x58(%rsp), %r13
leaq 0x98(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e154
movsd 0x18(%rbp), %xmm0
xorpd 0x87c7a(%rip), %xmm0    # 0xa2020
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3b582
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x435e3
testl %eax, %eax
je 0x1a5de
testb $0x8, 0xa8(%r14)
jne 0x1a3ec
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2d0(%rax)
leaq 0x58(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0x47df8
testl %eax, %eax
je 0x1a5de
movq 0x8(%r14), %r12
movq 0x10(%r14), %r13
addq $0x18, %r12
addq $0x18, %r13
movq 0x18(%r14), %rax
movq %rax, (%rsp)
leaq 0x130(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3cc88
movq %rbp, %rdi
callq 0x3d7e0
leaq 0x118(%rsp), %r15
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
movq %rbp, %rdx
callq 0x3de12
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3b5a6
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
callq 0x4a10f
leaq 0xcb721(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorpd 0x87b9d(%rip), %xmm1    # 0xa2020
xorl %eax, %eax
ucomisd %xmm0, %xmm1
ja 0x1a5f0
movq (%rsp), %rsi
addq $0x18, %rsi
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq %rsi, (%rsp)
movq %r13, %rdx
callq 0x3cc88
movq %r15, %rdi
callq 0x3d7e0
leaq 0xe8(%rsp), %rbp
leaq 0x28(%rsp), %rsi
movq %rbp, %rdi
movq %r15, %rdx
callq 0x3de12
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3b5a6
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4a10f
leaq 0xcb6a3(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorpd 0x87b1f(%rip), %xmm1    # 0xa2020
xorl %eax, %eax
ucomisd %xmm0, %xmm1
ja 0x1a5f0
testq %rbx, %rbx
sete %bpl
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq (%rsp), %r13
movq %r13, %rdx
callq 0x3cc88
movq %r15, %rdi
callq 0x3d7e0
leaq 0xb8(%rsp), %r12
leaq 0x28(%rsp), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x3de12
leaq 0x148(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3b5a6
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4a10f
leaq 0xcb620(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorpd 0x87a9c(%rip), %xmm1    # 0xa2020
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %dl
setbe %cl
orb %bpl, %dl
jne 0x1a5e2
movl $0x3, (%rbx)
leaq 0x20(%rbx), %rdi
leaq 0x10(%rsp), %rsi
movsd %xmm0, (%rsp)
movapd %xmm1, 0x40(%rsp)
callq 0x3cd94
movapd 0x40(%rsp), %xmm1
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rbx)
movq %r14, 0x18(%rbx)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x38(%rbx)
movsd (%rsp), %xmm0
movl $0x3, %ecx
jmp 0x1a5e7
xorl %eax, %eax
jmp 0x1a5f0
movb %cl, %al
leal (%rax,%rax,2), %ecx
xorl %eax, %eax
ucomisd %xmm0, %xmm1
cmovbel %ecx, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq