// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3OrientedBox.cpp
// Function: gaps::R3OrientedBox::R3OrientedBox(gaps::RNArray<gaps::R3Point*> const&)
R3OrientedBox::
R3OrientedBox(const RNArray<R3Point *>& points)
{
#if 0
    // This is correct, but produces bboxes that are too big (diagonal)
  
    // Compute coordinate system
    R3Point centroid = R3Centroid(points);
    R3Triad axes = R3PrincipleAxes(centroid, points);

    // Compute ranges
    R3Box r = R3null_box;
    for (int i = 0; i < points.NEntries(); i++) {
      R3Point *point = points.Kth(i);
      R3Vector v = *point - centroid;
      RNLength r0 = v.Dot(axes[0]);
      RNLength r1 = v.Dot(axes[1]);
      RNLength r2 = v.Dot(axes[2]);
      r.Union(R3Point(r0, r1, r2));
    }

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

    // Initialize everything
    Reset(center, axes[0], axes[1], r.XRadius(), r.YRadius(), r.ZRadius());
#else
    // Get centroid 
    R3Point centroid = R3Centroid(points);

    // Search for best axes
    R3Box best_range = R3null_box;
    R3Triad best_axes = R3xyz_triad;
    int nxsteps = 2;
    int nysteps = 2;
    int nzsteps = 4;
    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_Z, ix*0.5*RN_PI/nxsteps);
          axes.Rotate(RN_Z, iy*0.5*RN_PI/nysteps);
          axes.Rotate(RN_Z, iz*0.5*RN_PI/nzsteps);
    
          // Compute ranges
          R3Box range = R3null_box;
          for (int i = 0; i < points.NEntries(); i++) {
            R3Point *point = points.Kth(i);
            R3Vector v = *point - 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));
          }

          // Check if range is best
          if (best_range.IsEmpty() || (range.Volume() < best_range.Volume())) {
            best_range = range;
            best_axes = axes;
          }
        }
      }
    }
      
    // Compute center
    if (best_range.IsEmpty()) best_range = R3zero_box;
    R3Point c = best_range.Centroid();
    R3Point center = centroid + best_axes[0]*c[0] + best_axes[1]*c[1] + best_axes[2]*c[2];
           
    // Initialize everything
    Reset(center, best_axes[0], best_axes[1], best_range.XRadius(), best_range.YRadius(), best_range.ZRadius());
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %r12
movq %rdi, %rbx
callq 0x280e8
leaq 0x8e62f(%rip), %rax     # 0xe4078
movq %rax, (%rbx)
movq %rbx, 0x60(%rsp)
leaq 0x8(%rbx), %rdi
callq 0x2a6e8
leaq 0x140(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x4c1ec
leaq 0x90425(%rip), %rsi     # 0xe5e98
leaq 0x28(%rsp), %rdi
callq 0x272d4
leaq 0x90734(%rip), %rsi     # 0xe61b8
leaq 0xf8(%rsp), %rdi
callq 0x2ab46
leaq 0x54530(%rip), %rax     # 0xa9fc8
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
xorpd %xmm1, %xmm1
movb $0x1, %al
movl %eax, 0x8(%rsp)
movsd 0x4c57c(%rip), %xmm0    # 0xa2030
leaq 0xb0(%rsp), %r15
leaq 0x158(%rsp), %rbp
leaq 0x98(%rsp), %r13
leaq 0x80(%rsp), %r14
mulsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorpd %xmm1, %xmm1
mulsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rsp)
xorl %ebx, %ebx
movq %r15, %rdi
leaq 0x906ae(%rip), %rsi     # 0xe61b8
callq 0x2ab46
movq %r15, %rdi
movl $0x2, %esi
movsd 0x68(%rsp), %xmm0
callq 0x2add4
movq %r15, %rdi
movl $0x2, %esi
movsd 0x70(%rsp), %xmm0
callq 0x2add4
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x4c4ec(%rip), %xmm0    # 0xa2030
mulsd 0x10(%rsp), %xmm0
mulsd 0x4d25e(%rip), %xmm0    # 0xa2db0
movq %r15, %rdi
movl $0x2, %esi
callq 0x2add4
movq %rbp, %rdi
leaq 0x9032f(%rip), %rsi     # 0xe5e98
callq 0x272d4
movl %ebx, 0x24(%rsp)
cmpl $0x0, 0xc(%r12)
jle 0x55bff
xorl %ebx, %ebx
movq (%r12), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %rdi
leaq 0x140(%rsp), %rdx
callq 0x3c78e
movq %r13, %rdi
movq %r15, %rsi
callq 0x3d252
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3d252
movsd %xmm0, 0x78(%rsp)
movq %r13, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d252
movapd %xmm0, %xmm2
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
callq 0x3c532
movq %rbp, %rdi
movq %r14, %rsi
callq 0x27cc6
incq %rbx
movslq 0xc(%r12), %rax
cmpq %rax, %rbx
jl 0x55b80
movsd 0x30(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
ja 0x55c49
movsd 0x38(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
ja 0x55c49
movsd 0x40(%rsp), %xmm0
ucomisd 0x58(%rsp), %xmm0
ja 0x55c49
movq %rbp, %rdi
callq 0x27ac4
movsd %xmm0, 0x18(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x27ac4
ucomisd 0x18(%rsp), %xmm0
jbe 0x55ca7
leaq 0x30(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x3c89a
leaq 0x48(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x3c89a
leaq 0xf8(%rsp), %rdi
movq %r15, %rsi
callq 0x3d752
leaq 0x110(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3d752
leaq 0x128(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d752
movq %rbp, %rdi
callq 0x280f4
movl 0x24(%rsp), %ebx
incl %ebx
cmpl $0x4, %ebx
jne 0x55b00
movsd 0x4c36a(%rip), %xmm0    # 0xa2030
movapd %xmm0, %xmm1
testb $0x1, 0xc(%rsp)
movl $0x0, 0xc(%rsp)
jne 0x55aee
movapd %xmm0, %xmm1
testb $0x1, 0x8(%rsp)
movl $0x0, 0x8(%rsp)
jne 0x55ad4
movsd 0x30(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
ja 0x55d1e
movsd 0x38(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
ja 0x55d1e
movsd 0x40(%rsp), %xmm0
ucomisd 0x58(%rsp), %xmm0
jbe 0x55d44
leaq 0x900cb(%rip), %rbx     # 0xe5df0
leaq 0x8(%rbx), %rsi
leaq 0x30(%rsp), %rdi
callq 0x3c89a
addq $0x20, %rbx
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c89a
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x27aea
movsd 0xb0(%rsp), %xmm0
leaq 0x1c0(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x3d962
leaq 0x80(%rsp), %rdi
leaq 0x140(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
callq 0x3c877
movsd 0xb8(%rsp), %xmm0
leaq 0x1a8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x3d962
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x1a8(%rsp), %rdx
callq 0x3c877
movsd 0xc0(%rsp), %xmm0
leaq 0x190(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x3d962
leaq 0x158(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0x3c877
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
subsd 0x30(%rsp), %xmm0
movsd 0x4c20f(%rip), %xmm3    # 0xa2030
mulsd %xmm3, %xmm0
subsd 0x38(%rsp), %xmm1
mulsd %xmm3, %xmm1
movsd 0x58(%rsp), %xmm2
subsd 0x40(%rsp), %xmm2
mulsd %xmm3, %xmm2
movq 0x60(%rsp), %rdi
movq (%rdi), %rax
leaq 0x158(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
leaq 0x110(%rsp), %rcx
callq *0x268(%rax)
leaq 0x28(%rsp), %rdi
callq 0x280f4
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x55e9a
jmp 0x55e9a
jmp 0x55e9a
jmp 0x55e9a
jmp 0x55e9a
jmp 0x55e9a
jmp 0x55e9a
jmp 0x55e93
jmp 0x55e93
movq %rax, %rbx
jmp 0x55ec3
jmp 0x55e9a
movq %rax, %rbx
jmp 0x55eb9
jmp 0x55ea9
jmp 0x55ea9
jmp 0x55ea9
jmp 0x55ea9
jmp 0x55ea9
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x280f4
leaq 0x28(%rsp), %rdi
callq 0x280f4
movq 0x60(%rsp), %rdi
callq 0x280f4
movq %rbx, %rdi
callq 0xe7e0
nop