// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movq %rdx, 0x30(%rsp)
movq %rsi, %r14
testb $0x4, 0xa8(%rsi)
jne 0x2377d
movq (%rdi), %rax
movq %r14, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r14), %r12
movq %r12, %rdi
movq 0x30(%rsp), %r15
movq %r15, %rsi
callq 0x49617
movaps %xmm0, (%rsp)
leaq 0x58(%rsp), %r13
movq %r13, %rdi
movq %r12, 0x38(%rsp)
movq %r12, %rsi
callq 0x3d962
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x3cf53
movapd (%rsp), %xmm0
andpd 0x7eaa9(%rip), %xmm0    # 0xa2270
movapd %xmm0, (%rsp)
movq %rbx, 0x20(%rsp)
leaq 0x20(%rbx), %rax
movq %rax, 0x28(%rsp)
movl $0x4, %ebx
movq $0x0, 0x18(%rsp)
movq (%r14,%rbx,8), %r15
xorl %eax, %eax
xorl %ecx, %ecx
movq %r14, %r13
cmpq %r14, 0x18(%r15)
sete %al
setne %cl
movq 0x8(%r15,%rcx,8), %rsi
movq 0x8(%r15,%rax,8), %r12
addq $0x18, %rsi
leaq 0xe0(%rsp), %rbp
movq %rbp, %rdi
callq 0x3c542
addq $0x18, %r12
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x3c542
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3c78e
movq %r12, %rdi
callq 0x3d2dc
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x3d8fa
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x3b0b2
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
callq 0x49617
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x23945
cmpq $0x0, 0x20(%rsp)
sete %r12b
leaq 0x78(%rsp), %rdi
callq 0x3c530
movq %r15, %rsi
movq 0x30(%rsp), %rdx
leaq 0x58(%rsp), %rcx
callq 0x23637
movapd %xmm0, %xmm1
movq 0x18(%rsp), %rdx
testq %rdx, %rdx
setne %cl
movapd (%rsp), %xmm0
ucomisd %xmm1, %xmm0
setbe %al
andb %cl, %al
cmoveq %r15, %rdx
movq %rdx, 0x18(%rsp)
jne 0x238ec
movapd %xmm1, (%rsp)
orb %r12b, %al
movq %r13, %r14
jne 0x23948
movups 0x58(%rsp), %xmm0
movsd %xmm1, (%rsp)
movupd 0x68(%rsp), %xmm1
movq 0x20(%rsp), %r12
movupd %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
movq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x3c89a
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x38(%r12)
movq %r15, 0x18(%rsp)
movsd (%rsp), %xmm0
movapd %xmm0, (%rsp)
jmp 0x23948
movq %r13, %r14
incq %rbx
cmpq $0x7, %rbx
jne 0x237e8
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x23993
movq %r14, 0x18(%rbx)
cmpq $0x0, 0x18(%rsp)
jne 0x23993
movl $0x3, (%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rbx)
leaq 0x40(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x3c89a
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
nop