// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: ReadMTurkAnnotationProperties(gaps::R3Mesh*, gaps::R3MeshProperty*, gaps::RNSymbolTable<MTurkLabel*>*, char const*)
static R3MeshPropertySet *
ReadMTurkAnnotationProperties(R3Mesh *mesh,
  R3MeshProperty *segmentation_property, RNSymbolTable<MTurkLabel *> *label_mapping,
  const char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Reading mturk annotation properties ...\n");
    fflush(stdout);
  }

  // Open file
  FILE* fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open json file %s\n", filename);
    return 0;
  }

  // Read file 
  std::string text;
  fseek(fp, 0, SEEK_END);
  long const size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  char* buffer = new char[size + 1];
  unsigned long const usize = static_cast<unsigned long const>(size);
  if (fread(buffer, 1, usize, fp) != usize) { RNFail("Unable to read %s\n", filename); return 0; }
  else { buffer[size] = 0; text = buffer; }
  delete[] buffer;

  // Close file
  fclose(fp);

  // Parse file
  Json::Value json_root;
  Json::Reader json_reader;
  if (!json_reader.parse(text, json_root, false)) {
    RNFail("Unable to parse %s\n", filename);
    return 0;
  }

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

  // Allocate properites
  R3MeshProperty *instance = new R3MeshProperty(mesh, "Instance");
  R3MeshProperty *categoryId = new R3MeshProperty(mesh, "Category");
  R3MeshProperty *nyu40id = new R3MeshProperty(mesh, "NYU40Id");

  // Parse instance identifiers
  if (json_root.isMember("segGroups")) {
    MTurkLabel *label = NULL;
    Json::Value json_groups = json_root["segGroups"];
    for (Json::ArrayIndex group_index = 0; group_index < json_groups.size(); group_index++) {
      Json::Value json_group = json_groups[group_index];
      if (!json_group.isMember("segments")) continue;
      if (!json_group.isMember("label")) continue;
      Json::Value json_segments = json_group["segments"];
      Json::Value json_label = json_group["label"];
      label_mapping->Find(json_label.asString(), &label);
      for (Json::ArrayIndex segment_index = 0; segment_index < json_segments.size(); segment_index++) {
        Json::Value json_segment = json_segments[segment_index];
        int segment_id = json_segment.asInt();
        for (int i = 0; i < mesh->NVertices(); i++) {
          if (RNIsEqual(segmentation_property->VertexValue(i), segment_id)) {
            instance->SetVertexValue(i, group_index);
            if (label) {
              categoryId->SetVertexValue(i, label->id);
              nyu40id->SetVertexValue(i, label->nyu40id);
            }
          }
        }
      }
    }
  }

  // Insert properties
  InsertProperty(properties, instance);
  InsertProperty(properties, categoryId);
  InsertProperty(properties, nyu40id);

  // 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 $0x3a8, %rsp            # imm = 0x3A8
movq %rdi, 0x398(%rsp)
movq %rsi, 0x390(%rsp)
movq %rdx, 0x388(%rsp)
movq %rcx, 0x380(%rsp)
leaq 0x370(%rsp), %rdi
callq 0xe98e0
leaq 0x370(%rsp), %rdi
callq 0x1f280
cmpl $0x0, 0x1ac4e8(%rip)    # 0x1ca740
je 0x1e277
leaq 0x15655e(%rip), %rdi    # 0x1747bf
movb $0x0, %al
callq 0x11090
movq 0x1a2d21(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movq 0x380(%rsp), %rdi
leaq 0x1563f2(%rip), %rsi    # 0x174678
callq 0x11800
movq %rax, 0x368(%rsp)
cmpq $0x0, 0x368(%rsp)
jne 0x1e2c5
movq 0x380(%rsp), %rsi
leaq 0x1563ce(%rip), %rdi    # 0x17467b
movb $0x0, %al
callq 0xedb00
movq $0x0, 0x3a0(%rsp)
jmp 0x1ecdd
leaq 0x348(%rsp), %rdi
callq 0x11980
movq 0x368(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x11760
jmp 0x1e2ea
movq 0x368(%rsp), %rdi
callq 0x11030
movq %rax, 0xb0(%rsp)
jmp 0x1e301
movq 0xb0(%rsp), %rax
movq %rax, 0x330(%rsp)
movq 0x368(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x11760
jmp 0x1e324
movq 0x330(%rsp), %rdi
incq %rdi
callq 0x11040
movq %rax, 0xa8(%rsp)
jmp 0x1e33e
movq 0xa8(%rsp), %rax
movq %rax, 0x328(%rsp)
movq 0x330(%rsp), %rax
movq %rax, 0x320(%rsp)
movq 0x328(%rsp), %rdi
movq 0x320(%rsp), %rdx
movq 0x368(%rsp), %rcx
movl $0x1, %esi
callq 0x11a10
movq %rax, 0xa0(%rsp)
jmp 0x1e38a
movq 0xa0(%rsp), %rax
cmpq 0x320(%rsp), %rax
je 0x1e3e9
movq 0x380(%rsp), %rsi
leaq 0x1562ed(%rip), %rdi    # 0x174698
xorl %eax, %eax
callq 0xedb00
jmp 0x1e3b4
movq $0x0, 0x3a0(%rsp)
movl $0x1, 0x31c(%rsp)
jmp 0x1ecbf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1ecce
movq 0x328(%rsp), %rax
movq 0x330(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x328(%rsp), %rsi
leaq 0x348(%rsp), %rdi
callq 0x11930
jmp 0x1e414
jmp 0x1e416
movq 0x328(%rsp), %rax
movq %rax, 0x98(%rsp)
cmpq $0x0, %rax
je 0x1e439
movq 0x98(%rsp), %rdi
callq 0x118b0
movq 0x368(%rsp), %rdi
callq 0x11670
jmp 0x1e448
leaq 0x2f0(%rsp), %rdi
xorl %esi, %esi
callq 0xfad90
jmp 0x1e459
leaq 0x1e0(%rsp), %rdi
callq 0xedd40
jmp 0x1e468
leaq 0x1e0(%rsp), %rdi
leaq 0x348(%rsp), %rsi
leaq 0x2f0(%rsp), %rdx
xorl %ecx, %ecx
callq 0xedf70
movb %al, 0x97(%rsp)
jmp 0x1e490
movb 0x97(%rsp), %al
testb $0x1, %al
jne 0x1e503
jmp 0x1e49d
movq 0x380(%rsp), %rsi
leaq 0x1561ff(%rip), %rdi    # 0x1746ab
xorl %eax, %eax
callq 0xedb00
jmp 0x1e4b5
movq $0x0, 0x3a0(%rsp)
movl $0x1, 0x31c(%rsp)
jmp 0x1ec87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1ecb0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1eca3
movl $0x18, %edi
callq 0x11680
movq %rax, 0x88(%rsp)
jmp 0x1e517
movq 0x88(%rsp), %rdi
movq 0x398(%rsp), %rsi
callq 0x24ce0
jmp 0x1e52e
movq 0x88(%rsp), %rax
movq %rax, 0x1d8(%rsp)
cmpq $0x0, 0x1d8(%rsp)
jne 0x1e5a0
leaq 0x155d06(%rip), %rdi    # 0x174256
xorl %eax, %eax
callq 0xedb00
jmp 0x1e559
movq $0x0, 0x3a0(%rsp)
movl $0x1, 0x31c(%rsp)
jmp 0x1ec87
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
movl $0x18, %esi
callq 0x11690
jmp 0x1eca3
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, 0x80(%rsp)
jmp 0x1e5b4
movq 0x80(%rsp), %rdi
movq 0x398(%rsp), %rsi
leaq 0x15621d(%rip), %rdx    # 0x1747e8
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1e5d6
movq 0x80(%rsp), %rax
movq %rax, 0x1d0(%rsp)
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, 0x78(%rsp)
jmp 0x1e5f7
movq 0x78(%rsp), %rdi
movq 0x398(%rsp), %rsi
leaq 0x1561e6(%rip), %rdx    # 0x1747f1
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1e616
movq 0x78(%rsp), %rax
movq %rax, 0x1c8(%rsp)
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, 0x70(%rsp)
jmp 0x1e634
movq 0x70(%rsp), %rdi
movq 0x398(%rsp), %rsi
leaq 0x1561b2(%rip), %rdx    # 0x1747fa
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1e653
movq 0x70(%rsp), %rax
movq %rax, 0x1c0(%rsp)
leaq 0x15619b(%rip), %rsi    # 0x174802
leaq 0x2f0(%rsp), %rdi
callq 0xff890
movb %al, 0x6f(%rsp)
jmp 0x1e67a
movb 0x6f(%rsp), %al
testb $0x1, %al
jne 0x1e687
jmp 0x1ebbb
movq $0x0, 0x1b8(%rsp)
leaq 0x156168(%rip), %rsi    # 0x174802
leaq 0x2f0(%rsp), %rdi
callq 0xf99c0
movq %rax, 0x60(%rsp)
jmp 0x1e6ae
movq 0x60(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0xfb370
jmp 0x1e6c2
movl $0x0, 0x18c(%rsp)
movl 0x18c(%rsp), %eax
movl %eax, 0x58(%rsp)
leaq 0x190(%rsp), %rdi
callq 0xf94b0
movl %eax, 0x5c(%rsp)
jmp 0x1e6eb
movl 0x58(%rsp), %eax
movl 0x5c(%rsp), %ecx
cmpl %ecx, %eax
jae 0x1eb9a
movl 0x18c(%rsp), %esi
leaq 0x190(%rsp), %rdi
callq 0xfde50
movq %rax, 0x50(%rsp)
jmp 0x1e716
movq 0x50(%rsp), %rsi
leaq 0x160(%rsp), %rdi
callq 0xfb370
jmp 0x1e72a
leaq 0x1560db(%rip), %rsi    # 0x17480c
leaq 0x160(%rsp), %rdi
callq 0xff890
movb %al, 0x4f(%rsp)
jmp 0x1e744
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x1e80f
jmp 0x1e752
movl $0x4, 0x31c(%rsp)
jmp 0x1eb3d
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1eca3
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1eca3
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1eca3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1eba9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1eb8b
leaq 0x155fff(%rip), %rsi    # 0x174815
leaq 0x160(%rsp), %rdi
callq 0xff890
movb %al, 0x4e(%rsp)
jmp 0x1e829
movb 0x4e(%rsp), %al
testb $0x1, %al
jne 0x1e843
jmp 0x1e833
movl $0x4, 0x31c(%rsp)
jmp 0x1eb3d
leaq 0x155fc2(%rip), %rsi    # 0x17480c
leaq 0x160(%rsp), %rdi
callq 0xf99c0
movq %rax, 0x40(%rsp)
jmp 0x1e85e
movq 0x40(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0xfb370
jmp 0x1e872
leaq 0x155f9c(%rip), %rsi    # 0x174815
leaq 0x160(%rsp), %rdi
callq 0xf99c0
movq %rax, 0x38(%rsp)
jmp 0x1e88d
movq 0x38(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xfb370
jmp 0x1e8a1
movq 0x388(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xf1000
jmp 0x1e8c5
movq 0x30(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x1b8(%rsp), %rdx
callq 0x23080
jmp 0x1e8e1
leaq 0xf0(%rsp), %rdi
callq 0x11400
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0x138(%rsp), %rdi
callq 0xf94b0
movl %eax, 0x2c(%rsp)
jmp 0x1e917
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jae 0x1eb18
movl 0xec(%rsp), %esi
leaq 0x138(%rsp), %rdi
callq 0xfde50
movq %rax, 0x20(%rsp)
jmp 0x1e942
movq 0x20(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xfb370
jmp 0x1e956
leaq 0xc0(%rsp), %rdi
callq 0xf8210
movl %eax, 0x1c(%rsp)
jmp 0x1e969
movl 0x1c(%rsp), %eax
movl %eax, 0xbc(%rsp)
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x398(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x1eaf5
movq 0x390(%rsp), %rdi
movl 0xb8(%rsp), %esi
callq 0x1ffd0
cvtsi2sdl 0xbc(%rsp), %xmm1
leaq 0x1abcc7(%rip), %rax    # 0x1ca690
movsd (%rax), %xmm2
callq 0x23110
movl %eax, 0x14(%rsp)
jmp 0x1e9d8
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
je 0x1eadd
movq 0x1d0(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0x18c(%rsp), %eax
cvtsi2sd %rax, %xmm0
callq 0x2ae50
jmp 0x1ea07
cmpq $0x0, 0x1b8(%rsp)
je 0x1eadb
movq 0x1c8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq 0x1b8(%rsp), %rax
cvtsi2sdl (%rax), %xmm0
callq 0x2ae50
jmp 0x1ea38
movq 0x1c0(%rsp), %rdi
movl 0xb8(%rsp), %esi
movq 0x1b8(%rsp), %rax
cvtsi2sdl 0x10(%rax), %xmm0
callq 0x2ae50
jmp 0x1ea5b
jmp 0x1eadb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1eb7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
jmp 0x1eb71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x11400
jmp 0x1eb71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x340(%rsp)
movl %eax, 0x33c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xfb7f0
jmp 0x1eb71
jmp 0x1eadd
jmp 0x1eadf
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x1e97f
leaq 0xc0(%rsp), %rdi
callq 0xfb7f0
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x1e8f9
leaq 0x110(%rsp), %rdi
callq 0xfb7f0
leaq 0x138(%rsp), %rdi
callq 0xfb7f0
movl $0x0, 0x31c(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xfb7f0
movl 0x31c(%rsp), %eax
testl %eax, %eax
je 0x1eb59
jmp 0x1eb57
jmp 0x1eb5b
jmp 0x1eb5b
movl 0x18c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18c(%rsp)
jmp 0x1e6cd
leaq 0x110(%rsp), %rdi
callq 0xfb7f0
leaq 0x138(%rsp), %rdi
callq 0xfb7f0
leaq 0x160(%rsp), %rdi
callq 0xfb7f0
jmp 0x1eba9
leaq 0x190(%rsp), %rdi
callq 0xfb7f0
jmp 0x1ebbb
leaq 0x190(%rsp), %rdi
callq 0xfb7f0
jmp 0x1eca3
movq 0x1d8(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
jmp 0x1ebd4
movq 0x1d8(%rsp), %rdi
movq 0x1c8(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
jmp 0x1ebed
movq 0x1d8(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
jmp 0x1ec06
cmpl $0x0, 0x1abb33(%rip)    # 0x1ca740
je 0x1ec6c
leaq 0x370(%rsp), %rdi
callq 0xe99a0
movsd %xmm0, 0x8(%rsp)
jmp 0x1ec24
movsd 0x8(%rsp), %xmm0
leaq 0x1555db(%rip), %rdi    # 0x17420c
movb $0x1, %al
callq 0x11090
jmp 0x1ec3a
movq 0x1d8(%rsp), %rdi
callq 0x1f400
movl %eax, %esi
leaq 0x155667(%rip), %rdi    # 0x1742b7
xorl %eax, %eax
callq 0x11090
jmp 0x1ec59
movq 0x1a2330(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
jmp 0x1ec6a
jmp 0x1ec6c
movq 0x1d8(%rsp), %rax
movq %rax, 0x3a0(%rsp)
movl $0x1, 0x31c(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x20160
leaq 0x2f0(%rsp), %rdi
callq 0xfb7f0
jmp 0x1ecbf
leaq 0x1e0(%rsp), %rdi
callq 0x20160
leaq 0x2f0(%rsp), %rdi
callq 0xfb7f0
jmp 0x1ecce
leaq 0x348(%rsp), %rdi
callq 0x11400
jmp 0x1ecdd
leaq 0x348(%rsp), %rdi
callq 0x11400
jmp 0x1eced
movq 0x3a0(%rsp), %rax
addq $0x3a8, %rsp            # imm = 0x3A8
retq
movq 0x340(%rsp), %rdi
callq 0x11bb0
nopw (%rax,%rax)