// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ClosestPointOnFace(gaps::R3MeshFace const*, gaps::R3Point const&, gaps::R3MeshIntersection*) const
R3Point R3Mesh::
ClosestPointOnFace(const R3MeshFace *face, const R3Point& point, R3MeshIntersection *closest_point) const
{
  // Get face plane and normal
  const R3Plane& plane = FacePlane(face);
  const R3Vector& face_normal = plane.Normal();
  if (RNIsZero(face_normal.Dot(face_normal))) return FaceCentroid(face);

  // Project point point onto face plane
  RNScalar plane_signed_distance = R3SignedDistance(plane, point);
  R3Point plane_point = point - plane_signed_distance * face_normal;

  // Check if point is outside each edge
  R3MeshEdge *closest_edge = NULL;
  R3Point closest_edge_point = R3zero_point;
  RNScalar closest_edge_distance_squared = FLT_MAX;
  for (int i = 0; i < 3; i++) {
    R3MeshEdge *edge = EdgeOnFace(face, i);
    R3MeshVertex *v0 = VertexOnEdge(edge, face, RN_CW);
    R3MeshVertex *v1 = VertexOnEdge(edge, face, RN_CCW);
    R3Point p0 = v0->position;
    R3Point p1 = v1->position;
    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) {
      R3Point edge_point = ClosestPointOnEdge(edge, point);
      RNScalar distance_squared = R3SquaredDistance(edge_point, point);
      if (distance_squared < closest_edge_distance_squared) {
        closest_edge_distance_squared = distance_squared;
        closest_edge_point = edge_point;
        closest_edge = edge;
      }
    }
  }

  // Compute closest position
  R3Point p = (closest_edge) ? closest_edge_point : plane_point;

  // Fill in closest point info
  if (closest_point) {
    closest_point->face = (R3MeshFace *) face;
    closest_point->point = p;
    closest_point->t = R3Distance(p, point);
    if (closest_edge) {
      closest_point->edge = closest_edge;
      R3Span edge_span = EdgeSpan(closest_edge);
      RNScalar t = edge_span.T(point);
      if (t <= 0) {
        closest_point->type = R3_MESH_VERTEX_TYPE;
        closest_point->vertex = VertexOnEdge(closest_edge, 0);
      }
      else if (t >= edge_span.Length()) {
        closest_point->type = R3_MESH_VERTEX_TYPE;
        closest_point->vertex = VertexOnEdge(closest_edge, 1);
      }
      else {
        closest_point->type = R3_MESH_EDGE_TYPE;
        closest_point->vertex = NULL;
      }
    }
    else {
      closest_point->type = R3_MESH_FACE_TYPE;
      closest_point->vertex = NULL;
      closest_point->edge = NULL;
    }
  }

  // Return closest point
  return p;
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x1e0(%rsp)
movq %rsi, 0x1d8(%rsp)
movq %rdx, 0x1d0(%rsp)
movq %rcx, 0x1c8(%rsp)
movq %r8, 0x1c0(%rsp)
movq 0x1d8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movq 0x1d0(%rsp), %rsi
callq 0x13df0
movq %rax, 0x1b8(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x13e60
movq %rax, 0x1b0(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
callq 0x702c0
leaq 0x153741(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x24dc9
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq 0x1d0(%rsp), %rdx
callq 0x14ff0
jmp 0x25223
movq 0x1b8(%rsp), %rdi
movq 0x1c8(%rsp), %rsi
callq 0x86f90
movsd %xmm0, 0x1a8(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x10(%rsp)
movsd 0x1a8(%rsp), %xmm0
movq 0x1b0(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x13800
movq 0x10(%rsp), %rsi
leaq 0x190(%rsp), %rdi
leaq 0x178(%rsp), %rdx
callq 0x6fab0
movq $0x0, 0x170(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x554669(%rip), %rsi    # 0x5794b0
callq 0x6eb90
movsd 0x10e1cc(%rip), %xmm0   # 0x133020
movsd %xmm0, 0x150(%rsp)
movl $0x0, 0x14c(%rsp)
cmpl $0x3, 0x14c(%rsp)
jge 0x25056
movq 0x28(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
movl 0x14c(%rsp), %edx
callq 0x13d30
movq 0x28(%rsp), %rdi
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rsi
movq 0x1d0(%rsp), %rdx
movl $0x1, %ecx
callq 0x38ae0
movq 0x28(%rsp), %rdi
movq %rax, 0x138(%rsp)
movq 0x140(%rsp), %rsi
movq 0x1d0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x38ae0
movq %rax, 0x130(%rsp)
movq 0x138(%rsp), %rsi
addq $0x18, %rsi
leaq 0x118(%rsp), %rdi
callq 0x6eb90
movq 0x130(%rsp), %rsi
addq $0x18, %rsi
leaq 0x100(%rsp), %rdi
callq 0x6eb90
leaq 0xe8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0x6eeb0
leaq 0xe8(%rsp), %rdi
callq 0x70410
movq 0x1b0(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0x70af0
leaq 0xb0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x6c260
leaq 0xb0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x86f90
movsd %xmm0, 0xa8(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xa8(%rsp), %xmm0
jbe 0x2503e
movq 0x28(%rsp), %rsi
movq 0x140(%rsp), %rdx
movq 0x1c8(%rsp), %rcx
leaq 0x90(%rsp), %rdi
xorl %eax, %eax
movl %eax, %r8d
callq 0x24a40
movq 0x1c8(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x86c10
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm1
movsd 0x150(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2503c
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x150(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x6f1e0
movq 0x140(%rsp), %rax
movq %rax, 0x170(%rsp)
jmp 0x2503e
jmp 0x25040
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0x24e68
cmpq $0x0, 0x170(%rsp)
je 0x25070
leaq 0x158(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x2507f
leaq 0x190(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x2507f
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x6eb90
cmpq $0x0, 0x1c0(%rsp)
je 0x25221
movq 0x18(%rsp), %rsi
movq 0x1d0(%rsp), %rcx
movq 0x1c0(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x1c0(%rsp), %rdi
addq $0x20, %rdi
callq 0x6f1e0
movq 0x18(%rsp), %rdi
movq 0x1c8(%rsp), %rsi
callq 0x86c40
movq 0x1c0(%rsp), %rax
movsd %xmm0, 0x38(%rax)
cmpq $0x0, 0x170(%rsp)
je 0x251f1
movq 0x28(%rsp), %rsi
movq 0x170(%rsp), %rcx
movq 0x1c0(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x170(%rsp), %rdx
leaq 0x38(%rsp), %rdi
callq 0x37a00
movq 0x1c8(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x6d920
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x30(%rsp), %xmm0
jb 0x25176
movq 0x28(%rsp), %rdi
movq 0x1c0(%rsp), %rax
movl $0x1, (%rax)
movq 0x170(%rsp), %rsi
xorl %edx, %edx
callq 0x36960
movq %rax, %rcx
movq 0x1c0(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x251ef
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x38(%rsp), %rdi
callq 0x37a50
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x251cf
movq 0x28(%rsp), %rdi
movq 0x1c0(%rsp), %rax
movl $0x1, (%rax)
movq 0x170(%rsp), %rsi
movl $0x1, %edx
callq 0x36960
movq %rax, %rcx
movq 0x1c0(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x251ed
movq 0x1c0(%rsp), %rax
movl $0x2, (%rax)
movq 0x1c0(%rsp), %rax
movq $0x0, 0x8(%rax)
jmp 0x251ef
jmp 0x2521f
movq 0x1c0(%rsp), %rax
movl $0x3, (%rax)
movq 0x1c0(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x1c0(%rsp), %rax
movq $0x0, 0x10(%rax)
jmp 0x25221
jmp 0x25223
movq 0x20(%rsp), %rax
addq $0x1e8, %rsp            # imm = 0x1E8
retq