コード例 #1
0
void testCosineDistribution()
{
    Plot2D plot_radial_hist( output_path + "/cosine_radial_hist.png", plot_size, plot_size );
    Plot2D plot_angle_hist( output_path + "/cosine_angle_hist.png", plot_size, plot_size, -0.5f * M_PI, 0.5f * M_PI );
    float x = 0.0f, y = 0.0f, z = 0.0f;

    // Build a histogram of random angles using a cosine distribution
    const int bins = 30;
    const float angle_range = 0.5f * M_PI;
    const float bin_width = angle_range / bins;
    int hist[bins] = {};

    for( auto i = 0; i < 1000000; i++ ) {
        float angle = rng.cosineQuarterWave();
        x = cosf( angle );
        y = sinf( angle );
        int bin = angle / angle_range * (bins - 1);
        hist[bin]++;
    }

    int *max_it = std::max_element( hist, hist + bins );
    int hist_max = *max_it;

    plot_radial_hist.drawAxes();
    plot_angle_hist.drawAxes();

    // Draw histogram bins
    plot_radial_hist.strokeColor( 1.0, 0.0, 0.0 );
    plot_angle_hist.strokeColor( 1.0, 0.0, 0.0 );
    for( auto i = 0; i < bins; i++ ) {
        float angle = (float) i / (bins - 1) * angle_range;
        float r = hist[i] / (float) hist_max;
        float x1 = cosf( angle ) * r;
        float y1 = sinf( angle ) * r;
        angle = (float) (i + 1) / (bins - 1) * angle_range;
        float x2 = cosf( angle ) * r;
        float y2 = sinf( angle ) * r;

        plot_radial_hist.drawLine( x1, y1, 0.0, 0.0 );
        plot_radial_hist.drawLine( x1, y1, x2, y2 );
        plot_radial_hist.drawLine( 0.0, 0.0, x2, y2 );
        plot_angle_hist.drawLine( angle, 0.0, angle, r );
        plot_angle_hist.drawLine( angle, r, angle + bin_width, r );
        plot_angle_hist.drawLine( angle + bin_width, r, angle + bin_width, 0.0 );
    }

    // Reference
    plot_angle_hist.strokeColor( 1.0, 0.0, 0.0 );
    for( auto i = 0; i < 100; i++ ) {
        float angle1 = (float) i / (bins - 1) * angle_range;
        float angle2 = (float) (i + 1) / (bins - 1) * angle_range;
        float r1 = cos(angle1);
        float r2 = cos(angle2);
        plot_angle_hist.drawLine( angle1, r1, angle2, r2 );
    }
}