// File: /workspace/github_cpp_makefile_star2_O3/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 0x27fef
movq %rsi, %r14
leaq 0x48(%rsp), %rdi
callq 0x327dc
leaq 0x10c967(%rip), %rsi    # 0x134790
movq %r15, %rdi
callq 0x6bf0a
leaq 0xbe9a0(%rip), %rax     # 0xe67d8
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 0xb52dc(%rip), %xmm0    # 0xdd168
movsd %xmm0, 0x20(%rsp)
leaq 0xe0(%rsp), %r12
leaq 0x60(%rsp), %rbp
movq %r12, %rdi
leaq 0x10ce3f(%rip), %rsi    # 0x134ce8
callq 0x70bc6
movq %r12, %rdi
xorl %esi, %esi
movsd 0x28(%rsp), %xmm0
callq 0x70e54
movq %r12, %rdi
movl $0x1, %esi
movsd 0x28(%rsp), %xmm0
callq 0x70e54
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0xb5257(%rip), %xmm0    # 0xdd138
mulsd 0x40(%rsp), %xmm0
mulsd 0xb5c21(%rip), %xmm0    # 0xddb10
movq %r12, %rdi
movl $0x2, %esi
callq 0x70e54
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
movq %r12, %rcx
callq 0x27b11
movq %rbp, %rdi
callq 0x6caa0
movsd %xmm0, 0x30(%rsp)
movsd 0x20(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x27f8b
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x8412a
movq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x84fe2
movq 0x8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x84fe2
movq 0x38(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x84fe2
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 0x6e174
incl %r15d
cmpl $0x10, %r15d
jne 0x27e9f
movq (%rsp), %r14
movsd 0x70(%r14), %xmm0
ucomisd 0x68(%r14), %xmm0
jbe 0x27fea
leaq 0x60(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x85490
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 0x6d17a
movq %r14, %rax
jmp 0x28001
leaq 0x10c79a(%rip), %rsi    # 0x134790
movq %r15, %rdi
callq 0x6bf0a
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 0x28030
jmp 0x28029
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x6e174
jmp 0x2802c
movq %rax, %rbx
movq (%rsp), %r14
movq %r14, %rdi
callq 0x6e174
movq %rbx, %rdi
callq 0x138b0