// File: /workspace/github_c_makefile_star2_O0/archer1357[P]Offline-Raytracer/utils.c
// Function: intersectSceneObject
bool intersectSceneObject(const struct Ray *ray,
                          struct KdTraverseNode *traverseStack,
                          const float *minBound,const float *maxBound,
                          struct KdNode *rootNode,
                          struct Triangle *triangles,int trianglesNum,
                          struct Sphere *spheres,int spheresNum,
                          float *colPtOut, float *norOut,
                          struct Material **materialOut) {

  float colPt[3],nor[3],t;//,t2;
  struct Material *material=0;
  float colPt2[3];
  float bc[3];
  int primInd;

  // t2=-1.0;

#ifdef RUN_TEST
  if(!intersectKdtree_test(ray->origin,ray->vec,minBound,maxBound,
                           traverseStack,rootNode,&t,colPt2,&primInd,bc,
                           triangles,trianglesNum,spheres,spheresNum)) {
    return false;
  }
#else
  if(!intersectKdtree(ray->origin,ray->vec,minBound,maxBound,
    traverseStack,rootNode,&t,colPt2,&primInd,bc,
    triangles,trianglesNum,spheres,spheresNum)) {
  return false;
}
#endif

  // if(!kdtreeblaa(ray->origin,ray->vec,minBound,maxBound,
               // traverseStack,rootNode,&t,colPt2,&primInd,bc,
                 // triangles)) {
    // return false;
  // }

  // if(t < t2 || t2 <0) {
  //   t2=t;

    colPt[0]=colPt2[0];
    colPt[1]=colPt2[1];
    colPt[2]=colPt2[2];

    if(primInd<trianglesNum) {//triangle

      //nor
      nor[0]=triangles[primInd].plane[0];
      nor[1]=triangles[primInd].plane[1];
      nor[2]=triangles[primInd].plane[2];

      // float aa[3],bb[3];
      // vec3_sub(aa,sceneTriangles[q].nor1,sceneTriangles[q].nor0);
      // vec3_sub(bb,sceneTriangles[q].nor2,sceneTriangles[q].nor0);
      // vec3_mulScalar(aa,aa,bc[0]);
      // vec3_mulScalar(bb,bb,bc[1]);
      // vec3_add(nor,sceneTriangles[q].nor0,aa);
      // vec3_add(nor,nor,bb);
      // vec3_normal(nor,nor);

      // nor[0]=sceneTriangles[q].nor0[0];
      // nor[1]=sceneTriangles[q].nor0[1];
      // nor[2]=sceneTriangles[q].nor0[2];
      // printf("%g %g %g\n",nor[0],nor[1],nor[2]);
      //
      // N1 + uu * (N2 - N1) + vv * (N3 - N1);
      // printf("bc %g %g %g\n",bc[0],bc[1],bc[2]);



      ////////////////
      float nn0[3],nn1[3],nn2[3];
      vec3_mulScalar(nn0,triangles[primInd].nor0,bc[2]);
      vec3_mulScalar(nn1,triangles[primInd].nor1,bc[0]);
      vec3_mulScalar(nn2,triangles[primInd].nor2,bc[1]);
      vec3_add(nor,nn0,nn1);
      vec3_add(nor,nor,nn2);
      /////////////



      // vec3_normal(nor,nor);

      // nor[0]=sceneTriangles[q].nor0[0];
      // nor[1]=sceneTriangles[q].nor0[1];
      // nor[2]=sceneTriangles[q].nor0[2];
      //

      //
      material=&triangles[primInd].material;

    } else {//sphere
      struct Sphere *sphere;
      sphere=&spheres[primInd-trianglesNum];

      vec3_sub(nor,colPt,sphere->pos);
      vec3_normal(nor,nor);

      material=&sphere->material;

    }

    //insert primitives here

    if(!material) {
      return false;
    }
  // }

  // if(t2==-1.0) {
  //   return false;
  // }

  colPtOut[0]=colPt[0];
  colPtOut[1]=colPt[1];
  colPtOut[2]=colPt[2];

  norOut[0]=nor[0];
  norOut[1]=nor[1];
  norOut[2]=nor[2];

  *materialOut=material;

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0xc, %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x58(%rbp), %r9
leaq -0x94(%rbp), %r12
leaq -0xa0(%rbp), %r14
movq -0x60(%rbp), %rbx
movl 0x10(%rbp), %r11d
movq 0x18(%rbp), %r10
movl 0x20(%rbp), %eax
leaq -0x7c(%rbp), %r13
leaq -0xa4(%rbp), %r15
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movl %r11d, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movl %eax, 0x38(%rsp)
callq 0x421e80
testb $0x1, %al
jne 0x422de9
movb $0x0, -0x29(%rbp)
jmp 0x422ffc
movss -0x94(%rbp), %xmm0
movss %xmm0, -0x6c(%rbp)
movss -0x90(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x8c(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x422f37
movq -0x60(%rbp), %rax
movslq -0xa4(%rbp), %rcx
imulq $0x74, %rcx, %rcx
addq %rcx, %rax
movss 0x64(%rax), %xmm0
movss %xmm0, -0x78(%rbp)
movq -0x60(%rbp), %rax
movslq -0xa4(%rbp), %rcx
imulq $0x74, %rcx, %rcx
addq %rcx, %rax
movss 0x68(%rax), %xmm0
movss %xmm0, -0x74(%rbp)
movq -0x60(%rbp), %rax
movslq -0xa4(%rbp), %rcx
imulq $0x74, %rcx, %rcx
addq %rcx, %rax
movss 0x6c(%rax), %xmm0
movss %xmm0, -0x70(%rbp)
leaq -0xb0(%rbp), %rdi
movq -0x60(%rbp), %rsi
movslq -0xa4(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %rsi
addq $0x40, %rsi
movss -0x98(%rbp), %xmm0
callq 0x40cb40
leaq -0xbc(%rbp), %rdi
movq -0x60(%rbp), %rsi
movslq -0xa4(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %rsi
addq $0x4c, %rsi
movss -0xa0(%rbp), %xmm0
callq 0x40cb40
leaq -0xc8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movslq -0xa4(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %rsi
addq $0x58, %rsi
movss -0x9c(%rbp), %xmm0
callq 0x40cb40
leaq -0x78(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xbc(%rbp), %rdx
callq 0x40ca80
leaq -0x78(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
callq 0x40ca80
movq -0x60(%rbp), %rax
movslq -0xa4(%rbp), %rcx
imulq $0x74, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
jmp 0x422f88
movq 0x18(%rbp), %rax
movl -0xa4(%rbp), %ecx
subl 0x10(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x2c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
movq -0xd0(%rbp), %rdx
addq $0x1c, %rdx
callq 0x40cae0
leaq -0x78(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x40ca00
movq -0xd0(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
jne 0x422f98
movb $0x0, -0x29(%rbp)
jmp 0x422ffc
movss -0x6c(%rbp), %xmm0
movq 0x28(%rbp), %rax
movss %xmm0, (%rax)
movss -0x68(%rbp), %xmm0
movq 0x28(%rbp), %rax
movss %xmm0, 0x4(%rax)
movss -0x64(%rbp), %xmm0
movq 0x28(%rbp), %rax
movss %xmm0, 0x8(%rax)
movss -0x78(%rbp), %xmm0
movq 0x30(%rbp), %rax
movss %xmm0, (%rax)
movss -0x74(%rbp), %xmm0
movq 0x30(%rbp), %rax
movss %xmm0, 0x4(%rax)
movss -0x70(%rbp), %xmm0
movq 0x30(%rbp), %rax
movss %xmm0, 0x8(%rax)
movq -0x88(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
movb $0x1, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)