// File: /workspace/github_cpp_makefile_star2_O3/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 0x297ac
jp 0x297ac
movl (%r15), %eax
movl %eax, (%r14)
leaq 0x10(%r14), %rdi
leaq 0x10(%r15), %rsi
callq 0x9e4d6
leaq 0x28(%r14), %rdi
leaq 0x28(%r15), %rsi
callq 0x9e4d6
leaq 0x40(%r15), %rsi
leaq 0x40(%r14), %rdi
callq 0x9e4d6
leaq 0x58(%r15), %rsi
leaq 0x58(%r14), %rdi
callq 0x9e4d6
leaq 0x70(%r14), %rdi
leaq 0x70(%r15), %rsi
callq 0x9f38e
movl $0x88, %edi
leaq (%r15,%rdi), %rsi
addq %r14, %rdi
callq 0x9f38e
movsd 0xa0(%r15), %xmm0
jmp 0x29824
movq %rsi, %r12
leaq 0x40(%r14), %rbx
ucomisd %xmm0, %xmm1
movl (%rsi), %ecx
jne 0x2983f
jp 0x2983f
movl %ecx, (%r14)
leaq 0x10(%r14), %rdi
leaq 0x10(%r12), %rsi
callq 0x9e4d6
leaq 0x28(%r14), %rdi
leaq 0x28(%r12), %rsi
callq 0x9e4d6
leaq 0x40(%r12), %rsi
movq %rbx, %rdi
callq 0x9e4d6
leaq 0x58(%r12), %rsi
leaq 0x58(%r14), %rdi
callq 0x9e4d6
leaq 0x70(%r14), %rdi
leaq 0x70(%r12), %rsi
callq 0x9f38e
movl $0x88, %edi
leaq (%r12,%rdi), %rsi
addq %r14, %rdi
callq 0x9f38e
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 0x2984f
movapd %xmm2, %xmm0
cmpl %eax, %ecx
cmovgl %ecx, %eax
jg 0x2985f
movapd %xmm0, %xmm2
movsd %xmm1, (%rsp)
movsd %xmm2, 0x8(%rsp)
movl %eax, (%r14)
leaq 0x5470a1(%rip), %rsi    # 0x570910
movq %rbx, %rdi
callq 0x9e4d6
leaq 0x40(%r12), %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x9ebb2
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9eaa8
leaq 0x40(%r15), %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movsd (%rsp), %xmm0
callq 0x9ebb2
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9eaa8
movsd 0x8(%rsp), %xmm0
addsd (%rsp), %xmm0
movq %rbx, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x9eb26
leaq 0x8(%r14), %r13
leaq 0x10(%r14), %rdi
leaq 0x146385(%rip), %rbp    # 0x16fc68
leaq 0x8(%rbp), %rsi
callq 0x9e4d6
leaq 0x28(%r14), %rdi
addq $0x20, %rbp
movq %rbp, %rsi
callq 0x9e4d6
leaq 0x8(%r12), %rsi
movq %r13, %rdi
callq 0x881b8
leaq 0x8(%r15), %rsi
movq %r13, %rdi
callq 0x881b8
leaq 0x58(%r14), %rdi
leaq 0x546ea0(%rip), %rbp    # 0x5707c0
movq %rdi, 0x18(%rsp)
movq %rbp, %rsi
callq 0x9e4d6
leaq 0x70(%r14), %rdi
addq $0x18, %rbp
movq %rdi, 0x20(%rsp)
movq %rbp, %rsi
callq 0x9f38e
leaq 0x88(%r14), %rbp
leaq 0x546bf0(%rip), %r13    # 0x570540
movq %rbp, %rdi
movq %r13, %rsi
callq 0x9f38e
movsd 0x18(%r13), %xmm0
movsd %xmm0, 0xa0(%r14)
movl (%r14), %eax
cmpl $0x3, %eax
je 0x29a5f
cmpl $0x2, %eax
jne 0x2982d
addq $0x70, %r12
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x9ec12
addq $0x70, %r15
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x9ec12
movq %r14, %rdi
movq %r12, %rsi
callq 0x9ee8e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x299c6
leaq 0x28(%rsp), %rdi
callq 0x9ef70
leaq 0xe8beb(%rip), %rsi     # 0x1125b8
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x9ec12
leaq 0x58(%rsp), %r15
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x9f59e
movq %r14, %rdi
movq %r15, %rsi
callq 0x9f5c0
leaq 0x58(%rsp), %r15
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x9f59e
movq %r14, %rdi
movq %r15, %rsi
callq 0x9f5c0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x9f7d4
movq %r14, %rdi
callq 0x9ef18
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x9e4d6
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9f38e
movq %rbx, %rdi
callq 0x9ef18
jmp 0x2982d
movl $0x88, %r13d
addq %r13, %r12
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x9ec12
addq %r13, %r15
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x9ec12
movq %r14, %rdi
movq %r12, %rsi
callq 0x9ee8e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x29aaa
leaq 0x28(%rsp), %rdi
callq 0x9ef70
leaq 0xe8b07(%rip), %rsi     # 0x1125b8
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x9ec12
leaq 0x58(%rsp), %r15
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x9f59e
movq %r14, %rdi
movq %r15, %rsi
callq 0x9f5c0
leaq 0x58(%rsp), %r15
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x9f59e
movq %r14, %rdi
movq %r15, %rsi
callq 0x9f5c0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x9f7d4
movq %r14, %rdi
callq 0x9ef18
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x9d4c0
jmp 0x2982d