Skip to Content.
Sympa Menu

cgal-discuss - Re: [cgal-discuss] How to associate indices to points in the same order as they are added to a triangulation?

Subject: CGAL users discussion list

List archive

Re: [cgal-discuss] How to associate indices to points in the same order as they are added to a triangulation?


Chronological Thread 
  • From: "Sebastien Loriot (GeometryFactory)" <>
  • To:
  • Subject: Re: [cgal-discuss] How to associate indices to points in the same order as they are added to a triangulation?
  • Date: Tue, 29 Mar 2011 08:21:36 +0200

With the 3.8-beta1 release of CGAL:

http://www.cgal.org/Manual/beta/doc_html/cgal_manual/Triangulation_2/Chapter_main.html#Subsection_35.11.2

For the projection, you may be interested by this example:
http://www.cgal.org/Manual/beta/doc_html/cgal_manual/Triangulation_2/Chapter_main.html#Subsection_35.5.2

S.

Adrian B wrote:
How can I associate indices to
CGAL::Exact_predicates_inexact_constructions_kernel::Point_2 points in
the same order as the points are inserted into a Delaunay
triangulation structure?

Background:
In my current project I create a number of 3D points, each of which
has a colour, to represent a terrain. Each point is therefore
described by its x-, y-, z-, r-, g- and b-value. Now I want to mesh
them for creating a surface model, which I safe as a ply-file. To do
so, I project the points into the x-y plane and apply 2D Delaunay
triangulation for generating the connections. The z-coordinate, as
well as the RGB values are preserved. In the end I need to output the
points (x, y, z, r, g, b) toghether with the triplets of point's
indicies, which define the triangles.
On
http://www.cgal.org/Manual/3.8-beta1/doc_html/cgal_manual/BGL/Chapter_main.html
section 72.5.2 I found a way of associating an index to a vertex.
The problem is now, that the indices are apparantly not associated in
the same order as the points (vertices) were added. Therefore the
vertex IDs point to complete different input points than the line
number in my input file. This is my code so far:

#include <fstream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_id_2.h>
#include <CGAL/boost/graph/graph_traits_Delaunay_triangulation_2.h>

#include <climits>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/filtered_graph.hpp>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point;

typedef CGAL::Triangulation_vertex_base_with_id_2<K> Tvb;
typedef CGAL::Triangulation_face_base_2<K> Tfb;
typedef CGAL::Triangulation_data_structure_2<Tvb,Tfb> Tds;
typedef CGAL::Delaunay_triangulation_2<K, Tds> Triangulation;

template <typename T>
struct Is_finite {

const T* t_;

Is_finite()
: t_(NULL)
{}

Is_finite(const T& t)
: t_(&t)
{ }

template <typename VertexOrEdge>
bool operator()(const VertexOrEdge& voe) const {
return ! t_->is_infinite(voe);
}
};

typedef Is_finite<Triangulation> Filter;
typedef boost::filtered_graph<Triangulation,Filter,Filter>
Finite_triangulation;
typedef boost::graph_traits<Finite_triangulation>::vertex_descriptor
vertex_descriptor;
typedef boost::graph_traits<Finite_triangulation>::vertex_iterator
vertex_iterator;

int main()
{
Triangulation dt;
Filter is_finite(dt);
Finite_triangulation ft(dt, is_finite, is_finite);
vertex_iterator vit, ve;
int index = 0, nbVertices = 0;
const int maxLineLength = 200;
char line[maxLineLength];
float x,y,z;
int r,g,b;
std::ifstream fPoints("D:/input_file.txt");
std::ofstream fPointsAndMeshOut;
std::ifstream fPointsAndMeshIn;
std::string path_pointsAndMeshFile= "D:/temp_file.txt";
std::ofstream fPly("D:/output_file.ply");

fPointsAndMeshOut.open(path_pointsAndMeshFile.c_str());
fPointsAndMeshOut << std::setprecision(4);
while (!fPoints.eof()) {
fPoints.getline(line, maxLineLength);
sscanf(line, "%f, %f, %f, %d, %d, %d", &x, &y, &z, &r, &g,
&b);
fPointsAndMeshOut << std::fixed << x << " " << y << " " << z << "
"
<< r << " " << g << " " << b << std::endl;
dt.insert(Point(x,y));
nbVertices++;
}
fPoints.close();

for(boost::tie(vit,ve)=boost::vertices(ft); vit!=ve; ++vit ){
vertex_descriptor vd = *vit;
vd->id()= index++;
vd->point();
}

Triangulation::Finite_faces_iterator it;
for (it = dt.finite_faces_begin(); it != dt.finite_faces_end(); it++)
{
fPointsAndMeshOut << "3 " << it->vertex(0)->id() << " " <<
it->vertex(1)->id() << " " << it->vertex(2)->id() << std::endl; //
Here the IDs don't correspond to the line number in the input file,
producing a meaningless file here
}
fPointsAndMeshOut.close();

fPly << "ply" << std::endl;
fPly << "format ascii 1.0" << std::endl;
fPly << "element vertex " << nbVertices << std::endl;
fPly << "property float x" << std::endl;
fPly << "property float y" << std::endl;
fPly << "property float z" << std::endl;
fPly << "property uchar red" << std::endl;
fPly << "property uchar green" << std::endl;
fPly << "property uchar blue" << std::endl;
fPly << "element face " << dt.number_of_faces() << std::endl;
fPly << "property list uchar int vertex_index" << std::endl;
fPly << "end_header" << std::endl;

fPointsAndMeshIn.open(path_pointsAndMeshFile.c_str());
while (!fPointsAndMeshIn.eof()) {
fPointsAndMeshIn.getline(line, maxLineLength);
fPly << line << std::endl;
}
fPointsAndMeshIn.close();
fPly.close();

return 0;
}


Needless to say that the mesh output like this is compeltely wrong,
with triangles going all over the place but for sure nothing like in a
smooth mesh model.
Therefore my question: How can I associate the indices (IDs) in the
same order as the points are added?





Archive powered by MHonArc 2.6.16.

Top of Page