// File: 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:
subq $0xd8, %rsp
movq %rdi, 0x8(%rsp)
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movq 0xc8(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x36960
movq 0x18(%rsp), %rdi
movq %rax, 0xa8(%rsp)
movq 0xc0(%rsp), %rsi
movl $0x1, %edx
callq 0x36960
movq 0x18(%rsp), %rdi
movq %rax, 0xa0(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x13660
movq 0x18(%rsp), %rdi
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rsi
callq 0x13660
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
leaq 0x78(%rsp), %rdi
callq 0x6eeb0
leaq 0x78(%rsp), %rdi
callq 0x6fe90
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x24b45
jp 0x24b45
movq 0x8(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x6eb90
jmp 0x24d01
movsd 0x70(%rsp), %xmm0
leaq 0x78(%rsp), %rdi
callq 0x70ec0
movq 0xb8(%rsp), %rsi
movq 0x98(%rsp), %rdx
leaq 0x58(%rsp), %rdi
callq 0x6eeb0
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x702c0
movq 0x8(%rsp), %rdi
movsd %xmm0, 0x50(%rsp)
callq 0x6eb40
xorps %xmm0, %xmm0
ucomisd 0x50(%rsp), %xmm0
jb 0x24bad
movq 0x8(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x6f1e0
jmp 0x24c14
movsd 0x50(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jb 0x24bcf
movq 0x8(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x6f1e0
jmp 0x24c12
movq 0x98(%rsp), %rax
movq %rax, (%rsp)
movsd 0x50(%rsp), %xmm0
leaq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x13800
movq (%rsp), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x6f110
movq 0x8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x6f1e0
jmp 0x24c14
cmpq $0x0, 0xb0(%rsp)
je 0x24cff
movq 0x8(%rsp), %rsi
movq 0xc0(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0xb0(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
callq 0x6f1e0
movq 0x8(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x86c40
movq 0xb0(%rsp), %rax
movsd %xmm0, 0x38(%rax)
xorps %xmm0, %xmm0
ucomisd 0x50(%rsp), %xmm0
jb 0x24cab
movq 0xb0(%rsp), %rax
movl $0x1, (%rax)
movq 0xa8(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x24cfd
movsd 0x50(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jb 0x24cdd
movq 0xb0(%rsp), %rax
movl $0x1, (%rax)
movq 0xa0(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x24cfb
movq 0xb0(%rsp), %rax
movl $0x2, (%rax)
movq 0xb0(%rsp), %rax
movq $0x0, 0x8(%rax)
jmp 0x24cfd
jmp 0x24cff
jmp 0x24d01
movq 0x10(%rsp), %rax
addq $0xd8, %rsp
retq
nop