// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %rbx
testb $0x4, 0xa8(%rdx)
jne 0x1a79f
movq (%rsi), %rax
movq %rsi, %rdi
movq %r12, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r12), %rdi
movq %rdi, 0x18(%rsp)
movq %rdi, %rsi
callq 0x3d252
leaq 0xcb3e0(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x8785c(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jb 0x1a7e7
ucomisd %xmm0, %xmm1
jb 0x1a7e7
movq %rbx, %rdi
movq %r12, %rdx
callq 0x11c12
movq %rbx, %rax
jmp 0x1aa34
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x49617
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3d962
leaq 0x48(%rsp), %rdi
movq %r15, 0x8(%rsp)
movq %r15, %rsi
movq %r14, %rdx
callq 0x3cf53
leaq 0x4cc233(%rip), %rsi    # 0x4e6a60
leaq 0x30(%rsp), %rdi
callq 0x3c542
movsd 0x87801(%rip), %xmm0    # 0xa2040
movsd %xmm0, 0x10(%rsp)
movl $0x4, %ebp
movq $0x0, (%rsp)
movq (%r12,%rbp,8), %r13
xorl %eax, %eax
xorl %ecx, %ecx
movq %r12, %rbx
cmpq %r12, 0x18(%r13)
sete %al
setne %cl
movq 0x8(%r13,%rcx,8), %rsi
movq 0x8(%r13,%rax,8), %r14
addq $0x18, %rsi
leaq 0x110(%rsp), %r15
movq %r15, %rdi
callq 0x3c542
addq $0x18, %r14
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x3c542
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3c78e
movq %r14, %rdi
callq 0x3d2dc
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x3d8fa
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3b0b2
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
callq 0x49617
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1a94b
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r13, %rdx
movq 0x8(%rsp), %r14
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x1a5a2
movq %r15, %rdi
movq %r14, %rsi
callq 0x493db
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1a94b
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movsd %xmm1, 0x10(%rsp)
callq 0x3c89a
movq %r13, (%rsp)
movq %rbx, %r12
incq %rbp
cmpq $0x7, %rbp
jne 0x1a852
movq (%rsp), %r13
testq %r13, %r13
leaq 0x48(%rsp), %rax
leaq 0x30(%rsp), %rsi
cmoveq %rax, %rsi
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x3c542
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x1aa31
movq %r12, 0x18(%r15)
leaq 0x20(%r15), %rdi
movq %r14, %rsi
callq 0x3c89a
movq %r14, %rdi
movq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x49404
movsd %xmm0, 0x38(%r15)
testq %r13, %r13
je 0x1aa02
movq %r13, 0x10(%r15)
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
addq $0x18, %rsi
addq $0x18, %rdx
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0x3b9b6
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3bc5c
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x1aa15
ucomisd 0xc0(%rsp), %xmm0
jae 0x1aa22
movl $0x2, (%r15)
xorl %eax, %eax
jmp 0x1aa2d
movl $0x3, (%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15)
jmp 0x1aa31
movl $0x1, (%r15)
movq 0x8(%r13), %rax
jmp 0x1aa2d
movl $0x1, (%r15)
movq 0x10(%r13), %rax
movq %rax, 0x8(%r15)
movq %r14, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq