具有行进立方体的隐式曲面上的CSG操作

7 math graphics marching-cubes implicit-surface

我使用行进立方体渲染等值面(或者可能是行进的正方形,因为这是2D),我想做集合操作,如设置差异,交集和联合.我认为这很容易实现,只需在两个不同的隐式曲面中选择两个顶点标量,但事实并非如此.

对于我的初步测试,我尝试了两个球体圆圈,以及设置的操作差异.即A - B.一个圆圈正在移动而另一个圆圈是静止的.这是我在选择顶点标量时以及在将角顶点分类为内部或外部时尝试的方法.代码是用C++编写的.OpenGL用于渲染,但这并不重要.没有任何CSG操作的正常渲染确实给出了预期的结果.



       void march(const vec2& cmin, //min x and y for the grid cell
                  const vec2& cmax, //max x and y for the grid cell
                  std::vector<vec2>& tri, 
                  float iso,
                  float (*cmp1)(const vec2&), //distance from stationary circle
                  float (*cmp2)(const vec2&) //distance from moving circle
)
{
  unsigned int squareindex = 0;
  float scalar[4];
  vec2 verts[8];
  /* initial setup of the grid cell */
  verts[0] = vec2(cmax.x, cmax.y);
  verts[2] = vec2(cmin.x, cmax.y);
  verts[4] = vec2(cmin.x, cmin.y);
  verts[6] = vec2(cmax.x, cmin.y);

  float s1,s2;
  /**********************************
   ********For-loop of interest******
   *******Set difference between ****
   *******two implicit surfaces******
   **********************************/
  for(int i=0,j=0; i<4; ++i, j+=2){
    s1 = cmp1(verts[j]);
    s2 = cmp2(verts[j]);
    if((s1 < iso)){ //if inside circle1
      if((s2 < iso)){ //if inside circle2
        scalar[i] = s2; //then set the scalar to the moving circle
      } else {
        scalar[i] = s1; //only inside circle1
        squareindex |= (1<<i); //mark as inside
      }
    }
    else {
      scalar[i] = s1; //inside neither circle
    }
  }

  if(squareindex == 0)
    return;
  /* Usual interpolation between edge points to compute
     the new intersection points */
  verts[1] = mix(iso, verts[0], verts[2], scalar[0], scalar[1]);
  verts[3] = mix(iso, verts[2], verts[4], scalar[1], scalar[2]);
  verts[5] = mix(iso, verts[4], verts[6], scalar[2], scalar[3]);
  verts[7] = mix(iso, verts[6], verts[0], scalar[3], scalar[0]);

  for(int i=0; i<10; ++i){ //10 = maxmimum 3 triangles, + one end token
    int index = triTable[squareindex][i]; //look up our indices for triangulation
    if(index == -1)
      break;
    tri.push_back(verts[index]);
  }
}
Run Code Online (Sandbox Code Playgroud)

这给了我奇怪的锯齿:这里http://www.mechcore.net/images/gfx/csgbug2.png
看起来CSG操作是在没有插值的情况下完成的.它只是"丢弃"整个三角形.我是否需要以其他方式进行插值,或者组合顶点标量值?我会喜欢这方面的帮助.可在此处下载完整的测试用例

编辑:基本上,我的行军广场的实施工作正常.这是我的标量字段被破坏了,我想知道正确的方式会是什么样子.最好是我正在寻找一种通用方法来实现我上面讨论的三组操作,对于通常的基元(圆形,矩形/方形,平面)

编辑2:这是在实施回答者的白皮书后的一些新图像:

1.Difference
2.Intersection
3.Union

编辑3:我也使用适当的阴影/灯光在3D中实现了这一点:

1.较大球体与较小球体
之间的差异2.中心较大球体与较小球体之间的差异,两侧由两个平面夹住,然后与中心的球体结合.
3.两个气缸之间的联合.

Rom*_*nka 4

这不是混合标量场的方式。你的标量说明了一件事,但无论你是否在里面,你的标志说明了另一件事。首先合并字段,然后像执行单个复合对象一样进行渲染:

for(int i=0,j=0; i<4; ++i, j+=2){
  s1 = cmp1(verts[j]);
  s2 = cmp2(verts[j]);
  s = max(s1, iso-s2); // This is the secret sauce
  if(s < iso) { // inside circle1, but not inside circle2
    squareindex |= (1<<i);
  }
  scalar[i] = s;
}
Run Code Online (Sandbox Code Playgroud)

本文可能会有所帮助:使用基于 Lipschitz 的隐式曲面将 CSG 建模与软混合相结合