// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::Smooth(double)
void R3Mesh::
Smooth(RNScalar factor)
{
  // Allocate buffer for copy of vertex positions
  R3Point *positions = new R3Point [ NVertices() ];
  if (!positions) RNAbort("Unable to allocate vertex positions");
    
  // Iteratively average vertex positions
  for (int iter = 0; iter < factor; iter++) {
    // Copy vertex positions
    for (int i = 0; i < NVertices(); i++) {
      R3MeshVertex *vertex = Vertex(i);
      positions[i] = VertexPosition(vertex);
    }

    // Update vertex positions
    for (int i = 0; i < NVertices(); i++) {
      R3MeshVertex *vertex = Vertex(i);
      RNScalar weight = 1;
      R3Point position = positions[vertex->id];
      RNLength radius = VertexAverageEdgeLength(vertex);
      if (radius > 0) {
        for (int j = 0; j < VertexValence(vertex); j++) {
          R3MeshEdge *edge = EdgeOnVertex(vertex, j);
          R3MeshVertex *neighbor_vertex = VertexAcrossEdge(edge, vertex);
          const R3Point& neighbor_position = positions[neighbor_vertex->id];
          RNLength length = EdgeLength(edge);
          RNLength sigma = (length > 0) ? length / radius : 1;
          RNScalar w = exp((length * length) / (-2.0 * sigma * sigma));
          position += w * neighbor_position;
          weight += w;
        }
      
        // Update vertex position
        R3Point smooth_position = position / weight;
        SetVertexPosition(vertex, smooth_position);
      }
    }
  }
  
  // Delete copy of vertex positions
  delete [] positions;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
movslq 0x14(%rdi), %r12
movl $0x18, %ecx
movq %r12, %rax
mulq %rcx
movq $-0x1, %r15
cmovnoq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, (%rsp)
testq %r12, %r12
je 0x14041
shlq $0x3, %r12
leaq (%r12,%r12,2), %r14
xorl %r12d, %r12d
movq (%rsp), %rax
leaq (%rax,%r12), %rdi
callq 0x3ca24
addq $0x18, %r12
cmpq %r12, %r14
jne 0x1402b
xorpd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14214
xorl %eax, %eax
leaq 0x50(%rsp), %r12
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x14(%rbx)
jle 0x14092
movq (%rsp), %r13
xorl %r14d, %r14d
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %rsi
addq $0x18, %rsi
movq %r13, %rdi
callq 0x3cd94
incq %r14
movslq 0x14(%rbx), %rax
addq $0x18, %r13
cmpq %rax, %r14
jl 0x1406e
cmpl $0x0, 0x14(%rbx)
jle 0x141f6
xorl %r14d, %r14d
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %r13
movslq 0x70(%r13), %rax
leaq (%rax,%rax,2), %rax
movq (%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
leaq 0x38(%rsp), %rdi
callq 0x3ca36
movq %rbx, %rdi
movq %r13, %rsi
callq 0x12ce6
movsd %xmm0, 0x20(%rsp)
ucomisd 0x8e10e(%rip), %xmm0    # 0xa21e8
jbe 0x141e6
movq %r14, 0x18(%rsp)
cmpl $0x0, 0x14(%r13)
movsd 0x8df46(%rip), %xmm0    # 0xa2038
jle 0x141c6
xorl %r14d, %r14d
movsd 0x8df35(%rip), %xmm0    # 0xa2038
movq 0x8(%r13), %rax
movq (%rax,%r14,8), %rbp
movq 0x8(%rbp), %rax
cmpq %r13, %rax
jne 0x14118
movq 0x10(%rbp), %rax
movsd %xmm0, 0x30(%rsp)
movslq 0x70(%rax), %r15
testb $0x2, 0x38(%rbp)
jne 0x14137
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x2b8(%rax)
leaq (%r15,%r15,2), %rax
movq (%rsp), %rcx
leaq (%rcx,%rax,8), %r15
movsd 0x28(%rbp), %xmm0
movapd %xmm0, %xmm1
divsd 0x20(%rsp), %xmm1
xorpd %xmm2, %xmm2
cmpltsd %xmm0, %xmm2
andpd %xmm2, %xmm1
movsd 0x8ded1(%rip), %xmm3    # 0xa2038
andnpd %xmm3, %xmm2
orpd %xmm1, %xmm2
mulsd %xmm0, %xmm0
movapd %xmm2, %xmm1
mulsd 0x8e081(%rip), %xmm1    # 0xa2200
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0xe7f0
movsd %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x3d470
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x3d366
movsd 0x30(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
incq %r14
movslq 0x14(%r13), %rax
cmpq %rax, %r14
jl 0x14103
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
callq 0x3d492
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x13808
movq 0x18(%rsp), %r14
incq %r14
movslq 0x14(%rbx), %rax
cmpq %rax, %r14
jl 0x1409f
movq 0x10(%rsp), %rax
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1405c
movq (%rsp), %rdi
callq 0xe5b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0
nop