// File: /workspace/github_cpp_makefile_star2_O2/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 0x3c8be
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 0x4834c
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x3c8ea
leaq 0x50(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
callq 0x6c044
movq %r12, %rdi
callq 0x6c17e
cltq
imulq $0x18, %rax, %rdx
addq 0xf18d5(%rip), %rdx     # 0x12dfa8
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x6c6e6
movq %r13, %rdi
callq 0x6c336
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %r13
callq 0x6c6e6
movq %rbp, %rdi
callq 0x6c336
leaq 0x58(%r14), %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0xa8(%rsp), %rbp
leaq 0xf29cb(%rip), %r14     # 0x12f0f8
cmpl $0x3, %r12d
jg 0x3c8c4
callq 0x7dcd2
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x88d84(%rip), %xmm0    # 0xc54d0
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x7dcd2
addsd %xmm0, %xmm0
addsd 0x88d69(%rip), %xmm0    # 0xc54d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd 0x88893(%rip), %xmm1    # 0xc5018
ja 0x3c72d
movl %r12d, 0xc(%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movsd %xmm0, 0x40(%rsp)
callq 0x6b884
movsd 0x10(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
leaq 0x90(%rsp), %r12
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
callq 0x6c72a
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6bbc4
movsd 0x40(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x6c72a
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6bbc4
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x6ba51
movq %r12, %rdi
callq 0x6c336
movsd 0x28(%r13), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x6c99a
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x6c2ae
mulsd 0x10(%rsp), %xmm0
movsd (%r14), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3c8e6
movl 0xc(%rsp), %r12d
incl %r12d
movq %rbx, %rdi
movq %rbp, %rsi
movapd %xmm0, 0x10(%rsp)
callq 0x48146
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)
jmp 0x3c72d
xorpd %xmm1, %xmm1
jmp 0x3c8fb
movq 0x28(%rsp), %rdi
callq 0x5ee84
cvtsi2sd %r12d, %xmm1
movsd (%rsp), %xmm2
divsd %xmm1, %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
jmp 0x3c8ea
xorpd %xmm1, %xmm1
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movapd %xmm1, %xmm0
retq