// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: ComputeRayTracingProperties(gaps::R3Mesh*)
static R3MeshPropertySet *
ComputeRayTracingProperties(R3Mesh *mesh)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Computing ray tracing properties ...\n");
    fflush(stdout);
  }

  // Allocate property set
  R3MeshPropertySet *properties = new R3MeshPropertySet(mesh);
  if (!properties) {
    RNFail("Unable to allocate property set.\n");
    return NULL;
  }

  // Allocate/insert properties
  R3MeshProperty *median_property = new R3MeshProperty(mesh, "RayLengthMedian");
  R3MeshProperty *ten_property = new R3MeshProperty(mesh, "RayLengthTen");
  R3MeshProperty *ninety_property = new R3MeshProperty(mesh, "RayLengthNinety");
  R3MeshProperty *coverage_property = new R3MeshProperty(mesh, "RayCoverage");
    
  // Compute properties based on intersections of random rays
  const int nphis = 8;
  const int nthetas = 8;
  const int num_rays = nphis * nthetas;
  double interior_distances[num_rays];
  for (int i = 0; i < mesh->NVertices(); i++) {
    // Get vertex info
    R3MeshVertex *vertex = mesh->Vertex(i);
    const R3Point& vertex_position = mesh->VertexPosition(vertex);
    const R3Vector& vertex_normal = mesh->VertexNormal(vertex);
    R3Vector phi_rotation_axis = vertex_normal % R3xyz_triad.Axis(vertex_normal.MinDimension());
    R3Vector theta_rotation_axis = vertex_normal;

    // Compute intersections of mesh with random rays from vertex 
    int num_intersections = 0;
    int num_interior_distances = 0;
    for (int j = 0; j < nthetas; j++) {
      RNAngle theta = (j+RNRandomScalar()) * RN_TWO_PI / nthetas;
      for (int k = 0; k < nphis; k++) {
        RNAngle phi = (k+RNRandomScalar()) * RN_PI / nphis;

        // Compute ray
        R3Vector ray_direction = vertex_normal;
        ray_direction.Rotate(phi_rotation_axis, phi);
        ray_direction.Rotate(theta_rotation_axis, theta);
        R3Point ray_source_position = vertex_position + 1000 * RN_EPSILON * ray_direction;
        R3Ray ray(ray_source_position, ray_direction);

        // Compute ray intersection
        R3MeshIntersection intersection;
        if (mesh->Intersection(ray, &intersection)) {
          num_intersections++;
          const R3Vector& face_normal = mesh->FaceNormal(intersection.face);
          if (ray_direction.Dot(face_normal) > 0) {
            interior_distances[num_interior_distances] = intersection.t;
            num_interior_distances++;
          }
        }
      }
    }

    // Compute properties
    median_property->SetVertexValue(i, Median(interior_distances, num_interior_distances));
    ten_property->SetVertexValue(i, Percentile(interior_distances, num_interior_distances, 10));
    ninety_property->SetVertexValue(i, Percentile(interior_distances, num_interior_distances, 90));
    coverage_property->SetVertexValue(i, (RNScalar) num_intersections / (RNScalar) num_rays);
  }

  // Blur the properties to reduce effects of undersampling
  RNScalar sigma = Sigma(mesh);
  median_property->Blur(sigma);
  ten_property->Blur(sigma);
  ninety_property->Blur(sigma);
  coverage_property->Blur(sigma);

  // Insert the properties
  InsertProperty(properties, median_property);
  InsertProperty(properties, ten_property);
  InsertProperty(properties, ninety_property);
  InsertProperty(properties, coverage_property);

  // Print statistics
  if (print_verbose) {
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Properties = %d\n", properties->NProperties());
    fflush(stdout);
  }

  // Return property set
  return properties;
}

// Assembly:
subq $0x428, %rsp            # imm = 0x428
movq %rdi, 0x418(%rsp)
leaq 0x408(%rsp), %rdi
callq 0xe98e0
leaq 0x408(%rsp), %rdi
callq 0x1f280
cmpl $0x0, 0x1afdd0(%rip)    # 0x1ca740
je 0x1a98f
leaq 0x159c6d(%rip), %rdi    # 0x1745e6
movb $0x0, %al
callq 0x11090
movq 0x1a6609(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movl $0x18, %edi
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x78(%rsp)
movq 0x418(%rsp), %rsi
callq 0x24ce0
jmp 0x1a9b3
movq 0x78(%rsp), %rax
movq %rax, 0x400(%rsp)
cmpq $0x0, 0x400(%rsp)
jne 0x1aa12
leaq 0x159884(%rip), %rdi    # 0x174256
movb $0x0, %al
callq 0xedb00
movq $0x0, 0x420(%rsp)
jmp 0x1b133
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3f8(%rsp)
movl %eax, 0x3f4(%rsp)
movl $0x18, %esi
callq 0x11690
jmp 0x1b143
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
movq 0x418(%rsp), %rsi
leaq 0x159bd6(%rip), %rdx    # 0x17460c
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1aa41
movq 0x70(%rsp), %rax
movq %rax, 0x3e8(%rsp)
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x68(%rsp)
movq 0x418(%rsp), %rsi
leaq 0x159baa(%rip), %rdx    # 0x17461c
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1aa7d
movq 0x68(%rsp), %rax
movq %rax, 0x3e0(%rsp)
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x60(%rsp)
movq 0x418(%rsp), %rsi
leaq 0x159b7b(%rip), %rdx    # 0x174629
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1aab9
movq 0x60(%rsp), %rax
movq %rax, 0x3d8(%rsp)
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x58(%rsp)
movq 0x418(%rsp), %rsi
leaq 0x159b4f(%rip), %rdx    # 0x174639
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1aaf5
movq 0x58(%rsp), %rax
movq %rax, 0x3d0(%rsp)
movl $0x8, 0x3cc(%rsp)
movl $0x8, 0x3c8(%rsp)
movl $0x40, 0x3c4(%rsp)
movl $0x0, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x418(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x1b009
movq 0x418(%rsp), %rdi
movl 0x1bc(%rsp), %esi
callq 0x1f380
movq %rax, 0x1b0(%rsp)
movq 0x418(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
callq 0x1f4b0
movq %rax, 0x1a8(%rsp)
movq 0x418(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
callq 0x1f4d0
movq %rax, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x896a0
movl %eax, %esi
leaq 0x1b014b(%rip), %rdi    # 0x1cad18
callq 0x1fff0
movq 0x48(%rsp), %rsi
movq %rax, %rdx
leaq 0x188(%rsp), %rdi
callq 0x8a280
movq 0x1a0(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0x89480
movl $0x0, 0x16c(%rsp)
movl $0x0, 0x168(%rsp)
movl $0x0, 0x164(%rsp)
cmpl $0x8, 0x164(%rsp)
jge 0x1af0e
cvtsi2sdl 0x164(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
callq 0xed2e0
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
addsd %xmm1, %xmm0
leaq 0x15da2d(%rip), %rax    # 0x178680
mulsd (%rax), %xmm0
movsd 0x159401(%rip), %xmm1   # 0x174060
divsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movl $0x0, 0x154(%rsp)
cmpl $0x8, 0x154(%rsp)
jge 0x1aef6
cvtsi2sdl 0x154(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0xed2e0
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
leaq 0x15d9b3(%rip), %rax    # 0x178660
mulsd (%rax), %xmm0
movsd 0x1593a7(%rip), %xmm1   # 0x174060
divsd %xmm1, %xmm0
movsd %xmm0, 0x148(%rsp)
movq 0x1a0(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0x89480
movsd 0x148(%rsp), %xmm0
leaq 0x130(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x8a140
movsd 0x158(%rsp), %xmm0
leaq 0x130(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x8a140
movq 0x1a8(%rsp), %rax
movq %rax, 0x38(%rsp)
movsd 0x15933c(%rip), %xmm0   # 0x174068
leaq 0x1af95d(%rip), %rax    # 0x1ca690
mulsd (%rax), %xmm0
leaq 0x100(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x1f810
movq 0x38(%rsp), %rsi
leaq 0x118(%rsp), %rdi
leaq 0x100(%rsp), %rdx
callq 0x888a0
leaq 0xd0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0x130(%rsp), %rdx
xorl %ecx, %ecx
callq 0x87ba0
leaq 0x90(%rsp), %rdi
callq 0x20010
movq 0x418(%rsp), %rdi
movq (%rdi), %rax
leaq 0xd0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq *0x170(%rax)
cmpl $0x0, %eax
je 0x1aede
movl 0x16c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16c(%rsp)
movq 0x418(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x20030
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0x89a50
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1aedc
movsd 0xc8(%rsp), %xmm0
movslq 0x168(%rsp), %rax
movsd %xmm0, 0x1c0(%rsp,%rax,8)
movl 0x168(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x168(%rsp)
jmp 0x1aedc
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3f8(%rsp)
movl %eax, 0x3f4(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1b143
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3f8(%rsp)
movl %eax, 0x3f4(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1b143
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3f8(%rsp)
movl %eax, 0x3f4(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1b143
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3f8(%rsp)
movl %eax, 0x3f4(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1b143
jmp 0x1aede
jmp 0x1aee0
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
jmp 0x1ac77
jmp 0x1aef8
movl 0x164(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x164(%rsp)
jmp 0x1ac1d
movq 0x3e8(%rsp), %rax
movq %rax, (%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x1c0(%rsp), %rdi
movl 0x168(%rsp), %esi
callq 0x1cfb0
movq (%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x2ae50
movq 0x3e0(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0x1c(%rsp)
leaq 0x1c0(%rsp), %rdi
movl 0x168(%rsp), %esi
movsd 0x1590d3(%rip), %xmm0   # 0x174048
callq 0x1cfe0
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0x2ae50
movq 0x3d8(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0x2c(%rsp)
leaq 0x1c0(%rsp), %rdi
movl 0x168(%rsp), %esi
movsd 0x159089(%rip), %xmm0   # 0x174040
callq 0x1cfe0
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x2ae50
movq 0x3d0(%rsp), %rdi
movl 0x1bc(%rsp), %esi
cvtsi2sdl 0x16c(%rsp), %xmm0
movsd 0x15906e(%rip), %xmm1   # 0x174058
divsd %xmm1, %xmm0
callq 0x2ae50
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x1ab2e
movq 0x418(%rsp), %rdi
callq 0x14850
movsd %xmm0, 0x80(%rsp)
movq 0x3e8(%rsp), %rdi
movsd 0x80(%rsp), %xmm0
callq 0x2ba30
movq 0x3e0(%rsp), %rdi
movsd 0x80(%rsp), %xmm0
callq 0x2ba30
movq 0x3d8(%rsp), %rdi
movsd 0x80(%rsp), %xmm0
callq 0x2ba30
movq 0x3d0(%rsp), %rdi
movsd 0x80(%rsp), %xmm0
callq 0x2ba30
movq 0x400(%rsp), %rdi
movq 0x3e8(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
movq 0x400(%rsp), %rdi
movq 0x3e0(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
movq 0x400(%rsp), %rdi
movq 0x3d8(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
movq 0x400(%rsp), %rdi
movq 0x3d0(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
cmpl $0x0, 0x1af666(%rip)    # 0x1ca740
je 0x1b123
leaq 0x408(%rsp), %rdi
callq 0xe99a0
leaq 0x15911c(%rip), %rdi    # 0x17420c
movb $0x1, %al
callq 0x11090
movq 0x400(%rsp), %rdi
callq 0x1f400
movl %eax, %esi
leaq 0x1591aa(%rip), %rdi    # 0x1742b7
movb $0x0, %al
callq 0x11090
movq 0x1a5e75(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movq 0x400(%rsp), %rax
movq %rax, 0x420(%rsp)
movq 0x420(%rsp), %rax
addq $0x428, %rsp            # imm = 0x428
retq
movq 0x3f8(%rsp), %rdi
callq 0x11bb0