// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::EstimateOrientedBBox(gaps::R3SurfelPointSet*, gaps::R3Point const&, gaps::R3Triad const&)
R3OrientedBox 
EstimateOrientedBBox(R3SurfelPointSet *pointset,
  const R3Point& centroid, const R3Triad& axes)
{
  // Check pointset
  if (pointset->NPoints() == 0) return R3null_oriented_box;
    
  // Compute ranges
  R3Box range = R3null_box;
  for (int i = 0; i < pointset->NPoints(); i++) {
    R3SurfelPoint *point = pointset->Point(i);
    R3Vector v = point->Position() - centroid;
    RNLength r0 = v.Dot(axes[0]);
    RNLength r1 = v.Dot(axes[1]);
    RNLength r2 = v.Dot(axes[2]);
    range.Union(R3Point(r0, r1, r2));
  }

  // Compute center
  if (range.IsEmpty()) range = R3zero_box;
  R3Point c = range.Centroid();
  R3Point center = centroid + axes[0]*c[0] + axes[1]*c[1] + axes[2]*c[2];

  // Return oriented box
  return R3OrientedBox(center, axes[0], axes[1],
    range.XRadius(), range.YRadius(), range.ZRadius());
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0xa8(%rsp)
movq %rdi, %rax
movq %rax, 0xb0(%rsp)
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
movq %rdx, 0x220(%rsp)
movq %rcx, 0x218(%rsp)
movq 0x228(%rsp), %rdi
callq 0x18700
cmpl $0x0, %eax
jne 0x3d7a5
movq 0xa8(%rsp), %rdi
leaq 0x1bf935(%rip), %rsi    # 0x1fd0d0
callq 0xc7980
jmp 0x3dbfb
leaq 0x1e0(%rsp), %rdi
leaq 0x1bfb54(%rip), %rsi    # 0x1fd308
callq 0xc9de0
movl $0x0, 0x1dc(%rsp)
movl 0x1dc(%rsp), %eax
movl %eax, 0xa0(%rsp)
movq 0x228(%rsp), %rdi
callq 0x18700
movl %eax, 0xa4(%rsp)
jmp 0x3d7e8
movl 0xa0(%rsp), %eax
movl 0xa4(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3d9b6
movq 0x228(%rsp), %rdi
movl 0x1dc(%rsp), %esi
callq 0x18710
movq %rax, 0x98(%rsp)
jmp 0x3d81c
movq 0x98(%rsp), %rax
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0x18730
jmp 0x3d843
movq 0x220(%rsp), %rdx
leaq 0x1a8(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0xf86b0
jmp 0x3d862
movq 0x218(%rsp), %rdi
xorl %esi, %esi
callq 0x44220
movq %rax, 0x90(%rsp)
jmp 0x3d87b
movq 0x90(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0xf9ac0
movsd %xmm0, 0x88(%rsp)
jmp 0x3d89b
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x188(%rsp)
movq 0x218(%rsp), %rdi
movl $0x1, %esi
callq 0x44220
movq %rax, 0x80(%rsp)
jmp 0x3d8c9
movq 0x80(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0xf9ac0
movsd %xmm0, 0x78(%rsp)
jmp 0x3d8e6
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x180(%rsp)
movq 0x218(%rsp), %rdi
movl $0x2, %esi
callq 0x44220
movq %rax, 0x70(%rsp)
jmp 0x3d90e
movq 0x70(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0xf9ac0
movsd %xmm0, 0x68(%rsp)
jmp 0x3d928
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x178(%rsp)
movsd 0x188(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
movsd 0x178(%rsp), %xmm2
leaq 0x160(%rsp), %rdi
callq 0xf8350
jmp 0x3d961
leaq 0x1e0(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0xcb9d0
jmp 0x3d978
jmp 0x3d97a
movl 0x1dc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1dc(%rsp)
jmp 0x3d7c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x1b990
jmp 0x3dc0b
leaq 0x1e0(%rsp), %rdi
callq 0x44290
movl %eax, 0x64(%rsp)
jmp 0x3d9c9
movl 0x64(%rsp), %eax
cmpl $0x0, %eax
je 0x3d9ea
leaq 0x1bf887(%rip), %rsi    # 0x1fd260
leaq 0x1e0(%rsp), %rdi
callq 0x1bd40
jmp 0x3d9e8
jmp 0x3d9ea
leaq 0x148(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0xcb780
jmp 0x3da01
movq 0x220(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x218(%rsp), %rdi
xorl %esi, %esi
callq 0x44220
movq %rax, 0x58(%rsp)
jmp 0x3da24
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0x48(%rsp)
jmp 0x3da3a
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rax
movsd (%rax), %xmm0
leaq 0xe8(%rsp), %rdi
callq 0xfa380
jmp 0x3da57
movq 0x50(%rsp), %rsi
leaq 0x100(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0xf8910
jmp 0x3da73
movq 0x218(%rsp), %rdi
movl $0x1, %esi
callq 0x44220
movq %rax, 0x40(%rsp)
jmp 0x3da8c
leaq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0x38(%rsp)
jmp 0x3daa5
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
leaq 0xd0(%rsp), %rdi
callq 0xfa380
jmp 0x3dac2
leaq 0x118(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0xf8910
jmp 0x3dae1
movq 0x218(%rsp), %rdi
movl $0x2, %esi
callq 0x44220
movq %rax, 0x30(%rsp)
jmp 0x3dafa
leaq 0x148(%rsp), %rdi
movl $0x2, %esi
callq 0x1bde0
movq %rax, 0x28(%rsp)
jmp 0x3db13
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rax
movsd (%rax), %xmm0
leaq 0xb8(%rsp), %rdi
callq 0xfa380
jmp 0x3db30
leaq 0x130(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xf8910
jmp 0x3db4f
movq 0x218(%rsp), %rdi
xorl %esi, %esi
callq 0x44220
movq %rax, 0x20(%rsp)
jmp 0x3db65
movq 0x218(%rsp), %rdi
movl $0x1, %esi
callq 0x44220
movq %rax, 0x18(%rsp)
jmp 0x3db7e
leaq 0x1e0(%rsp), %rdi
callq 0x44350
movsd %xmm0, 0x10(%rsp)
jmp 0x3db93
leaq 0x1e0(%rsp), %rdi
callq 0x44370
movsd %xmm0, 0x8(%rsp)
jmp 0x3dba8
leaq 0x1e0(%rsp), %rdi
callq 0x44390
movsd %xmm0, (%rsp)
jmp 0x3dbbc
movsd (%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq 0xa8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xc7b10
jmp 0x3dbee
leaq 0x1e0(%rsp), %rdi
callq 0x1b990
movq 0xb0(%rsp), %rax
addq $0x238, %rsp            # imm = 0x238
retq
movq 0x1d0(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)