使用高斯进行网格平滑

发布于 2024-08-23 02:52:13 字数 2448 浏览 7 评论 0原文

我希望使用高斯函数平滑给定的 3D 网格,该网格使用半边结构来存储邻接信息。以下是提出的算法:

通过移动每个顶点来平滑网格 到由加权确定的位置 其近邻的平均值 (权重由高斯确定 西格玛等于平均长度 连接到顶点的边, 归一化,使得权重总和 到一)。

因此,对于每个顶点 curr_vertex,我

  1. 计算其附加边的平均长度,
  2. 获取所有相邻顶点,
  3. 通过执行以下操作确定每个相邻顶点的权重: `

重量= exp(-(距离*距离)/(2.西格玛西格玛))

其中 distance 是curr_vertex与邻居之间的 3D 距离, sigma= curr_vertex` 附加边的平均长度

  1. 将所有权重相加,并将每个邻居的权重除以该总和(归一化步骤)
  2. 将每个相邻顶点的位置乘以其相应的权重
  3. 将所有权重相加加权顶点并将结果添加到 curr_vertex 以生成新顶点。

当我这样做并运行我的算法时,实际发生的不是平滑,而是缩放 - 我的网格只是变大,没有任何明显的平滑。

对我做错了什么有什么想法吗?

编辑: 这是我的代码:

void R3Mesh::
Smooth(void)
{
  R3Mesh *new_mesh = new R3Mesh(*this);
  for(int i = 0; i < NVertices(); i++)
  {
    R3MeshVertex *v = Vertex(i);
    map<R3MeshVertex *, double> neighbors; // stores vertex-weight pairs 
    map<R3MeshVertex *, double>::iterator neighbors_iter;

    // store each vertex neighbor by going through
    // all adjacent edges and obtaining those edges' vertices
    R3MeshHalfEdge *tmp = v->half_edge;
    do
    {
      neighbors.insert(make_pair(tmp->opposite->vertex,0));
      tmp = tmp->opposite->next;
    }while(tmp != v->half_edge);

    // determine the sigma to use for Gaussian
    double sigma = v->AverageEdgeLength();
    double weight = 0, total_weight = 0;
    double distance;

    // determine and store the weight of each neighboring vertex
    for(neighbors_iter = neighbors.begin(); neighbors_iter != neighbors.end();
        neighbors_iter++)
    {
      distance = R3Distance(v->position, neighbors_iter->first->position);
      weight = (1./(sigma*sqrt(2.*3.14)))*exp(-(distance*distance)/(2.*sigma*sigma));
      total_weight += weight;
      neighbors_iter->second = weight;
    }

    // determine new position of current vertex
    R3Point new_pos = v->position;
    for(neighbors_iter = neighbors.begin(); neighbors_iter != neighbors.end();
        neighbors_iter++)
    {
      new_pos += (neighbors_iter->second/total_weight)*(neighbors_iter->first->position);
    }

    new_pos.Translate(new_pos - v->position);
    new_mesh->Vertex(i)->position.Reset(new_pos.X(), new_pos.Y(), new_pos.Z());

    neighbors.clear();
  }



  *this = *new_mesh;
}

I wish to smooth a given 3D mesh, which uses the Half-Edge structure to store adjacency information, using a Gaussian function. The following is the algorithm that was proposed:

Smooth the mesh by moving every vertex
to a position determined by a weighted
average of its immediate neighbors
(with weights determined by a Gaussian
with sigma equal to the average length
of edges attached to the vertex,
normalized such that the weights sum
to one).

So for each vertex curr_vertex, I

  1. calculate the average length of its attached edges
  2. obtain all of the neighboring vertices
  3. determine the weight of each neighboring vertex by doing the following:
    `

weight =
exp(-(distance*distance)/(2.sigmasigma))

where distance is the 3D distance between thecurr_vertexand the neighbor andsigma= average length of attached edges ofcurr_vertex`

  1. sum up all of the weights and divide each neighbor's weight by this sum (normalization step)
  2. multiply the position of each neighboring vertex by its corresponding weight
  3. add up all of the weighted vertices and add the result to curr_vertex to produce the new vertex.

When I do this and run my algorithm, instead of smoothing what actually happens is a scale - my mesh just becomes enlarged without any apparent smoothing.

Any thoughts on what I'm doing wrong?

Edit:
Here's the code that I have:

void R3Mesh::
Smooth(void)
{
  R3Mesh *new_mesh = new R3Mesh(*this);
  for(int i = 0; i < NVertices(); i++)
  {
    R3MeshVertex *v = Vertex(i);
    map<R3MeshVertex *, double> neighbors; // stores vertex-weight pairs 
    map<R3MeshVertex *, double>::iterator neighbors_iter;

    // store each vertex neighbor by going through
    // all adjacent edges and obtaining those edges' vertices
    R3MeshHalfEdge *tmp = v->half_edge;
    do
    {
      neighbors.insert(make_pair(tmp->opposite->vertex,0));
      tmp = tmp->opposite->next;
    }while(tmp != v->half_edge);

    // determine the sigma to use for Gaussian
    double sigma = v->AverageEdgeLength();
    double weight = 0, total_weight = 0;
    double distance;

    // determine and store the weight of each neighboring vertex
    for(neighbors_iter = neighbors.begin(); neighbors_iter != neighbors.end();
        neighbors_iter++)
    {
      distance = R3Distance(v->position, neighbors_iter->first->position);
      weight = (1./(sigma*sqrt(2.*3.14)))*exp(-(distance*distance)/(2.*sigma*sigma));
      total_weight += weight;
      neighbors_iter->second = weight;
    }

    // determine new position of current vertex
    R3Point new_pos = v->position;
    for(neighbors_iter = neighbors.begin(); neighbors_iter != neighbors.end();
        neighbors_iter++)
    {
      new_pos += (neighbors_iter->second/total_weight)*(neighbors_iter->first->position);
    }

    new_pos.Translate(new_pos - v->position);
    new_mesh->Vertex(i)->position.Reset(new_pos.X(), new_pos.Y(), new_pos.Z());

    neighbors.clear();
  }



  *this = *new_mesh;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2024-08-30 02:52:13

这有点令人头疼的打字错误风格的错误。你所描述的算法对我来说看起来不错。

首先明确验证权重的归一化。

接下来检查您的代码,将计算出的顶点位置写回到新的平滑网格中。

这些是我的猜测。如果这些不起作用,请随意发布代码片段。

This smacks of a head-slapping typo style bug. The algorithm as you described looks fine to me.

First clearly verify the normalization of the weights.

Next check your code where you write back the calculated vertex pos into the new smoothed mesh.

Those are my guesses. Feel free to post a code snippet if those don't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文