// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3AreaLight.cpp
// Function: gaps::R3AreaLight::IntensityAtPoint(gaps::R3Point const&) const
RNScalar R3AreaLight::
IntensityAtPoint(const R3Point& point) const
{
    // Parameter ????
    const int max_samples = 4;

    // Check if light is active
    if (!IsActive()) return 0.0;

    // Check if point is in front of light 
    if (R3SignedDistance(circle.Plane(), point) <= 0) return 0.0;

    // Get circle axes
    R3Vector direction = circle.Normal();
    RNDimension dim = direction.MinDimension();
    R3Vector axis1 = direction % R3xyz_triad[dim];
    axis1.Normalize();
    R3Vector axis2 = direction % axis1;
    axis2.Normalize();

    // Sample points on light source
    int sample_count = 0;
    RNScalar sample_sum = 0.0;
    while (sample_count < max_samples) {
        // Sample point in circle
        RNScalar r1 = 2.0 * RNRandomScalar() - 1.0;
        RNScalar r2 = 2.0 * RNRandomScalar() - 1.0;
        if (r1*r1 + r2*r2 > 1) continue;
        R3Point sample_point = Position();
        sample_point += r1 * Radius() * axis1;
        sample_point += r2 * Radius() * axis2;
        sample_count++;
        
        // Compute direction at point
        R3Vector L = sample_point - point;
        L.Normalize();

        // Compute intensity at point
        RNScalar I = Intensity() * Direction().Dot(-L);
        if (RNIsNegativeOrZero(I)) return 0.0;
        RNLength d = R3Distance(point, sample_point);
        RNScalar denom = constant_attenuation;
        denom += d * linear_attenuation;
        denom += d * d * quadratic_attenuation;
        if (RNIsPositive(denom)) I /= denom;

        // Add to result
        sample_sum += I;
    }

    // Return average
    RNArea area = circle.Area();
    RNScalar sample_mean = sample_sum;
    if (sample_count > 0) sample_mean /= sample_count;
    return area * sample_mean;
}

// Assembly:
cmpl $0x0, 0x20(%rdi)
je 0x40c28
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x78(%rdi), %r15
movq %r15, %rdi
callq 0x4e2e7
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x40c32
leaq 0x50(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
callq 0x75e6e
movq %r12, %rdi
callq 0x75faa
cltq
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xfc587(%rip), %rdx     # 0x13cfa0
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x76792
movq %r13, %rdi
callq 0x76174
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %r13
callq 0x76792
movq %rbp, %rdi
callq 0x76174
leaq 0x58(%r14), %rax
movq %rax, 0x38(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r12d, %r12d
leaq 0xa8(%rsp), %rbp
leaq 0xfd665(%rip), %r14     # 0x13e0d8
callq 0x89368
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x99a48(%rip), %xmm0    # 0xda4d0
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x89368
addsd %xmm0, %xmm0
addsd 0x99a2d(%rip), %xmm0    # 0xda4d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd 0x99557(%rip), %xmm1    # 0xda018
ja 0x40bf9
movl %r12d, 0xc(%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movsd %xmm0, 0x40(%rsp)
callq 0x753da
movsd 0x10(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
leaq 0x90(%rsp), %r12
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
callq 0x767fa
movq %rbp, %rdi
movq %r12, %rsi
callq 0x757ac
movsd 0x40(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x767fa
movq %rbp, %rdi
movq %r12, %rsi
callq 0x757ac
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x75626
movq %r12, %rdi
callq 0x76174
movsd 0x28(%r13), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x76a98
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x760ea
mulsd 0x10(%rsp), %xmm0
movsd (%r14), %xmm1
ucomisd %xmm0, %xmm1
jae 0x40c2e
movl 0xc(%rsp), %r12d
incl %r12d
movq %rbx, %rdi
movq %rbp, %rsi
movapd %xmm0, 0x10(%rsp)
callq 0x4e0d4
movsd 0xa0(%r13), %xmm1
mulsd %xmm0, %xmm1
addsd 0x98(%r13), %xmm1
mulsd %xmm0, %xmm0
mulsd 0xa8(%r13), %xmm0
addsd %xmm1, %xmm0
movsd (%r14), %xmm1
movapd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm2
divsd %xmm0, %xmm2
cmpnltsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andnpd %xmm2, %xmm0
andpd %xmm3, %xmm1
orpd %xmm0, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
cmpl $0x4, %r12d
jl 0x40a73
movq 0x28(%rsp), %rdi
callq 0x676ea
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movsd (%rsp), %xmm2
divsd %xmm1, %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
jmp 0x40c32
xorpd %xmm1, %xmm1
jmp 0x40c43
xorpd %xmm1, %xmm1
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movapd %xmm1, %xmm0
retq