// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshPropertySet.cpp
// Function: gaps::R3MeshPropertySet::ReadToronto(char const*)
int R3MeshPropertySet::
ReadToronto(const char *filename)
{
  // Get base property name
  char *name = NULL;
  char buffer[512];
  strcpy(buffer, filename);
  name = strrchr(buffer, '/');
  if (name) name++; 
  else name = buffer;
  char *end = strrchr(name, '.');
  if (end) *end = '\0';

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

  // Read header
  int nprops, nfaces, dummy;
  if (fscanf(fp, "%d%d%d\n", &nprops, &nfaces, &dummy) != (unsigned int) 3) {
    RNFail("Unable to read toronto file: %s\n", filename);
    return 0;
  }

  // Check header
  if ((nprops == 0) || (nfaces == 0) || (nfaces != mesh->NFaces())) {
    RNFail("Invalid header in toronto file: %s\n", filename);
    return 0;
  }

  // Allocate memory for face values
  double *face_values = new double [ nprops * nfaces ];
  if (!face_values) {
    RNFail("Unable to allocate memory for toronto file: %s\n", filename);
    return 0;
  }

  // Read face values
  for (int j = 0; j < nfaces; j++) {
    for (int i = 0; i < nprops; i++) {
      if (fscanf(fp, "%lf", &face_values[i*nfaces+j]) != (unsigned int) 1) {
        RNFail("Unable to read value %d for face %d in %s\n", i, j, filename);
        return 0;
      }
    }
  }

  // Create properties
  for (int i = 0; i < nprops; i++) {
    // Allocate property
    char property_name[1024];
    sprintf(property_name, "%s%d", name, i);
    R3MeshProperty *property = new R3MeshProperty(mesh, property_name);

    // Compute property values by interpolation from attached faces
    for (int j = 0; j < mesh->NVertices(); j++) {
      R3MeshVertex *vertex = mesh->Vertex(j);
      RNScalar total_value = 0;
      RNScalar total_weight = 0;
      for (int k = 0; k < mesh->VertexValence(vertex); k++) {
        R3MeshEdge *edge = mesh->EdgeOnVertex(vertex, k);
        R3MeshFace *face = mesh->FaceOnVertex(vertex, edge);
        if (!face) continue;
        int face_index = mesh->FaceID(face);
        RNScalar value = face_values[i*nfaces + face_index];
        RNScalar weight = mesh->FaceArea(face);
        total_value += weight * value;
        total_weight += weight;
      }
      if (total_weight > 0) {
        property->SetVertexValue(j, total_value / total_weight);
      }
    }

    // Insert property
    Insert(property);
  }

  // Close file
  fclose(fp);

  // Delete memory for face values
  delete [] face_values;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x668, %rsp            # imm = 0x668
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x217f0
movq %r14, %rdi
movl $0x2f, %esi
callq 0x219a0
leaq 0x1(%rax), %r12
testq %rax, %rax
cmoveq %r14, %r12
movq %r12, %rdi
movl $0x2e, %esi
callq 0x219a0
testq %rax, %rax
je 0x4762d
movb $0x0, (%rax)
leaq 0x9ef8f(%rip), %rsi     # 0xe65c3
movq %r13, %rdi
callq 0x21a90
testq %rax, %rax
je 0x478c8
movq %rax, %rbp
leaq 0x99b29(%rip), %rsi     # 0xe1178
xorl %r15d, %r15d
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x5c(%rsp), %r8
movq %rax, %rdi
xorl %eax, %eax
callq 0x22170
cmpl $0x3, %eax
jne 0x478d1
movl 0xc(%rsp), %ebx
testl %ebx, %ebx
sete %al
movl 0x8(%rsp), %r14d
testl %r14d, %r14d
sete %cl
orb %al, %cl
jne 0x478da
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpl 0x34(%rax), %r14d
jne 0x478da
movq %rbp, 0x28(%rsp)
movq %r12, 0x38(%rsp)
movl %r14d, %eax
imull %ebx, %eax
cltq
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x21040
movq %rax, %r12
testl %r14d, %r14d
jle 0x47726
leaq 0x9a25d(%rip), %r15     # 0xe193a
xorl %ebp, %ebp
testl %ebx, %ebx
jle 0x4771f
xorl %r14d, %r14d
movl 0x8(%rsp), %eax
imull %r14d, %eax
addl %ebp, %eax
cltq
leaq (%r12,%rax,8), %rdx
movq 0x28(%rsp), %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x22170
cmpl $0x1, %eax
jne 0x47903
incl %r14d
movl 0xc(%rsp), %ebx
cmpl %ebx, %r14d
jl 0x476e6
movl 0x8(%rsp), %r14d
incl %ebp
cmpl %r14d, %ebp
jl 0x476df
testl %ebx, %ebx
movq 0x10(%rsp), %rbx
jle 0x478ae
leaq 0x8(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %ecx, %ecx
leaq 0x260(%rsp), %r14
movq %r14, %rdi
leaq 0x987de(%rip), %rsi     # 0xdff2e
movq 0x38(%rsp), %rdx
movl %ecx, 0x1c(%rsp)
xorl %eax, %eax
callq 0x21130
movl $0x448, %edi            # imm = 0x448
callq 0x218b0
movq (%rbx), %rsi
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x48134
movq (%rbx), %rax
cmpl $0x0, 0x14(%rax)
movq %rbx, %rsi
jle 0x47889
movslq 0x1c(%rsp), %r13
xorl %r15d, %r15d
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %rbp
movl 0x14(%rbp), %eax
testl %eax, %eax
jle 0x47876
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movq 0x8(%rbp), %rcx
movq (%rcx,%rbx,8), %rcx
xorl %edx, %edx
cmpq %rbp, 0x8(%rcx)
setne %dl
movq 0x18(%rcx,%rdx,8), %r14
testq %r14, %r14
je 0x47847
movslq 0xa4(%r14), %rcx
movslq 0x8(%rsp), %rdx
imulq %r13, %rdx
addq %rcx, %rdx
movsd (%r12,%rdx,8), %xmm3
testb $0x2, 0xa8(%r14)
jne 0x47832
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movsd %xmm1, 0x50(%rsp)
movsd %xmm0, 0x48(%rsp)
movsd %xmm3, 0x40(%rsp)
callq *0x2c0(%rax)
movsd 0x40(%rsp), %xmm3
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movq 0x10(%rsp), %rsi
movl 0x14(%rbp), %eax
movsd 0x90(%r14), %xmm2
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %rbx
movslq %eax, %rcx
cmpq %rcx, %rbx
jl 0x477b4
ucomisd 0x9781a(%rip), %xmm1    # 0xdf078
jbe 0x47876
divsd %xmm1, %xmm0
movq 0x20(%rsp), %rdi
movl %r15d, %esi
callq 0x49b8e
movq 0x10(%rsp), %rsi
incq %r15
movq (%rsi), %rax
movslq 0x14(%rax), %rcx
cmpq %rcx, %r15
jl 0x47797
movl 0x14(%rsi), %edx
movq %rsi, %rbx
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x8bdb2
movl 0x1c(%rsp), %ecx
incl %ecx
cmpl 0xc(%rsp), %ecx
jl 0x4773e
movq 0x28(%rsp), %rdi
callq 0x218a0
movq %r12, %rdi
callq 0x21c00
movl $0x1, %r15d
jmp 0x478ee
leaq 0x99888(%rip), %rdi     # 0xe1157
jmp 0x478e1
leaq 0x998a8(%rip), %rdi     # 0xe1180
jmp 0x478e4
leaq 0x998c0(%rip), %rdi     # 0xe11a1
xorl %r15d, %r15d
movq %r13, %rsi
xorl %eax, %eax
callq 0x8e88e
movl %r15d, %eax
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x998bb(%rip), %rdi     # 0xe11c5
xorl %r15d, %r15d
movl %r14d, %esi
movl %ebp, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x8e88e
jmp 0x478ee
movq %rax, %rbx
movl $0x448, %esi            # imm = 0x448
movq 0x20(%rsp), %rdi
callq 0x218d0
movq %rbx, %rdi
callq 0x220a0