// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rsi
leaq 0xe8(%rsp), %rbp
movq %rbp, %rdi
movq %rcx, %rdx
callq 0x51d8
leaq 0xf8(%rsp), %r13
movss 0x8(%r13), %xmm5
movss -0x8(%r13), %xmm2
movaps %xmm5, %xmm0
subss %xmm2, %xmm0
movsd (%r13), %xmm1
movsd -0x10(%r13), %xmm3
leaq 0x108(%rsp), %r12
movsd (%r12), %xmm4
movaps %xmm4, %xmm6
subps %xmm1, %xmm6
subps %xmm3, %xmm1
movlps %xmm1, 0x60(%rsp)
movss %xmm0, 0x68(%rsp)
movlps %xmm6, 0x50(%rsp)
movss 0x8(%r12), %xmm7
subps %xmm4, %xmm3
movlps %xmm3, 0x40(%rsp)
movaps %xmm1, %xmm4
shufps $0x55, %xmm1, %xmm4     # xmm4 = xmm4[1,1],xmm1[1,1]
movaps %xmm3, %xmm9
shufps $0x55, %xmm3, %xmm9     # xmm9 = xmm9[1,1],xmm3[1,1]
movaps %xmm1, %xmm8
shufps $0x0, %xmm1, %xmm8      # xmm8 = xmm8[0,0],xmm1[0,0]
movss %xmm0, %xmm8            # xmm8 = xmm0[0],xmm8[1,2,3]
mulps %xmm3, %xmm8
unpcklps %xmm7, %xmm2            # xmm2 = xmm2[0],xmm7[0],xmm2[1],xmm7[1]
unpcklps %xmm5, %xmm7            # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
subps %xmm7, %xmm2
movaps %xmm2, %xmm5
shufps $0x55, %xmm2, %xmm5     # xmm5 = xmm5[1,1],xmm2[1,1]
movss %xmm5, 0x58(%rsp)
movss %xmm2, 0x48(%rsp)
movaps %xmm4, %xmm5
mulss %xmm2, %xmm5
mulss %xmm0, %xmm9
subss %xmm5, %xmm9
movss %xmm9, 0x70(%rsp)
movaps %xmm3, %xmm5
movlhps %xmm2, %xmm5            # xmm5 = xmm5[0],xmm2[0]
shufps $0xe2, %xmm2, %xmm5     # xmm5 = xmm5[2,0],xmm2[2,3]
mulps %xmm1, %xmm5
subps %xmm8, %xmm5
movlps %xmm5, 0x74(%rsp)
movaps %xmm5, %xmm10
shufps $0x55, %xmm5, %xmm10    # xmm10 = xmm10[1,1],xmm5[1,1]
movaps %xmm6, %xmm7
shufps $0x11, %xmm3, %xmm7     # xmm7 = xmm7[1,0],xmm3[1,0]
shufps $0xe2, %xmm3, %xmm7     # xmm7 = xmm7[2,0],xmm3[2,3]
movaps %xmm5, %xmm8
shufps $0x0, %xmm5, %xmm8      # xmm8 = xmm8[0,0],xmm5[0,0]
unpcklps %xmm6, %xmm3            # xmm3 = xmm3[0],xmm6[0],xmm3[1],xmm6[1]
movaps %xmm10, %xmm6
mulps %xmm3, %xmm6
movaps %xmm6, %xmm11
mulps %xmm8, %xmm3
mulps %xmm2, %xmm8
movaps %xmm9, %xmm6
shufps $0x0, %xmm9, %xmm6      # xmm6 = xmm6[0,0],xmm9[0,0]
mulps %xmm6, %xmm2
mulps %xmm7, %xmm6
mulps %xmm10, %xmm7
subps %xmm7, %xmm8
movaps %xmm8, 0xc0(%rsp)
subps %xmm2, %xmm11
movaps %xmm11, 0xa0(%rsp)
subps %xmm3, %xmm6
movaps %xmm6, 0x20(%rsp)
movaps %xmm5, %xmm2
mulss %xmm0, %xmm2
mulss %xmm9, %xmm0
mulss %xmm4, %xmm9
mulss %xmm10, %xmm4
subss %xmm4, %xmm2
movaps %xmm2, 0xb0(%rsp)
mulss %xmm1, %xmm10
subss %xmm0, %xmm10
movaps %xmm10, 0xd0(%rsp)
mulss %xmm1, %xmm5
subss %xmm5, %xmm9
movaps %xmm9, 0x10(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x4d62
movaps %xmm0, 0x80(%rsp)
movaps %xmm1, 0x90(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4d62
movaps 0x80(%rsp), %xmm3
movaps %xmm3, %xmm2
shufps $0x11, %xmm0, %xmm2     # xmm2 = xmm2[1,0],xmm0[1,0]
shufps $0xe2, %xmm0, %xmm2     # xmm2 = xmm2[2,0],xmm0[2,3]
mulps 0xa0(%rsp), %xmm2
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
mulps 0xc0(%rsp), %xmm0
addps %xmm2, %xmm0
unpcklps 0x90(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
mulps 0x20(%rsp), %xmm1
addps %xmm0, %xmm1
movaps %xmm1, 0x20(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4d62
movaps %xmm1, %xmm3
movaps 0xb0(%rsp), %xmm1
mulss %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss 0xd0(%rsp), %xmm0
addss %xmm0, %xmm1
mulss 0x10(%rsp), %xmm3
addss %xmm1, %xmm3
movaps 0x20(%rsp), %xmm1
movaps %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm0     # xmm0 = xmm0[1,1],xmm1[1,1]
minss %xmm0, %xmm1
movaps %xmm3, %xmm2
minss %xmm1, %xmm2
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm2
jae 0x5151
movl $0x7f800000, 0xc(%rsp)  # imm = 0x7F800000
ucomiss %xmm0, %xmm1
movss %xmm3, 0x10(%rsp)
jb 0x50fe
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x50(%rsp), %r8
movq %r15, %rdx
movq %r13, %rcx
callq 0x5365
movss 0x10(%rsp), %xmm3
xorps %xmm0, %xmm0
ucomiss 0x20(%rsp), %xmm0
jb 0x512b
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x40(%rsp), %r8
movq %r15, %rdx
movq %r12, %rcx
callq 0x5365
xorps %xmm0, %xmm0
movss 0x10(%rsp), %xmm3
ucomiss %xmm3, %xmm0
jb 0x5170
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xe8(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %r15, %rdx
callq 0x5365
jmp 0x5170
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x70(%rsp), %rcx
leaq 0xe8(%rsp), %r8
movq %r15, %rdx
callq 0x525f
movss 0xc(%rsp), %xmm0
ucomiss 0x10(%r14), %xmm0
jae 0x5190
movss %xmm0, 0x10(%r14)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%r14)
movl %ebx, 0x14(%r14)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq