// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::FaceBarycentric(gaps::R3MeshFace const*, gaps::R3Point const&) const
R3Point R3Mesh::
FaceBarycentric(const R3MeshFace *f, const R3Point &point) const
{
  // From http://www.devmaster.net/wiki/Ray-triangle_intersection
  R3Point p0 = f->vertex[0]->position;
  R3Point p1 = f->vertex[1]->position;
  R3Point p2 = f->vertex[2]->position;
  R3Vector b = p1 - p0;
  R3Vector c = p2 - p0;
  R3Vector p = point - p0;
  RNDimension dim = f->plane.Normal().MaxDimension();
  RNDimension dim0 = (dim+1)%3;
  RNDimension dim1 = (dim+2)%3;
  RNScalar denom = b[dim1]*c[dim0] - b[dim0]*c[dim1];
  if (denom == 0) return R3zero_point;
  RNScalar b1 = (p[dim1]*c[dim0] - p[dim0]*c[dim1]) / denom;
  RNScalar b2 = (p[dim1]*b[dim0] - p[dim0]*b[dim1]) / -denom;
  RNScalar b0 = 1 - b1 - b2;
  return R3Point(b0, b1, b2);
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x30(%rsp)
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq %rdx, 0x120(%rsp)
movq %rcx, 0x118(%rsp)
movq 0x120(%rsp), %rax
movq 0x8(%rax), %rsi
addq $0x18, %rsi
leaq 0x100(%rsp), %rdi
callq 0x6eb90
movq 0x120(%rsp), %rax
movq 0x10(%rax), %rsi
addq $0x18, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x6eb90
movq 0x120(%rsp), %rax
movq 0x18(%rax), %rsi
addq $0x18, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x6eb90
leaq 0xb8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x6eeb0
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x6eeb0
movq 0x118(%rsp), %rsi
leaq 0x88(%rsp), %rdi
leaq 0x100(%rsp), %rdx
callq 0x6eeb0
movq 0x120(%rsp), %rdi
addq $0x38, %rdi
callq 0x13e60
movq %rax, %rdi
callq 0x6ffd0
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0x80(%rsp)
movl 0x84(%rsp), %eax
addl $0x2, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0x7c(%rsp)
movl 0x7c(%rsp), %esi
leaq 0xb8(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x80(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x80(%rsp), %esi
leaq 0xb8(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x7c(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x36c00
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm2
mulsd (%rax), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x17cdb
jp 0x17cdb
movq 0x30(%rsp), %rdi
leaq 0x5617df(%rip), %rsi    # 0x5794b0
callq 0x6eb90
jmp 0x17e7c
movl 0x7c(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x80(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x80(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movl 0x7c(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x36c00
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm2
mulsd (%rax), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
divsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x7c(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x80(%rsp), %esi
leaq 0xb8(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x80(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x7c(%rsp), %esi
leaq 0xb8(%rsp), %rdi
callq 0x36c00
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm2
movq 0x30(%rsp), %rdi
mulsd (%rax), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x70(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x11b1b5(%rip), %xmm0   # 0x133008
subsd 0x68(%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
callq 0x6eb50
movq 0x38(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
nopl (%rax)