#include <cmath>

#include "globale.h"
#include "CVector3.h"

//#define TEMPERATURE

void DeriveXX(float Avant[DIV_X][DIV_Y], float Apres[DIV_X][DIV_Y])
{
	int i, j;

	for (i = 1; i < DIV_X - 1; i++)
		for (j = 0; j < DIV_Y; j++)
			Apres[i][j] = (Avant[i+1][j] + Avant[i-1][j] - 2*Avant[i][j]) / 2 * DIV_X;

	for (j = 0; j < DIV_Y; j++)
	{
		Apres[0][j] = (Avant[1][j] - Avant[0][j]) * DIV_X;
		Apres[DIV_X - 1][j] = (Avant[DIV_X - 2][j] - Avant[DIV_X - 1][j]) * DIV_X;
	}
}

void DeriveYY(float Avant[DIV_X][DIV_Y], float Apres[DIV_X][DIV_Y])
{
	int i, j;

	for (i = 0; i < DIV_X; i++)
		for (j = 1; j < DIV_Y - 1; j++)
			Apres[i][j] = (Avant[i][j+1] + Avant[i][j-1] - 2*Avant[i][j]) / 2 * DIV_Y;

	for (i = 0; i < DIV_X; i++)
	{
		Apres[i][0] = (Avant[i][1] - Avant[i][0]) * DIV_Y;
		Apres[i][DIV_Y - 1] = (Avant[i][DIV_Y - 2] - Avant[i][DIV_Y - 1]) * DIV_Y;
	}
}


void Contraintes(void);

void CalculHauteur(void)
{
	float Uxx[DIV_X][DIV_Y];
	float Uyy[DIV_X][DIV_Y];

	DeriveXX(Hauteurs, Uxx);
	DeriveYY(Hauteurs, Uyy);

	int i, j;

	for (i = 0; i < DIV_X; i++)
		for (j = 0; j < DIV_Y; j++)
		{
#ifdef TEMPERATURE
			Hauteurs[i][j] += ALPHA2*(Uxx[i][j] + Uyy[i][j])*DT;
#else			
			Vitesses[i][j] += ALPHA2*(Uxx[i][j] + Uyy[i][j])*DT;
			Hauteurs[i][j] += Vitesses[i][j] * DT;
#endif
		}

	Contraintes();

	for (i = 1; i < DIV_X-1; i++)
		for (j = 1; j < DIV_Y-1; j++)
		{
			Normales[i][j].x = (Hauteurs[i-1][j] - Hauteurs[i+1][j]) * DIV_X / 2;
			Normales[i][j].y = (Hauteurs[i][j-1] - Hauteurs[i][j+1]) * DIV_Y / 2;
			Normales[i][j].z = 1;
			Normales[i][j].Normalize();
		}
}
