# How to apply filter when denoising the mesh?

I’m implementing a mesh denoising algorithm using openGL.The mesh is using half-edge structure.
While I had some problem when applying the bilateral filter.
Here’s my code:
void bilatRemove(float FsigmaC, float FsigmaS)
{
map<HE_vert*, float>neighbor;
HE_vert centPoint;
vector3d centNorm, neighPoint;
centNorm.setVector(0, 0, 0);
float amp = AverageEdgeLength() / 100;
int i = 0;
double sigmaC = FsigmaC;
double sigmaS = FsigmaS;

``````for(vector&lt;HE_vert&gt;::iterator iter = vertex_map.begin();
iter != vertex_map.end(); ++iter)
{
neighbor.clear();
centPoint = *iter;
centNorm.setVector(0, 0, 0);
centNorm.setVector(iter-&gt;vx, iter-&gt;vy, iter-&gt;vz);
double sum = 0, normalizer = 0;

double x = 0, y = 0, z = 0, vx = 0, vy = 0, vz = 0;
i++;

//store each vertex's neighbor by going through
HE_edge *curEdge = iter-&gt;edge;
HE_edge *tmpEdge = iter-&gt;edge;
do
{
neighbor.insert(make_pair(tmpEdge-&gt;to_vert, 0));
tmpEdge = tmpEdge-&gt;twin_edge-&gt;next_edge;
}while(tmpEdge != iter-&gt;edge);

for(map&lt;HE_vert*, float&gt;::iterator neighbor_iter = neighbor.begin();
neighbor_iter != neighbor.end(); ++neighbor_iter)
{
vector3d neighEdge;
neighEdge.xPos = (curEdge-&gt;prev_edge-&gt;to_vert-&gt;x) - (neighbor_iter-&gt;first-&gt;x);
neighEdge.yPos = (curEdge-&gt;prev_edge-&gt;to_vert-&gt;y) - (neighbor_iter-&gt;first-&gt;y);
neighEdge.zPos = (curEdge-&gt;prev_edge-&gt;to_vert-&gt;z) - (neighbor_iter-&gt;first-&gt;z);
//neighEdge.printVector();

double t = Magnitude(neighEdge);
double h = Dot(neighEdge, centNorm);
double omegaC = exp(-t*t / (2.0 * sigmaC * sigmaC));
double omegaS = exp(-h*h / (2.0 * sigmaS * sigmaS));

sum += omegaC * omegaS * t;
normalizer += omegaC * omegaS;
}
//move along the direction of vertex normal
x = curEdge-&gt;prev_edge-&gt;to_vert-&gt;x;
y = curEdge-&gt;prev_edge-&gt;to_vert-&gt;y;
z = curEdge-&gt;prev_edge-&gt;to_vert-&gt;z;

vx = curEdge-&gt;prev_edge-&gt;to_vert-&gt;vx;
vy = curEdge-&gt;prev_edge-&gt;to_vert-&gt;vy;
vz = curEdge-&gt;prev_edge-&gt;to_vert-&gt;vz;

x += (sum / normalizer) * vx * amp;
y += (sum / normalizer) * vy * amp;
z += (sum / normalizer) * vz * amp;

iter-&gt;x = x;
iter-&gt;y = y;
iter-&gt;z = z;
}
glutPostRedisplay();
getNorm();
cout&lt;&lt;"Noise Removed using Bilateral Filter!
``````

";
}