Serializing vertex_descriptor of a boost::graph separately? - serialization

Is there any way to make vertex_descriptor of a boost::graph serializable? Boost does define <boost/graph/adj_list_serialize.hpp> for serializing the whole graph structure, but not separately.
I have an adjacency-list graph which contains 3dvector (which represents 3d world in X, Y, Z coordinates by the way) vertices as such
typedef boost::adjacency_list<
boost::listS,
boost::listS,
boost::undirectedS,
3dvector>
Graph;
typedef Graph::vertex_descriptor VertexId;
typedef Graph::edge_descriptor EdgeId;
However I also need to keep a separate mapping information for lookup and this is where the problem is: boost doesn't know how to serialize VertexId. They are stored in an STL map as such: std::map<string, VertexId> so that a string lookup can return the right vertex in the graph, as a vertex_descriptor is essentially a reference that points to a specific vertex in the graph.
My intention is to serialize both the graph and the map into a file, but I can't find a way to serialize the vertex_descriptor.

You will need to store the graph and the locations (some vector/list of vertices). You will have to write the serialization method for whatever represents your vertices.
template<typename Archive>
void serialize(Archive &ar, LatLon &loc, const unsigned int /*version*/) {
ar &loc.latitude &loc.longitude;
}
Now you can serialize your graph using:
std::vector<LatLon> locations; // or a list
archive & graph & locations;
Where graph is the adjencency_list and locations is a vector/list of LatLon classes/structs. I think boost already knows how to handle the list/vector.

Related

How can I input a valid Triangulation in right format through the function of input_file()?

I want to use the function of Triangulation_3 by my own data include vertexs and cells. So I have to initialize a Triangulation_3 throught the function input_file().
My question is how can I use this funtiom in a right way?
https://doc.cgal.org/latest/Triangulation_3/group__PkgIOTriangulation3.html#gabb84b5cde2cbb8c580790c10f3f0ddbb,the funtion short introduction can be found here.
As dercribed in user manual,"
A triangulation is a collection of vertices and cells that are linked together through incidence and adjacency relations. Each cell gives access to its four incident vertices and to its four adjacent cells. Each vertex gives access to one of its incident cells."
I think that the hard one is the input of four adjacent cells of each cells.
In brief, I appreciate a demo to tell me how to input a triangulation_3 in right way.
Thank you !
The description of funtion file_input()
The information in the iostream is: the dimension, the number of finite vertices, the non-combinatorial information about vertices (point, etc; note that the infinite vertex is numbered 0), the number of cells, the indices of the vertices of each cell, plus the non-combinatorial information about each cell, then the indices of the neighbors of each cell, where the index corresponds to the preceding list of cells.
When dimension < 3, the same information is stored for faces of maximal dimension instead of cells.
istream & CGAL::Triangulation_3< Traits, TDS, SLDS >::operator>> (istream &is, Triangulation_3 &t)
Reads the underlying combinatorial triangulation from is by calling the corresponding input operator of the triangulation data structure class (note that the infinite vertex is numbered 0), and the non-combinatorial information by calling the corresponding input operators of the vertex and the cell classes (such as point coordinates), which are provided by overloading the stream operators of the vertex and cell types. More...
ostream & CGAL::Triangulation_3< Traits, TDS, SLDS >::operator<< (ostream &os, const Triangulation_3 &t)
Writes the triangulation t into os.
template<typename Tr_src , typename ConvertVertex , typename ConvertCell >
std::istream & CGAL::Triangulation_3< Traits, TDS, SLDS >::file_input (std::istream &is, ConvertVertex convert_vertex=ConvertVertex(), ConvertCell convert_cell=ConvertCell())
The triangulation streamed in is, of original type Tr_src, is written into the triangulation. More...

Fill holes in 3d mesh from image segmentation with CGAL or something else?

I got some segmented image data from a CT scan of a body part in .raw file format. Segmentation was performed in Amira.
In CGAL, I created an .inr file, read this, then meshed this with certain mesh criteria:
typedef CGAL::Labeled_mesh_domain_3<K> Mesh_domain;
typedef CGAL::Mesh_triangulation_3<Mesh_domain,CGAL::Default,Concurrency_tag>::type Tr;
typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3;
typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;
(...)
Mesh_domain domain = Mesh_domain::create_labeled_image_mesh_domain(inr_image);
Mesh_criteria criteria(facet_angle=fangle, facet_size=fsize, facet_distance=fdist,
cell_radius_edge_ratio=creratio, cell_size=csize);
C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain, criteria, lloyd());
I could create a 3d tetrahedral mesh (.vtu) with CGAL for finite element stuff which works fine.
BUT: Unfortunately the final mesh contains too much "holes" between the different tissue types and that's... very bad. So my question is: Is there a way to fill these holes with CGAL? I am open for other ways, too, because it won't be possible to segment the huge amount of data again (this was not done by me).
Thanks a lot, any help appreciated.

CGAL: problem accessing the neighbors of every vertex using edge iterator in periodic triangulation

I am using periodic Delaunay triangulation in CGAL in my code, and producing for each vertex all neighboring vertices. For this I use Edge iterator, since in my case it will be much more faster than Vertex iterator.
Here is the code snippet,
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Periodic_2_triangulation_traits_2<Kernel> Gt;
typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned int, Gt> Vb;
typedef CGAL::Periodic_2_triangulation_face_base_2<Gt> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
typedef CGAL::Periodic_2_Delaunay_triangulation_2<Gt, Tds> Triangulation;
typedef Triangulation::Iso_rectangle Iso_rectangle;
typedef Triangulation::Edge_iterator Edge_iterator;
typedef Triangulation::Vertex_handle Vertex_handle;
typedef Triangulation::Point Point;
typedef vector<pair<Point, unsigned> > Vector_Paired;
Vector_Paired points;
Iso_rectangle domain(0,0,L,L);
for(int iat = 0; iat < N; iat++)
{
points.push_back(make_pair(Point(r_tot[iat][0],r_tot[iat][1]),iat));
}
Triangulation T(points.begin(), points.end(), domain);
for(Edge_iterator ei=T.finite_edges_begin(); ei!=T.finite_edges_end(); ei++)
{
Triangulation::Face& f = *(ei->first);
int ii = ei->second;
Vertex_handle vi = f.vertex(f.cw(ii));
Vertex_handle vj = f.vertex(f.ccw(ii));
int iat = vi->info();
int jat = vj->info();
VecInd[iat].push_back(jat);
VecInd[jat].push_back(iat);
}
But, sometimes instead of one special neighbors for each vertex I get 8 or 9 or ... copy of the same neighbor.
For example in VecInd which is a 2D vector containing neighboring indices I get some thing like this:
VecInd[0]=[2,2,2,2,4,4,4,...]
I couldn't find an example using edge iterator in CGAL website, and nothing related in stackoverflow.
I am wondering whether this implementation is correct? What should I add to my code in order to get one copy per each neighbor, I can use STL::sets, but I would like to know the source of problem.
Here is the answer that was posted on the CGAL-discuss mailing-list, by Mael:
If your point set is not geometrically well spaced, it's possible that the triangulation of these points do not form a simplicial complex over the flat torus (in other words, there are short cycles in the triangulation). In this case, the algorithm uses 8 copies of the triangulation to artificially create a simplicial complex. You can check if this is the case using the function is_triangulation_in_1_sheet() and read more about these mechanisms in the User Manual.
When copies are being used, iterating over the edges will indeed give you exactly what the underlying data structure has : 9 entities for each edge. To get unique ones, you can simply filter 8 out of the 9 by looking at the offset of the vertices of the edge. This is what is done in the iterator that returns unique periodic segments. Unfortunately, you want edges and this iterator converts directly to the geometry of the edge (the segment). Nevertheless, you can simply use the main filtering function from that iterator, that is: is_canonical(). This function will look at the offset of the two vertices of your edges, and keep only those that have at least one vertex in the first copy of the domain, which is enough to make it unique.

Triangulating Polyhedron faces in CGAL

Having an arbitrary polyhedron in CGAL (one that can be convex, concave or, even, have holes) how can I triangulate its faces so that I can create OpenGL Buffers for rendering?
I have seen the convex_hull_3() returns a polyhedron that has triangulated faces, but it won't do what I want for arbitrary polyhedrons.
The header file <CGAL/triangulate_polyhedron.h> contains a non-documented function
template <typename Polyhedron>
void triangulate_polyhedron(Polyhedron& p)
that is working with CGAL::Exact_predicates_inexact_constructions_kernel for example.
The Polygon Mesh Processing package provides the function CGAL::Polygon_mesh_processing::triangulate_faces with multiple overloads. The simplest thing to do would be
typedef CGAL::Simple_cartesian<float> Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
Polyhedron_3 polyhedron = load_my_polyhedron();
CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron);
After that, all faces in polyhedron are triangles.
The function modifies the model in-place, so one has to use a HalfedgeDS that supports removal. This is the default, but, for example, HalfedgeDS_vector won't do.
See also an official example that uses Surface_mesh instead of Polyhedron_3:
Polygon_mesh_processing/triangulate_faces_example.cpp

Best (and fastest) way to store triangles and lines in C++?

I've got a few 3D apps going, and I was wondering, what is the best way to store lines and triangles? At the moment, I have lines as an array of typedef'd vectors as such:
typedef struct
{
float x, y, z;
}
Vector
Vector line[2];
Now, I could do it like this:
typedef struct
{
Vector start, end;
}
Line
Line lineVar;
Faces could be similar:
typdef struct
{
Vector v1, v2, v3;
}
Face faceVar;
My question is this: Is there a better or faster way to store lines and faces? Or am I doing it OK?
Thanks,
James
What you have is pretty much how vectors are represented in computer programs. I can't imagine any other way to do it. This is perfectly fine:
typedef struct
{
float x, y, z;
} Vector;
(DirectX stores vector components like this, by the way.)
However, 3D intensive programs typically have the faces index into a vector array to save space since the same points often appear on different faces of a 3D model:
typedef struct
{
int vectorIndex1, vectorIndex2, vectorIndex3;
} Face;