// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %rbx
callq 0x25826
leaq 0x8aa2d(%rip), %rax     # 0xd9088
movq %rax, (%rbx)
movq %rbx, 0x58(%rsp)
leaq 0x8(%rbx), %rdi
callq 0x27d56
leaq 0xb0(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x45a87
leaq 0x8c823(%rip), %rsi     # 0xdaea8
leaq 0x20(%rsp), %rdi
callq 0x24b3e
leaq 0x8cb32(%rip), %rsi     # 0xdb1c8
leaq 0x158(%rsp), %rdi
callq 0x28198
leaq 0x4c89e(%rip), %rax     # 0x9af48
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %eax, %eax
leaq 0xc8(%rsp), %rbp
leaq 0x1a0(%rsp), %r13
leaq 0x98(%rsp), %rbx
leaq 0x80(%rsp), %r12
cmpl $0x2, %eax
je 0x4e8c0
movq %rax, 0x60(%rsp)
cvtsi2sd %eax, %xmm1
movsd 0x44920(%rip), %xmm0    # 0x93010
mulsd %xmm0, %xmm1
mulsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
xorl %eax, %eax
cmpl $0x2, %eax
je 0x4e8b4
movl %eax, 0x1c(%rsp)
cvtsi2sd %eax, %xmm1
movsd 0x448f1(%rip), %xmm0    # 0x93010
mulsd %xmm0, %xmm1
mulsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rsp)
xorl %eax, %eax
movl %eax, 0x4(%rsp)
cmpl $0x4, %eax
je 0x4e8a9
movq %rbp, %rdi
leaq 0x8ca7c(%rip), %rsi     # 0xdb1c8
callq 0x28198
pushq $0x2
popq %r15
movq %rbp, %rdi
movl %r15d, %esi
movsd 0x68(%rsp), %xmm0
callq 0x28412
movq %rbp, %rdi
movl %r15d, %esi
movsd 0x70(%rsp), %xmm0
callq 0x28412
cvtsi2sdl 0x4(%rsp), %xmm0
mulsd 0x4488b(%rip), %xmm0    # 0x93010
mulsd 0x8(%rsp), %xmm0
mulsd 0x455cd(%rip), %xmm0    # 0x93d60
movq %rbp, %rdi
movl %r15d, %esi
callq 0x28412
movq %r13, %rdi
leaq 0x8c700(%rip), %rsi     # 0xdaea8
callq 0x24b3e
xorl %r15d, %r15d
movslq 0xc(%r14), %rax
cmpq %rax, %r15
jge 0x4e82f
movq (%r14), %rax
movq (%rax,%r15,8), %rsi
movq %rbx, %rdi
leaq 0xb0(%rsp), %rdx
callq 0x37d29
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x38586
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x38586
movsd %xmm0, 0x78(%rsp)
movq %rbx, %rdi
leaq 0xf8(%rsp), %rsi
callq 0x38586
movapd %xmm0, %xmm2
movq %r12, %rdi
movsd 0x10(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
callq 0x37b4c
movq %r13, %rdi
movq %r12, %rsi
callq 0x254a4
incq %r15
jmp 0x4e7b0
movsd 0x28(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
ja 0x4e879
movsd 0x30(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
ja 0x4e879
movsd 0x38(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
ja 0x4e879
movq %r13, %rdi
callq 0x252f4
movsd %xmm0, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x252f4
ucomisd 0x10(%rsp), %xmm0
jbe 0x4e896
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x2141e
leaq 0x158(%rsp), %rdi
movq %rbp, %rsi
callq 0x28134
movq %r13, %rdi
callq 0x25832
movl 0x4(%rsp), %eax
incl %eax
jmp 0x4e735
movl 0x1c(%rsp), %eax
incl %eax
jmp 0x4e706
movq 0x60(%rsp), %rax
incl %eax
jmp 0x4e6d6
movsd 0x28(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
ja 0x4e8ea
movsd 0x30(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
ja 0x4e8ea
movsd 0x38(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
jbe 0x4e8fb
leaq 0x8c50f(%rip), %rsi     # 0xdae00
leaq 0x20(%rsp), %rdi
callq 0x2141e
leaq 0xc8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2531a
movsd 0xc8(%rsp), %xmm0
leaq 0x140(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x38a02
leaq 0x80(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x37e13
leaq 0x170(%rsp), %rbx
movsd 0xd0(%rsp), %xmm0
leaq 0x128(%rsp), %rdi
movq %rbx, %rsi
callq 0x38a02
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x37e13
leaq 0x188(%rsp), %rsi
movsd 0xd8(%rsp), %xmm0
leaq 0x110(%rsp), %rdi
callq 0x38a02
leaq 0x1a0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x110(%rsp), %rdx
callq 0x37e13
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
subsd 0x28(%rsp), %xmm0
movsd 0x44635(%rip), %xmm3    # 0x93010
mulsd %xmm3, %xmm0
subsd 0x30(%rsp), %xmm1
mulsd %xmm3, %xmm1
movsd 0x50(%rsp), %xmm2
subsd 0x38(%rsp), %xmm2
mulsd %xmm3, %xmm2
movq 0x58(%rsp), %rdi
movq (%rdi), %rax
leaq 0x1a0(%rsp), %rsi
leaq 0x158(%rsp), %rdx
movq %rbx, %rcx
callq *0x268(%rax)
leaq 0x20(%rsp), %rdi
callq 0x25832
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4ea51
jmp 0x4ea51
jmp 0x4ea51
jmp 0x4ea51
jmp 0x4ea51
jmp 0x4ea51
jmp 0x4ea51
jmp 0x4ea48
jmp 0x4ea48
movq %rax, %rbx
jmp 0x4ea78
jmp 0x4ea51
jmp 0x4ea5e
movq %rax, %rbx
jmp 0x4ea6e
jmp 0x4ea5e
jmp 0x4ea5e
jmp 0x4ea5e
jmp 0x4ea5e
movq %rax, %rbx
leaq 0x1a0(%rsp), %rdi
callq 0x25832
leaq 0x20(%rsp), %rdi
callq 0x25832
movq 0x58(%rsp), %rdi
callq 0x25832
movq %rbx, %rdi
callq 0xe810