double bndIntegration(const pointSet& mesh, const scalarField& sf, int bnd)
{
	double sum = 0.0;
	int n = mesh.getNbBndPoints();
	int n1, n2;
	double dx;
	for (int i=0; i<n; i++)
	{
		if (mesh.getBND(i) == bnd)
		{
			mesh.findLeftRightBndPoint(i, n1, n2);
			// Check first neighbour
			if (n1 > i)
			{
				dx = mesh.dist(i,n1);
				sum += 0.5*(sf(i)+sf(n1)) * dx;
			}
			if (n2 > i)
			{
				dx = mesh.dist(i,n2);
				sum += 0.5*(sf(i)+sf(n2)) * dx;
			}
		}
	}
	return sum;
}
void approximatedNormals(const pointSet& mesh, vector<int> neighb, int pnb, double& nx_out, double& ny_out)
{

    if (mesh.getBND(pnb)==0)
        throw mfpmExcept(0);

    int NP = neighb.size();
    double sl2 = mesh.getSL(pnb);


    // New approach
    double sumX = 0, sumY = 0;
    double nx, ny;
    int count = 0;
    for (int i=0; i<NP; i++)
    {
        if (pnb!=neighb[i] )
        {
            nx = (mesh.y(pnb)-mesh.y(neighb[i]));
            ny = -(mesh.x(pnb)-mesh.x(neighb[i]));
            double weight = exp(-5.0* (nx*nx+ny*ny) / sl2);

            double length = sqrt(nx*nx+ny*ny);
            nx /= length;
            ny /= length;
            if ( nx*mesh.getNormal(0,pnb) + ny*mesh.getNormal(1,pnb) < 0)
            {
                nx *= -1.0;
                ny *= -1.0;
            }
            sumX += nx*weight;
            sumY += ny*weight;
        }
    }
    sumX /= NP-1;
    sumY /= NP-1;
    double length = sqrt(sumX*sumX+sumY*sumY);
    if (length==0)
        throw mfpmExcept(1000);
    nx_out = sumX / length;
    ny_out = sumY / length;


}