// File: ingowald[P]closestSurfacePointQueries/src/distanceQueries.cpp
// Function: bvhlib::GeneralTriangleMesh<float, int, false>::testPrimAndUpdateIfCloser(bvhlib::QueryResult&, ospcommon::vec_t<float, 3, 1> const&, unsigned long)
void GeneralTriangleMesh<coord_t,index_t,has_strides>
  ::testPrimAndUpdateIfCloser(QueryResult &result,
                              const vec3fa &queryPoint,
                              const size_t primID)
  {
    const Triangle tri = getTriangle(primID);

    const vec3fa e0 = tri.v1-tri.v0;
    const vec3fa e1 = tri.v2-tri.v1;
    const vec3fa e2 = tri.v0-tri.v2;
    const vec3fa N  = cross(e2,e0);

    const vec3fa Na = cross(N,e1);
    const vec3fa Nb = cross(N,e2);
    const vec3fa Nc = cross(N,e0);
    
    const float a = dot(queryPoint-tri.v1,Na);
    const float b = dot(queryPoint-tri.v2,Nb);
    const float c = dot(queryPoint-tri.v0,Nc);
      
    vec3fa closestPoint;
    float closestDist;
    if (std::min(std::min(a,b),c) >= 0.f)
      closestPoint = projectToPlane(closestDist,queryPoint,N,tri.v0);
    else {
      closestDist = std::numeric_limits<float>::infinity();
      if (a <= 0.f) 
        checkEdge(closestPoint,closestDist,queryPoint,tri.v1,e1);
      if (b <= 0.f) 
        checkEdge(closestPoint,closestDist,queryPoint,tri.v2,e2);
      if (c <= 0.f) 
        checkEdge(closestPoint,closestDist,queryPoint,tri.v0,e0);
    }
      
    if (closestDist >= result.distance) return;
      
    result.distance = closestDist;
    result.point    = closestPoint;
    result.primID   = primID;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq -0x50(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0xa2d0
movq -0x1b0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x9610
movss %xmm1, -0x78(%rbp)
movlpd %xmm0, -0x80(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
leaq -0x70(%rbp), %rsi
callq 0x95a0
movq -0x1d0(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x9610
movss %xmm1, -0xa8(%rbp)
movlpd %xmm0, -0xb0(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x90(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
leaq -0xa0(%rbp), %rsi
callq 0x95a0
movq -0x1c0(%rbp), %rsi
movq -0x1b0(%rbp), %rdi
callq 0x9610
movss %xmm1, -0xd8(%rbp)
movlpd %xmm0, -0xe0(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
leaq -0xd0(%rbp), %rsi
callq 0x95a0
movq -0x1e8(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0xa460
movq -0x1f0(%rbp), %rdx
movq -0x1e0(%rbp), %rsi
leaq -0x100(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0xa460
movq -0x1e8(%rbp), %rdx
movq -0x1e0(%rbp), %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0xa460
movq -0x1e0(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0xa460
movq -0x1d0(%rbp), %rsi
movq -0x18(%rbp), %rdi
callq 0x9610
movq -0x1c8(%rbp), %rsi
movss %xmm1, -0x138(%rbp)
movlpd %xmm0, -0x140(%rbp)
movl -0x138(%rbp), %eax
movl %eax, -0x128(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x130(%rbp), %rdi
callq 0xa540
movq -0x1c0(%rbp), %rsi
movss %xmm0, -0x124(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9610
movq -0x1b8(%rbp), %rsi
movss %xmm1, -0x158(%rbp)
movlpd %xmm0, -0x160(%rbp)
movl -0x158(%rbp), %eax
movl %eax, -0x148(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x150(%rbp), %rdi
callq 0xa540
movq -0x1b0(%rbp), %rsi
movss %xmm0, -0x144(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9610
movss %xmm1, -0x178(%rbp)
movlpd %xmm0, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x170(%rbp)
movl -0x178(%rbp), %eax
movl %eax, -0x168(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0xa540
movss %xmm0, -0x164(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x9930
leaq -0x124(%rbp), %rdi
leaq -0x144(%rbp), %rsi
callq 0x9200
movq %rax, %rdi
leaq -0x164(%rbp), %rsi
callq 0x9200
movss (%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x9dd1
movq -0x18(%rbp), %rdx
leaq -0x50(%rbp), %r8
leaq -0x1a4(%rbp), %rdi
leaq -0x194(%rbp), %rsi
leaq -0xf0(%rbp), %rcx
callq 0xa5a0
movq -0x1a4(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x19c(%rbp), %rax
movq %rax, -0x188(%rbp)
jmp 0x9e6f
callq 0x90e0
movss %xmm0, -0x194(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x124(%rbp), %xmm0
jb 0x9e10
movq -0x18(%rbp), %rdx
leaq -0x50(%rbp), %rcx
addq $0x10, %rcx
leaq -0x190(%rbp), %rdi
leaq -0x194(%rbp), %rsi
leaq -0x90(%rbp), %r8
callq 0xa750
xorps %xmm0, %xmm0
ucomiss -0x144(%rbp), %xmm0
jb 0x9e42
movq -0x18(%rbp), %rdx
leaq -0x50(%rbp), %rcx
addq $0x20, %rcx
leaq -0x190(%rbp), %rdi
leaq -0x194(%rbp), %rsi
leaq -0xc0(%rbp), %r8
callq 0xa750
xorps %xmm0, %xmm0
ucomiss -0x164(%rbp), %xmm0
jb 0x9e6d
movq -0x18(%rbp), %rdx
leaq -0x50(%rbp), %rcx
leaq -0x190(%rbp), %rdi
leaq -0x194(%rbp), %rsi
leaq -0x60(%rbp), %r8
callq 0xa750
jmp 0x9e6f
movss -0x194(%rbp), %xmm0
movq -0x10(%rbp), %rax
ucomiss 0x10(%rax), %xmm0
jb 0x9e83
jmp 0x9eba
movss -0x194(%rbp), %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq -0x190(%rbp), %rcx
movq %rcx, (%rax)
movq -0x188(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)