// File: /workspace/github_cpp_makefile_star2_O2/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 $0x138, %rsp            # imm = 0x138
movq %r8, %r14
movq %rcx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, %rdi
movq %rdx, (%rsp)
movq %rdx, %rsi
callq 0x10dd0
movq %rax, %rdi
movq %rax, 0x20(%rsp)
movq %rax, %rsi
callq 0x38586
leaq 0xc201d(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
movapd 0x7a499(%rip), %xmm2    # 0x93030
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jb 0x18bbb
ucomisd %xmm0, %xmm1
jb 0x18bbb
movq %rbx, %rdi
movq (%rsp), %rdx
callq 0x11472
movq %rbx, %rax
jmp 0x18e18
movq %r12, 0x28(%rsp)
movq %r14, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x43420
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x38a02
leaq 0x58(%rsp), %rdi
movq %r15, 0x10(%rsp)
movq %r15, %rsi
movq %r14, %rdx
callq 0x3829d
leaq 0x4c2e67(%rip), %rsi    # 0x4dba70
leaq 0x40(%rsp), %rdi
callq 0x37b5c
movsd 0x7a3fd(%rip), %xmm0    # 0x93018
movsd %xmm0, 0x18(%rsp)
pushq $0x4
popq %r14
movq $0x0, 0x8(%rsp)
leaq 0x120(%rsp), %rbp
leaq 0xf0(%rsp), %r15
cmpq $0x7, %r14
je 0x18d3f
movq (%rsp), %rdx
movq (%rdx,%r14,8), %r12
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rdx, 0x18(%r12)
sete %al
setne %cl
movq 0x8(%r12,%rcx,8), %rsi
movq 0x8(%r12,%rax,8), %rbx
addq $0x18, %rsi
movq %rbp, %rdi
callq 0x37b5c
addq $0x18, %rbx
leaq 0x108(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x37b5c
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x37d29
movq %r15, %rdi
callq 0x3860e
leaq 0xd8(%rsp), %r13
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x389be
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x367ac
movq %rbx, %rdi
leaq 0x58(%rsp), %rsi
callq 0x43420
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x18d37
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x18982
movq %r13, %rdi
movq %rbx, %rsi
callq 0x431f1
movapd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x18d37
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movsd %xmm1, 0x18(%rsp)
callq 0x37e36
movq %r12, 0x8(%rsp)
incq %r14
jmp 0x18c3e
movq 0x8(%rsp), %rbp
testq %rbp, %rbp
leaq 0x58(%rsp), %rax
leaq 0x40(%rsp), %rsi
cmoveq %rax, %rsi
movq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x37b5c
movq 0x30(%rsp), %r15
testq %r15, %r15
je 0x18e15
movq (%rsp), %rax
movq %rax, 0x18(%r15)
leaq 0x20(%r15), %rdi
movq %r14, %rsi
callq 0x37e36
movq %r14, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x4321a
movsd %xmm0, 0x38(%r15)
testq %rbp, %rbp
je 0x18de6
movq %rbp, 0x10(%r15)
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
callq 0x214e0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x37276
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x18df9
ucomisd 0xd0(%rsp), %xmm0
jae 0x18e06
movl $0x2, (%r15)
xorl %eax, %eax
jmp 0x18e11
movl $0x3, (%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15)
jmp 0x18e15
movl $0x1, (%r15)
movq 0x8(%rbp), %rax
jmp 0x18e11
movl $0x1, (%r15)
movq 0x10(%rbp), %rax
movq %rax, 0x8(%r15)
movq %r14, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq