// File: /workspace/github_cpp_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
testq %rcx, %rcx
je 0x1a295
movl $0x0, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq $0x0, 0x18(%rbx)
leaq 0xcb91c(%rip), %rax     # 0xe5ba8
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rbx)
testb $0x4, 0xa8(%r14)
jne 0x1a2ae
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r14), %r13
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3cfd6
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x3d252
leaq 0xcb8c4(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x87d40(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jb 0x1a2f6
xorl %ebp, %ebp
ucomisd %xmm0, %xmm1
jae 0x1a58d
leaq 0x18(%rsp), %rdi
callq 0x3c530
testb $0x4, 0xa8(%r14)
jne 0x1a319
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2c8(%rax)
leaq 0xb8(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3b026
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x43016
testl %eax, %eax
jne 0x1a39a
leaq 0x78(%rsp), %r13
leaq 0xb8(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3dc00
movsd 0x18(%rbp), %xmm0
xorpd 0x87cb6(%rip), %xmm0    # 0xa2020
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3b08e
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x43016
testl %eax, %eax
je 0x1a57d
testb $0x8, 0xa8(%r14)
jne 0x1a3b3
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2d0(%rax)
leaq 0x58(%r14), %rdi
leaq 0x18(%rsp), %rsi
callq 0x47508
xorl %ebp, %ebp
testl %eax, %eax
je 0x1a58d
movq 0x8(%r14), %r12
movq 0x10(%r14), %r13
addq $0x18, %r12
addq $0x18, %r13
movq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
leaq 0x120(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3c78e
movq %r15, %rdi
callq 0x3d2dc
leaq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %r15, %rdx
callq 0x3d8fa
leaq 0x98(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
leaq 0x60(%rsp), %rdx
callq 0x3b0b2
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x49617
leaq 0xcb758(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorpd 0x87bd4(%rip), %xmm1    # 0xa2020
ucomisd %xmm0, %xmm1
ja 0x1a58d
movq 0x8(%rsp), %rsi
addq $0x18, %rsi
leaq 0x108(%rsp), %r15
movq %r15, %rdi
movq %rsi, 0x8(%rsp)
movq %r13, %rdx
callq 0x3c78e
movq %r15, %rdi
callq 0x3d2dc
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %r15, %rdx
callq 0x3d8fa
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
leaq 0x48(%rsp), %rdx
callq 0x3b0b2
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x49617
leaq 0xcb6de(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorpd 0x87b5a(%rip), %xmm1    # 0xa2020
ucomisd %xmm0, %xmm1
ja 0x1a58d
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rbp
movq %rbp, %rdx
movq %rax, %r13
callq 0x3c78e
movq %r15, %rdi
callq 0x3d2dc
leaq 0xd8(%rsp), %r12
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x3d8fa
leaq 0x138(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3b0b2
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x49617
movsd (%r13), %xmm1
xorpd 0x87ae1(%rip), %xmm1    # 0xa2020
testq %rbx, %rbx
je 0x1a581
ucomisd %xmm0, %xmm1
ja 0x1a581
movl $0x3, (%rbx)
leaq 0x20(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3c89a
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rbx)
movq %r14, 0x18(%rbx)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x38(%rbx)
movl $0x3, %ebp
jmp 0x1a58d
xorl %ebp, %ebp
jmp 0x1a58d
xorl %eax, %eax
ucomisd %xmm0, %xmm1
setbe %al
leal (%rax,%rax,2), %ebp
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop