// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationPrimitive::Update(gaps::R3SegmentationPrimitive, gaps::R3SegmentationPrimitive, double, double)
void R3SegmentationPrimitive::
Update(R3SegmentationPrimitive primitive1, R3SegmentationPrimitive primitive2, RNScalar weight1, RNScalar weight2)
{
  // Just checking
  if (weight1 == 0) {
    primitive_type = primitive2.primitive_type;
    bbox = primitive2.bbox;
    centroid = primitive2.centroid;
    line = primitive2.line;
    plane = primitive2.plane;
  }
  else if (weight2 == 0) {
    primitive_type = primitive1.primitive_type;
    bbox = primitive1.bbox;
    centroid = primitive1.centroid;
    line = primitive1.line;
    plane = primitive1.plane;
  }
  else {
    // Update primitive type
    if (primitive1.primitive_type > primitive2.primitive_type) {
      primitive_type = primitive1.primitive_type;
      weight2 = 0;
    }
    else if (primitive2.primitive_type > primitive1.primitive_type) {
      primitive_type = primitive2.primitive_type;
      weight1 = 0;
    }
    else {
      primitive_type = primitive1.primitive_type;
    }

    // Update centroid
    centroid = R3zero_point;
    centroid += weight1 * primitive1.centroid;
    centroid += weight2 * primitive2.centroid;
    centroid /= weight1 + weight2;

    // Update bbox
    bbox = R3null_box;
    bbox.Union(primitive1.bbox);
    bbox.Union(primitive2.bbox);

    // Update other stuff
    line = R3null_line;
    plane = R3null_plane;
    if (primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) {
      // Compute line
      R3Vector vector1 = primitive1.line.Vector();
      R3Vector vector2 = primitive2.line.Vector();
      if (vector1.Dot(vector2) < 0) vector2.Flip();
      R3Vector vector = R3zero_vector;
      vector += weight1 * vector1;
      vector += weight2 * vector2;
      vector /= weight1 + weight2;
      vector.Normalize();
      line.Reset(centroid, vector);
    }
    else if (primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) {
      // Compute plane
      R3Vector normal1 = primitive1.plane.Normal();
      R3Vector normal2 = primitive2.plane.Normal();
      if (normal1.Dot(normal2) < 0) normal2.Flip();
      R3Vector normal = R3zero_vector;
      normal += weight1 * normal1;
      normal += weight2 * normal2;
      normal /= weight1 + weight2;
      normal.Normalize();
      plane.Reset(centroid, normal);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movapd %xmm0, %xmm2
movq %rdx, %r15
movq %rdi, %r14
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x29ae8
jp 0x29ae8
movl (%r15), %eax
movl %eax, (%r14)
leaq 0x10(%r14), %rdi
leaq 0x10(%r15), %rsi
callq 0x9f83c
leaq 0x28(%r14), %rdi
leaq 0x28(%r15), %rsi
callq 0x9f83c
leaq 0x40(%r15), %rsi
leaq 0x40(%r14), %rdi
callq 0x9f83c
leaq 0x58(%r15), %rsi
leaq 0x58(%r14), %rdi
callq 0x9f83c
leaq 0x70(%r14), %rdi
leaq 0x70(%r15), %rsi
callq 0xa0712
movl $0x88, %edi
leaq (%r15,%rdi), %rsi
addq %r14, %rdi
callq 0xa0712
movsd 0xa0(%r15), %xmm0
jmp 0x29b5d
movq %rsi, %r12
ucomisd %xmm0, %xmm1
movl (%rsi), %ecx
jne 0x29b78
jp 0x29b78
movl %ecx, (%r14)
leaq 0x10(%r14), %rdi
leaq 0x10(%r12), %rsi
callq 0x9f83c
leaq 0x28(%r14), %rdi
leaq 0x28(%r12), %rsi
callq 0x9f83c
leaq 0x40(%r12), %rsi
leaq 0x40(%r14), %rdi
callq 0x9f83c
leaq 0x58(%r12), %rsi
leaq 0x58(%r14), %rdi
callq 0x9f83c
leaq 0x70(%r14), %rdi
leaq 0x70(%r12), %rsi
callq 0xa0712
movl $0x88, %edi
leaq (%r12,%rdi), %rsi
addq %r14, %rdi
callq 0xa0712
movsd 0xa0(%r12), %xmm0
movsd %xmm0, 0xa0(%r14)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r15), %eax
movsd %xmm0, (%rsp)
cmpl %ecx, %eax
jg 0x29b88
movapd %xmm2, %xmm0
cmpl %eax, %ecx
cmovgl %ecx, %eax
jg 0x29b98
movapd %xmm0, %xmm2
movsd %xmm1, (%rsp)
movsd %xmm2, 0x8(%rsp)
movl %eax, (%r14)
leaq 0x40(%r14), %rbx
leaq 0x548d74(%rip), %rsi    # 0x572920
movq %rbx, %rdi
callq 0x9f83c
leaq 0x40(%r12), %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x9ff18
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9fe0e
leaq 0x40(%r15), %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movsd (%rsp), %xmm0
callq 0x9ff18
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9fe0e
movsd 0x8(%rsp), %xmm0
addsd (%rsp), %xmm0
movq %rbx, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x9fe8c
leaq 0x8(%r14), %r13
leaq 0x10(%r14), %rdi
leaq 0x148058(%rip), %rbp    # 0x171c78
leaq 0x8(%rbp), %rsi
callq 0x9f83c
leaq 0x28(%r14), %rdi
addq $0x20, %rbp
movq %rbp, %rsi
callq 0x9f83c
leaq 0x8(%r12), %rsi
movq %r13, %rdi
callq 0x891c0
leaq 0x8(%r15), %rsi
movq %r13, %rdi
callq 0x891c0
leaq 0x58(%r14), %rdi
leaq 0x548b73(%rip), %rbp    # 0x5727d0
movq %rdi, 0x18(%rsp)
movq %rbp, %rsi
callq 0x9f83c
leaq 0x70(%r14), %rdi
addq $0x18, %rbp
movq %rdi, 0x20(%rsp)
movq %rbp, %rsi
callq 0xa0712
leaq 0x88(%r14), %rbp
leaq 0x5488c3(%rip), %r13    # 0x572550
movq %rbp, %rdi
movq %r13, %rsi
callq 0xa0712
movsd 0x18(%r13), %xmm0
movsd %xmm0, 0xa0(%r14)
movl (%r14), %eax
cmpl $0x3, %eax
je 0x29d9c
cmpl $0x2, %eax
jne 0x29b66
addq $0x70, %r12
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x9ff76
addq $0x70, %r15
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x9ff76
movq %r14, %rdi
movq %r12, %rsi
callq 0xa01fe
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x29d03
leaq 0x28(%rsp), %rdi
callq 0xa02f4
leaq 0xea8b6(%rip), %rsi     # 0x1145c0
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x9ff76
leaq 0x58(%rsp), %r15
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
callq 0xa0936
movq %r14, %rdi
movq %r15, %rsi
callq 0xa0958
leaq 0x58(%rsp), %r15
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0xa0936
movq %r14, %rdi
movq %r15, %rsi
callq 0xa0958
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0xa0b94
movq %r14, %rdi
callq 0xa0288
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x9f83c
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa0712
movq %rbx, %rdi
callq 0xa0288
jmp 0x29b66
movl $0x88, %r13d
addq %r13, %r12
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x9ff76
addq %r13, %r15
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x9ff76
movq %r14, %rdi
movq %r12, %rsi
callq 0xa01fe
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x29de7
leaq 0x28(%rsp), %rdi
callq 0xa02f4
leaq 0xea7d2(%rip), %rsi     # 0x1145c0
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x9ff76
leaq 0x58(%rsp), %r15
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
callq 0xa0936
movq %r14, %rdi
movq %r15, %rsi
callq 0xa0958
leaq 0x58(%rsp), %r15
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0xa0936
movq %r14, %rdi
movq %r15, %rsi
callq 0xa0958
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0xa0b94
movq %r14, %rdi
callq 0xa0288
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x9e822
jmp 0x29b66
nop