/******************************************************************************
* NOMAD -  Nonlinear Optimization for Mixed vAriables and Derivatives.        *
* Copyright (C) 2001-2004  Gilles Couture - Ecole Polytechnique,              *
*                          Charles Audet - Ecole Polytechnique,               *
*		           John Dennis - Rice University.                     *
*                                                                             *
* Author: Gilles Couture                                                      *
* École Polytechnique de Montréal - GERAD                                     *
* C.P. 6079, Succ. Centre-ville, Montréal (Québec) H3C 3A7 Canada             *
* Tel:  (514) 340-6053 x6928                                                  *
* Email: Gilles.Couture@gerad.ca                                              *
* Fax: (514) 340-5665                                                         *
*                                                                             *
* This program is free software; you can redistribute it and/or               *
* modify it under the terms of the GNU General Public License                 *
* as published by the Free Software Foundation. You can find the              *
* GNU General Public License at:                                              *
*             http://www.gnu.org/copyleft/gpl.html                            *
*                                                                             *
* This program is distributed in the hope that it will be useful,             *
* but WITHOUT ANY WARRANTY; without even the implied warranty of              *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with this program; if not, write to the Free Software                 *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
*                                                                             *
* You can find information on the NOMAD software at:                          *
*             http://www.gerad.ca/NOMAD/                                      *
******************************************************************************/

// continuouspollster.cpp -- ContinuousPollster class methods.

#include <cmath>
#include <fstream>
#include <iostream>
using namespace std;
#include "continuouspollster.h"
#include "defines.h"

// Private methods.
// ****************************************************************************

// Public methods.
// ****************************************************************************

// Default constructor.
ContinuousPollster::ContinuousPollster(const Description * const des)
  : Pollster(des)
{}

// Destructor.
ContinuousPollster::~ContinuousPollster()
{}

// This method fills the list of points with points polled around the poll center.
void ContinuousPollster::poll(const double delta,
			      const double * primary_poll_center,
			      std::deque<Direction> & primary_dirs,
			      const double * secondary_poll_center,
			      std::deque<Direction> & secondary_dirs,
			      std::vector<AugmentedPoint *> & list_of_points)
{
  int dimension = description->getDimension(), i = 0;
  double * coord = new double[dimension];
  std::vector<double> dir;
  std::vector<double>::iterator j = dir.begin();
  std::deque<Direction>::iterator direction = primary_dirs.begin();

  // We poll around the primary poll center, according to the primary poll
  // directions.
  for (direction = primary_dirs.begin(); direction != primary_dirs.end();
       direction++)
    {
      i = 0;
      dir = (*direction).getDirection();
      // "j" will point the current primary poll direction.
      // This loop evaluates the following vector operation:
      // trial = primary poll_center + (primary poll direction * mesh size)
      for (j = dir.begin(); j != dir.end(); j++)
				{
					coord[i] = primary_poll_center[i] + (*j * delta);
					i++;
				}
      dir.clear();
      AugmentedPoint * ap = new AugmentedPoint(dimension, coord);
      list_of_points.push_back(ap);
    }

  // Test to see if we have a secondary poll center.
  if ((secondary_poll_center != NULL) && (secondary_dirs.size() > 0))
    {
      // We poll around the secondary poll center, according to the secondary
      // poll directions.
      for (direction = secondary_dirs.begin();
	   direction != secondary_dirs.end(); direction++)
	{
	  i = 0;
	  dir = (*direction).getDirection();
	  // "j" will point the current secondary poll direction.
	  // This loop evaluates the following vector operation:
	  // trial = secondary poll_center +
	  //         (secondary poll direction * mesh size)
	  for (j = dir.begin(); j != dir.end(); j++)
	    {
	      coord[i] = secondary_poll_center[i] + (*j * delta);
	      i++;
	    }
	  dir.clear();
	  AugmentedPoint * ap = new AugmentedPoint(dimension, coord);
	  list_of_points.push_back(ap);
	}
    }
  // The 'coord' array is erased from memory.
  delete [] coord;
}
