// File: tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: CreateWorldInHandCameras()
static void
CreateWorldInHandCameras(void)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  int camera_count = 0;

  // Get useful variables
  R3Point centroid = scene->Centroid();
  RNScalar radius = scene->BBox().DiagonalRadius();
  RNScalar neardist = 0.01 * radius;
  RNScalar fardist = 100 * radius;
  RNScalar aspect = (RNScalar) height / (RNScalar) width;
  RNAngle yfov = atan(aspect * tan(xfov));
  RNLength distance = 2.5 * radius;

  // Determine number of cameras
  int ncameras = 0;
  if (position_sampling > 0) {
    RNArea area_of_viewpoint_sphere = 4.0 * RN_PI * distance * distance;
    RNScalar area_per_camera = 4.0 * position_sampling * position_sampling;
    int position_ncameras = (int) (area_of_viewpoint_sphere / area_per_camera + 0.5);
    if (position_ncameras > ncameras) ncameras = position_ncameras;
  }
  if (angle_sampling > 0) {
    RNArea area_of_viewpoint_sphere = 4.0 * RN_PI * distance * distance;
    RNScalar arc_length = (distance * angle_sampling);
    RNScalar area_per_camera = arc_length * arc_length;
    int angle_ncameras = (int) (area_of_viewpoint_sphere / area_per_camera + 0.5);
    if (angle_ncameras > ncameras) ncameras = angle_ncameras;
  }
  if (ncameras == 0) ncameras = 1024;
  
  // Create cameras at random directions from viewpoint sphere looking at centroid
  for (int i = 0; i < ncameras; i++) {
    // Compute view directions
    R3Vector towards = R3RandomDirection();
    towards.Normalize();
    R3Vector right = towards % R3xyz_triad[gravity_dimension];
    if (RNIsZero(right.Length())) continue;
    right.Normalize();
    R3Vector up = right % towards;
    if (RNIsZero(up.Length())) continue;
    up.Normalize();

    // Compute eyepoint
    RNScalar d = distance + (2.0*RNRandomScalar()-1.0) * position_sampling;
    R3Point viewpoint = centroid - d * towards;

    // Compute name
    char name[1024];
    sprintf(name, "WORLDINHAND#%d", i);

    // Create camera
    R3Camera c(viewpoint, towards, up, xfov, yfov, neardist, fardist);
    Camera *camera = new Camera(c, name);
    cameras.Insert(camera);
    camera_count++;
  }

  // Print statistics
  if (print_verbose) {
    printf("Created world in hand cameras ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Cameras = %d\n", camera_count++);
    fflush(stdout);
  }
}

// Assembly:
subq $0x618, %rsp            # imm = 0x618
leaq 0x608(%rsp), %rdi
callq 0x11bad0
leaq 0x608(%rsp), %rdi
callq 0x321a0
movl $0x0, 0x604(%rsp)
movq 0x204525(%rip), %rsi    # 0x22fc38
leaq 0x5e8(%rsp), %rdi
callq 0x35980
movq 0x204511(%rip), %rdi    # 0x22fc38
callq 0x32290
movq %rax, %rdi
callq 0x322b0
movsd %xmm0, 0x5e0(%rsp)
movsd 0x18f8db(%rip), %xmm0   # 0x1bb020
mulsd 0x5e0(%rsp), %xmm0
movsd %xmm0, 0x5d8(%rsp)
movsd 0x18f8b9(%rip), %xmm0   # 0x1bb018
mulsd 0x5e0(%rsp), %xmm0
movsd %xmm0, 0x5d0(%rsp)
cvtsi2sdl 0x1fe313(%rip), %xmm0 # 0x229a8c
cvtsi2sdl 0x1fe307(%rip), %xmm1 # 0x229a88
divsd %xmm1, %xmm0
movsd %xmm0, 0x5c8(%rsp)
movsd 0x5c8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x1fe2eb(%rip), %xmm0   # 0x229a90
callq 0x234f0
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
mulsd %xmm1, %xmm0
callq 0x237e0
movsd %xmm0, 0x5c0(%rsp)
movsd 0x18f893(%rip), %xmm0   # 0x1bb060
mulsd 0x5e0(%rsp), %xmm0
movsd %xmm0, 0x5b8(%rsp)
movl $0x0, 0x5b4(%rsp)
movsd 0x1fe2d6(%rip), %xmm0   # 0x229ac8
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2b897
movsd 0x18f861(%rip), %xmm0   # 0x1bb068
leaq 0x193dba(%rip), %rax    # 0x1bf5c8
mulsd (%rax), %xmm0
mulsd 0x5b8(%rsp), %xmm0
mulsd 0x5b8(%rsp), %xmm0
movsd %xmm0, 0x5a8(%rsp)
movsd 0x18f833(%rip), %xmm0   # 0x1bb068
mulsd 0x1fe28b(%rip), %xmm0   # 0x229ac8
mulsd 0x1fe283(%rip), %xmm0   # 0x229ac8
movsd %xmm0, 0x5a0(%rsp)
movsd 0x5a8(%rsp), %xmm0
divsd 0x5a0(%rsp), %xmm0
movsd 0x18f7c8(%rip), %xmm1   # 0x1bb030
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x59c(%rsp)
movl 0x59c(%rsp), %eax
cmpl 0x5b4(%rsp), %eax
jle 0x2b895
movl 0x59c(%rsp), %eax
movl %eax, 0x5b4(%rsp)
jmp 0x2b897
movsd 0x204361(%rip), %xmm0   # 0x22fc00
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2b958
movsd 0x18f7b4(%rip), %xmm0   # 0x1bb068
leaq 0x193d0d(%rip), %rax    # 0x1bf5c8
mulsd (%rax), %xmm0
mulsd 0x5b8(%rsp), %xmm0
mulsd 0x5b8(%rsp), %xmm0
movsd %xmm0, 0x590(%rsp)
movsd 0x5b8(%rsp), %xmm0
mulsd 0x204315(%rip), %xmm0   # 0x22fc00
movsd %xmm0, 0x588(%rsp)
movsd 0x588(%rsp), %xmm0
mulsd 0x588(%rsp), %xmm0
movsd %xmm0, 0x580(%rsp)
movsd 0x590(%rsp), %xmm0
divsd 0x580(%rsp), %xmm0
movsd 0x18f707(%rip), %xmm1   # 0x1bb030
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x57c(%rsp)
movl 0x57c(%rsp), %eax
cmpl 0x5b4(%rsp), %eax
jle 0x2b956
movl 0x57c(%rsp), %eax
movl %eax, 0x5b4(%rsp)
jmp 0x2b958
cmpl $0x0, 0x5b4(%rsp)
jne 0x2b96d
movl $0x400, 0x5b4(%rsp) # imm = 0x400
movl $0x0, 0x578(%rsp)
movl 0x578(%rsp), %eax
cmpl 0x5b4(%rsp), %eax
jge 0x2bbcc
leaq 0x560(%rsp), %rdi
callq 0xdc640
leaq 0x560(%rsp), %rdi
callq 0xdb800
movl 0x1fe0d8(%rip), %esi    # 0x229a84
leaq 0x20d0f5(%rip), %rdi    # 0x238aa8
callq 0x32530
movq %rax, %rdx
leaq 0x548(%rsp), %rdi
leaq 0x560(%rsp), %rsi
callq 0xdbee0
leaq 0x548(%rsp), %rdi
callq 0xdb280
leaq 0x1fe8a4(%rip), %rax    # 0x22a288
movsd (%rax), %xmm1
callq 0x33240
cmpl $0x0, %eax
je 0x2b9f7
jmp 0x2bb97
leaq 0x548(%rsp), %rdi
callq 0xdb800
leaq 0x530(%rsp), %rdi
leaq 0x548(%rsp), %rsi
leaq 0x560(%rsp), %rdx
callq 0xdbee0
leaq 0x530(%rsp), %rdi
callq 0xdb280
leaq 0x1fe853(%rip), %rax    # 0x22a288
movsd (%rax), %xmm1
callq 0x33240
cmpl $0x0, %eax
je 0x2ba48
jmp 0x2bb97
leaq 0x530(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xdb800
movsd 0x5b8(%rsp), %xmm0
movsd %xmm0, (%rsp)
callq 0x11f4d0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm0
movsd 0x18f5f2(%rip), %xmm2   # 0x1bb070
addsd %xmm2, %xmm0
movsd 0x1fe03e(%rip), %xmm2   # 0x229ac8
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x528(%rsp)
movsd 0x528(%rsp), %xmm0
leaq 0x4f8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x560(%rsp), %rsi
movq %rsi, 0x18(%rsp)
callq 0x324a0
movq 0x8(%rsp), %rdx
leaq 0x510(%rsp), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x5e8(%rsp), %rsi
callq 0xdaea0
movl 0x578(%rsp), %edx
leaq 0x18fc08(%rip), %rsi    # 0x1bb6f8
xorl %eax, %eax
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x23150
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movsd 0x1fdf75(%rip), %xmm0   # 0x229a90
movsd 0x5c0(%rsp), %xmm1
movsd 0x5d8(%rsp), %xmm2
movsd 0x5d0(%rsp), %xmm3
leaq 0x60(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x67810
movl $0x98, %edi
callq 0x239b0
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
callq 0x32570
jmp 0x2bb6b
movq 0x38(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rsi
leaq 0x203fff(%rip), %rdi    # 0x22fb80
callq 0x32370
movl 0x604(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x604(%rsp)
movl 0x578(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x578(%rsp)
jmp 0x2b978
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
movl $0x98, %esi
callq 0x239d0
jmp 0x2bc36
cmpl $0x0, 0x203fe1(%rip)    # 0x22fbb4
je 0x2bc2e
leaq 0x18fb2b(%rip), %rdi    # 0x1bb707
movb $0x0, %al
callq 0x23090
leaq 0x608(%rsp), %rdi
callq 0x11bb90
leaq 0x18f843(%rip), %rdi    # 0x1bb43a
movb $0x1, %al
callq 0x23090
movl 0x604(%rsp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, 0x604(%rsp)
leaq 0x18f8e7(%rip), %rdi    # 0x1bb4ff
movb $0x0, %al
callq 0x23090
movq 0x1fd36a(%rip), %rax    # 0x228f90
movq (%rax), %rdi
callq 0x23bc0
addq $0x618, %rsp            # imm = 0x618
retq
movq 0x50(%rsp), %rdi
callq 0x24250