/** @(#) graphdoglegfile.cpp */

#include "graphdoglegfile.h"
/**
* Ejemplo:
* <pre>
* graphDoglegFile("finiteNewtonDogleg.dat", DogImage,
*			xMin, xMax, yMin, yMax,
*                       CheckPauses->Checked, CheckShowEvaluation->Checked);
* </pre>
 @author Daniel Alba Cuellar
* @author Omar Posada Villarreal
* @version 1.0, /03/2002
*/
void graphDoglegFile(string pathFile, TImage *image,
		double xMin, double xMax, double yMin, double yMax,
                bool pauses, bool showEvaluation, double pixelsPerUnit) {
        // log file
        // Crea y abre el archivo
        ifstream inFile(pathFile.data(), ios::in);
	if ( !inFile ) {	// !sobrecargado
		throw runtime_error(
                "graphDoglegFile: Error de apertura de archivo.");
        }

        // Quita encabezado
        string dummy;
        //        Iter     Radio1   Radio2   Paso     fc       dim(xc)  xc
        inFile >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy;

        string	dogNewt;
        int	iter;
        double	fc;
	double	deltaIn, deltaOut;
        VectorDouble xc(1, 1);	// logica 1, resize en inFile >>
        double	xi, yi, xf, yf;	//logical init, final
        int	x1g, y1g, x2g, y2g;	// graphical
        int	pixHeig;

	TCanvas *canvas = image->Canvas;

        // Convertidor de escalas
        ScalePos	scale(xMin, xMax, yMin, yMax,
        		0, image->Width, 0, image->Height);

        // Limpia canvas
        canvas->Pen->Color = clWhite; // invisible al inicio
        canvas->Brush->Style = bsSolid;	// elipses vacias???
        canvas->Rectangle(0, 0, image->Width, image->Height);

        // Traza ejes x =0, y = 0
        canvas->Pen->Color = clBlack;
        // horiz
        canvas->MoveTo(0, scale.toGraphY(0.0) );
        canvas->LineTo(image->Width, scale.toGraphY(0.0));
        //vertic
        canvas->MoveTo(scale.toGraphX(0.0), 0);
        canvas->LineTo(scale.toGraphX(0.0), image->Height);

        while (!inFile.eof()) {
        	// xc necesita la dimension luego los datos: 3 1.1 2.2 3.3
        // IMPORTANTE: Si no esta el primer elemento, termina
        	if (!(inFile >> iter)) {
			break;
                }
                inFile >> deltaIn >> deltaOut >> dogNewt >> fc >> xc;

//fc no se usa
                // linea a xc
                x1g = scale.toGraphX(xc[1]);	y1g = scale.toGraphY(xc[2]);

        	// color y relleno del paso
        	canvas->Brush->Style = bsClear;	// elipses vacias
        	if (dogNewt.compare("Dogleg") == 0) {
                	canvas->Pen->Color = clBlue;
                } else if (dogNewt.compare("Newton") == 0) {
                	canvas->Pen->Color = clGreen;
                } else {	// Inicial, circulo relleno
			canvas->MoveTo(x1g, y1g);
        		canvas->Brush->Style = bsSolid;
        		canvas->Brush->Color = clYellow;
                }

                // linea a xc
                canvas->LineTo(x1g, y1g);	// 1a linea se mueve al inicio

                // Centro de xc
		canvas->Pen->Color = clBlack;
                canvas->Ellipse(x1g - 2, y1g - 2, x1g + 2, y1g + 2);
                canvas->Ellipse(x1g - 1, y1g - 1, x1g + 1, y1g + 1);

                // Mostrar evaluaciones
                if (showEvaluation) {
			canvas->Pen->Color = clPurple;
                	pixHeig = static_cast<int>(pixelsPerUnit * fc);
			canvas->Rectangle(x1g, y1g-2, x1g+1, y1g-2 - pixHeig);
                }

        	// Circulo in
		canvas->Pen->Color = clGray;
		xi = xc[1] - deltaIn/2.0;	yi = xc[2] + deltaIn/2.0;
		xf = xc[1] + deltaIn/2.0;       yf = xc[2] - deltaIn/2.0;

                x1g = scale.toGraphX(xi);	y1g = scale.toGraphY(yi);
                x2g = scale.toGraphX(xf);	y2g = scale.toGraphY(yf);

                canvas->Ellipse(x1g, y1g, x2g, y2g);

                // Circulo out
		canvas->Pen->Color = clRed;
		xi = xc[1] - deltaOut/2.0;	yi = xc[2] + deltaOut/2.0;
		xf = xc[1] + deltaOut/2.0;      yf = xc[2] - deltaOut/2.0;

                x1g = scale.toGraphX(xi);	y1g = scale.toGraphY(yi);
                x2g = scale.toGraphX(xf);	y2g = scale.toGraphY(yf);

                canvas->Ellipse(x1g, y1g, x2g, y2g);

                canvas->Refresh();

                // Pausas
		if (pauses) {
			ShowMessage( AnsiString(
                        	"Aceptar para ver el siguiente paso.") );
                }
        }
        inFile.close();
	if ( !inFile ) {	// !sobrecargado
		throw runtime_error(
                "graphDoglegFile: Error al cerrar el archivo.");
        }
}
// Fin------------------------------------------------------------------------