// File: Chrom3D[P]Chrom3D/src/Util.cpp
// Function: util::createSelfAvoidingWalk(std::vector<double, std::allocator<double>>, boost::numeric::ublas::vector<double, boost::numeric::ublas::unbounded_array<double, std::allocator<double>>>, boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&, unsigned int)
CoordinateMatrix createSelfAvoidingWalk(vector<double> radii, CoordinateVector origin, ENG& eng, uint maxIter) {
  assert(radii.size() > 0);
  CoordinateVector tmpRadii;
  double prevRadius=radii[0];
  CoordinateVector prevCoords = origin;
  CoordinateMatrix newCoords(radii.size(), 3); newCoords(0,0) = origin(0);   newCoords(0,1) = origin(1); newCoords(0,2) = origin(2);
  uint j;
  
  for(uint i=1; i!=radii.size(); i++) {
    double R = radii[i] + prevRadius;
    assert(R != 0);
    double x;
    double y;
    double z;
    for(j=0; j != maxIter; j++) {
      double theta = unif_real(0, 2*PI, eng);
      z = unif_real(-R,R, eng);
      x = sqrt(pow(R,2) - pow(z,2)) * cos(theta); // OBS: Speed up by doing sqrt only once??
      y = sqrt(pow(R,2) - pow(z,2)) * sin(theta); // Archimedes method (see http://tinyurl.com/nquq2d9)

      x += prevCoords(0);
      y += prevCoords(1);
      z += prevCoords(2);

      CoordinateVector proposedCoords(3); proposedCoords(0) = x; proposedCoords(1) = y; proposedCoords(2) = z;
      vector<double> tmpRadii(radii.begin(), radii.begin()+i+1);
     
      cout.precision(15);
      CoordinateVector newc = ublas::row(newCoords,i);

      if(not coordClash(proposedCoords, project(newCoords, boost::numeric::ublas::range(0, i), boost::numeric::ublas::range(0,3)), radii[i], tmpRadii)) {
	       break;
      }
    }
    
    if (j==maxIter) {
      // reached the end of maxIter without finding a structure that does not clash
      CoordinateMatrix newCoordsEmpty(0,0);
      return newCoordsEmpty;
    }
    
    prevCoords(0) = x; prevCoords(1) = y;  prevCoords(2) = z;
    prevRadius = radii[i];
    newCoords(i,0) = x;
    newCoords(i,1) = y;
    newCoords(i,2) = z;    
  }
  return newCoords;
}

// Assembly:
subq $0x368, %rsp            # imm = 0x368
movq %rdx, 0x158(%rsp)
movq %rsi, 0x160(%rsp)
movq %rdi, %rax
movq 0x160(%rsp), %rdi
movq %rax, 0x168(%rsp)
movq %rax, %rsi
movq %rsi, 0x170(%rsp)
movq %rax, 0x360(%rsp)
movq %rdi, 0x358(%rsp)
movq %rdx, 0x350(%rsp)
movq %rcx, 0x348(%rsp)
movl %r8d, 0x344(%rsp)
callq 0xbd90
cmpq $0x0, %rax
jbe 0x9d8a
jmp 0x9da9
leaq 0x452bf(%rip), %rdi     # 0x4f050
leaq 0x452c9(%rip), %rsi     # 0x4f061
movl $0x5c, %edx
leaq 0x452ca(%rip), %rcx     # 0x4f06e
callq 0x8340
leaq 0x328(%rsp), %rdi
callq 0xbdb0
movq 0x160(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xbd40
movq 0x158(%rsp), %rsi
movsd (%rax), %xmm0
movsd %xmm0, 0x320(%rsp)
leaq 0x308(%rsp), %rdi
callq 0xb710
jmp 0x9deb
movq 0x160(%rsp), %rdi
callq 0xbd90
movq %rax, %rsi
leaq 0x2d0(%rsp), %rdi
movl $0x3, %edx
callq 0xb1a0
jmp 0x9e0f
movq 0x158(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xb270
movq %rax, 0x150(%rsp)
jmp 0x9e2a
movq 0x150(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x140(%rsp)
xorl %eax, %eax
movl %eax, %edx
leaq 0x2d0(%rsp), %rdi
movq %rdx, %rsi
callq 0xb2a0
movq %rax, 0x148(%rsp)
jmp 0x9e5d
movq 0x158(%rsp), %rdi
movq 0x148(%rsp), %rax
movsd 0x140(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
callq 0xb270
movq %rax, 0x138(%rsp)
jmp 0x9e8e
movq 0x138(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x128(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x2d0(%rsp), %rdi
movl $0x1, %edx
callq 0xb2a0
movq %rax, 0x130(%rsp)
jmp 0x9ec3
movq 0x158(%rsp), %rdi
movq 0x130(%rsp), %rax
movsd 0x128(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
callq 0xb270
movq %rax, 0x120(%rsp)
jmp 0x9ef4
movq 0x120(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x110(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x2d0(%rsp), %rdi
movl $0x2, %edx
callq 0xb2a0
movq %rax, 0x118(%rsp)
jmp 0x9f29
movq 0x118(%rsp), %rax
movsd 0x110(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, 0x2c8(%rsp)
movq 0x160(%rsp), %rdi
movl 0x2c8(%rsp), %eax
movq %rax, 0x108(%rsp)
callq 0xbd90
movq %rax, %rcx
movq 0x108(%rsp), %rax
cmpq %rcx, %rax
je 0xa94a
movq 0x160(%rsp), %rdi
movl 0x2c8(%rsp), %eax
movl %eax, %esi
callq 0xbd40
movsd (%rax), %xmm0
addsd 0x320(%rsp), %xmm0
movsd %xmm0, 0x2c0(%rsp)
movsd 0x2c0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x9fbb
jp 0x9fbb
jmp 0x9fbd
jmp 0xa027
leaq 0x45107(%rip), %rdi     # 0x4f0cb
leaq 0x45096(%rip), %rsi     # 0x4f061
movl $0x65, %edx
leaq 0x45097(%rip), %rcx     # 0x4f06e
callq 0x8340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa9bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa9b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa9a3
movl $0x0, 0x2cc(%rsp)
movl 0x2cc(%rsp), %eax
cmpl 0x344(%rsp), %eax
je 0xa751
movq 0x348(%rsp), %rdi
xorps %xmm0, %xmm0
movsd 0x44fbf(%rip), %xmm1    # 0x4f018
callq 0x9940
movsd %xmm0, 0x100(%rsp)
jmp 0xa069
movsd 0x100(%rsp), %xmm0
movsd %xmm0, 0x2a0(%rsp)
movsd 0x2c0(%rsp), %xmm1
movaps %xmm1, %xmm0
movaps 0x44fa2(%rip), %xmm2    # 0x4f030
pxor %xmm2, %xmm0
movq 0x348(%rsp), %rdi
callq 0x9940
movsd %xmm0, 0xf8(%rsp)
jmp 0xa0aa
movsd 0xf8(%rsp), %xmm0
movsd %xmm0, 0x2a8(%rsp)
movsd 0x2c0(%rsp), %xmm0
movl $0x2, %edi
callq 0xbbf0
movsd %xmm0, 0xf0(%rsp)
jmp 0xa0da
movsd 0x2a8(%rsp), %xmm0
movl $0x2, %edi
callq 0xbbf0
movsd %xmm0, 0xe8(%rsp)
jmp 0xa0f8
movsd 0xe8(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm0
subsd %xmm1, %xmm0
callq 0x83e0
movsd %xmm0, 0xd8(%rsp)
movsd 0x2a0(%rsp), %xmm0
callq 0x88f0
movaps %xmm0, %xmm1
movsd 0xd8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x2b8(%rsp)
movsd 0x2c0(%rsp), %xmm0
movl $0x2, %edi
callq 0xbbf0
movsd %xmm0, 0xe0(%rsp)
jmp 0xa161
movsd 0x2a8(%rsp), %xmm0
movl $0x2, %edi
callq 0xbbf0
movsd %xmm0, 0xd0(%rsp)
jmp 0xa17f
movsd 0xd0(%rsp), %xmm1
movsd 0xe0(%rsp), %xmm0
subsd %xmm1, %xmm0
callq 0x83e0
movsd %xmm0, 0xc0(%rsp)
movsd 0x2a0(%rsp), %xmm0
callq 0x8100
movaps %xmm0, %xmm1
movsd 0xc0(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x2b0(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x308(%rsp), %rdi
callq 0xb270
movq %rax, 0xc8(%rsp)
jmp 0xa1e5
movq 0xc8(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x2b8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b8(%rsp)
leaq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0xb270
movq %rax, 0xb8(%rsp)
jmp 0xa223
movq 0xb8(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x2b0(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b0(%rsp)
leaq 0x308(%rsp), %rdi
movl $0x2, %esi
callq 0xb270
movq %rax, 0xb0(%rsp)
jmp 0xa261
movq 0xb0(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x2a8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a8(%rsp)
leaq 0x288(%rsp), %rdi
movl $0x3, %esi
callq 0xbe30
jmp 0xa297
movsd 0x2b8(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x288(%rsp), %rdi
callq 0xb270
movq %rax, 0xa8(%rsp)
jmp 0xa2c4
movq 0xa8(%rsp), %rax
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x2b0(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
leaq 0x288(%rsp), %rdi
movl $0x1, %esi
callq 0xb270
movq %rax, 0x98(%rsp)
jmp 0xa307
movq 0x98(%rsp), %rax
movsd 0x90(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x2a8(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x288(%rsp), %rdi
movl $0x2, %esi
callq 0xb270
movq %rax, 0x88(%rsp)
jmp 0xa34a
movq 0x160(%rsp), %rdi
movq 0x88(%rsp), %rax
movsd 0x80(%rsp), %xmm0
movsd %xmm0, (%rax)
callq 0xbec0
movq 0x160(%rsp), %rdi
movq %rax, 0x268(%rsp)
callq 0xbec0
movq %rax, 0x250(%rsp)
movl 0x2c8(%rsp), %eax
movl %eax, %esi
leaq 0x250(%rsp), %rdi
callq 0xbef0
movq %rax, 0x258(%rsp)
leaq 0x258(%rsp), %rdi
movl $0x1, %esi
callq 0xbef0
movq %rax, 0x260(%rsp)
leaq 0x24f(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0xbf30
movq 0x78(%rsp), %rcx
movq 0x268(%rsp), %rsi
movq 0x260(%rsp), %rdx
leaq 0x270(%rsp), %rdi
callq 0xbf40
jmp 0xa3f7
leaq 0x24f(%rsp), %rdi
callq 0xbfd0
movq 0x5fb8d(%rip), %rdi     # 0x69f98
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
movl $0xf, %esi
callq 0xbfe0
jmp 0xa421
movl 0x2c8(%rsp), %eax
movl %eax, %edx
leaq 0x220(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0xbc20
jmp 0xa441
leaq 0x230(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0xbc60
jmp 0xa458
leaq 0x220(%rsp), %rdi
callq 0xbd60
leaq 0x208(%rsp), %rdi
leaq 0x288(%rsp), %rsi
callq 0xb710
jmp 0xa47c
movl 0x2c8(%rsp), %eax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %esi
leaq 0x1a8(%rsp), %rdi
callq 0xc050
jmp 0xa498
xorl %eax, %eax
movl %eax, %esi
leaq 0x198(%rsp), %rdi
movl $0x3, %edx
callq 0xc050
jmp 0xa4b0
leaq 0x1b8(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
leaq 0x1a8(%rsp), %rdx
leaq 0x198(%rsp), %rcx
callq 0xc010
jmp 0xa4d7
leaq 0x1e0(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0xc170
jmp 0xa4ee
movq 0x160(%rsp), %rdi
movl 0x2c8(%rsp), %eax
movl %eax, %esi
callq 0xbd40
movsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0xc2c0
jmp 0xa525
movsd 0x70(%rsp), %xmm0
leaq 0x208(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
leaq 0x180(%rsp), %rdx
callq 0x9b80
movb %al, 0x6f(%rsp)
jmp 0xa54e
movb 0x6f(%rsp), %al
xorb $-0x1, %al
movb %al, 0x6e(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xc3c0
leaq 0x1e0(%rsp), %rdi
callq 0xb2d0
leaq 0x1b8(%rsp), %rdi
callq 0xc420
leaq 0x208(%rsp), %rdi
callq 0xb5b0
movb 0x6e(%rsp), %al
testb $0x1, %al
jne 0xa599
jmp 0xa6cc
movl $0x5, 0x17c(%rsp)
jmp 0xa6d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa73f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
leaq 0x24f(%rsp), %rdi
callq 0xbfd0
jmp 0xa73f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa732
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
leaq 0x220(%rsp), %rdi
callq 0xbd60
jmp 0xa732
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa725
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa6bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa6b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
jmp 0xa6a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x300(%rsp)
movl %eax, 0x2fc(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xc3c0
leaq 0x1e0(%rsp), %rdi
callq 0xb2d0
leaq 0x1b8(%rsp), %rdi
callq 0xc420
leaq 0x208(%rsp), %rdi
callq 0xb5b0
jmp 0xa725
movl $0x0, 0x17c(%rsp)
leaq 0x230(%rsp), %rdi
callq 0xb5b0
leaq 0x270(%rsp), %rdi
callq 0xc3c0
leaq 0x288(%rsp), %rdi
callq 0xb5b0
movl 0x17c(%rsp), %eax
testl %eax, %eax
je 0xa70d
jmp 0xa70b
jmp 0xa751
jmp 0xa70f
movl 0x2cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2cc(%rsp)
jmp 0xa032
leaq 0x230(%rsp), %rdi
callq 0xb5b0
leaq 0x270(%rsp), %rdi
callq 0xc3c0
leaq 0x288(%rsp), %rdi
callq 0xb5b0
jmp 0xa9a3
movl 0x2cc(%rsp), %eax
cmpl 0x344(%rsp), %eax
jne 0xa7ae
movq 0x168(%rsp), %rdi
movb $0x0, 0x17b(%rsp)
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0xb1a0
jmp 0xa77f
movb $0x1, 0x17b(%rsp)
movl $0x1, 0x17c(%rsp)
testb $0x1, 0x17b(%rsp)
jne 0xa7a9
movq 0x168(%rsp), %rdi
callq 0xb2d0
jmp 0xa96c
movsd 0x2b8(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x308(%rsp), %rdi
callq 0xb270
movq %rax, 0x60(%rsp)
jmp 0xa7d5
movq 0x60(%rsp), %rax
movsd 0x58(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x2b0(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
leaq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0xb270
movq %rax, 0x50(%rsp)
jmp 0xa80c
movq 0x50(%rsp), %rax
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x2a8(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x308(%rsp), %rdi
movl $0x2, %esi
callq 0xb270
movq %rax, 0x40(%rsp)
jmp 0xa843
movq 0x160(%rsp), %rdi
movq 0x40(%rsp), %rax
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x2c8(%rsp), %eax
movl %eax, %esi
callq 0xbd40
movsd (%rax), %xmm0
movsd %xmm0, 0x320(%rsp)
movsd 0x2b8(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x2c8(%rsp), %eax
movl %eax, %esi
xorl %eax, %eax
movl %eax, %edx
leaq 0x2d0(%rsp), %rdi
callq 0xb2a0
movq %rax, 0x30(%rsp)
jmp 0xa8a5
movq 0x30(%rsp), %rax
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x2b0(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x2c8(%rsp), %eax
movl %eax, %esi
leaq 0x2d0(%rsp), %rdi
movl $0x1, %edx
callq 0xb2a0
movq %rax, 0x20(%rsp)
jmp 0xa8e5
movq 0x20(%rsp), %rax
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x2a8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x2c8(%rsp), %eax
movl %eax, %esi
leaq 0x2d0(%rsp), %rdi
movl $0x2, %edx
callq 0xb2a0
movq %rax, 0x10(%rsp)
jmp 0xa925
movq 0x10(%rsp), %rax
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x2c8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c8(%rsp)
jmp 0x9f49
movq 0x168(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0xc450
jmp 0xa961
movl $0x1, 0x17c(%rsp)
leaq 0x2d0(%rsp), %rdi
callq 0xb2d0
leaq 0x308(%rsp), %rdi
callq 0xb5b0
leaq 0x328(%rsp), %rdi
callq 0xb5b0
movq 0x170(%rsp), %rax
addq $0x368, %rsp            # imm = 0x368
retq
leaq 0x2d0(%rsp), %rdi
callq 0xb2d0
leaq 0x308(%rsp), %rdi
callq 0xb5b0
leaq 0x328(%rsp), %rdi
callq 0xb5b0
movq 0x300(%rsp), %rdi
callq 0x8870
nopw (%rax,%rax)