void GetPlane(double x1, double y1, double x2, double y2, double *coef)
{
coef[0] = y2 - y1;
coef[1] = x1 - x2;
coef[2] = y1 * (x2 - x1) - x1 * (y2 - y1);
return;
}
double GetPlaneValue(double x, double y, double *coef)
{
return coef[0] * x + coef[1] * y + coef[2];
}
void GetBasisPlane(double x, double y, double x1, double y1, double x2, double y2, double *coef)
{
GetPlane(x1, y1, x2, y2, coef);
double tmp = 1 / GetPlaneValue(x,y,coef);
coef[0] *= tmp;
coef[1] *= tmp;
coef[2] *= tmp;
}
void InitializeBasis( double *coef)
{
GetBasisPlane(0, 0, 0, 1, -1, 0, coef);
GetBasisPlane(0, 0, 1, 1, 1, 0, coef + 6);
GetBasisPlane(0, 0, 0, -1, -1, -1, coef + 12);
GetBasisPlane(0, 0, 0, 1, 1, 1, coef + 3);
GetBasisPlane(0, 0, 1, 0, 0, -1, coef + 9);
GetBasisPlane(0, 0, -1, 0, -1, -1, coef + 15);
/*GetBasisPlane(0, 0, -x, 0, 0, y, coef);
GetBasisPlane(0, 0, x, 0, x, y, coef + 6);
GetBasisPlane(0, 0, 0, -y, -x, -y, coef + 12);
GetBasisPlane(0, 0, 0, y, x, y, coef + 3);
GetBasisPlane(0, 0, x, 0, 0, -y, coef + 9);
GetBasisPlane(0, 0, -x, 0, -x, -y, coef + 15);*/
for (int i=0; i<6 ;i++)
qDebug()<<coef[i*3]<<coef[1+i*3]<<coef[2+i*3];
}
double GetBasisFunctionValue(double x, double y, int x_center, int y_center, double *coef)
{
int type = -1;
/* if (fabs(x - x_center) > 1) return 0;
if (fabs(y - y_center) > 1) return 0;*/
if (fabs(x - x_center) + fabs(y - y_center) < 1e-15) return 1;
if (x <= x_center)
{
if (y >= y_center) type = 0;
else if ( fabs(x_center - x) <= fabs(y_center - y) ) type = 4;
else type = 5;
}
else
{
if (y <= y_center) type = 3;
else if ( fabs(x - x_center) <= fabs(y - y_center) ) type = 1;
else type = 2;
}
double x_tmp = x - x_center;
double y_tmp = y - y_center;
double res = GetPlaneValue(x_tmp, y_tmp, coef + type * 3);
if (res > 1 || res < 0) res=0;
// qDebug()<<"!!!"<< x_tmp << y_tmp << type << "COEF"<< coef[type*3]<< coef[type*3 +1]<< coef[type*3 +2];
return res;
}
void PointSurround(int p_number, int m, int n, int *xi, int *yi)
{
for (int i=0; i<6; i++)
{
xi[i] = yi[i] = -900;
}
// point number -> coordinates
int x = p_number % m;
int y = n - (p_number / m) -1;
xi[6] = x;
yi[6] = y;
qDebug()<<"("<<x<<y<<")";
if (x != 0)
{
xi[0] = x - 1;
yi[0] = y;
}
if (y != n-1)
{
xi[1] = x;
yi[1] = y + 1;
}
if ((x != m-1) && (y != n-1))
{
xi[2] = x + 1;
yi[2] = y + 1;
}
if (x != m-1)
{
xi[3] = x + 1;
yi[3] = y;
}
if (y != 0)
{
xi[4] = x;
yi[4] = y - 1;
}
if ((x != 0) && (y != 0))
{
xi[5] = x - 1;
yi[5] = y - 1;
}
//*count = ind;
return;
}
void Plane3p(double* x, double *y, double *z, double *coef)
{
double a[9];
a[0] = x[2];
a[1] = y[2];
a[2] = z[2];
a[3] = x[0] - x[2];
a[4] = y[0] - y[2];
a[5] = z[0] - z[2];
a[6] = x[1] - x[2];
a[7] = y[1] - y[2];
a[8] = z[1] - z[2];
double tmp = 1 / (a[4] * a[6] - a[3] * a[7]);
coef[0] = tmp * (-a[5] * a[7] + a[4] * a[8] );
coef[1] = tmp * (a[5] * a[6] - a[3] * a[8] );
coef[2] = tmp * (a[2] * a[4] * a[6] - a[1] * a[5] * a[6] - a[2] * a[3] * a[7] + a[0] * a[5] * a[7] + a[1] * a[3] * a[8] - a[0] * a[4] * a[8]);
}
double Integrate0(double a1, double b1, double c1, double a2, double b2, double c2, double x0, double y0)
{
return
(a1 * (-4 * (c2 + 3*c2*x0) + 2 * a2 * (1 + 4*x0 + 6*x0*x0) + b2*(-1 - 4*x0 + 4*y0 + 12 * x0 * y0)) + 2 * (2 * c1 * (b2 + 3 * c2 - 3 * b2 * y0) + b1 * (b2 + 2*c2 - 4*b2*y0 - 6 *c2 * y0 + 6* b2 * y0 * y0)) + a2 * (-4 * (c1 + 3 * c1 * x0) + b1 * (-1 + 4*y0 + 4*x0 * (-1 + 3*y0))))/24;
}
double Integrate1(double a1, double b1, double c1, double a2, double b2, double c2, double x0, double y0)
{
return
(a1*(4*c2*(1 - 3* x0) + 2* a2* (1 - 4* x0 + 6* x0*x0) +
b2* (3 - 8* x0 - 4* y0 + 12* x0* y0)) +
a2*(4* c1* (1 - 3* x0) + b1* (3 - 4* y0 + 4* x0* (-2 + 3* y0))) +
2* (4* b2* c1 + 6* c1* c2 - 6* b2* c1* y0 +
b1* (4* c2 - 6* c2* y0 + b2* (3 - 8* y0 + 6* y0*y0))))/24;
}
void init_f(int m, int n, double *coef, double *f) // Initialize right side
{
int p_x[7]={-1}; // point surroundings x
int p_y[7]={-1}; // point surroundings y
double x[3], y[3], z[3];// point for plane
double fcoef[3]; // approximated plane coefficients
double x_val, y_val;
for (int i1=0; i1<n; i1++)
{
for(int i2=0; i2<m; i2++)
{
PointSurround(m*i1+i2, m, n, p_x, p_y);
for (int z=0; z<7; z++)
qDebug()<<(m*i1+i2)<<"COORD:"<<p_x[z]<<p_y[z];
qDebug("------");
for (int j=0; j<6; j++)
{
if ( (p_x[j] >= 0) && (p_x[ (j+1)%6 ] >= 0) )
{
//x_val = x[0]
x[0] = p_x[j] - p_x[6];
y[0] = p_y[j] - p_y[6];
z[0] = func( p_x[j], p_y[j] );
x[1] = p_x[(j+1)%6] - p_x[6];
y[1] = p_y[(j+1)%6] - p_y[6];
z[1] = func( p_x[(j+1)%6], p_y[(j+1)%6] );
x[2] = 0;
y[2] = 0;
z[2] = func(i1, i2);
Plane3p(x, y, z, fcoef);
double x0 = 0;
double y0 = 0;
if (j % 2 ==0)
{
if (j == 2) x0 = -1;
if (j == 4) y0 = 1;
f[i2 + i1 * m] += 24 * Integrate0(fcoef[0], fcoef[1], fcoef[2], coef[j*3], coef[j*3 + 1], coef[j*3 + 2], x0, y0);
}
else
{
if (j == 3) y0 = 1;
if (j == 5)
{
x0 = 1;
y0 = 1;
}
f[i2 + i1 * m] += 24 * Integrate1(fcoef[0], fcoef[1], fcoef[2], coef[j*3], coef[j*3 + 1], coef[j*3 + 2], x0, y0);
}
}
}
}
}
for (int i=0; i<m*n;i++)
qDebug()<<i<<f[i];
}