// File: /workspace/github_cpp_makefile_star2_O2/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 $0x658, %rsp            # imm = 0x658
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x21800
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x219b0
leaq 0x1(%rax), %r12
testq %rax, %rax
cmoveq %r14, %r12
pushq $0x2e
popq %rsi
movq %r12, %rdi
callq 0x219b0
testq %rax, %rax
je 0x4204f
movb $0x0, (%rax)
leaq 0x8e29d(%rip), %rsi     # 0xd02f3
movq %r13, %rdi
callq 0x21a90
testq %rax, %rax
je 0x42134
movq %rax, %r14
leaq 0x890df(%rip), %rsi     # 0xcb150
xorl %ebp, %ebp
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4c(%rsp), %r8
movq %rax, %rdi
xorl %eax, %eax
callq 0x22190
cmpl $0x3, %eax
jne 0x4213d
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
sete %al
movl (%rsp), %r15d
testl %r15d, %r15d
sete %cl
orb %al, %cl
jne 0x42146
movq (%rbx), %rax
cmpl 0x34(%rax), %r15d
jne 0x42146
movq %r14, 0x20(%rsp)
movq %r12, 0x30(%rsp)
movl %r15d, %eax
imull %ebp, %eax
cltq
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0x21040
movq %rax, %r12
xorl %r14d, %r14d
cmpl %r15d, %r14d
jge 0x42188
xorl %r15d, %r15d
cmpl %ebp, %r15d
jge 0x4212b
movl (%rsp), %eax
imull %r15d, %eax
addl %r14d, %eax
cltq
leaq (%r12,%rax,8), %rdx
movq 0x20(%rsp), %rdi
leaq 0x897c4(%rip), %rsi     # 0xcb8da
xorl %eax, %eax
callq 0x22190
cmpl $0x1, %eax
jne 0x4216d
incl %r15d
movl 0x4(%rsp), %ebp
jmp 0x420f5
incl %r14d
movl (%rsp), %r15d
jmp 0x420e9
leaq 0x88ff4(%rip), %rdi     # 0xcb12f
jmp 0x4214d
leaq 0x89014(%rip), %rdi     # 0xcb158
jmp 0x4214f
leaq 0x8902c(%rip), %rdi     # 0xcb179
xorl %ebp, %ebp
movq %r13, %rsi
xorl %eax, %eax
callq 0x82696
movl %ebp, %eax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x89029(%rip), %rdi     # 0xcb19d
xorl %ebp, %ebp
movl %r15d, %esi
movl %r14d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x82696
jmp 0x42159
leaq 0x8(%rbx), %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
cmpl %ebp, %r15d
movq 0x20(%rsp), %rdi
jge 0x422de
leaq 0x250(%rsp), %r14
movq %r14, %rdi
leaq 0x87d4a(%rip), %rsi     # 0xc9efe
movq 0x30(%rsp), %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x21130
movl $0x448, %edi            # imm = 0x448
callq 0x218c0
movq (%rbx), %rsi
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x42ae4
movl %r15d, 0x1c(%rsp)
movslq %r15d, %r14
xorl %r13d, %r13d
movq (%rbx), %rax
movslq 0x14(%rax), %rcx
cmpq %rcx, %r13
jge 0x422be
movq 0x8(%rax), %rax
movq (%rax,%r13,8), %r15
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movslq 0x14(%r15), %rax
cmpq %rax, %rbp
jge 0x42297
movq 0x8(%r15), %rax
movq (%rax,%rbp,8), %rax
xorl %ecx, %ecx
cmpq %r15, 0x8(%rax)
setne %cl
movq 0x18(%rax,%rcx,8), %rsi
testq %rsi, %rsi
je 0x4228f
movslq 0xa4(%rsi), %rax
movslq (%rsp), %rcx
imulq %r14, %rcx
addq %rax, %rcx
movsd (%r12,%rcx,8), %xmm2
movsd %xmm2, 0x38(%rsp)
movq (%rbx), %rdi
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x10(%rsp)
callq 0x42abe
movsd 0x40(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
incq %rbp
jmp 0x4220f
ucomisd 0x88bc1(%rip), %xmm0    # 0xcae60
jbe 0x422b6
divsd %xmm0, %xmm1
movq 0x8(%rsp), %rdi
movl %r13d, %esi
movapd %xmm1, %xmm0
callq 0x44320
incq %r13
jmp 0x421ed
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x26aa0
movl 0x1c(%rsp), %r15d
incl %r15d
movl 0x4(%rsp), %ebp
jmp 0x42194
callq 0x218b0
movq %r12, %rdi
callq 0x21c00
pushq $0x1
popq %rbp
jmp 0x42159
movq %rax, %rbx
movl $0x448, %esi            # imm = 0x448
movq 0x8(%rsp), %rdi
callq 0x218e0
movq %rbx, %rdi
callq 0x220c0
nop