// File: /workspace/github_cpp_makefile_star2_O1/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 0x1a7ff
movq (%rsi), %rax
movq %rsi, %rdi
movq %r12, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r12), %rdi
movq %rdi, 0x18(%rsp)
movq %rdi, %rsi
callq 0x3d756
leaq 0xcb380(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x877fc(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jb 0x1a847
ucomisd %xmm0, %xmm1
jb 0x1a847
movq %rbx, %rdi
movq %r12, %rdx
callq 0x11c54
movq %rbx, %rax
jmp 0x1aa94
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4a10f
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3de8e
leaq 0x48(%rsp), %rdi
movq %r15, 0x8(%rsp)
movq %r15, %rsi
movq %r14, %rdx
callq 0x3d44d
leaq 0x4cc1d3(%rip), %rsi    # 0x4e6a60
leaq 0x30(%rsp), %rdi
callq 0x3ca36
movsd 0x877a1(%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 0x3ca36
addq $0x18, %r14
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x3ca36
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3cc88
movq %r14, %rdi
callq 0x3d7e0
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x3de12
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3b5a6
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
callq 0x4a10f
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1a9ab
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r13, %rdx
movq 0x8(%rsp), %r14
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x1a602
movq %r15, %rdi
movq %r14, %rsi
callq 0x49ed3
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1a9ab
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movsd %xmm1, 0x10(%rsp)
callq 0x3cd94
movq %r13, (%rsp)
movq %rbx, %r12
incq %rbp
cmpq $0x7, %rbp
jne 0x1a8b2
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 0x3ca36
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x1aa91
movq %r12, 0x18(%r15)
leaq 0x20(%r15), %rdi
movq %r14, %rsi
callq 0x3cd94
movq %r14, %rdi
movq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x49efc
movsd %xmm0, 0x38(%r15)
testq %r13, %r13
je 0x1aa62
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 0x3beaa
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3c150
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x1aa75
ucomisd 0xc0(%rsp), %xmm0
jae 0x1aa82
movl $0x2, (%r15)
xorl %eax, %eax
jmp 0x1aa8d
movl $0x3, (%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15)
jmp 0x1aa91
movl $0x1, (%r15)
movq 0x8(%r13), %rax
jmp 0x1aa8d
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