/*
post.c

post process
*/

#include "oth.h"
#include "ev.h"
#include "oth_prototype.h"

static void setup_near1d(void);
static void setup_near2d(void);

void post(void)
{
	ev2d_init(Width2d, Height2d);

	ev3d_init();

	if (Piter[0] || Piter[1]) {
		plot2dIter();
	}

	plot2dFreq();

	if (IFar0d) {
		outputFar0d();
	}

	if (NFar1d) {
		outputFar1d();
	}

	if (NFar2d) {
		outputFar2d();
	}

	if (NNear1d) {
		setup_near1d();
		outputNear1d();
	}

	if (NNear2d) {
		setup_near2d();
		outputNear2d();
	}

	ev2d_file(!HTML, (!HTML ? FN_ev2d_1 : FN_ev2d_0));
	ev2d_output();

	ev3d_file(!HTML, (!HTML ? FN_ev3d_1 : FN_ev3d_0), 0);
	ev3d_output();
}


// setup near1d
static void setup_near1d(void)
{
	for (int n = 0; n < NNear1d; n++) {
		const char dir = Near1d[n].dir;
		const double p1 = Near1d[n].pos1;
		const double p2 = Near1d[n].pos2;
		if      (dir == 'X') {
			Near1d[n].id1 = nearest(p1, 0, Ny, Yn);
			Near1d[n].id2 = nearest(p2, 0, Nz, Zn);
		}
		else if (dir == 'Y') {
			Near1d[n].id1 = nearest(p1, 0, Nz, Zn);
			Near1d[n].id2 = nearest(p2, 0, Nx, Xn);
		}
		else if (dir == 'Z') {
			Near1d[n].id1 = nearest(p1, 0, Nx, Xn);
			Near1d[n].id2 = nearest(p2, 0, Ny, Yn);
		}
		//printf("near1d : %d %c %e %d %e %d\n", n, Near1d[n].dir, Near1d[n].pos1, Near1d[n].id1, Near1d[n].pos2, Near1d[n].id2);
	}
}


// setup near2d
static void setup_near2d(void)
{
	for (int n = 0; n < NNear2d; n++) {
		const char dir = Near2d[n].dir;
		const double p0 = Near2d[n].pos0;
		if      (dir == 'X') {
			Near2d[n].id0 = nearest(p0, 0, Nx, Xn);
		}
		else if (dir == 'Y') {
			Near2d[n].id0 = nearest(p0, 0, Ny, Yn);
		}
		else if (dir == 'Z') {
			Near2d[n].id0 = nearest(p0, 0, Nz, Zn);
		}
		//printf("near2d : %d %s %c %e %d\n", n, Near2d[n].cmp, Near2d[n].dir, Near2d[n].pos0, Near2d[n].id0);
	}
}
