// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: TransferMTurkPropertiesToFaces(gaps::R3Mesh*, gaps::R3MeshPropertySet*)
static int
TransferMTurkPropertiesToFaces(R3Mesh *mesh, R3MeshPropertySet *properties)
{
  // Get relevant mturk properties
  R3MeshProperty *material_property = properties->Property("Segment");
  R3MeshProperty *segment_property = properties->Property("Instance");
  R3MeshProperty *category_property = properties->Property("NYU40Id");
  if (!material_property || !segment_property || !category_property) return 1;

  // Allocate segment info
  if (mesh->NVertices() == 0) return 1;
  RNArea *areas = new RNArea [ mesh->NVertices() ];
  for (int i = 0; i < mesh->NVertices(); i++) areas[i] = 0;
  RNMap<int, int> segment_to_index;

  // Compute areas
  int index = -1;
  int nsegments = 0;
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(i);
    int segment = (int) (segment_property->VertexValue(vertex) + 0.5);
    if (!segment_to_index.Find(segment, &index)) {
      segment_to_index.Insert(segment, nsegments);
      index = nsegments++;
    }
    areas[index] += mesh->VertexArea(vertex);
  }

  // Compute face properties
  for (int i = 0; i < mesh->NFaces(); i++) {
    R3MeshFace *face = mesh->Face(i);
    RNArea best_area = FLT_MAX;
    for (int j = 0; j < 3; j++) {
      R3MeshVertex *vertex = mesh->VertexOnFace(face, j);
      int segment = (int) (segment_property->VertexValue(vertex) + 0.5);
      if (segment_to_index.Find(segment, &index)) {
        if (areas[index] < best_area) {
          int material = (int) (material_property->VertexValue(vertex) + 0.5);
          int category = (int) (category_property->VertexValue(vertex) + 0.5);
          mesh->SetFaceMaterial(face, material);
          mesh->SetFaceSegment(face, segment);
          mesh->SetFaceCategory(face, category);
          best_area = areas[index];
        }
      }
    }
  }

  // Delete temporary memory
  delete [] areas;
  
  // Return success
  return 1;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x15591f(%rip), %rsi    # 0x174645
callq 0x200e0
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x155aa6(%rip), %rsi    # 0x1747e8
callq 0x200e0
movq %rax, 0xe0(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x155a9c(%rip), %rsi    # 0x1747fa
callq 0x200e0
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xe8(%rsp)
je 0x1ed8c
cmpq $0x0, 0xe0(%rsp)
je 0x1ed8c
cmpq $0x0, 0xd8(%rsp)
jne 0x1ed9c
movl $0x1, 0x104(%rsp)
jmp 0x1f242
movq 0xf8(%rsp), %rdi
callq 0x1f2e0
cmpl $0x0, %eax
jne 0x1edbe
movl $0x1, 0x104(%rsp)
jmp 0x1f242
movq 0xf8(%rsp), %rdi
callq 0x1f2e0
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x11040
movq %rax, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x60(%rsp), %eax
cmpl %ecx, %eax
jge 0x1ee4e
movq 0xd0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x1ee01
leaq 0xc0(%rsp), %rdi
callq 0x23520
movl $0xffffffff, 0xbc(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x5c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1efe1
movq 0xf8(%rsp), %rdi
movl 0xb4(%rsp), %esi
callq 0x1f380
movq %rax, 0x50(%rsp)
jmp 0x1eebd
movq 0x50(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xe0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x235a0
movsd %xmm0, 0x48(%rsp)
jmp 0x1eee7
movsd 0x48(%rsp), %xmm0
movsd 0x15512b(%rip), %xmm1   # 0x174020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x98(%rsp)
movl 0x98(%rsp), %esi
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rdx
callq 0x235e0
movl %eax, 0x44(%rsp)
jmp 0x1ef26
movl 0x44(%rsp), %eax
cmpl $0x0, %eax
jne 0x1ef8e
movl 0x98(%rsp), %esi
movl 0xb8(%rsp), %edx
leaq 0xc0(%rsp), %rdi
callq 0x23670
jmp 0x1ef4c
movl 0xb8(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0xb8(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x1ef8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x23710
jmp 0x1f251
movq 0xf8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x31590
movsd %xmm0, 0x38(%rsp)
jmp 0x1efab
movsd 0x38(%rsp), %xmm0
movq 0xd0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x1ee7c
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1f2a0
movl %eax, 0x34(%rsp)
jmp 0x1f00a
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x1f20f
movq 0xf8(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x1f640
movq %rax, 0x28(%rsp)
jmp 0x1f035
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
movsd 0x155056(%rip), %xmm0   # 0x1740a0
movsd %xmm0, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
cmpl $0x3, 0x7c(%rsp)
jge 0x1f1f7
movq 0xf8(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl 0x7c(%rsp), %edx
callq 0x1f670
movq %rax, 0x70(%rsp)
movq 0xe0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x235a0
movsd %xmm0, 0x20(%rsp)
jmp 0x1f09e
movsd 0x20(%rsp), %xmm0
movsd 0x154f74(%rip), %xmm1   # 0x174020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %esi
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rdx
callq 0x235e0
movl %eax, 0x1c(%rsp)
jmp 0x1f0d7
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
je 0x1f1e5
movq 0xd0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd 0x80(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1f1e3
movq 0xe8(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x235a0
movsd %xmm0, 0x10(%rsp)
jmp 0x1f126
movsd 0x10(%rsp), %xmm0
movsd 0x154eec(%rip), %xmm1   # 0x174020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x68(%rsp)
movq 0xd8(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x235a0
movsd %xmm0, 0x8(%rsp)
jmp 0x1f15a
movsd 0x8(%rsp), %xmm0
movsd 0x154eb8(%rip), %xmm1   # 0x174020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x64(%rsp)
movq 0xf8(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl 0x68(%rsp), %edx
callq 0x236b0
jmp 0x1f18f
movq 0xf8(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl 0x6c(%rsp), %edx
callq 0x236d0
jmp 0x1f1aa
movq 0xf8(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl 0x64(%rsp), %edx
callq 0x236f0
jmp 0x1f1c5
movq 0xd0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x80(%rsp)
jmp 0x1f1e5
jmp 0x1f1e7
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1f05b
jmp 0x1f1f9
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x1efec
movq 0xd0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x1f22a
movq (%rsp), %rdi
callq 0x118b0
movl $0x1, 0x104(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x23710
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xa0(%rsp), %rdi
callq 0x11bb0
nop