// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rcx, 0x18(%rsp)
movq %rdx, %rbx
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
callq 0x10dd0
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x43420
movaps %xmm0, (%rsp)
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r15, 0x38(%rsp)
movq %r15, %rsi
callq 0x38a02
leaq 0x40(%rsp), %rdi
movq %rbx, 0x30(%rsp)
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3829d
movapd (%rsp), %xmm0
andpd 0x72378(%rip), %xmm0    # 0x93270
movapd %xmm0, (%rsp)
pushq $0x4
popq %r13
xorl %r12d, %r12d
cmpq $0x7, %r13
je 0x21037
movq 0x20(%rsp), %rdx
movq (%rdx,%r13,8), %r15
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rdx, 0x18(%r15)
sete %al
setne %cl
movq 0x8(%r15,%rcx,8), %rsi
movq 0x8(%r15,%rax,8), %rbx
addq $0x18, %rsi
leaq 0xe0(%rsp), %rbp
movq %rbp, %rdi
callq 0x37b5c
addq $0x18, %rbx
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x37b5c
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x37d29
movq %rbx, %rdi
callq 0x3860e
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
callq 0x389be
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x367ac
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x43420
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2102f
cmpq $0x0, 0x18(%rsp)
sete %bl
leaq 0x78(%rsp), %rdi
callq 0x37b4a
movq 0x28(%rsp), %rdi
movq %r15, %rsi
movq 0x30(%rsp), %rdx
leaq 0x58(%rsp), %rcx
callq 0x20d93
movapd %xmm0, %xmm1
testq %r12, %r12
setne %cl
movapd (%rsp), %xmm0
ucomisd %xmm1, %xmm0
setbe %al
andb %cl, %al
cmoveq %r15, %r12
jne 0x2100a
movapd %xmm1, (%rsp)
orb %bl, %al
jne 0x2102f
movq 0x18(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movsd %xmm1, (%rsp)
callq 0x21c48
movq %r15, %r12
movsd (%rsp), %xmm0
movapd %xmm0, (%rsp)
incq %r13
jmp 0x20f04
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
je 0x21076
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rbx)
testq %r12, %r12
jne 0x21076
movl $0x3, (%rbx)
leaq 0x20(%rbx), %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rbx)
leaq 0x40(%rsp), %rsi
callq 0x37e36
movapd (%rsp), %xmm0
movsd %xmm0, 0x38(%rbx)
movaps (%rsp), %xmm0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq