// File: /workspace/github_cpp_makefile_star2_O2/ingowald[P]closestSurfacePointQueries/examples/soaExample.cpp
// Function: main
int main(int ac, char **av)
  {
    srand48(290374);

    int numTriangles = 1000000;
    float maxEdgeLen = 1.f/(powf(numTriangles,1.f/3.f));
    int numPoints    = 1000000;
    
    // the triangles we're querying to
    std::vector<float>   vertex_x;
    std::vector<float>   vertex_y;
    std::vector<float>   vertex_z;
    std::vector<int32_t> index_x;
    std::vector<int32_t> index_y;
    std::vector<int32_t> index_z;
    
    for (int i=0;i<numTriangles;i++) {
      const vec3f P(drand48(),drand48(),drand48());
      const vec3f e0 = maxEdgeLen * vec3f(drand48(),drand48(),drand48());
      const vec3f e1 = maxEdgeLen * vec3f(drand48(),drand48(),drand48());
      const int vIdx = vertex_x.size();
      vertex_x.push_back(P.x);
      vertex_x.push_back(P.x+e0.x);
      vertex_x.push_back(P.x+e1.x);

      vertex_y.push_back(P.y);
      vertex_y.push_back(P.y+e0.y);
      vertex_y.push_back(P.y+e1.y);

      vertex_z.push_back(P.z);
      vertex_z.push_back(P.z+e0.z);
      vertex_z.push_back(P.z+e1.z);

      index_x.push_back(vIdx+0);
      index_y.push_back(vIdx+1);
      index_z.push_back(vIdx+2);
    }
    
    // the points we're query'ing
    std::vector<vec3f> queryPoint;
    for (int i=0;i<numPoints;i++) {
      int triID = int(drand48()*numTriangles);
      float u = drand48();
      float v = drand48();
      const vec3f A(vertex_x[index_x[triID]],
                    vertex_y[index_x[triID]],
                    vertex_z[index_x[triID]]);
      const vec3f B(vertex_x[index_y[triID]],
                    vertex_y[index_y[triID]],
                    vertex_z[index_y[triID]]);
      const vec3f C(vertex_x[index_z[triID]],
                    vertex_y[index_z[triID]],
                    vertex_z[index_z[triID]]);
      if (u+v >= 1.f) { u = 1-u; v = 1-v; }
      const vec3f pointOnTri = A+u*(B-A)+v*(C-A);
      queryPoint.push_back(pointOnTri + float(0.1f*drand48()*maxEdgeLen)*vec3f(drand48(),drand48(),drand48()));
    }

    // the result of our queries
    std::vector<vec3f>   result_pos;    result_pos.resize(queryPoint.size());
    std::vector<int32_t> result_primID; result_primID.resize(queryPoint.size());
    std::vector<float>   result_dist;   result_dist.resize(queryPoint.size());

    auto begin = std::chrono::system_clock::now();
    // create the actual scene:
    distance_query_scene scene
      = rtdqNewTriangleMeshfi(&vertex_x[0],&vertex_y[0],&vertex_z[0],1,
                              &index_x[0],&index_y[0],&index_z[0],1,
                              index_x.size());
    auto done_build = std::chrono::system_clock::now();

    // perform the queries - all together, in a single thread
    rtdqComputeClosestPointsfi(scene,
                             &result_pos[0].x,&result_pos[0].y,&result_pos[0].z,3,
                             &result_dist[0],1,
                             &result_primID[0],1,
                             &queryPoint[0].x,&queryPoint[0].y,&queryPoint[0].z,3,
                             numPoints);
    auto done_all = std::chrono::system_clock::now();

    std::chrono::duration<double> buildTime = done_build - begin;
    std::chrono::duration<double> queryTime = done_all   - done_build;
    std::cout << "time to build tree " << buildTime.count() << "s" << std::endl;
    std::cout << "time to query " << numPoints << " points: " << queryTime.count() << "s" << std::endl;
    std::cout << "(this is " << (queryTime.count()/numPoints) << " seconds/prim)" << std::endl;

    rtdqDestroy(scene);
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movl $0x46e46, %edi          # imm = 0x46E46
callq 0x2190
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rbx
andq $0x0, 0x10(%rbx)
leaq 0x140(%rsp), %r14
andq $0x0, 0x10(%r14)
movaps %xmm0, (%rbx)
movaps %xmm0, (%r14)
leaq 0x120(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0xa0(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x100(%rsp), %rax
andq $0x0, 0x10(%rax)
leaq 0xe0(%rsp), %rcx
andq $0x0, 0x10(%rcx)
movaps %xmm0, (%rax)
movaps %xmm0, (%rcx)
movl $0xf4240, %r13d         # imm = 0xF4240
leaq 0x20(%rsp), %r12
subl $0x1, %r13d
jb 0x25cf
callq 0x20f0
movaps %xmm0, 0x40(%rsp)
callq 0x20f0
movaps %xmm0, 0x50(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movapd 0x40(%rsp), %xmm1
unpcklpd 0x50(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
movlpd %xmm1, (%rsp)
movss %xmm0, 0x8(%rsp)
callq 0x20f0
movsd %xmm0, 0x40(%rsp)
callq 0x20f0
movsd %xmm0, 0x90(%rsp)
callq 0x20f0
movsd %xmm0, 0x70(%rsp)
callq 0x20f0
movsd %xmm0, 0x50(%rsp)
callq 0x20f0
movsd %xmm0, 0x80(%rsp)
callq 0x20f0
movsd %xmm0, 0x60(%rsp)
movq 0xc8(%rsp), %rbp
subq 0xc0(%rsp), %rbp
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x2ab8
cvtsd2ss 0x40(%rsp), %xmm0
movss 0x4b7a(%rip), %xmm1     # 0x7004
mulss %xmm1, %xmm0
addss (%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2d2e
cvtsd2ss 0x50(%rsp), %xmm0
mulss 0x4b52(%rip), %xmm0     # 0x7004
addss (%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2d2e
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0x2ab8
cvtsd2ss 0x90(%rsp), %xmm0
movss 0x4b1e(%rip), %xmm1     # 0x7004
mulss %xmm1, %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2d2e
cvtsd2ss 0x80(%rsp), %xmm0
mulss 0x4af2(%rip), %xmm0     # 0x7004
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2d2e
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x2ab8
cvtsd2ss 0x70(%rsp), %xmm0
movss 0x4ac0(%rip), %xmm1     # 0x7004
mulss %xmm1, %xmm0
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2d2e
cvtsd2ss 0x60(%rsp), %xmm0
mulss 0x4a97(%rip), %xmm0     # 0x7004
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2d2e
shrq $0x2, %rbp
movl %ebp, 0x20(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x2e2c
leal 0x1(%rbp), %eax
movl %eax, 0x20(%rsp)
leaq 0x100(%rsp), %rdi
movq %r12, %rsi
callq 0x2e2c
addl $0x2, %ebp
movl %ebp, 0x20(%rsp)
leaq 0xe0(%rsp), %rdi
movq %r12, %rsi
callq 0x2e2c
jmp 0x23d7
xorps %xmm0, %xmm0
movq %rsp, %rbx
andq $0x0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movl $0xf4240, %ebp          # imm = 0xF4240
leaq 0x20(%rsp), %r14
subl $0x1, %ebp
jb 0x27dc
callq 0x20f0
mulsd 0x4a13(%rip), %xmm0     # 0x7010
cvttsd2si %xmm0, %r15d
callq 0x20f0
movapd %xmm0, 0x40(%rsp)
callq 0x20f0
movslq %r15d, %rsi
movq 0xa0(%rsp), %rax
movslq (%rax,%rsi,4), %rdx
movq 0xc0(%rsp), %rax
movq 0x140(%rsp), %rcx
movq 0x120(%rsp), %r8
movss (%r8,%rdx,4), %xmm1
movq 0x100(%rsp), %rdi
movslq (%rdi,%rsi,4), %rdi
movq 0xe0(%rsp), %r9
movapd 0x40(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
cvtpd2ps %xmm2, %xmm0
movslq (%r9,%rsi,4), %rsi
movapd %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
addss %xmm0, %xmm2
xorl %r9d, %r9d
ucomiss 0x498d(%rip), %xmm2     # 0x7008
setb %r9b
movaps 0x49aa(%rip), %xmm2     # 0x7030
subps %xmm0, %xmm2
movd %r9d, %xmm3
pshufd $0x50, %xmm3, %xmm3     # xmm3 = xmm3[0,0,1,1]
pslld $0x1f, %xmm3
psrad $0x1f, %xmm3
andps %xmm3, %xmm0
pandn %xmm2, %xmm3
por %xmm0, %xmm3
movdqa %xmm3, 0x40(%rsp)
movss (%r8,%rsi,4), %xmm0
movss (%r8,%rdi,4), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movaps %xmm1, %xmm0
shufps $0x0, %xmm1, %xmm0      # xmm0 = xmm0[0,0],xmm1[0,0]
subps %xmm0, %xmm2
mulps %xmm3, %xmm2
addss %xmm2, %xmm1
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
addss %xmm1, %xmm2
movaps %xmm2, 0x50(%rsp)
movss (%rax,%rdi,4), %xmm1
movss (%rcx,%rdi,4), %xmm0
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x90(%rsp)
movss (%rax,%rdx,4), %xmm1
movss (%rcx,%rdx,4), %xmm0
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x70(%rsp)
movss (%rax,%rsi,4), %xmm1
movss (%rcx,%rsi,4), %xmm0
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd %xmm1, 0x80(%rsp)
callq 0x20f0
mulsd 0x48f3(%rip), %xmm0     # 0x7018
mulsd 0x48f3(%rip), %xmm0     # 0x7020
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
callq 0x20f0
movaps %xmm0, 0x60(%rsp)
callq 0x20f0
movaps %xmm0, 0x170(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps 0x160(%rsp), %xmm5
mulss %xmm5, %xmm0
addss 0x50(%rsp), %xmm0
movaps 0x70(%rsp), %xmm3
movaps 0x90(%rsp), %xmm4
subps %xmm3, %xmm4
movdqa 0x40(%rsp), %xmm2
pshufd $0x0, %xmm2, %xmm1      # xmm1 = xmm2[0,0,0,0]
mulps %xmm4, %xmm1
addps %xmm3, %xmm1
movaps 0x80(%rsp), %xmm4
subps %xmm3, %xmm4
pshufd $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulps %xmm4, %xmm2
addps %xmm1, %xmm2
movapd 0x60(%rsp), %xmm1
unpcklpd 0x170(%rsp), %xmm1      # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
movaps %xmm5, %xmm3
unpcklps %xmm5, %xmm3            # xmm3 = xmm3[0],xmm5[0],xmm3[1],xmm5[1]
mulps %xmm1, %xmm3
addps %xmm2, %xmm3
movlps %xmm3, 0x20(%rsp)
movss %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2fde
jmp 0x25e7
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
pushq $0xc
popq %rbx
cqto
idivq %rbx
movq %rax, %rsi
callq 0x2ada
leaq 0x1a0(%rsp), %rdi
andq $0x0, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cqto
idivq %rbx
movq %rax, %rsi
callq 0x2b12
xorpd %xmm0, %xmm0
leaq 0x180(%rsp), %rdi
andq $0x0, 0x10(%rdi)
movapd %xmm0, (%rdi)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cqto
idivq %rbx
movq %rax, %rsi
callq 0x2b44
callq 0x2030
movq %rax, %r15
movq 0xc0(%rsp), %rdi
movq 0x140(%rsp), %rsi
movq 0x120(%rsp), %rdx
movq 0x100(%rsp), %r9
movq 0xa0(%rsp), %r8
movq 0xa8(%rsp), %rax
subq %r8, %rax
sarq $0x2, %rax
subq $0x8, %rsp
pushq $0x1
popq %r12
movq %r12, %rcx
pushq %rax
pushq $0x1
pushq 0xf8(%rsp)
callq 0x3554
addq $0x20, %rsp
movq %rax, %rbx
callq 0x2030
movq %rax, %r14
movq 0x20(%rsp), %rsi
leaq 0x4(%rsi), %rdx
leaq 0x8(%rsi), %rcx
movq 0x180(%rsp), %r9
movq (%rsp), %rax
leaq 0x4(%rax), %r10
leaq 0x8(%rax), %r11
pushq $0x3
popq %r8
movq %rbx, %rdi
pushq $0xf4240                # imm = 0xF4240
pushq $0x3
pushq %r11
pushq %r10
pushq %rax
pushq %r12
pushq 0x1d0(%rsp)
pushq %r12
callq 0x39bc
addq $0x40, %rsp
callq 0x2030
movq %rax, %r12
movq 0x86bc(%rip), %rdi      # 0xafd8
leaq 0x472d(%rip), %rsi      # 0x7050
callq 0x2100
movq %r14, %rcx
subq %r15, %rcx
cvtsi2sd %rcx, %xmm0
divsd 0x46ed(%rip), %xmm0     # 0x7028
movq %rax, %rdi
callq 0x2160
leaq 0x471a(%rip), %rsi      # 0x7064
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x2070
movq 0x8677(%rip), %rdi      # 0xafd8
leaq 0x46fe(%rip), %rsi      # 0x7066
callq 0x2100
movq %rax, %rdi
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x21a0
leaq 0x46f4(%rip), %rsi      # 0x7075
movq %rax, %rdi
callq 0x2100
subq %r14, %r12
cvtsi2sd %r12, %xmm0
divsd 0x468f(%rip), %xmm0     # 0x7028
movq %rax, %rdi
movsd %xmm0, 0x40(%rsp)
callq 0x2160
leaq 0x46b6(%rip), %rsi      # 0x7064
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x2070
movq 0x8613(%rip), %rdi      # 0xafd8
leaq 0x46b3(%rip), %rsi      # 0x707f
callq 0x2100
movsd 0x40(%rsp), %xmm0
divsd 0x4631(%rip), %xmm0     # 0x7010
movq %rax, %rdi
callq 0x2160
leaq 0x469b(%rip), %rsi      # 0x7089
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x3cb6
jmp 0x2a0c
jmp 0x2a0c
jmp 0x2a0c
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x2b80
jmp 0x2a21
movq %rax, %rbx
leaq 0x1a0(%rsp), %rdi
callq 0x2bb4
jmp 0x2a33
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2fa2
jmp 0x2a42
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2fa2
jmp 0x2a61
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2bb4
leaq 0x100(%rsp), %rdi
callq 0x2bb4
leaq 0xa0(%rsp), %rdi
callq 0x2bb4
leaq 0x120(%rsp), %rdi
callq 0x2b80
leaq 0x140(%rsp), %rdi
callq 0x2b80
leaq 0xc0(%rsp), %rdi
callq 0x2b80
movq %rbx, %rdi
callq 0x21b0
nop