// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%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, 0x8(%rsp)
testq %r12, %r12
je 0x14067
shlq $0x3, %r12
leaq (%r12,%r12,2), %r14
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdi
callq 0x3c530
addq $0x18, %r12
cmpq %r12, %r14
jne 0x14050
xorpd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14240
movl 0x14(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
jle 0x14227
movq %rcx, 0x18(%rsp)
movq 0x8(%rsp), %r13
xorl %r14d, %r14d
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %rsi
addq $0x18, %rsi
movq %r13, %rdi
callq 0x3c89a
incq %r14
movslq 0x14(%rbx), %rax
addq $0x18, %r13
cmpq %rax, %r14
jl 0x14095
testl %eax, %eax
movq 0x18(%rsp), %rcx
jle 0x14227
xorl %r14d, %r14d
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %r13
movslq 0x70(%r13), %rax
leaq (%rax,%rax,2), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
leaq 0x38(%rsp), %rdi
callq 0x3c542
movq %rbx, %rdi
movq %r13, %rsi
callq 0x12d4e
movsd %xmm0, 0x20(%rsp)
ucomisd 0x8e0e3(%rip), %xmm0    # 0xa21e8
jbe 0x14212
cmpl $0x0, 0x14(%r13)
movsd 0x8df20(%rip), %xmm0    # 0xa2038
jle 0x141f2
xorl %r12d, %r12d
movsd 0x8df0f(%rip), %xmm0    # 0xa2038
movq 0x8(%r13), %rax
movq (%rax,%r12,8), %rbp
movq 0x8(%rbp), %rax
cmpq %r13, %rax
jne 0x1413e
movq 0x10(%rbp), %rax
movsd %xmm0, 0x30(%rsp)
movslq 0x70(%rax), %r15
testb $0x2, 0x38(%rbp)
jne 0x1415d
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x2b8(%rax)
leaq (%r15,%r15,2), %rax
movq 0x8(%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 0x8deaa(%rip), %xmm3    # 0xa2038
andnpd %xmm3, %xmm2
orpd %xmm1, %xmm2
mulsd %xmm0, %xmm0
movapd %xmm2, %xmm1
mulsd 0x8e05a(%rip), %xmm1    # 0xa2200
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0xe7f0
movsd %xmm0, 0x28(%rsp)
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3cf76
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x3ce6c
movsd 0x30(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
incq %r12
movslq 0x14(%r13), %rax
cmpq %rax, %r12
jl 0x14129
leaq 0x50(%rsp), %r15
movq %r15, %rdi
leaq 0x38(%rsp), %rsi
callq 0x3cf98
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x13858
incq %r14
movslq 0x14(%rbx), %rax
cmpq %rax, %r14
jl 0x140c9
movq 0x18(%rsp), %rcx
incl %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x14080
movq 0x8(%rsp), %rdi
callq 0xe5b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0
nop