Example #1
0
viewport::viewport(const vec &eyepos,
                   const vec &eyeforward,
                   const vec &eyeup,
                   double hangle,
                   double vangle)
 :
    mEyepos(eyepos),
    mEyeforward(eyeforward / norm(eyeforward, 2)),
    mEyeup((eyeup - dot(mEyeforward, eyeup)*mEyeforward)
           / norm(eyeup - dot(mEyeforward, eyeup)*mEyeforward, 2)),
    mHangle(hangle),
    mVangle(vangle)
{
    //cout << "Viewport constructor" << endl;

    mEyeright = cross(eyeforward, eyeup);

    //cout << rotmat(mEyeup, hangle/2.0) << endl;

    mEyel = rotmat(mEyeup, hangle/2.0) * mEyeforward;
    mEyer = rotmat(mEyeup, -hangle/2.0) * mEyeforward;

    mEyet = rotmat(mEyeright, vangle/2.0) * mEyeforward;
    mEyeb = rotmat(mEyeright, -vangle/2.0) * mEyeforward;
}
Example #2
0
File: phi.cpp Project: Horta/BASiCS
vec Phi::get(void) const
{
  vec phi(kappa.size() + 1);
  phi(0) = phi.size();
  phi.tail(kappa.size()) = phi(0) * exp(kappa) / sum(exp(kappa));
  return phi;
}
Example #3
0
AnyValue LogisticRegressionIRLS::transition(AbstractDBInterface &db,
    AnyValue args) {
    AnyValue::iterator arg(args);
    
    // Initialize Arguments from SQL call
    State state = *arg++;
    double y = *arg++ ? 1. : -1.;
    DoubleRow_const x = *arg++;

    // See MADLIB-138. At least on certain platforms and with certain versions,
    // LAPACK will run into an infinite loop if pinv() is called for non-finite
    // matrices. We extend the check also to the dependent variables.
    if (!boost::math::isfinite(y))
        throw std::invalid_argument("Dependent variables are not finite.");
    else if (!x.is_finite())
        throw std::invalid_argument("Design matrix is not finite.");

    if (state.numRows == 0) {
        state.initialize(db.allocator(AbstractAllocator::kAggregate), x.n_elem);
        if (!arg->isNull()) {
            const State previousState = *arg;
            
            state = previousState;
            state.reset();
        }
    }
    
    // Now do the transition step
    state.numRows++;

    // xc = x_i c
    double xc = as_scalar( x * state.coef );
        
    // a_i = sigma(x_i c) sigma(-x_i c)
    double a = sigma(xc) * sigma(-xc);
    
    // Note: sigma(-x) = 1 - sigma(x).
    //
    //             sigma(-y_i x_i c) y_i
    // z = x_i c + ---------------------
    //                     a_i
    double z = xc + sigma(-y * xc) * y / a;

    state.X_transp_Az += trans(x) * a * z;
    state.X_transp_AX += trans(x) * a * x;
        
    // We use state.sumy to store the log likelihood.
    //          n
    //         --
    // l(c) = -\  ln(1 + exp(-y_i * c^T x_i))
    //         /_
    //         i=1
    state.logLikelihood -= std::log( 1. + std::exp(-y * xc) );
    return state;
}
double DirectEvaluationSlaterWithJastrow::computeJastrowFactor() {
    double jastrowLogarithm = 0;
    for (unsigned int electron1 = 0; electron1 < m_system->getElectrons().size(); electron1++) {
        for (unsigned int electron2 = electron1+1; electron2 < m_system->getElectrons().size(); electron2++) {
            const double r12 = norm(m_system->getElectrons().at(electron1)->getPosition() -
                                  m_system->getElectrons().at(electron2)->getPosition());
            const int spin1 = m_system->getElectrons().at(electron1)->getSpin();
            const int spin2 = m_system->getElectrons().at(electron2)->getSpin();
            const double spinFactor = (spin1 == spin2) ? 0.25 : 0.5;

            jastrowLogarithm += spinFactor * r12 / (1 + r12 * m_beta);
        }
    }
    return exp(jastrowLogarithm);
}
Example #5
0
ray viewport::genray(double row, double col, double width, double height)
{
    vec hor_comp = -(col - width/2.0) / width * (mEyer - mEyel);
    //cout << "sub 1" << endl;
    vec ver_comp = -(row - height/2.0) / height * (mEyeb - mEyet);
    //cout << "sub 2" << endl;

    // if((int)floor(row)%100 == 0 && (int)floor(col)%100==0)
    // {
    //     cout << hor_comp << endl;
    //     cout << ver_comp << endl;
    //     cout << endl;
    // }

    ray generated(mEyepos, mEyeforward + hor_comp + ver_comp);
    generated.slope /= norm(generated.slope, 2);
    
    return generated;
}
Example #6
0
int main() {
  ofstream logfile("./expected.log");
  cout.rdbuf(logfile.rdbuf());
  set_stream_err1(logfile);
  set_stream_err2(logfile);

  arma_rng::set_seed(123456789);

  /**
   * Add comments to all classes below, before pushing a commit to avoid unnecessary conflicts.
   */

//  ExpectedDatum();
//  ExpectedGenColVec();
//  ExpectedGenColVecElemInd();
//  ExpectedGenColVecElemIndRange();
//  ExpectedGenColVecElemInds();
//  ExpectedGenColVecExp();
//  ExpectedGenColVecGenColVec();
//  ExpectedGenColVecGenColVecNormal();
//  ExpectedGenColVecGenDouble();
//  ExpectedGenColVecGenMat();
//  ExpectedGenColVecGenRowVec();
//  ExpectedGenColVecGenRowVecNormal();
//  ExpectedGenColVecMonColVec();
//  ExpectedGenColVecMonRowVec();
//  ExpectedGenColVecNormal();
//  ExpectedGenColVecNumElems();
//  ExpectedGenColVecNumRowsNumCols();
//  ExpectedGenColVecRowIndColInd();
//  ExpectedGenColVecRowIndColIndMatSize();
//  ExpectedGenColVecRowIndRangeColIndRange();
//  ExpectedGenColVecSort();
//  ExpectedGenColVecVecNormInt();
//  ExpectedGenColVecVecNormString();
//  ExpectedGenDouble();
//  ExpectedGenDoubleGenDouble();
//  ExpectedGenDoubleGenDoubleNumElems();
//  ExpectedGenMat();
//  ExpectedGenMatColInd();
//  ExpectedGenMatColIndRange();
//  ExpectedGenMatColInds();
//  ExpectedGenMatDim();
//  ExpectedGenMatElemInd();
//  ExpectedGenMatElemIndRange();
//  ExpectedGenMatElemInds();
//  ExpectedGenMatExp();
//  ExpectedGenMatGenColVec();
//  ExpectedGenMatGenDouble();
//  ExpectedGenMatGenMat();
//  ExpectedGenMatGenMatNormal();
//  ExpectedGenMatGenRowVec();
//  ExpectedGenMatMatNormInt();
//  ExpectedGenMatMatNormString();
//  ExpectedGenMatMonColVec();
//  ExpectedGenMatMonColVecDim();
//  ExpectedGenMatMonRowVec();
//  ExpectedGenMatMonRowVecDim();
//  ExpectedGenMatNormal();
//  ExpectedGenMatNormalDim();
//  ExpectedGenMatNumElems();
//  ExpectedGenMatNumElemsDim();
//  ExpectedGenMatNumRowsNumCols();
//  ExpectedGenMatRowInd();
//  ExpectedGenMatRowIndColInd();
//  ExpectedGenMatRowIndColIndMatSize();
//  ExpectedGenMatRowIndColIndRange();
//  ExpectedGenMatRowIndRange();
//  ExpectedGenMatRowIndRangeColInd();
//  ExpectedGenMatRowIndRangeColIndRange();
//  ExpectedGenMatRowInds();
//  ExpectedGenMatRowIndsColInds();
//  ExpectedGenMatSinValSel();
//  ExpectedGenMatSinValTol();
//  ExpectedGenMatSort();
//  ExpectedGenMatSortDim();
//  ExpectedGenMatVecNormInt();
//  ExpectedGenRowVec();
//  ExpectedGenRowVecElemInd();
//  ExpectedGenRowVecElemIndRange();
//  ExpectedGenRowVecElemInds();
//  ExpectedGenRowVecExp();
//  ExpectedGenRowVecGenColVec();
//  ExpectedGenRowVecGenColVecNormal();
//  ExpectedGenRowVecGenDouble();
//  ExpectedGenRowVecGenMat();
//  ExpectedGenRowVecGenRowVec();
//  ExpectedGenRowVecGenRowVecNormal();
//  ExpectedGenRowVecMonColVec();
//  ExpectedGenRowVecMonRowVec();
//  ExpectedGenRowVecNormal();
//  ExpectedGenRowVecNumElems();
//  ExpectedGenRowVecNumRowsNumCols();
//  ExpectedGenRowVecRowIndColInd();
//  ExpectedGenRowVecRowIndColIndMatSize();
//  ExpectedGenRowVecRowIndRangeColIndRange();
//  ExpectedGenRowVecSort();
//  ExpectedGenRowVecVecNormInt();
//  ExpectedGenRowVecVecNormString();
//  ExpectedInPlaceGenColVec();
//  ExpectedInPlaceGenColVecElemInd();
//  ExpectedInPlaceGenColVecElemIndGenColVec();
//  ExpectedInPlaceGenColVecElemIndGenDouble();
//  ExpectedInPlaceGenColVecElemIndGenMat();
//  ExpectedInPlaceGenColVecElemIndGenRowVec();
//  ExpectedInPlaceGenColVecElemIndRange();
//  ExpectedInPlaceGenColVecElemIndRangeGenColVec();
//  ExpectedInPlaceGenColVecElemIndRangeGenDouble();
//  ExpectedInPlaceGenColVecElemIndRangeGenMat();
//  ExpectedInPlaceGenColVecElemIndRangeGenRowVec();
//  ExpectedInPlaceGenColVecElemIndsGenColVec();
//  ExpectedInPlaceGenColVecElemIndsGenDouble();
//  ExpectedInPlaceGenColVecElemIndsGenMat();
//  ExpectedInPlaceGenColVecElemIndsGenRowVec();
//  ExpectedInPlaceGenColVecGenColVec();
//  ExpectedInPlaceGenColVecGenDouble();
//  ExpectedInPlaceGenColVecGenMat();
//  ExpectedInPlaceGenColVecGenRowVec();
//  ExpectedInPlaceGenColVecNumElems();
//  ExpectedInPlaceGenMat();
  ExpectedInPlaceGenMatColInd();
//  ExpectedInPlaceGenMatColIndGenColVec();
//  ExpectedInPlaceGenMatColIndGenDouble();
//  ExpectedInPlaceGenMatColIndGenMat();
//  ExpectedInPlaceGenMatColIndGenRowVec();
  ExpectedInPlaceGenMatColIndRange();
//  ExpectedInPlaceGenMatColIndRangeGenColVec();
//  ExpectedInPlaceGenMatColIndRangeGenDouble();
//  ExpectedInPlaceGenMatColIndRangeGenMat();
//  ExpectedInPlaceGenMatColIndRangeGenRowVec();
//  ExpectedInPlaceGenMatColIndsGenColVec();
//  ExpectedInPlaceGenMatColIndsGenDouble();
//  ExpectedInPlaceGenMatColIndsGenMat();
//  ExpectedInPlaceGenMatColIndsGenRowVec();
//  ExpectedInPlaceGenMatElemInd();
//  ExpectedInPlaceGenMatElemIndGenDouble();
//  ExpectedInPlaceGenMatElemIndsGenColVec();
//  ExpectedInPlaceGenMatElemIndsGenDouble();
//  ExpectedInPlaceGenMatElemIndsGenMat();
//  ExpectedInPlaceGenMatElemIndsGenRowVec();
//  ExpectedInPlaceGenMatExtColIndGenColVec();
//  ExpectedInPlaceGenMatExtColIndGenMat();
//  ExpectedInPlaceGenMatExtColIndGenRowVec();
//  ExpectedInPlaceGenMatExtColIndNumCols();
//  ExpectedInPlaceGenMatExtRowIndGenColVec();
//  ExpectedInPlaceGenMatExtRowIndGenMat();
//  ExpectedInPlaceGenMatExtRowIndGenRowVec();
//  ExpectedInPlaceGenMatExtRowIndNumRows();
//  ExpectedInPlaceGenMatGenColVec();
//  ExpectedInPlaceGenMatGenDouble();
//  ExpectedInPlaceGenMatGenMat();
//  ExpectedInPlaceGenMatGenRowVec();
//  ExpectedInPlaceGenMatNumRowsNumCols();
  ExpectedInPlaceGenMatRowInd();
//  ExpectedInPlaceGenMatRowIndColInd();
//  ExpectedInPlaceGenMatRowIndColIndGenDouble();
//  ExpectedInPlaceGenMatRowIndColIndMatSizeGenColVec();
//  ExpectedInPlaceGenMatRowIndColIndMatSizeGenDouble();
//  ExpectedInPlaceGenMatRowIndColIndMatSizeGenMat();
//  ExpectedInPlaceGenMatRowIndColIndMatSizeGenRowVec();
//  ExpectedInPlaceGenMatRowIndColIndRangeGenColVec();
//  ExpectedInPlaceGenMatRowIndColIndRangeGenDouble();
//  ExpectedInPlaceGenMatRowIndColIndRangeGenMat();
//  ExpectedInPlaceGenMatRowIndColIndRangeGenRowVec();
//  ExpectedInPlaceGenMatRowIndGenColVec();
//  ExpectedInPlaceGenMatRowIndGenDouble();
//  ExpectedInPlaceGenMatRowIndGenMat();
//  ExpectedInPlaceGenMatRowIndGenRowVec();
  ExpectedInPlaceGenMatRowIndRange();
//  ExpectedInPlaceGenMatRowIndRangeColIndGenColVec();
//  ExpectedInPlaceGenMatRowIndRangeColIndGenDouble();
//  ExpectedInPlaceGenMatRowIndRangeColIndGenMat();
//  ExpectedInPlaceGenMatRowIndRangeColIndGenRowVec();
//  ExpectedInPlaceGenMatRowIndRangeColIndRangeGenColVec();
//  ExpectedInPlaceGenMatRowIndRangeColIndRangeGenDouble();
//  ExpectedInPlaceGenMatRowIndRangeColIndRangeGenMat();
//  ExpectedInPlaceGenMatRowIndRangeColIndRangeGenRowVec();
//  ExpectedInPlaceGenMatRowIndRangeGenColVec();
//  ExpectedInPlaceGenMatRowIndRangeGenDouble();
//  ExpectedInPlaceGenMatRowIndRangeGenMat();
//  ExpectedInPlaceGenMatRowIndRangeGenRowVec();
//  ExpectedInPlaceGenMatRowIndsColIndsGenColVec();
//  ExpectedInPlaceGenMatRowIndsColIndsGenDouble();
//  ExpectedInPlaceGenMatRowIndsColIndsGenMat();
//  ExpectedInPlaceGenMatRowIndsColIndsGenRowVec();
//  ExpectedInPlaceGenMatRowIndsGenColVec();
//  ExpectedInPlaceGenMatRowIndsGenDouble();
//  ExpectedInPlaceGenMatRowIndsGenMat();
//  ExpectedInPlaceGenMatRowIndsGenRowVec();
//  ExpectedInPlaceGenRowVec();
//  ExpectedInPlaceGenRowVecElemInd();
//  ExpectedInPlaceGenRowVecElemIndGenColVec();
//  ExpectedInPlaceGenRowVecElemIndGenDouble();
//  ExpectedInPlaceGenRowVecElemIndGenMat();
//  ExpectedInPlaceGenRowVecElemIndGenRowVec();
//  ExpectedInPlaceGenRowVecElemIndRange();
//  ExpectedInPlaceGenRowVecElemIndRangeGenColVec();
//  ExpectedInPlaceGenRowVecElemIndRangeGenDouble();
//  ExpectedInPlaceGenRowVecElemIndRangeGenMat();
//  ExpectedInPlaceGenRowVecElemIndRangeGenRowVec();
//  ExpectedInPlaceGenRowVecElemIndsGenColVec();
//  ExpectedInPlaceGenRowVecElemIndsGenDouble();
//  ExpectedInPlaceGenRowVecElemIndsGenMat();
//  ExpectedInPlaceGenRowVecElemIndsGenRowVec();
//  ExpectedInPlaceGenRowVecGenColVec();
//  ExpectedInPlaceGenRowVecGenDouble();
//  ExpectedInPlaceGenRowVecGenMat();
//  ExpectedInPlaceGenRowVecGenRowVec();
//  ExpectedInPlaceRandomGenColVec();
//  ExpectedInPlaceRandomGenColVecNumElems();
//  ExpectedInPlaceRandomGenMat();
//  ExpectedInPlaceRandomGenMatNumRowsNumCols();
//  ExpectedInPlaceRandomGenRowVec();
//  ExpectedInPlaceRandomGenRowVecNumElems();
//  ExpectedInvMat();
//  ExpectedLogicColVec();
//  ExpectedLogicColVecNumElems();
//  ExpectedLogicMat();
//  ExpectedLogicMatDim();
//  ExpectedLogicMatNumElems();
//  ExpectedLogicMatNumElemsSearch();
//  ExpectedLogicRowVec();
//  ExpectedLogicRowVecNumElems();
//  ExpectedNumElems();
//  ExpectedNumRowsNumCols();
//  ExpectedOOColVec();
//  ExpectedOOMat();
//  ExpectedOORowVec();
//  ExpectedRandomGenColVec();
//  ExpectedRandomGenMat();
//  ExpectedRandomGenMatDim();
//  ExpectedRandomGenRowVec();
//  ExpectedRandomNumElems();
//  ExpectedRandomNumElemsDistrParam();
//  ExpectedRandomNumRowsNumCols();
//  ExpectedRandomNumRowsNumColsDistrParam();
//  ExpectedSquMat();
//  ExpectedSquMatSquMatGenMat();
//  ExpectedSymMat();
//  ExpectedSymPDMat();

  return EXIT_SUCCESS;
}
Example #7
0
File: phi.cpp Project: Horta/BASiCS
Phij Phi::phij(size_t j) const
{
  double phi0 = kappa.size() + 1;
  return Phij(kappa(j-1), phi0, sum(exp(kappa)));
}
int main(int argc, char **argv)
{
    PlayerClient client("localhost");
    RangerProxy  ranger(&client, 0);

    // Perform a preliminary fetch from the robot.
    client.Read();

    // Pull configuration information from remote ranger to the local ranger
    // proxy.
    ranger.RequestConfigure();
    ranger.RequestGeom();
    
        // Check the number of elements in the ranger
    // We only support laser-scanners (single-element, fan-of-readings devices)
    if(ranger.GetElementCount() != 1)
    {
        cerr << "This is not a laser rangefinder." << endl;
    }

    // Extract the scan parameters
    unsigned int num_readings = ranger.GetRangeCount();
    double       angle_min    = ranger.GetMinAngle();
    double       angle_max    = ranger.GetMaxAngle();
    double       angle_delta  = (angle_max - angle_min) / num_readings;

    cout << "Found laser ranger with properties: " << endl;
    cout << "min angle: "    << angle_min << endl;
    cout << "max angle: "    << angle_max << endl;
    cout << "num readings: " << num_readings << endl;
    cout << "angle delta: "  << angle_delta << endl;
    
    // Turn on the laser
    ranger.SetPower(true);

    // Storage for the point sequence
    mat points(2, num_readings);

    // Storage for the delta sequence
    mat deltas(2, num_readings-1);

    // Storage for the cross product and dot product sequences
    mat crossps(1, num_readings-2);
    mat dotps(1, num_readings-2);

    while(true)
    {
        // Fetch updates from the robot
        client.Read();
        
        // Copy readings from the ranger proxy
        double angle = angle_min;
        for(int count = 0; count < num_readings; count++)
        {
            points(0, count) = ranger[count] * cos(angle);
            points(1, count) = ranger[count] * sin(angle);
            
            angle += angle_delta;
        }

        // Compute deltas
        for(int count = 1; count < num_readings; count++)
        {
            deltas.col(count-1) = points.col(count) - points.col(count-1);
            deltas.col(count-1) /= norm(deltas.col(count-1), 2);
        }

        // Compute cross products and dot products
        for(int count = 2; count < num_readings; count++)
        {
            crossps(0, count-2) = deltas(0,count-2)*deltas(1, count-1)
                                  - deltas(0, count-1)*deltas(1, count-2);
            
            dotps(0, count-2) = dot(deltas.col(count-2), deltas.col(count-1));
        }

        // Bootstrap output
        angle = angle_min;
        for(int count = 2; count < num_readings; count++)
        {
            cout << angle << " " << crossps(0, count-2) << endl;
            angle += angle_delta;
        }

        cout << endl;

        break;
    }

    return 0;
}
Example #9
0
void keyboardCallback(unsigned char key, int x, int y)
{
    if(key == '1')
    {
        cout << "Composing and rendering scene 1..." << endl;

        scene scene1;
        scene1.voidcolor() = fvec("0 0 0 1");

        // Set up the viewport
        viewport view(vec("-15 0 10"),
                  vec("1 0 -0.2"),
                  vec("0 0 1"),
                  math::pi()/3.0,
                  math::pi()/3.0);
        scene1.viewports().insert(&view);
        
        // Insert an ellipsoid

        material_phong ellipse_mat(fvec("0.7 0 0.7"),
                                   fvec("0.7 0 0.7"),
                                   3.0,
                                   0.0,
                                   0.0,
                                   4.0);

        sphere ellipse_base(vec("0 0 0"),
                            1.0);
        set<renderable*> ellipse_children;
        ellipse_children.insert(&ellipse_base);
        affine_renderable ellipse(vec("0 0 1"),
                                  1.0,
                                  vec("1 2 3"),
                                  vec("5 -1 5"),
                                  ellipse_children);

        scene1.renderables().insert(&ellipse);
        scene1.materials()[&ellipse_base] = &ellipse_mat;

        // Insert a sphere
        
        sphere sphere_a(vec("5 2 10"),
                        2.0);
        
        material_phong sphere_a_mat(fvec("0 0.5 0.5"),
                                    fvec("0 0.5 0.5"),
                                    10.0,
                                    0.0,
                                    0.0,
                                    4.0);
        
        scene1.renderables().insert(&sphere_a);
        scene1.materials()[&sphere_a] = &sphere_a_mat;

        // Insert a plane
        
        infinite_plane plane_a(vec("0 0 0"),
                               vec("0 0 1"));

        material_phong plane_a_mat(fvec("0.2 0.7 0.1"),
                                   fvec("0.2 0.7 0.1"),
                                   12.0,
                                   0.0,
                                   0.0,
                                   3.0);
        
        scene1.renderables().insert(&plane_a);
        scene1.materials()[&plane_a] = &plane_a_mat;

        // Insert lights
        pointlight light_a(fvec("1 1 1"),
                           vec("5 3 20"));
        scene1.lights().insert(&light_a);

        pointlight light_b(fvec("1 1 1"),
                           vec("-5 -3 16"));
        scene1.lights().insert(&light_b);

        scene1.render(renderbuffer);
    }
    else if(key == '2')
    {
        cout << "Composing and rendering scene 2..." << endl;

        scene scene2;
        scene2.voidcolor() = fvec("0 0 0 1");

        // Set up the viewport
        viewport view(vec("-20 0 10"),
                  vec("1 0 -0.2"),
                  vec("0 0 1"),
                  math::pi()/3.0,
                  math::pi()/3.0);
        scene2.viewports().insert(&view);

        // Insert a plane
        
        infinite_plane plane_a(vec("0 0 0"),
                               vec("0 0 1"));

        material_phong plane_a_mat(fvec("0.2 0.7 0.1"),
                                   fvec("0.2 0.7 0.1"),
                                   12.0,
                                   0.0,
                                   0.0,
                                   3.0);
        
        scene2.renderables().insert(&plane_a);
        scene2.materials()[&plane_a] = &plane_a_mat;

        // Insert a sphere
        
        sphere sphere_a(vec("0 -5 3"),
                        3.0);

        material_phong mat_sphere_a(fvec("0.7 0.3 0.2"),
                                    fvec("0.7 0.3 0.2"),
                                    35.0,
                                    0.0,
                                    0.0,
                                    4.0);

        scene2.renderables().insert(&sphere_a);
        scene2.materials()[&sphere_a] = &mat_sphere_a;

        // Insert a cylinder

        cylinder cyl_a(vec("0 5 5"),
                       vec("1 1 1"),
                       2.0,
                       -2.0,
                       2.0);

        material_phong mat_cyl_a(fvec("0.3 0.2 0.7"),
                                 fvec("0.3 0.2 0.7"),
                                 10.0,
                                 0.0,
                                 0.0,
                                 3.0);
        
        scene2.renderables().insert(&cyl_a);
        scene2.materials()[&cyl_a] = &mat_cyl_a;

        // Insert lights
        
        pointlight light_a(fvec("1 1 1"),
                           vec("0 0 6"));
        scene2.lights().insert(&light_a);

        pointlight light_b(fvec("1 0.1 0.1"),
                           vec("5 10 10"));
        scene2.lights().insert(&light_b);

        scene2.render(renderbuffer);
        
    }
    else if(key == '3')
    {
        cout << "Composing and rendering scene 3..." << endl;

        scene scene3;
        scene3.voidcolor() = fvec("0 0 0 1");

        // Set up the viewport
        
        viewport view(vec("-20 0 10"),
                  vec("1 0 -0.2"),
                  vec("0 0 1"),
                  math::pi()/3.0,
                  math::pi()/3.0);
        scene3.viewports().insert(&view);

        // Insert a green ground plane
        infinite_plane plane_a(vec("0 0 0"),
                               vec("0 0 1"));

        material_phong plane_a_mat(fvec("0.2 0.7 0.1"),
                                   fvec("0.2 0.7 0.1"),
                                   12.0,
                                   0.0,
                                   0.0,
                                   3.0);
        
        scene3.renderables().insert(&plane_a);
        scene3.materials()[&plane_a] = &plane_a_mat;

        // Insert a reflective cylinder
        
        cylinder cyl_a(vec("5 5 5"),
                       vec("1 1 1"),
                       2.0,
                       -2.0,
                       2.0);

        material_phong mat_cyl_a(fvec("0.3 0.2 0.7"),
                                 fvec("0.3 0.2 0.7"),
                                 10.0,
                                 1.0,
                                 0.0,
                                 3.0);
        
        scene3.renderables().insert(&cyl_a);
        scene3.materials()[&cyl_a] = &mat_cyl_a;

        // Insert a nearby reflective sphere
        sphere sphere_a(vec("5 0 2"),
                        2.0);
        
        material_phong mat_sphere_a(fvec("0.2 0.2 0.2"),
                                    fvec("0.2 0.2 0.2"),
                                    100,
                                    1.0,
                                    0,
                                    2);

        scene3.renderables().insert(&sphere_a);
        scene3.materials()[&sphere_a] = &mat_sphere_a;

        sphere sphere_b(vec("0 0 2"),
                        1.0);
        
        material_phong mat_sphere_b(fvec("1.0 0.5 0.5"),
                                    fvec("1.0 0.5 0.5"),
                                    50,
                                    0.7,
                                    0.0,
                                    1);
        
        scene3.renderables().insert(&sphere_b);
        scene3.materials()[&sphere_b] = &mat_sphere_b;

        // Insert a huge sphere, far away
        sphere sphere_c(vec("40 -10 20"),
                        20);

        material_phong mat_sphere_c(fvec("0.5 0.5 0.5"),
                                    fvec("0.5 0.5 0.5"),
                                    35,
                                    1.0,
                                    0.0,
                                    3.0);
        
        scene3.renderables().insert(&sphere_c);
        scene3.materials()[&sphere_c] = &mat_sphere_c;

        // Insert lights

        pointlight light_a(fvec("1 1 1"),
                            vec("5 -1 10"));
        scene3.lights().insert(&light_a);

        scene3.render(renderbuffer);
        
    }
    else if(key == '4')
    {
        cout << "Composing and rendering scene 4.  This scene has many"
                " reflections, so please be patient..."
             << endl;

        scene scene4;
        scene4.voidcolor() = fvec("0 0 0 1");

        // Set up the viewport
        viewport view(vec("-20 0 10"),
                  vec("1 0 -0.2"),
                  vec("0 0 1"),
                  math::pi()/3.0,
                  math::pi()/3.0);
        scene4.viewports().insert(&view);

        material_phong mat_silver(fvec("0.2 0.2 0.2"),
                                  fvec("0.2 0.2 0.2"),
                                  50.0,
                                  1.0,
                                  0.0,
                                  3.0);

        vec center1(" 2  2 2");
        vec center2("-2  2 2");
        vec center3("-2 -2 2");
        vec center4(" 2 -2 2");
        vec center5(" 0  0 4.828");

        sphere sphere_1(center1, 2.0);
        sphere sphere_2(center2, 2.0);
        sphere sphere_3(center3, 2.0);
        sphere sphere_4(center4, 2.0);
        sphere sphere_5(center5, 2.0);

        scene4.renderables().insert(&sphere_1);
        scene4.renderables().insert(&sphere_2);
        scene4.renderables().insert(&sphere_3);
        scene4.renderables().insert(&sphere_4);
        scene4.renderables().insert(&sphere_5);

        scene4.materials()[&sphere_1] = &mat_silver;
        scene4.materials()[&sphere_2] = &mat_silver;
        scene4.materials()[&sphere_3] = &mat_silver;
        scene4.materials()[&sphere_4] = &mat_silver;
        scene4.materials()[&sphere_5] = &mat_silver;

        vec offset_a("10 10 0");

        sphere sphere_a_1(center1 + offset_a, 2.0);
        sphere sphere_a_2(center2 + offset_a, 2.0);
        sphere sphere_a_3(center3 + offset_a, 2.0);
        sphere sphere_a_4(center4 + offset_a, 2.0);
        sphere sphere_a_5(center5 + offset_a, 2.0);

        scene4.renderables().insert(&sphere_a_1);
        scene4.renderables().insert(&sphere_a_2);
        scene4.renderables().insert(&sphere_a_3);
        scene4.renderables().insert(&sphere_a_4);
        scene4.renderables().insert(&sphere_a_5);

        scene4.materials()[&sphere_a_1] = &mat_silver;
        scene4.materials()[&sphere_a_2] = &mat_silver;
        scene4.materials()[&sphere_a_3] = &mat_silver;
        scene4.materials()[&sphere_a_4] = &mat_silver;
        scene4.materials()[&sphere_a_5] = &mat_silver;

        vec offset_b("10 -10 0");
        
        sphere sphere_b_1(center1 + offset_b, 2.0);
        sphere sphere_b_2(center2 + offset_b, 2.0);
        sphere sphere_b_3(center3 + offset_b, 2.0);
        sphere sphere_b_4(center4 + offset_b, 2.0);
        sphere sphere_b_5(center5 + offset_b, 2.0);

        scene4.renderables().insert(&sphere_b_1);
        scene4.renderables().insert(&sphere_b_2);
        scene4.renderables().insert(&sphere_b_3);
        scene4.renderables().insert(&sphere_b_4);
        scene4.renderables().insert(&sphere_b_5);

        scene4.materials()[&sphere_b_1] = &mat_silver;
        scene4.materials()[&sphere_b_2] = &mat_silver;
        scene4.materials()[&sphere_b_3] = &mat_silver;
        scene4.materials()[&sphere_b_4] = &mat_silver;
        scene4.materials()[&sphere_b_5] = &mat_silver;

        vec offset_c("30 0 0");
        
        sphere sphere_c_1(center1 + offset_c, 2.0);
        sphere sphere_c_2(center2 + offset_c, 2.0);
        sphere sphere_c_3(center3 + offset_c, 2.0);
        sphere sphere_c_4(center4 + offset_c, 2.0);
        sphere sphere_c_5(center5 + offset_c, 2.0);

        scene4.renderables().insert(&sphere_c_1);
        scene4.renderables().insert(&sphere_c_2);
        scene4.renderables().insert(&sphere_c_3);
        scene4.renderables().insert(&sphere_c_4);
        scene4.renderables().insert(&sphere_c_5);

        scene4.materials()[&sphere_c_1] = &mat_silver;
        scene4.materials()[&sphere_c_2] = &mat_silver;
        scene4.materials()[&sphere_c_3] = &mat_silver;
        scene4.materials()[&sphere_c_4] = &mat_silver;
        scene4.materials()[&sphere_c_5] = &mat_silver;

        // Insert a ground plane
        infinite_plane plane_a(vec("0 0 0"),
                               vec("0 0 1"));

        material_phong plane_a_mat(fvec("0.5 0.5 0.5"),
                                   fvec("0.5 0.5 0.5"),
                                   12.0,
                                   0.7,
                                   0.0,
                                   3.0);
        
        scene4.renderables().insert(&plane_a);
        scene4.materials()[&plane_a] = &plane_a_mat;
        
        infinite_plane plane_b(vec("0 0 15"),
                               vec("0 0 -1"));

        material_phong plane_b_mat(fvec("0.5 0.5 0.5"),
                                   fvec("0.5 0.5 0.5"),
                                   12.0,
                                   0.0,
                                   0.0,
                                   3.0);
        
        scene4.renderables().insert(&plane_b);
        scene4.materials()[&plane_b] = &plane_b_mat;


        vec light_up("0 0 7.5");
        
        pointlight light_a(fvec("1 1 1 1"), light_up);
        scene4.lights().insert(&light_a);

        pointlight light_b(fvec("1 0 0 1"), light_up + offset_a);
        scene4.lights().insert(&light_b);

        pointlight light_c(fvec("0 1 0 1"), light_up + offset_b);
        scene4.lights().insert(&light_c);

        pointlight light_d(fvec("0 0 1 1"), light_up + offset_c);
        scene4.lights().insert(&light_d);

        scene4.render(renderbuffer);
    }

    glutPostRedisplay();
}