C++ (Cpp) Vector4 Examples

C++ (Cpp) Vector4 - 30 examples found. These are the top rated real world C++ (Cpp) examples of Vector4 extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
// ------------------------------------------------------------
// Simple ray intersection
// ------------------------------------------------------------
void testRayIntersect()
{
    Plot2D plot( output_path + "/ray_intersect.png", plot_size, plot_size,
                 -1.0, 1.0, -1.0, 1.0 );

    // Slab with top on z=0 plane
    AxisAlignedSlab slab( -1.0, -1.0, -1.0, 1.0, 0.0, 1.0 );
    plot.drawLine( slab.xmin, slab.ymax, slab.xmax, slab.ymax );

    auto o = Vector4( -0.95, 0.95, 0.0 );
    auto d = Vector4( 1.0, -1.0, 0.0 ).normalized();
    auto tip = add( o, d );
    auto ray = Ray( o, d );
    RayIntersection ri;

    plot.drawCircle( o.x, o.y, 0.01 );
    plot.drawLine( o, tip );

    if( slab.intersect( ray, ri ) ) {
        auto &p = ri.position;
        //plot.addPoint<Vector4>( p );
        plot.drawCircle( p.x, p.y, 0.01 );
        plot.drawLine( p, add( p, ri.normal) );
        plot.drawLine( p, add( p, mirror( d.negated(), ri.normal ) ) );
    }
}
Example #2
0
// ------------------------------------------------------------
// Timing
// ------------------------------------------------------------
void testRayObjectTiming(Traceable & traceable, const char * name, bool test_any = false)
{
    std::vector<Ray> rays;
    int ray_pool_size = 1000;
    int num_rays = 100000000;
    //int num_rays = 10000;
    double timer_cutoff = 5.0;

    for( int i = 0; i < ray_pool_size; i++ ) {
        Ray ray;
        float x = rng.uniformRange( -1.0, 1.0 );
        float y = rng.uniformRange( -1.0, 1.0 );
        ray.origin = Vector4( x, y, -10 );
        ray.direction = Vector4( 0, 0, 1, 0 );
        rays.push_back( ray );
    }

    RayIntersection isect;
    bool hit;

    Timer timer;
    timer.start();
    for( int i = 0; i < num_rays; i++ ) {
        int j = i % ray_pool_size;
        if( test_any ) {
            hit = traceable.intersectsAny( rays[j], 0.01 );
        }
        else {
            hit = traceable.intersect( rays[j], isect );
        }
        if( i % 1000 == 0
                && timer.elapsed() > timer_cutoff ) {
            num_rays = i + 1;
            break;
        }
    }
    timer.stop();
    float elapsed = timer.elapsed();
    float rays_per_second = (float) num_rays / elapsed;
    printf("%s (any=%d) : %d rays in %f seconds = %.2f rays / sec\n",
           name, (int)test_any, num_rays, elapsed, rays_per_second);
}
Example #3
0
void testReflectAngles()
{
    Plot2D plot( output_path + "/reflect_angles.png", plot_size, plot_size,
                 -2.0, 2.0, -2.0, 2.0 );

    // Slab with top on z=0 plane
    AxisAlignedSlab slab( -2.0, -2.0, -2.0, 2.0, 0.0, 2.0 );
    plot.drawLine( slab.xmin, slab.ymax, slab.xmax, slab.ymax );

    auto o = Vector4( -0.95, 0.95, 0.0 );

    float radius = 2.0;
    int nsteps = 30;
    for( int i = 1; i <= nsteps; i++ ) {
        float alpha = (float) i / (nsteps + 1);
        float angle = (1.0 - alpha) * M_PI + alpha * M_PI / 2.0;
        plot.strokeColor( alpha, 1.0, 1.0 - alpha );
        testReflectAnglesHelper( Vector4( radius*cos(angle), radius*sin(angle), 0.0 ), slab, plot );
    }
}
Example #4
0
void testRefractAnglesHelper( const Vector4 & o, Traceable & obj, Plot2D & plot,
                              float n1, float n2 )
{
    auto at = Vector4( 0.0, 0.0, 0.0 );
    auto d = subtract( at, o ).normalized();
    auto tip = add( o, d );
    auto ray = Ray( o, d );
    RayIntersection ri;

    plot.drawCircle( o.x, o.y, 0.01 );
    plot.drawLine( o, tip );

    if( obj.intersect( ray, ri ) ) {
        auto &p = ri.position;
        //plot.addPoint<Vector4>( p );
        //plot.drawCircle( p.x, p.y, 0.01 );
        //plot.drawLine( p, add( p, ri.normal) );

        Vector4 in = d.negated();

        Vector4 refracted = refract( in, ri.normal, n1, n2 );

        //r = scale(r, fresnelDialectric( dot( in, ri.normal ),
        //                                dot( r, ri.normal.negated() ),
        //                                n1, n2 ) );
        float cos_in = dot( in, ri.normal );
        float cos_out = dot( refracted, ri.normal.negated() );
        float f = fresnelDialectric( cos_in, cos_out, n1, n2 );
        refracted = scale(refracted, 1.0 - f);

        // Mark rays that saw total internal reflection
        if( refracted.magnitude() < 0.001 ) {
            plot.drawCircle( o.x, o.y, 0.03 );
        }

        plot.drawLine( p, add( p, refracted ) );

        // Draw reflected ray
        if( refracted.magnitude() < 1.0 ) {
            Vector4 reflected = mirror( in, ri.normal );
            reflected = scale( reflected, f );
            plot.drawLine( p, add( p, reflected ) );
        }
    }
}
Example #5
0
void testReflectAnglesHelper( const Vector4 & o, Traceable & obj, Plot2D & plot )
{
    auto at = Vector4( 0.0, 0.0, 0.0 );
    auto d = subtract( at, o ).normalized();
    auto tip = add( o, d );
    auto ray = Ray( o, d );
    RayIntersection ri;

    plot.drawCircle( o.x, o.y, 0.01 );
    plot.drawLine( o, tip );

    if( obj.intersect( ray, ri ) ) {
        auto &p = ri.position;
        //plot.addPoint<Vector4>( p );
        plot.drawCircle( p.x, p.y, 0.01 );
        plot.drawLine( p, add( p, ri.normal) );
        plot.drawLine( p, add( p, mirror( d.negated(), ri.normal ) ) );
    }
}
Example #6
0
void testRefractForIndices( Plot2D & plot, float n1, float n2 )
{
    // Slab with top on z=0 plane
    AxisAlignedSlab slab( -2.0, -2.0, -2.0, 2.0, 0.0, 2.0 );
    plot.drawLine( slab.xmin, slab.ymax, slab.xmax, slab.ymax );

    //float radius = 2.0;
    float radius = 1.5;
    int nsteps = 40;

    for( int i = 1; i <= nsteps; i++ ) {
        float alpha = (float) i / (nsteps + 2);
        float angle = (1.0 - alpha) * M_PI + alpha * M_PI / 2.0;
        if( i == nsteps / 2 )
            plot.strokeColor( 0.0, 1.0, 0.0 );
        else
            plot.strokeColor( alpha, 0.0, 1.0 - alpha );
        testRefractAnglesHelper( Vector4( radius*cos(angle), radius*sin(angle), 0.0 ), slab, plot, n1, n2 );
    }
}