// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Distance(gaps::R3Mesh*, gaps::R3MeshFace*, gaps::R3Point const&, gaps::R3MeshIntersection*)
RNLength
R3Distance(R3Mesh *mesh, R3MeshFace *face, const R3Point& point, R3MeshIntersection *closest_point) 
{
  // Compute projection of point onto face plane
  const R3Plane& plane = mesh->FacePlane(face);
  const R3Vector& face_normal = plane.Normal();
  RNScalar plane_signed_distance = R3SignedDistance(plane, point);
  R3Point plane_point = point - plane_signed_distance * face_normal;
  RNLength closest_distance = fabs(plane_signed_distance);

  // Check if point is outside any of the edges
  R3MeshEdge *closest_edge = NULL;
  for (int i = 0; i < 3; i++) {
    R3MeshEdge *edge = mesh->EdgeOnFace(face, i);
    R3MeshVertex *v0 = mesh->VertexOnEdge(edge, face, RN_CW);
    R3MeshVertex *v1 = mesh->VertexOnEdge(edge, face, RN_CCW);
    R3Point p0 = mesh->VertexPosition(v0);
    R3Point p1 = mesh->VertexPosition(v1);
    R3Vector edge_vector = p1 - p0;
    edge_vector.Normalize();
    R3Vector edge_normal = face_normal % edge_vector;
    R3Plane edge_plane(p0, edge_normal);
    RNScalar b = R3SignedDistance(edge_plane, plane_point);
    if (b < 0) {
      R3MeshIntersection intersection;
      RNLength distance = R3Distance(mesh, edge, point, &intersection);
      if (!closest_edge || (distance < closest_distance)) {
        if (closest_point) *closest_point = intersection;
        closest_distance = distance;
        closest_edge = edge;
      }
    }
  }

  // Fill in closest point info
  if (closest_point) {
    closest_point->face = face;
    if (!closest_edge) {
      closest_point->type = R3_MESH_FACE_TYPE;
      closest_point->vertex = NULL;
      closest_point->edge = NULL;
      closest_point->point = plane_point;
      closest_point->t = closest_distance;
    }
  }


  // Return distance
  return closest_distance;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x170(%rsp)
movq %rsi, 0x168(%rsp)
movq %rdx, 0x160(%rsp)
movq %rcx, 0x158(%rsp)
movq 0x170(%rsp), %rdi
movq 0x168(%rsp), %rsi
callq 0x13df0
movq %rax, 0x150(%rsp)
movq 0x150(%rsp), %rdi
callq 0x13e60
movq %rax, 0x148(%rsp)
movq 0x150(%rsp), %rdi
movq 0x160(%rsp), %rsi
callq 0x86f90
movsd %xmm0, 0x140(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movsd 0x140(%rsp), %xmm0
movq 0x148(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x13800
movq 0x8(%rsp), %rsi
leaq 0x128(%rsp), %rdi
leaq 0x110(%rsp), %rdx
callq 0x6fab0
movsd 0x140(%rsp), %xmm0
movaps 0xfd0d4(%rip), %xmm1    # 0x133270
pand %xmm1, %xmm0
movsd %xmm0, 0x108(%rsp)
movq $0x0, 0x100(%rsp)
movl $0x0, 0xfc(%rsp)
cmpl $0x3, 0xfc(%rsp)
jge 0x363bd
movq 0x170(%rsp), %rdi
movq 0x168(%rsp), %rsi
movl 0xfc(%rsp), %edx
callq 0x13d30
movq %rax, 0xf0(%rsp)
movq 0x170(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0x168(%rsp), %rdx
movl $0x1, %ecx
callq 0x38ae0
movq %rax, 0xe8(%rsp)
movq 0x170(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0x168(%rsp), %rdx
xorl %ecx, %ecx
callq 0x38ae0
movq %rax, 0xe0(%rsp)
movq 0x170(%rsp), %rdi
movq 0xe8(%rsp), %rsi
callq 0x13660
movq %rax, %rsi
leaq 0xc8(%rsp), %rdi
callq 0x6eb90
movq 0x170(%rsp), %rdi
movq 0xe0(%rsp), %rsi
callq 0x13660
movq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x6eb90
leaq 0x98(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x6eeb0
leaq 0x98(%rsp), %rdi
callq 0x70410
movq 0x148(%rsp), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x70af0
leaq 0x60(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x6c260
leaq 0x60(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x86f90
movsd %xmm0, 0x58(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x58(%rsp), %xmm0
jbe 0x363a5
leaq 0x18(%rsp), %rdi
callq 0x38ed0
movq 0x170(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0x160(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x35ea0
movsd %xmm0, 0x10(%rsp)
cmpq $0x0, 0x100(%rsp)
je 0x36367
movsd 0x10(%rsp), %xmm1
movsd 0x108(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x363a3
cmpq $0x0, 0x158(%rsp)
je 0x36384
movq 0x158(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x38ef0
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x108(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x100(%rsp)
jmp 0x363a5
jmp 0x363a7
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x361c0
cmpq $0x0, 0x158(%rsp)
je 0x36446
movq 0x168(%rsp), %rcx
movq 0x158(%rsp), %rax
movq %rcx, 0x18(%rax)
cmpq $0x0, 0x100(%rsp)
jne 0x36444
movq 0x158(%rsp), %rax
movl $0x3, (%rax)
movq 0x158(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x158(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x158(%rsp), %rdi
addq $0x20, %rdi
leaq 0x128(%rsp), %rsi
callq 0x6f1e0
movsd 0x108(%rsp), %xmm0
movq 0x158(%rsp), %rax
movsd %xmm0, 0x38(%rax)
jmp 0x36446
movsd 0x108(%rsp), %xmm0
addq $0x178, %rsp            # imm = 0x178
retq
nopw (%rax,%rax)