// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationPrimitive::Update(gaps::R3SegmentationPoint*, gaps::RNArray<gaps::R3SegmentationPoint*> const*)
void R3SegmentationPrimitive::
Update(R3SegmentationPoint *seed_point, const RNArray<R3SegmentationPoint *> *points)
{
  // Remember stuff about primitive (so can set same orientation)
  R3Vector previous_vector(0,0,0);
  if (primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) previous_vector = line.Vector();
  if (primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) previous_vector = plane.Normal();

  // Update bounding box
  bbox = R3null_box;
  if (points) {
    for (int i = 0; i < points->NEntries(); i++) {
      R3SegmentationPoint *point = points->Kth(i);
      bbox.Union(point->position);
    }
  }

  // Initialize everything
  if (seed_point) {
    R3Point seed_position = seed_point->position;
    centroid = seed_position;
    line.Reset(seed_position, line.Vector()); 
    plane.Reset(seed_position, seed_point->normal);
    bbox.Union(seed_position);
  }
  else {
    // Temporary
    RNAbort("Need seed point");
  }

  // Update based on points
  if (points && (points->NEntries() > 0)) {
    // Allocate arrays of point positions and weights
    const int max_positions = 1024;
    R3Point *positions = new R3Point [ max_positions ];
    RNScalar *weights = new RNScalar [ max_positions ];

    // Fill arrays of point positions and weights
    int npositions = 0;
    int skip = points->NEntries() / max_positions + 1;
    for (int i = 0; i < points->NEntries(); i += skip) {
      R3SegmentationPoint *point = points->Kth(i);
      if (npositions >= max_positions-1) break;
      positions[npositions] = point->position;
      if (primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE)
        weights[npositions] = fabs(plane.Normal().Dot(point->normal));
      else if (primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE)
        weights[npositions] = 1.0 - fabs(plane.Normal().Dot(point->normal));
      else weights[npositions] = 1.0;
      npositions++;
    }

    // Add seed point with 20% of the total weight
    if (seed_point) {
      positions[npositions] = seed_point->position;
      weights[npositions] = 0.2 * points->NEntries();
      npositions++;
    }

    // Compute centroid
    centroid = R3Centroid(npositions, positions, weights);

    // Update primitive parameters
    if ((primitive_type == R3_SEGMENTATION_NULL_PRIMITIVE_TYPE) && (npositions >= 2)) {
      RNScalar variances[3];
      R3Triad axes = R3PrincipleAxes(centroid, npositions, positions, weights, variances);
      if (variances[0] > RN_EPSILON) {
        if (variances[1] > RN_EPSILON) {
          RNScalar ratio10 = variances[1] / variances[0];
          RNScalar ratio21 = variances[2] / variances[1];
          if (ratio10 < ratio21) {
            primitive_type = R3_SEGMENTATION_LINE_PRIMITIVE_TYPE;
            line.Reset(centroid, axes[0]);
          }
          else {
            primitive_type = R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE;
            plane.Reset(centroid, axes[2]);
          }
        }
      }
    }
    else if ((primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) && (npositions >= 2)) {
      // Compute principle directions
      RNScalar variances[3];
      R3Triad axes = R3PrincipleAxes(centroid, npositions, positions, weights, variances);
      if (variances[0] > RN_EPSILON) {
        // Update line
        R3Vector direction = axes[0];
        line.Reset(centroid, direction);

        // Check if should flip line
        RNScalar dot = direction.Dot(previous_vector);
        if (dot < 0) line.Flip();
      }
    }
    else if ((primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) && (npositions >= 3)) {
      // Compute principle directions
      RNScalar variances[3];
      R3Triad axes = R3PrincipleAxes(centroid, npositions, positions, weights, variances);
      if (variances[1] > RN_EPSILON) {
        // Update plane
        R3Vector normal = axes[2];
        plane.Reset(centroid, normal);

        // Check if should flip plane
        if (seed_point) {
          RNScalar dot = normal.Dot(seed_point->normal);
          if (dot < 0) plane.Flip();
        }
        else {
          RNScalar dot = normal.Dot(previous_vector);
          if (dot < 0) plane.Flip();
        }
      }
    }

#if 0
    // Rasterize planar grid
    if (primitive_type == R3_SEGMENTATION_PLANAR_GRID_PRIMITIVE_TYPE) {
      // Rasterize points into planar grid
      R3PlanarGrid density(plane, bbox, min_cluster_spacing);
      for (int i = 0; i < points->NEntries(); i++) {
        R3SegmentationPoint *point = points->Kth(i);
        R3Point position = point->position;
        density.RasterizeWorldR3SegmentationPoint(position.X(), position.Y(), position.Z(), 1.0);
      }

      // Reset planar grid
      planar_grid.Reset(plane, bbox, min_cluster_spacing);
      R3Point seed_position = seed_point->position;
      R2R3SegmentationPoint grid_position = planar_grid.GridPosition(seed_position);
      int ix = (int) (grid_position.X() + 0.5);
      int iy = (int) (grid_position.Y() + 0.5);
      FloodCopy(density.grid, planar_grid.grid, ix, iy);
    }
#endif

    // Delete array of point positions and weights
    delete [] positions;
    delete [] weights;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r12
leaq 0x50(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x9ec02
movl (%r12), %eax
cmpl $0x2, %eax
jne 0x2908c
leaq 0x70(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x9f38e
movl (%r12), %eax
cmpl $0x3, %eax
jne 0x290a3
leaq 0x88(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x9f38e
leaq 0x8(%r12), %r14
leaq 0x10(%r12), %rdi
leaq 0x146bb4(%rip), %rbx    # 0x16fc68
leaq 0x8(%rbx), %rsi
callq 0x9e4d6
leaq 0x28(%r12), %rdi
addq $0x20, %rbx
movq %rbx, %rsi
callq 0x9e4d6
testq %r13, %r13
je 0x290fc
cmpl $0x0, 0xc(%r13)
jle 0x290fc
xorl %ebx, %ebx
movq (%r13), %rax
movq (%rax,%rbx,8), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x880f2
incq %rbx
movslq 0xc(%r13), %rax
cmpq %rax, %rbx
jl 0x290dc
testq %rbp, %rbp
je 0x29168
leaq 0x8(%rbp), %rsi
leaq 0x80(%rsp), %r15
movq %r15, %rdi
callq 0x9e17e
leaq 0x40(%r12), %rdi
movq %r15, %rsi
callq 0x9e4d6
leaq 0x58(%r12), %rdi
leaq 0x70(%r12), %rbx
movq %r15, %rsi
callq 0x9e4d6
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x9f38e
movq %rbx, %rdi
callq 0x9ef18
leaq 0x88(%r12), %rdi
leaq 0x20(%rbp), %rdx
movq %r15, %rsi
callq 0x9d4c0
movq %r14, %rdi
movq %r15, %rsi
callq 0x880f2
jmp 0x29176
leaq 0xe114d(%rip), %rdi     # 0x10a2bc
xorl %eax, %eax
callq 0xc5fc2
testq %r13, %r13
je 0x29549
cmpl $0x0, 0xc(%r13)
jle 0x29549
movl $0x6000, %edi           # imm = 0x6000
callq 0x15040
movq %rax, %r14
xorl %ebx, %ebx
leaq (%r14,%rbx), %rdi
callq 0x9e16c
addq $0x18, %rbx
cmpq $0x6000, %rbx           # imm = 0x6000
jne 0x29199
movq %rbp, 0x18(%rsp)
movl $0x2000, %edi           # imm = 0x2000
callq 0x15040
movq %rax, 0x28(%rsp)
movl 0xc(%r13), %r15d
testl %r15d, %r15d
jle 0x29291
shrl $0xa, %r15d
incq %r15
movq %r12, 0x8(%rsp)
leaq 0x88(%r12), %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq %r14, 0x10(%rsp)
xorl %ebp, %ebp
cmpq $0x3ff, %rbp            # imm = 0x3FF
je 0x29295
movq (%r13), %rax
movq (%rax,%r12,8), %rbx
leaq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x9e4d6
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl $0x2, %eax
je 0x29248
movsd 0xdfe28(%rip), %xmm0    # 0x109050
cmpl $0x3, %eax
jne 0x29271
addq $0x20, %rbx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x9ee8e
andpd 0xe032a(%rip), %xmm0    # 0x109570
jmp 0x29271
addq $0x20, %rbx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x9ee8e
movapd %xmm0, %xmm1
andpd 0xe030b(%rip), %xmm1    # 0x109570
movsd 0xdfde3(%rip), %xmm0    # 0x109050
subsd %xmm1, %xmm0
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
addq %r15, %r12
addq $0x18, %r14
cmpl %r12d, 0xc(%r13)
jg 0x291f3
jmp 0x2929a
xorl %ebp, %ebp
jmp 0x292a4
movl $0x3ff, %ebp            # imm = 0x3FF
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
testq %rax, %rax
movq 0x28(%rsp), %r15
je 0x292df
leaq 0x8(%rax), %rsi
movl %ebp, %ebx
leaq (%rbx,%rbx,2), %rax
leaq (%r14,%rax,8), %rdi
callq 0x9e4d6
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%r13), %xmm0
mulsd 0xe0d99(%rip), %xmm0    # 0x10a070
movsd %xmm0, (%r15,%rbx,8)
incl %ebp
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0x6dedd
leaq 0x40(%r12), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x9e4d6
movl (%r12), %eax
testl %eax, %eax
setne %cl
cmpl $0x2, %ebp
setl %dl
orb %cl, %dl
jne 0x293c1
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rbx
movq %r13, %rsi
movl %ebp, %edx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0x6df67
movsd (%rbx), %xmm0
leaq 0x1459ff(%rip), %rax    # 0x16ed48
movsd (%rax), %xmm2
ucomisd %xmm2, %xmm0
jbe 0x29539
movsd 0x38(%rsp), %xmm1
ucomisd %xmm2, %xmm1
jbe 0x29539
movapd %xmm1, %xmm2
movhpd 0x40(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
divpd %xmm0, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
ucomisd %xmm2, %xmm0
jbe 0x294f4
movl $0x2, (%r12)
leaq 0x58(%r12), %rdi
movq %r13, %rsi
callq 0x9e4d6
addq $0x70, %r12
leaq 0x80(%rsp), %rsi
movq %r12, %rdi
callq 0x9f38e
movq %r12, %rdi
callq 0x9ef18
jmp 0x29539
cmpl $0x2, %ebp
setl %cl
cmpl $0x2, %eax
setne %dl
orb %cl, %dl
jne 0x2946b
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rbx
movq %r13, %rsi
movl %ebp, %edx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0x6df67
movsd (%rbx), %xmm0
leaq 0x145948(%rip), %rax    # 0x16ed48
ucomisd (%rax), %xmm0
jbe 0x29539
leaq 0x68(%rsp), %rbx
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
callq 0x9ec12
leaq 0x58(%r12), %rdi
movq %r13, %rsi
callq 0x9e4d6
addq $0x70, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x9f38e
movq %r12, %rdi
callq 0x9ef18
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x9ee8e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x29539
movq %r12, %rdi
callq 0x9ef70
jmp 0x29539
cmpl $0x3, %eax
setne %al
cmpl $0x3, %ebp
setl %cl
orb %al, %cl
jne 0x29539
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rbx
movq %r13, %rsi
movl %ebp, %edx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0x6df67
movsd 0x8(%rbx), %xmm0
leaq 0x14589d(%rip), %rax    # 0x16ed48
ucomisd (%rax), %xmm0
jbe 0x29539
leaq 0xb0(%rsp), %rsi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x9ec12
addq $0x88, %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x9d4c0
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x29518
addq $0x20, %rsi
leaq 0x68(%rsp), %rdi
jmp 0x29522
movl $0x3, (%r12)
addq $0x88, %r12
leaq 0xb0(%rsp), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x9d4c0
jmp 0x29539
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x9ee8e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x29539
movq %r12, %rdi
callq 0x2e594
movq %r14, %rdi
callq 0x15810
movq %r15, %rdi
callq 0x15810
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x6000, %esi           # imm = 0x6000
movq %r14, %rdi
callq 0x15c20
movq %rbx, %rdi
callq 0x15ad0
nop