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

  // Get useful variables
  RNScalar neardist = 0.01 * scene->BBox().DiagonalRadius();
  RNScalar fardist = 100 * scene->BBox().DiagonalRadius();
  RNScalar aspect = (RNScalar) height / (RNScalar) width;
  RNAngle yfov = atan(aspect * tan(xfov));
  RNScalar dd = position_sampling * position_sampling;
  if (RNIsZero(dd)) dd = 0.1;

  // Create kdtree for storing cameras
  R3Box bbox = scene->BBox();  bbox.Inflate(2.0);
  R3Kdtree<Camera *> kdtree(bbox, CameraPosition);
  
  // Sample positions on surface
  for (int i = 0; i < scene->NNodes(); i++) {
    R3SceneNode *node = scene->Node(i);
    if (node->NChildren() > 0) continue;
    for (int j = 0; j < node->NElements(); j++) {
      R3SceneElement *element = node->Element(j);
      for (int k = 0; k < element->NShapes(); k++) {
        R3Shape *shape = element->Shape(k);
        if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
          R3TriangleArray *triangles = (R3TriangleArray *) shape;
          for (int t = 0; t < triangles->NTriangles(); t++) {
            R3Triangle *triangle = triangles->Triangle(t);
            RNScalar real_nsamples = triangle->Area() / dd;
            int nsamples = (int) real_nsamples;
            if (RNRandomScalar() < (real_nsamples - nsamples)) nsamples++;
            for (int s = 0; s < nsamples; s++) {
              R3Point position = triangle->RandomPoint();
              R3Vector normal = triangle->Normal();
              triangle_count++;

              // Get randomized towards direction
              R3Vector towards = -normal;
              towards.XRotate(RNRandomScalar() * max_surface_normal_angle);
              towards.YRotate(RNRandomScalar() * max_surface_normal_angle);
              towards.ZRotate(RNRandomScalar() * max_surface_normal_angle);
              towards.Normalize();

              // Compute view distance
              R3Ray ray(position, -towards);
              RNScalar hit_t = FLT_MAX;
              RNLength surface_distance = min_surface_distance + RNRandomScalar() * (max_surface_distance - min_surface_distance);
              if (0 || scene->Intersects(ray, NULL, NULL, NULL, NULL, NULL, &hit_t, RN_EPSILON, surface_distance)) {
                if (0.9*hit_t < surface_distance) surface_distance = 0.9*hit_t;
                if (surface_distance < min_surface_distance) continue;
              }

              // Compute camera
              R3Point viewpoint = position - surface_distance * towards;
              R3Vector right = towards % R3xyz_triad[gravity_dimension];
              right.Normalize();
              R3Vector up = right % towards;
              up.Normalize();
              R3Camera c(viewpoint, towards, up, xfov, yfov, neardist, fardist);
              
              // Compute score for camera
              c.SetValue(SceneCoverageScore(c, scene));
              if (c.Value() <= 0) continue;
              if (c.Value() < min_score) continue;

              // Create new camera
              char name[1024];
              sprintf(name, "C_%d", triangle_count);
              Camera *camera = new Camera(c, name);
              if (!camera) continue;

              // Check if overlapping with any previous camera
              if (kdtree.FindAny(camera, 0.0, position_sampling, IsDifferentCameraOrientation, NULL)) {
                delete camera;
                continue;
              }
              
              // Insert camera 
              if (print_debug) printf("SURFACE %d : %g\n", triangle_count, c.Value());
              kdtree.InsertPoint(camera);
              cameras.Insert(camera);
              camera_count++;
            }
          }
        }
      }
    }
  }
        
  // Print statistics
  if (print_verbose) {
    printf("Created surface cameras ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Cameras = %d\n", camera_count++);
    fflush(stdout);
  }
}

// Assembly:
subq $0x7c8, %rsp            # imm = 0x7C8
leaq 0x7b8(%rsp), %rdi
callq 0x11bad0
leaq 0x7b8(%rsp), %rdi
callq 0x321a0
movl $0x0, 0x7b4(%rsp)
movl $0x0, 0x7b0(%rsp)
movq 0x204ffa(%rip), %rdi    # 0x22fc38
callq 0x32290
movq %rax, %rdi
callq 0x322b0
movaps %xmm0, %xmm1
movsd 0x1903ca(%rip), %xmm0   # 0x1bb020
mulsd %xmm1, %xmm0
movsd %xmm0, 0x7a8(%rsp)
movq 0x204fce(%rip), %rdi    # 0x22fc38
callq 0x32290
movq %rax, %rdi
callq 0x322b0
movaps %xmm0, %xmm1
movsd 0x190396(%rip), %xmm0   # 0x1bb018
mulsd %xmm1, %xmm0
movsd %xmm0, 0x7a0(%rsp)
cvtsi2sdl 0x1fedf5(%rip), %xmm0 # 0x229a8c
cvtsi2sdl 0x1fede9(%rip), %xmm1 # 0x229a88
divsd %xmm1, %xmm0
movsd %xmm0, 0x798(%rsp)
movsd 0x798(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0x1fedca(%rip), %xmm0   # 0x229a90
callq 0x234f0
movaps %xmm0, %xmm1
movsd 0xe0(%rsp), %xmm0
mulsd %xmm1, %xmm0
callq 0x237e0
movsd %xmm0, 0x790(%rsp)
movsd 0x1fedd7(%rip), %xmm0   # 0x229ac8
mulsd 0x1fedcf(%rip), %xmm0   # 0x229ac8
movsd %xmm0, 0x788(%rsp)
movsd 0x788(%rsp), %xmm0
leaq 0x1ff576(%rip), %rax    # 0x22a288
movsd (%rax), %xmm1
callq 0x33240
cmpl $0x0, %eax
je 0x2ad31
movsd 0x190328(%rip), %xmm0   # 0x1bb050
movsd %xmm0, 0x788(%rsp)
movq 0x204f00(%rip), %rdi    # 0x22fc38
callq 0x32290
movq %rax, %rsi
leaq 0x750(%rsp), %rdi
movq %rdi, 0xd8(%rsp)
callq 0xc4a10
movq 0xd8(%rsp), %rdi
movsd 0x1902ab(%rip), %xmm0   # 0x1bb010
callq 0xc64e0
jmp 0x2ad6c
leaq 0x4ffd(%rip), %rdx      # 0x2fd70
xorl %eax, %eax
movl %eax, %ecx
leaq 0x6e0(%rsp), %rdi
leaq 0x750(%rsp), %rsi
callq 0x33750
jmp 0x2ad8e
movl $0x0, 0x6dc(%rsp)
movl 0x6dc(%rsp), %eax
movl %eax, 0xd0(%rsp)
movq 0x204e8a(%rip), %rdi    # 0x22fc38
callq 0x321c0
movl %eax, 0xd4(%rsp)
jmp 0x2adbc
movl 0xd0(%rsp), %eax
movl 0xd4(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2b61d
movq 0x204e5f(%rip), %rdi    # 0x22fc38
movl 0x6dc(%rsp), %esi
callq 0x32430
movq %rax, 0xc8(%rsp)
jmp 0x2adef
movq 0xc8(%rsp), %rax
movq %rax, 0x6d0(%rsp)
movq 0x6d0(%rsp), %rdi
callq 0x33610
cmpl $0x0, %eax
jle 0x2ae48
jmp 0x2b607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x748(%rsp)
movl %eax, 0x744(%rsp)
jmp 0x2b6c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x748(%rsp)
movl %eax, 0x744(%rsp)
jmp 0x2b6b8
movl $0x0, 0x6cc(%rsp)
movl 0x6cc(%rsp), %eax
movl %eax, 0xc4(%rsp)
movq 0x6d0(%rsp), %rdi
callq 0x32480
movl %eax, %ecx
movl 0xc4(%rsp), %eax
cmpl %ecx, %eax
jge 0x2b605
movq 0x6d0(%rsp), %rdi
movl 0x6cc(%rsp), %esi
callq 0x331c0
movq %rax, 0xb8(%rsp)
jmp 0x2ae9d
movq 0xb8(%rsp), %rax
movq %rax, 0x6c0(%rsp)
movl $0x0, 0x6bc(%rsp)
movl 0x6bc(%rsp), %eax
movl %eax, 0xb4(%rsp)
movq 0x6c0(%rsp), %rdi
callq 0x331f0
movl %eax, %ecx
movl 0xb4(%rsp), %eax
cmpl %ecx, %eax
jge 0x2b5ed
movq 0x6c0(%rsp), %rdi
movl 0x6bc(%rsp), %esi
callq 0x33210
movq %rax, 0xa8(%rsp)
jmp 0x2af02
movq 0xa8(%rsp), %rax
movq %rax, 0x6b0(%rsp)
movq 0x6b0(%rsp), %rdi
movq (%rdi), %rax
movq 0xb8(%rax), %rax
callq *%rax
movl %eax, 0xa4(%rsp)
jmp 0x2af2f
callq 0xb9d80
movl %eax, 0xa0(%rsp)
jmp 0x2af3d
movl 0xa4(%rsp), %eax
movl 0xa0(%rsp), %ecx
cmpl %ecx, %eax
jne 0x2b5d5
movq 0x6b0(%rsp), %rax
movq %rax, 0x6a8(%rsp)
movl $0x0, 0x6a4(%rsp)
movl 0x6a4(%rsp), %eax
movl %eax, 0x9c(%rsp)
movq 0x6a8(%rsp), %rdi
callq 0x332a0
movl %eax, %ecx
movl 0x9c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2b5d3
movq 0x6a8(%rsp), %rdi
movl 0x6a4(%rsp), %esi
callq 0x332c0
movq %rax, 0x90(%rsp)
jmp 0x2afb8
movq 0x90(%rsp), %rax
movq %rax, 0x698(%rsp)
movq 0x698(%rsp), %rdi
movq (%rdi), %rax
movq 0x58(%rax), %rax
callq *%rax
movsd %xmm0, 0x88(%rsp)
jmp 0x2afe4
movsd 0x88(%rsp), %xmm0
movsd 0x788(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x690(%rsp)
cvttsd2si 0x690(%rsp), %eax
movl %eax, 0x68c(%rsp)
callq 0x11f4d0
movsd %xmm0, 0x80(%rsp)
jmp 0x2b023
movsd 0x80(%rsp), %xmm1
movsd 0x690(%rsp), %xmm0
cvtsi2sdl 0x68c(%rsp), %xmm2
subsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2b059
movl 0x68c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68c(%rsp)
movl $0x0, 0x688(%rsp)
movl 0x688(%rsp), %eax
cmpl 0x68c(%rsp), %eax
jge 0x2b5bb
movq 0x698(%rsp), %rsi
movq (%rsi), %rax
movq 0x218(%rax), %rax
leaq 0x670(%rsp), %rdi
callq *%rax
jmp 0x2b096
movq 0x698(%rsp), %rdi
callq 0x33830
movq %rax, 0x78(%rsp)
jmp 0x2b0aa
movq 0x78(%rsp), %rsi
leaq 0x658(%rsp), %rdi
callq 0xdb0e0
jmp 0x2b0be
movl 0x7b4(%rsp), %eax
incl %eax
movl %eax, 0x7b4(%rsp)
leaq 0x640(%rsp), %rdi
leaq 0x658(%rsp), %rsi
callq 0xdc390
jmp 0x2b0e5
callq 0x11f4d0
movsd %xmm0, 0x70(%rsp)
jmp 0x2b0f2
movsd 0x70(%rsp), %xmm0
movsd 0x1fe9a8(%rip), %xmm1   # 0x229aa8
mulsd %xmm1, %xmm0
leaq 0x640(%rsp), %rdi
callq 0xdb9f0
jmp 0x2b113
callq 0x11f4d0
movsd %xmm0, 0x68(%rsp)
jmp 0x2b120
movsd 0x68(%rsp), %xmm0
movsd 0x1fe97a(%rip), %xmm1   # 0x229aa8
mulsd %xmm1, %xmm0
leaq 0x640(%rsp), %rdi
callq 0xdbab0
jmp 0x2b141
callq 0x11f4d0
movsd %xmm0, 0x60(%rsp)
jmp 0x2b14e
movsd 0x60(%rsp), %xmm0
movsd 0x1fe94c(%rip), %xmm1   # 0x229aa8
mulsd %xmm1, %xmm0
leaq 0x640(%rsp), %rdi
callq 0xdbb70
jmp 0x2b16f
leaq 0x640(%rsp), %rdi
callq 0xdb800
jmp 0x2b17e
leaq 0x5f8(%rsp), %rdi
leaq 0x640(%rsp), %rsi
callq 0xdc390
jmp 0x2b195
leaq 0x610(%rsp), %rdi
leaq 0x670(%rsp), %rsi
leaq 0x5f8(%rsp), %rdx
xorl %ecx, %ecx
callq 0xd9830
jmp 0x2b1b6
movabsq $0x47efffffe0000000, %rax # imm = 0x47EFFFFFE0000000
movq %rax, 0x5f0(%rsp)
movsd 0x1fe8e0(%rip), %xmm0   # 0x229ab0
movsd %xmm0, 0x50(%rsp)
callq 0x11f4d0
movsd %xmm0, 0x58(%rsp)
jmp 0x2b1e3
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
movsd 0x1fe8c1(%rip), %xmm2   # 0x229ab8
movsd 0x1fe8b1(%rip), %xmm3   # 0x229ab0
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x5e8(%rsp)
movq 0x204a1d(%rip), %rdi    # 0x22fc38
leaq 0x1ff066(%rip), %rax    # 0x22a288
movsd (%rax), %xmm0
movsd 0x5e8(%rsp), %xmm1
movq %rsp, %rax
leaq 0x5f0(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq $0x0, (%rax)
xorl %eax, %eax
movl %eax, %r9d
leaq 0x610(%rsp), %rsi
movq %r9, %rdx
movq %r9, %rcx
movq %r9, %r8
callq 0x388b0
movl %eax, 0x4c(%rsp)
jmp 0x2b266
movl 0x4c(%rsp), %eax
cmpl $0x0, %eax
je 0x2b2c7
movsd 0x18fde1(%rip), %xmm1   # 0x1bb058
mulsd 0x5f0(%rsp), %xmm1
movsd 0x5e8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2b2a9
movsd 0x18fdc1(%rip), %xmm0   # 0x1bb058
mulsd 0x5f0(%rsp), %xmm0
movsd %xmm0, 0x5e8(%rsp)
movsd 0x5e8(%rsp), %xmm1
movsd 0x1fe7f6(%rip), %xmm0   # 0x229ab0
ucomisd %xmm1, %xmm0
jbe 0x2b2c5
jmp 0x2b5a5
jmp 0x2b2c7
movsd 0x5e8(%rsp), %xmm0
leaq 0x5b8(%rsp), %rdi
leaq 0x640(%rsp), %rsi
callq 0x324a0
jmp 0x2b2e7
leaq 0x5d0(%rsp), %rdi
leaq 0x670(%rsp), %rsi
leaq 0x5b8(%rsp), %rdx
callq 0xdaea0
jmp 0x2b306
movl 0x1fe778(%rip), %esi    # 0x229a84
leaq 0x20d795(%rip), %rdi    # 0x238aa8
callq 0x32530
movq %rax, 0x40(%rsp)
jmp 0x2b31f
movq 0x40(%rsp), %rdx
leaq 0x5a0(%rsp), %rdi
leaq 0x640(%rsp), %rsi
callq 0xdbee0
jmp 0x2b33b
leaq 0x5a0(%rsp), %rdi
callq 0xdb800
jmp 0x2b34a
leaq 0x588(%rsp), %rdi
leaq 0x5a0(%rsp), %rsi
leaq 0x640(%rsp), %rdx
callq 0xdbee0
jmp 0x2b369
leaq 0x588(%rsp), %rdi
callq 0xdb800
jmp 0x2b378
movsd 0x1fe710(%rip), %xmm0   # 0x229a90
movsd 0x790(%rsp), %xmm1
movsd 0x7a8(%rsp), %xmm2
movsd 0x7a0(%rsp), %xmm3
leaq 0x4f8(%rsp), %rdi
leaq 0x5d0(%rsp), %rsi
leaq 0x640(%rsp), %rdx
leaq 0x588(%rsp), %rcx
callq 0x67810
jmp 0x2b3c2
movq 0x20486f(%rip), %rsi    # 0x22fc38
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0x4f8(%rsp), %rdi
callq 0x2f130
movsd %xmm0, 0x38(%rsp)
jmp 0x2b3e2
movsd 0x38(%rsp), %xmm0
leaq 0x4f8(%rsp), %rdi
callq 0x32350
leaq 0x4f8(%rsp), %rdi
callq 0x32550
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jb 0x2b413
jmp 0x2b5a5
leaq 0x4f8(%rsp), %rdi
callq 0x32550
movaps %xmm0, %xmm1
movsd 0x2047c5(%rip), %xmm0   # 0x22fbf0
ucomisd %xmm1, %xmm0
jbe 0x2b436
jmp 0x2b5a5
movl 0x7b4(%rsp), %edx
leaq 0x190266(%rip), %rsi    # 0x1bb6aa
xorl %eax, %eax
leaq 0xf0(%rsp), %rdi
callq 0x23150
movl $0x98, %edi
callq 0x239b0
movq %rax, 0x30(%rsp)
jmp 0x2b464
movq 0x30(%rsp), %rdi
leaq 0x4f8(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
callq 0x32570
jmp 0x2b480
movq 0x30(%rsp), %rax
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0xe8(%rsp)
jne 0x2b4c5
jmp 0x2b5a5
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x748(%rsp)
movl %eax, 0x744(%rsp)
movl $0x98, %esi
callq 0x239d0
jmp 0x2b6b8
movq 0xe8(%rsp), %rsi
movsd 0x1fe5f3(%rip), %xmm1   # 0x229ac8
leaq 0x48d4(%rip), %rdx      # 0x2fdb0
xorl %eax, %eax
movl %eax, %ecx
leaq 0x6e0(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x33850
movq %rax, 0x28(%rsp)
jmp 0x2b4f7
movq 0x28(%rsp), %rax
cmpq $0x0, %rax
je 0x2b530
movq 0xe8(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x2b52e
movq 0x20(%rsp), %rdi
callq 0x33970
movq 0x20(%rsp), %rdi
movl $0x98, %esi
callq 0x239d0
jmp 0x2b5a5
cmpl $0x0, 0x204681(%rip)    # 0x22fbb8
je 0x2b567
movl 0x7b4(%rsp), %eax
movl %eax, 0x1c(%rsp)
leaq 0x4f8(%rsp), %rdi
callq 0x32550
movl 0x1c(%rsp), %esi
leaq 0x190153(%rip), %rdi    # 0x1bb6af
movb $0x1, %al
callq 0x23090
jmp 0x2b565
jmp 0x2b567
movq 0xe8(%rsp), %rsi
leaq 0x6e0(%rsp), %rdi
callq 0x339b0
jmp 0x2b57e
movq 0xe8(%rsp), %rsi
leaq 0x2045f3(%rip), %rdi    # 0x22fb80
callq 0x32370
jmp 0x2b594
movl 0x7b0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7b0(%rsp)
movl 0x688(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x688(%rsp)
jmp 0x2b064
jmp 0x2b5bd
movl 0x6a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6a4(%rsp)
jmp 0x2af6e
jmp 0x2b5d5
jmp 0x2b5d7
movl 0x6bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6bc(%rsp)
jmp 0x2aeb8
jmp 0x2b5ef
movl 0x6cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6cc(%rsp)
jmp 0x2ae53
jmp 0x2b607
movl 0x6dc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6dc(%rsp)
jmp 0x2ad99
cmpl $0x0, 0x204590(%rip)    # 0x22fbb4
je 0x2b696
leaq 0x190093(%rip), %rdi    # 0x1bb6c0
xorl %eax, %eax
callq 0x23090
jmp 0x2b636
leaq 0x7b8(%rsp), %rdi
callq 0x11bb90
movsd %xmm0, 0x10(%rsp)
jmp 0x2b64b
movsd 0x10(%rsp), %xmm0
leaq 0x18fde2(%rip), %rdi    # 0x1bb43a
movb $0x1, %al
callq 0x23090
jmp 0x2b661
movl 0x7b0(%rsp), %esi
movl %esi, %eax
incl %eax
movl %eax, 0x7b0(%rsp)
leaq 0x18fe85(%rip), %rdi    # 0x1bb4ff
xorl %eax, %eax
callq 0x23090
jmp 0x2b683
movq 0x1fd906(%rip), %rax    # 0x228f90
movq (%rax), %rdi
callq 0x23bc0
jmp 0x2b694
jmp 0x2b696
leaq 0x6e0(%rsp), %rdi
callq 0x339f0
leaq 0x750(%rsp), %rdi
callq 0x33500
addq $0x7c8, %rsp            # imm = 0x7C8
retq
leaq 0x6e0(%rsp), %rdi
callq 0x339f0
leaq 0x750(%rsp), %rdi
callq 0x33500
movq 0x748(%rsp), %rdi
callq 0x24250
nop