// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::EstimateOrientedBBox(gaps::R3SurfelPointSet*)
R3OrientedBox 
EstimateOrientedBBox(R3SurfelPointSet *pointset)
{
  // Check pointset
  if (pointset->NPoints() == 0) return R3null_oriented_box;
    
  // Get centroid 
  R3Point centroid = pointset->Centroid();

  // Search for best axes
  int nxsteps = 1;
  int nysteps = 1;
  int nzsteps = 16;
  RNVolume best_volume = FLT_MAX;
  R3OrientedBox best_obb = R3null_oriented_box;
  for (int ix = 0; ix < nxsteps; ix++) {
    for (int iy = 0; iy < nysteps; iy++) {
      for (int iz = 0; iz < nzsteps; iz++) {
        R3Triad axes = R3xyz_triad;
        axes.Rotate(RN_X, ix*0.5*RN_PI/nxsteps);
        axes.Rotate(RN_Y, iy*0.5*RN_PI/nysteps);
        axes.Rotate(RN_Z, iz*0.5*RN_PI/nzsteps);

        // Compute oriented bbox
        R3OrientedBox obb = EstimateOrientedBBox(pointset, centroid, axes);
        RNVolume volume = obb.Volume();
        if (volume < best_volume) {
          best_volume = volume;
          best_obb = obb;
        }
      }
    }
  }
      
  // Check if should rotate around Z by 90 degrees to make axis0 the longer axis
  if (best_obb.Radius(0) < best_obb.Radius(1)) {
    best_obb.Reset(best_obb.Center(), best_obb.Axis(1), -(best_obb.Axis(0)),
      best_obb.Radius(1), best_obb.Radius(0), best_obb.Radius(2));
  }

  // Return best oriented box
  return best_obb;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %r15
cmpl $0x0, 0x10(%rsi)
je 0x28349
movq %rsi, %r14
leaq 0x48(%rsp), %rdi
callq 0x32b74
leaq 0x10d60d(%rip), %rsi    # 0x135790
movq %r15, %rdi
callq 0x6cb2e
leaq 0xbf656(%rip), %rax     # 0xe77e8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
movsd %xmm0, 0x40(%rsp)
mulsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
leaq 0x8(%r15), %rax
movq %rax, 0x18(%rsp)
leaq 0x68(%rsp), %rbx
leaq 0x20(%r15), %rax
movq %rax, 0x10(%rsp)
leaq 0x38(%r15), %rax
movq %rax, 0x8(%rsp)
leaq 0x50(%r15), %rax
movq %rax, 0x38(%rsp)
movq %r15, (%rsp)
leaq 0x68(%r15), %r13
xorl %r15d, %r15d
movsd 0xb5f8a(%rip), %xmm0    # 0xde170
movsd %xmm0, 0x20(%rsp)
leaq 0xe0(%rsp), %r12
leaq 0x60(%rsp), %rbp
movq %r12, %rdi
leaq 0x10dae5(%rip), %rsi    # 0x135ce8
callq 0x71826
movq %r12, %rdi
xorl %esi, %esi
movsd 0x28(%rsp), %xmm0
callq 0x71ab4
movq %r12, %rdi
movl $0x1, %esi
movsd 0x28(%rsp), %xmm0
callq 0x71ab4
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0xb5f05(%rip), %xmm0    # 0xde140
mulsd 0x40(%rsp), %xmm0
mulsd 0xb68bf(%rip), %xmm0    # 0xdeb08
movq %r12, %rdi
movl $0x2, %esi
callq 0x71ab4
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
movq %r12, %rcx
callq 0x27e6b
movq %rbp, %rdi
callq 0x6d6b8
movsd %xmm0, 0x30(%rsp)
movsd 0x20(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x282e5
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x850ec
movq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x85fc2
movq 0x8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x85fc2
movq 0x38(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x85fc2
movq 0x70(%rbx), %rax
movq %rax, 0x10(%r13)
movups 0x60(%rbx), %xmm0
movups %xmm0, (%r13)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rbp, %rdi
callq 0x6ede0
incl %r15d
cmpl $0x10, %r15d
jne 0x281f9
movq (%rsp), %r14
movsd 0x70(%r14), %xmm0
ucomisd 0x68(%r14), %xmm0
jbe 0x28344
leaq 0x60(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x864ac
movsd 0x68(%r14), %xmm1
movsd 0x70(%r14), %xmm0
movsd 0x78(%r14), %xmm2
leaq 0x60(%rsp), %rcx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x6ddac
movq %r14, %rax
jmp 0x2835b
leaq 0x10d440(%rip), %rsi    # 0x135790
movq %r15, %rdi
callq 0x6cb2e
movq %r15, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2838a
jmp 0x28383
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x6ede0
jmp 0x28386
movq %rax, %rbx
movq (%rsp), %r14
movq %r14, %rdi
callq 0x6ede0
movq %rbx, %rdi
callq 0x138b0