bl4ckb0ne / delaunay-triangulation

C++ version the delaunay triangulation
GNU General Public License v3.0
441 stars 109 forks source link

Adding triangle indices #13

Closed AndreAhmed closed 6 years ago

AndreAhmed commented 6 years ago

Hi, I liked your library and I added how to get indices so that people use OpenGL or DirectX Get Benefits.


                    int index = 0;
            for (auto p = begin(vertices); p != end(vertices); p++)
            {
                Vector2<float> f = *p;
                if (coordinate_indices.find(f) == coordinate_indices.end()) {
                    coordinate_indices[f] = index++;
                    indx.push_back(index);
                }

            }

        std::map<VertexType, unsigned int> coordinate_indices;

void writeObj(std::string &name)
        {
            std::ofstream objFile;
            objFile.open(name);
            objFile << "o " << "mesh" << "\n";
            for (auto p = begin(getVertices()); p != end(getVertices()); p++)
            {
                Vector2<float> f = *p;
                objFile << "v " << f.x << " " << f.y << "\n";
            }
            for (auto t = begin(getTriangles()); t != end(getTriangles()); t++) {

                Vector2<float> t1 = t->p1;
                Vector2<float> t2 = t->p2;
                Vector2<float> t3 = t->p3;

                int index1 = coordinate_indices[t1] + 1;
                int index2 = coordinate_indices[t2] + 1;
                int index3 = coordinate_indices[t3] + 1;

                objFile << "f " << index1 << " " << index2 << " " << index3 << "\n";
            }
            objFile.close();
bl4ckb0ne commented 6 years ago

Hi, thanks for your code! If you want to see it in the repo make a PR please.