-1

I tried the code below, but I have incorrect indices. I'm trying to render lines using CGAL and 3D Delaunay, but I get correct indices. It was hard to get the indices of the 3D Delaunay. I don't know which part is wrong, I try with 4 vertices, it looks correct, but with more than 5 indices I get wrong triangles...

std::vector<int> triangles;

std::map<Delaunay3::Vertex_handle, int> index_of_vertex;
int j = 0;
for (Delaunay3::Finite_vertices_iterator it = dt.finite_vertices_begin();
     it != dt.finite_vertices_end(); ++it, ++j) {
    index_of_vertex[it.base()] = j;
}

for (Delaunay3::Finite_facets_iterator itFacet = dt.finite_facets_begin();
     itFacet != dt.finite_facets_end(); itFacet++) {
    triangles.push_back(index_of_vertex[itFacet->first->vertex(0)]);
    triangles.push_back(index_of_vertex[itFacet->first->vertex(1)]);
    triangles.push_back(index_of_vertex[itFacet->first->vertex(2)]);

    triangles.push_back(index_of_vertex[itFacet->first->vertex(0)]);
    triangles.push_back(index_of_vertex[itFacet->first->vertex(2)]);
    triangles.push_back(index_of_vertex[itFacet->first->vertex(3)]);

    triangles.push_back(index_of_vertex[itFacet->first->vertex(1)]);
    triangles.push_back(index_of_vertex[itFacet->first->vertex(2)]);
    triangles.push_back(index_of_vertex[itFacet->first->vertex(3)]);
}
std::vector<WORD> lineIndex;

lineIndex.resize(triangles.size() * sizeof(int) * 4);
int l, t;
for (l = 0, t = 0; t < triangles.size(); t += 4) {
    // Each triangle has 3 lines, so D3D_PRIMITIVE_TOPOLOGY_LINELIST needs 6 vertices
    // Each vertex has to be listed twice
    lineIndex[l] = triangles[t];
    l++;
    lineIndex[l] = triangles[t + 1];
    l++;
    lineIndex[l] = triangles[t + 1];
    l++;
    lineIndex[l] = triangles[t + 2];
    l++;
    lineIndex[l] = triangles[t + 2];
    l++;
    lineIndex[l] = triangles[t];
    l++;
}

// Fill in a buffer description for drawing only the triangle outlines
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = numTriangleVertices * 3 * sizeof(int); // <---- from the function
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = static_cast<void*>(lineIndex.data());
pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTriOutlineIndexBuffer);
plamut
  • 3,085
  • 10
  • 29
  • 40
andre
  • 731
  • 2
  • 13
  • 27

1 Answers1

0

Let me explain it with a triangulation with only 4 non-coplanar vertices.

You then have 4 finite facets. Each of them is represented as a std::pair<Delaunay3::Cell_handle, int> pair;, where pair. first is the cell incident to the facet and pair.second is the index of the vertex opposite to the facet in the cell.

If pair.second == 0, the indices of the vertices of the facet are 1,2,3; If pair.second == 1, the indices of the vertices of the facet are 2,3,0; etc.

That is in general, f pair.second == i, the indices of the vertices of the facet are (i+1)%4, (i+2)%4, (i+3)%4

There is also a helper class, which is the base class of the triangulations https://doc.cgal.org/latest/TDS_3/classCGAL_1_1Triangulation__utils__3.html

It allows you to write Delaunay_3::vertex_triple_index(i,j).

I agree that this is not well documented at all, and we are going to improve that.

Andreas Fabri
  • 1,144
  • 6
  • 9