// File: /workspace/github_cpp_makefile_star2_O1/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 0x410ca
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 0x4ee87
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x410d4
leaq 0x50(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
callq 0x76d62
movq %r12, %rdi
callq 0x76eaa
cltq
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xfe10b(%rip), %rdx     # 0x13efa0
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x776a6
movq %r13, %rdi
callq 0x77074
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x776a6
movq %rbp, %rdi
callq 0x77074
leaq 0x58(%r14), %rax
movq %rax, 0x38(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r12d, %r12d
leaq 0x48(%rsp), %rbp
leaq 0xff1ef(%rip), %r15     # 0x1400d8
callq 0x8a700
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x9b5d2(%rip), %xmm0    # 0xdc4d0
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x8a700
addsd %xmm0, %xmm0
addsd 0x9b5b7(%rip), %xmm0    # 0xdc4d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
movl $0x2, %eax
ucomisd 0x9b0dc(%rip), %xmm1    # 0xdc018
jbe 0x40f48
movsd (%rsp), %xmm0
jmp 0x41092
movl %r12d, 0xc(%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movsd %xmm0, 0x40(%rsp)
callq 0x762ca
movsd 0x10(%rsp), %xmm0
mulsd 0x70(%r14), %xmm0
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
callq 0x77722
movq %rbp, %rdi
movq %r12, %rsi
callq 0x766a2
movsd 0x40(%rsp), %xmm0
mulsd 0x70(%r14), %xmm0
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
callq 0x77722
movq %rbp, %rdi
movq %r12, %rsi
callq 0x766a2
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x7651c
movq %r12, %rdi
callq 0x77074
movsd 0x28(%r14), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %r13
movq %r15, %rbx
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x779e8
movq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %r15
callq 0x76fea
mulsd 0x10(%rsp), %xmm0
movsd (%rbx), %xmm1
movl $0x1, %eax
ucomisd %xmm0, %xmm1
jae 0x4107d
movq %r13, %rdi
movq %rbp, %rsi
movapd %xmm0, 0x10(%rsp)
callq 0x4ec74
movsd 0xa0(%r14), %xmm1
mulsd %xmm0, %xmm1
addsd 0x98(%r14), %xmm1
mulsd %xmm0, %xmm0
mulsd 0xa8(%r14), %xmm0
addsd %xmm1, %xmm0
movsd (%r15), %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
xorl %eax, %eax
jmp 0x41082
movsd (%rsp), %xmm0
movq %r13, %rbx
movl 0xc(%rsp), %r12d
incl %r12d
leaq 0x48(%rsp), %rbp
testb $0x1, %al
jne 0x410d0
movsd %xmm0, (%rsp)
cmpl $0x4, %r12d
jl 0x40ee9
movq 0x28(%rsp), %rdi
callq 0x6847a
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movsd (%rsp), %xmm2
divsd %xmm1, %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
jmp 0x410d4
xorpd %xmm1, %xmm1
jmp 0x410e5
xorpd %xmm1, %xmm1
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movapd %xmm1, %xmm0
retq