// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ClosestPointOnEdge(gaps::R3MeshEdge const*, gaps::R3Point const&, gaps::R3MeshIntersection*) const
R3Point R3Mesh::
ClosestPointOnEdge(const R3MeshEdge *e, const R3Point& point, R3MeshIntersection *closest_point) const
{
  // Compute parametric value of closest point on edge
  R3MeshVertex *v0 = VertexOnEdge(e, 0);
  R3MeshVertex *v1 = VertexOnEdge(e, 1);
  const R3Point& p0 = VertexPosition(v0);
  const R3Point& p1 = VertexPosition(v1);
  R3Vector edge_vector = p1 - p0;
  RNScalar edge_length = edge_vector.Length();
  if (edge_length == 0) return p0;
  edge_vector /= edge_length;
  R3Vector point_vector = point - p0;
  RNScalar t = edge_vector.Dot(point_vector);

  // Compute closest point position
  R3Point p;
  if (t <= 0) p = p0;
  else if (t >= edge_length) p = p1;
  else p = p0 + t * edge_vector;
  
  // Fill in closest point info
  if (closest_point) {
    closest_point->edge = (R3MeshEdge *) e;
    closest_point->face = NULL;
    closest_point->point = p;
    closest_point->t = R3Distance(p, point);
    if (t <= 0) {
      closest_point->type = R3_MESH_VERTEX_TYPE;
      closest_point->vertex = v0;
    }
    else if (t >= edge_length) {
      closest_point->type = R3_MESH_VERTEX_TYPE;
      closest_point->vertex = v1;
    }
    else {
      closest_point->type = R3_MESH_EDGE_TYPE;
      closest_point->vertex = NULL;
    }
  }

  // Return closest point
  return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, 0x40(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %rbp
movq %rdi, 0x30(%rsp)
movq 0x8(%rdx), %r14
movq 0x10(%rdx), %rbx
leaq 0x18(%r14), %r12
leaq 0x18(%rbx), %r13
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3c78e
movq %r15, %rdi
callq 0x3d0cc
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a611
jp 0x1a611
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3c542
jmp 0x1a754
movq %r13, 0x38(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x40(%rsp), %r14
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movsd %xmm0, 0x20(%rsp)
callq 0x3db98
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
callq 0x3c78e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d252
movsd %xmm0, 0x10(%rsp)
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x3c530
movsd 0x10(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movq %r12, %r13
jb 0x1a698
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3c89a
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %r12
jmp 0x1a6e3
ucomisd 0x20(%rsp), %xmm0
movq 0x18(%rsp), %r12
jae 0x1a6d1
leaq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3d962
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
leaq 0x60(%rsp), %rdx
callq 0x3c877
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x1a6d9
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x3c89a
movq 0x8(%rsp), %r15
testq %r14, %r14
movq 0x28(%rsp), %r13
movsd 0x20(%rsp), %xmm0
je 0x1a754
movq %rbp, %rcx
xorl %eax, %eax
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movl $0x0, %ebp
adcl $0x1, %ebp
ucomisd %xmm0, %xmm1
movq %rcx, 0x10(%r14)
movq %rax, 0x18(%r14)
cmovbq %rax, %r15
leaq 0x20(%r14), %rdi
movq %rbx, %rsi
callq 0x3c89a
movq %rbx, %rdi
movq %r13, %rsi
callq 0x49404
xorpd %xmm1, %xmm1
ucomisd 0x10(%rsp), %xmm1
movsd %xmm0, 0x38(%r14)
movl $0x1, %eax
cmovbl %ebp, %eax
cmovaeq %r12, %r15
movl %eax, (%r14)
movq %r15, 0x8(%r14)
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop