// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelObjectProperty.cpp
// Function: gaps::R3SurfelObjectProperty::UpdateOperands()
void R3SurfelObjectProperty::
UpdateOperands(void)
{
  // Check if already uptodate
  if (noperands > 0) return;

  // Check object
  if (!object) return;

  // Check property type
  switch(type) {
  case R3_SURFEL_OBJECT_PCA_PROPERTY: {
    // PCA Properties
    // noperands = 21
    // operands[0-2]: centroid
    // operands[3-5]: xyz of axis1
    // operands[6-8]: xyz of axis2
    // operands[9-11]: xyz of axis3
    // operands[12-14]: variances
    // operands[15-20]: extents 

    // Allocate operands
    noperands = 21;
    operands = new RNScalar [ noperands ];
    for (int i = 0; i < noperands; i++) operands[i] = 0;

    // Create pointset
    R3SurfelPointSet *pointset = object->PointSet();
    if (!pointset) return;
    if (pointset->NPoints() < 3) { delete pointset; return; }

    // Compute principle axes
    RNScalar variances[3];
    R3Point centroid = pointset->Centroid();
    R3Triad triad = pointset->PrincipleAxes(&centroid, variances);

    // Check if positive z of triad is facing away from viewpoint(s)
    int npositive = 0, nnegative = 0;
    R3Plane plane(centroid, triad.Axis(2));
    for (int i = 0; i < pointset->NPoints(); i++) {
      R3SurfelPoint *point = pointset->Point(i);
      R3SurfelBlock *block = point->Block();
      if (!block) continue;
      R3SurfelNode *node = block->Node();
      if (!node) continue;
      R3SurfelScan *scan = node->Scan();
      if (!scan) continue;
      RNScalar d = R3SignedDistance(plane, scan->Viewpoint());
      if (d < 0) nnegative++;
      else if (d > 0) npositive++;
    }

    // Rotate triad so that z is not backfacing to viewpoints
    if (nnegative > npositive) triad = R3Triad(triad[0], -triad[1], -triad[2]);

    // Find extents
    R3Box extent = R3null_box;
    for (int i = 0; i < pointset->NPoints(); i++) {
      R3SurfelPoint *point = pointset->Point(i);
      R3Point position = point->Position();
      R3Vector vector = position - centroid;
      RNScalar x = triad.Axis(0).Dot(vector);
      RNScalar y = triad.Axis(1).Dot(vector);
      RNScalar z = triad.Axis(2).Dot(vector);
      extent.Union(R3Point(x, y, z));
    }

    // Fill operands
    operands[0] = centroid.X();
    operands[1] = centroid.Y();
    operands[2] = centroid.Z();
    operands[3] = triad.Axis(0).X();
    operands[4] = triad.Axis(0).Y();
    operands[5] = triad.Axis(0).Z();
    operands[6] = triad.Axis(1).X();
    operands[7] = triad.Axis(1).Y();
    operands[8] = triad.Axis(1).Z();
    operands[9] = triad.Axis(2).X();
    operands[10] = triad.Axis(2).Y();
    operands[11] = triad.Axis(2).Z();
    operands[12] = variances[0];
    operands[13] = variances[1];
    operands[14] = variances[2];
    operands[15] = extent[0][0];
    operands[16] = extent[0][1];
    operands[17] = extent[0][2];
    operands[18] = extent[1][0];
    operands[19] = extent[1][1];
    operands[20] = extent[1][2];

    // Delete pointset
    delete pointset;
    break; }

  case R3_SURFEL_OBJECT_AMODAL_OBB_PROPERTY: {
    // PCA Properties
    // noperands = 16
    // operands[0-2]: centroid
    // operands[3-5]: xyz of axis1
    // operands[6-8]: xyz of axis2
    // operands[9-11]: xyz of axis3
    // operands[12-14]: radii
    // operands[15]: confidence
    // operands[16]: originator
    // operands[17-19]: reserved

    // Allocate operands
    noperands = 20;
    operands = new RNScalar [ noperands ];
    for (int i = 0; i < noperands; i++) operands[i] = 0;

    // Estimate obb
    R3OrientedBox obb = EstimateOrientedBBox(object);
    double confidence = 0.01;
    int originator = R3_SURFEL_MACHINE_ORIGINATOR;
    
    // Fill operands
    operands[0] = obb.Center().X();
    operands[1] = obb.Center().Y();
    operands[2] = obb.Center().Z();
    operands[3] = obb.Axis(0).X();
    operands[4] = obb.Axis(0).Y();
    operands[5] = obb.Axis(0).Z();
    operands[6] = obb.Axis(1).X();
    operands[7] = obb.Axis(1).Y();
    operands[8] = obb.Axis(1).Z();
    operands[9] = obb.Axis(2).X();
    operands[10] = obb.Axis(2).Y();
    operands[11] = obb.Axis(2).Z();
    operands[12] = obb.Radius(0);
    operands[13] = obb.Radius(1);
    operands[14] = obb.Radius(2);
    operands[15] = confidence;
    operands[16] = originator;
    operands[17] = 0;
    operands[18] = 0;
    operands[19] = 0;
    break; }
  }

  // Mark scene as dirty
  if (scene) scene->SetDirty();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
cmpl $0x0, 0x28(%rdi)
jle 0x3efe1
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
movq 0x18(%rdi), %r14
testq %r14, %r14
je 0x3efcf
movl 0x2c(%rbx), %eax
cmpl $0x2, %eax
je 0x3f05c
cmpl $0x1, %eax
jne 0x3f418
movl $0x15, 0x28(%rbx)
movl $0xa8, %edi
callq 0x13030
movq %rax, 0x20(%rbx)
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x132a0
xorpd %xmm0, %xmm0
movq %r14, %rdi
xorl %esi, %esi
callq 0x174a6
testq %rax, %rax
je 0x3efcf
movq %rax, %r14
cmpl $0x2, 0x10(%rax)
jg 0x3f161
movq (%r14), %rax
movq %r14, %rdi
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x8(%rax)
movl $0x14, 0x28(%rbx)
movl $0xa0, %edi
callq 0x13030
movq %rax, 0x20(%rbx)
movl $0xa0, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x132a0
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x28040
movsd 0x8(%r15), %xmm0
movq 0x20(%rbx), %rax
movsd %xmm0, (%rax)
movsd 0x10(%r15), %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x10(%rax)
movsd 0x20(%r15), %xmm0
movsd %xmm0, 0x18(%rax)
movsd 0x28(%r15), %xmm0
movsd %xmm0, 0x20(%rax)
movsd 0x30(%r15), %xmm0
movsd %xmm0, 0x28(%rax)
movsd 0x38(%r15), %xmm0
movsd %xmm0, 0x30(%rax)
movsd 0x40(%r15), %xmm0
movsd %xmm0, 0x38(%rax)
movsd 0x48(%r15), %xmm0
movsd %xmm0, 0x40(%rax)
movsd 0x50(%r15), %xmm0
movsd %xmm0, 0x48(%rax)
movsd 0x58(%r15), %xmm0
movsd %xmm0, 0x50(%rax)
movsd 0x60(%r15), %xmm0
movsd %xmm0, 0x58(%rax)
movsd 0x68(%r15), %xmm0
movsd %xmm0, 0x60(%rax)
movsd 0x70(%r15), %xmm0
movsd %xmm0, 0x68(%rax)
movsd 0x78(%r15), %xmm0
movsd %xmm0, 0x70(%rax)
movapd 0x9f6c0(%rip), %xmm0    # 0xde800
movupd %xmm0, 0x78(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x88(%rax)
movq $0x0, 0x98(%rax)
jmp 0x3f410
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x327dc
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movq %r14, %rsi
movq %r15, %rdx
callq 0x328ac
leaq 0x80(%rsp), %rdx
leaq 0x150(%rsp), %rdi
movq %r15, %rsi
callq 0x82942
cmpl $0x0, 0x10(%r14)
jle 0x3f272
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movq (%rax,%r12), %rax
testq %rax, %rax
je 0x3f207
movq 0xb8(%rax), %rdi
testq %rdi, %rdi
je 0x3f207
movl $0x1, %esi
callq 0x37798
testq %rax, %rax
je 0x3f207
addq $0x28, %rax
leaq 0x150(%rsp), %rdi
movq %rax, %rsi
callq 0x50127
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3f1ff
incl %r15d
jmp 0x3f207
ucomisd %xmm1, %xmm0
jbe 0x3f207
incl %ebp
incq %r13
movslq 0x10(%r14), %rax
addq $0x10, %r12
cmpq %rax, %r13
jl 0x3f1b4
cmpl %ebp, %r15d
jle 0x3f272
leaq 0x68(%rsp), %rsi
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
callq 0x85490
leaq 0xd8(%rsp), %r12
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
callq 0x85490
leaq 0x108(%rsp), %r13
leaq 0x50(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x70c18
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3fda6
leaq 0xf574f(%rip), %rsi     # 0x1349c8
leaq 0x108(%rsp), %rdi
callq 0x6d354
cmpl $0x0, 0x10(%r14)
jle 0x3f36f
movl $0x8, %r12d
xorl %r13d, %r13d
leaq 0xd8(%rsp), %rbp
movq 0x8(%r14), %rax
movq -0x8(%rax,%r12), %rcx
movq (%rax,%r12), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
callq 0x83dc2
movq %rbp, %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
callq 0x8401e
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x84ae2
movsd %xmm0, 0x10(%rsp)
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x84ae2
movsd %xmm0, 0x8(%rsp)
leaq 0x80(%rsp), %rdi
movq %rbp, %rsi
callq 0x84ae2
movapd %xmm0, %xmm2
leaq 0x170(%rsp), %r15
movq %r15, %rdi
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x83dc2
leaq 0x108(%rsp), %rdi
movq %r15, %rsi
callq 0x6dd46
incq %r13
movslq 0x10(%r14), %rax
addq $0x10, %r12
cmpq %rax, %r13
jl 0x3f2a2
movsd 0x18(%rsp), %xmm0
movq 0x20(%rbx), %rax
movsd %xmm0, (%rax)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
movaps 0x50(%rsp), %xmm0
movups %xmm0, 0x18(%rax)
movaps 0x60(%rsp), %xmm0
movups %xmm0, 0x28(%rax)
movaps 0x70(%rsp), %xmm0
movups %xmm0, 0x38(%rax)
movaps 0x80(%rsp), %xmm0
movups %xmm0, 0x48(%rax)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x58(%rax)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0x60(%rax)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x70(%rax)
leaq 0x108(%rsp), %r15
movups 0x8(%r15), %xmm0
movups %xmm0, 0x78(%rax)
movups 0x18(%r15), %xmm0
movups %xmm0, 0x88(%rax)
movupd 0x28(%r15), %xmm0
movupd %xmm0, 0x98(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movq %r15, %rdi
callq 0x6e174
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x3efcf
orb $0x1, 0x1e8(%rax)
jmp 0x3efcf
jmp 0x3f43b
jmp 0x3f43b
jmp 0x3f43b
jmp 0x3f43b
jmp 0x3f43b
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x6e174
movq %rbx, %rdi
callq 0x138b0
nop