boost Kamada-Kawai spring layout vertices collision - c++

I'm trying to layout a graph using boost Kamada-Kawai-spring-layout algorithm.The problem is that vertices collide with each other in some graphs.I add all edges with weight 1.0
add_edge(a,b,1.0,g);
then I layout the graph like this :
minstd_rand gen;
topology_type topo(gen,0,0,widht,height);
random_graph_layout(g,position,topo);
bool kamada=kamada_kawai_spring_layout(g,position,get(edge_weight,g),topo,side_length(width),kamada_kawai_done());
Some graphs work fine while other produce output like this
I think the layout is fine but the length of edges need to be increased but i don't know how

You can used boost::edge_length(e) instead of boost::side_length(s) in order to control the edge length on the graph
bool kamada=kamada_kawai_spring_layout(g,position,get(edge_weight,g),topo,boost::edge_length(length),kamada_kawai_done());

Related

Find all neighbours of a certain primitive in a CGAL::Surface_mesh

I wrote a small raytracer (with CGAL::Surface_mesh Mesh) with tree acceleration in cgal. I would like to find all neighbours of a hit primitive.
Ray_intersection hit = tree.first_intersection(rays[y][x]);
if(hit)
{
const Point& point = boost::get<Point>(hit->first);
const Primitive_id& primitive_id = boost::get<Primitive_id>(hit->second);
//i need the neighbours of the hit primitive
}
How do I this? I found this documentation but it seems to work only for points not primitives:
https://doc.cgal.org/latest/Spatial_searching/index.html
And it searches for its euclidan distance not for being connected together.
Is there something like:
std::vector<Primitive_id&> ids = getNeighoursOfPrimive(primitive_id);
Like I said I am using CGAL::Surface_mesh Mesh for my mesh and their is only one mesh in the scene.
You can use the range returned by vertices_around_face() to get all vertices of a face, then for each vertex you can use the range returned by halfedges_around_target() to get one halfedge per face incident to that vertex (or you can do it by hand using a combinaison of next and opposite).

Is there a way to make this shortest path algorithm faster?

Using the CGAL lib, I'm trying to implement the Shortest Path methods.
I've been kind of successful, but the time it takes to map a path is not nearly acceptable, taking up to 1.5 seconds running in Release.
I'm aware that the input might be overwhelmingly big, having 50000 faces, but that is what I have to work with.
To be more detailed on what I'm trying to do is being able to draw a spline along the surface of a mesh by clicking in two different places and generating a path from them just like in the image:
My type definitions are:
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Surface_mesh<Kernel::Point_3> Triangle_mesh;
typedef CGAL::Surface_mesh_shortest_path_traits<Kernel, Triangle_mesh> Traits;
// default property maps
typedef boost::property_map<Triangle_mesh,
boost::vertex_external_index_t>::type Vertex_index_map;
typedef boost::property_map<Triangle_mesh,
CGAL::halfedge_external_index_t>::type Halfedge_index_map;
typedef boost::property_map<Triangle_mesh,
CGAL::face_external_index_t>::type Face_index_map;
typedef CGAL::Surface_mesh_shortest_path<Traits> Surface_mesh_shortest_path;
typedef boost::graph_traits<Triangle_mesh> Graph_traits;
typedef Graph_traits::vertex_iterator vertex_iterator;
typedef Graph_traits::halfedge_iterator halfedge_iterator;
typedef Graph_traits::face_iterator face_iterator;
My code looks like the following:
Traits::Barycentric_coordinates src_face_location = { { p1.barycentric[2], p1.barycentric[0], p1.barycentric[1] } };
face_iterator src_face_it = faces(map->m_cgal_mesh).first;
std::advance(src_face_it, src_faceIndex);
map->m_shortest_paths->remove_all_source_points();
map->m_shortest_paths->add_source_point(*src_face_it, src_face_location);
Traits::Barycentric_coordinates dest_face_location = { { p2.barycentric[2], p2.barycentric[0], p2.barycentric[1] } };
face_iterator dest_face_it = faces(map->m_cgal_mesh).first;
std::advance(dest_face_it, dest_faceIndex);
std::vector<Traits::Point_3> cgal_points;
auto r = map->m_shortest_paths->shortest_path_points_to_source_points(*dest_face_it, dest_face_location, std::back_inserter(cgal_points));
points.resize(cgal_points.size(), 3);
for (int i = 0; i < cgal_points.size(); ++i) {
auto const& p = cgal_points[i];
points.row(i) = RowVector3d(p.x(), p.y(), p.z());
}
The process that takes 99% of the total time is on this line:
auto r = map->m_shortest_paths->shortest_path_points_to_source_points(*dest_face_it, dest_face_location, std::back_inserter(cgal_points));
Any idea on how to improve performance?
The CGAL docs state the shortest route is always a straight line when you would unfold the mesh on a 2D plane.
The input for the shortest path algorithm is a vertex or plane with barycentric coordinates. You could map these input coordinates to a 2D texture which was mapped on your mesh.
Draw a red line between start and end point on your texture.
You will have to dig deeper on how to translate the vertices input coordinates into absolute XY coordinates in the texture.
Also keep in mind that the shortest path could be running over the back of the mesh. Depending on how the texture is mapped it could be possible that you need to draw more than 1 line.
It is pretty clear from the documention. You need to call build_sequence_tree first.
My suggestion is you put in this performance hit somewhere before the user clicks the destination - this could be when the source is selected first, so that it is not felt when the user starts clicking around. Even better if you can find a way to safely run this in the background.
2.1.3 Building the Internal Sequence Tree
A time consuming operation for shortest path queries consists in
building an internal data structure used to make the queries. This
data structure is called the sequence tree. It will be built
automatically when the first shortest path query is done and will be
reused for any subsequent query as long as the set of source points
does not change. Each time the set of source points is changed the
sequence tree needs to be rebuilt (if already built). Note that it can
also be built manually by a call to
Surface_mesh_shortest_path::build_sequence_tree().
https://doc.cgal.org/latest/Surface_mesh_shortest_path/index.html
Additional, it looks like the algorithm runs in worst case polynomial time. As others have pointed it could potentially be optimized if you know your problem is convex in all cases.

Kinect2 C++ get Indices for Face Triangle vertices

I want to debug the face mesh generated from kinect 2 sensor's HD face tracking.
I can get the Vertex data using CalculateVerticesForAlignment however I don't know the index for these vertices to form triangles.
I tried to use result = GetFaceModelTriangles(TriangleCount, TraingleIndices) but the resukt it returns is not S_OK and Traingle indices are not a multiple of three meaning GetFaceModelTriangles does not gives the indices.
The C++ KInect API documentation is very poor so I don't know if GetFaceModelTriangles even does what I think it does.

Draw Ring with shapeRenderer in LibGDX

I want to draw a ring (circle with big border) with the shaperenderer.
I tried two different solutions:
Solution: draw n-circles, each with 1 pixel width and 1 pixel bigger than the one before. Problem with that: it produces a graphic glitch. (also with different Multisample Anti-Aliasing values)
Solution: draw one big filled circle and then draw a smaller one with the backgroundcolor. Problem: I can't realize overlapping ring shapes. Everything else works fine.
I can't use a ring texture, because I have to increase/decrease the ring radius dynamic. The border-width should always have the same value.
How can I draw smooth rings with the shaperenderer?
EDIT:
Increasing the line-width doesn't help:
MeshBuilder has the option to create a ring using the ellipse method. It allows you to specify the inner and outer size of the ring. Normally this would result in a Mesh, which you would need to render yourself. But because of a recent change it is also possible to use in conjunction with PolygonSpriteBatch (an implementation of Batch that allows more flexible shapes, while SpriteBatch only allows quads). You can use PolygonSpriteBatch instead of where you normally would use a SpriteBatch (e.g. for your Stage or Sprite class).
Here is an example how to use it: https://gist.github.com/xoppa/2978633678fa1c19cc47, but keep in mind that you do need the latest nightly (or at least release 1.6.4) for this.
Maybe you can try making a ring some other way, such as using triangles. I'm not familiar with LibGDX, so here's some
pseudocode.
// number of sectors in the ring, you may need
// to adapt this value based on the desired size of
// the ring
int sectors=32;
float outer=0.8; // distance to outer edge
float inner=1.2; // distance to inner edge
glBegin(GL_TRIANGLES)
glNormal3f(0,0,1)
for(int i=0;i<sectors;i++){
// define each section of the ring
float angle=(i/sectors)*Math.PI*2
float nextangle=((i+1)/sectors)*Math.PI*2
float s=Math.sin(angle)
float c=Math.cos(angle)
float sn=Math.sin(nextangle)
float cn=Math.cos(nextangle)
glVertex3f(inner*c,inner*s,0)
glVertex3f(outer*cn,outer*sn,0)
glVertex3f(outer*c,outer*s,0)
glVertex3f(inner*c,inner*s,0)
glVertex3f(inner*cn,inner*sn,0)
glVertex3f(outer*cn,outer*sn,0)
}
glEnd()
Alternatively, divide the ring into four polygons, each of which consists of one quarter of the whole ring. Then use ShapeRenderer to fill each of these polygons.
Here's an illustration of how you would divide the ring:
if I understand your question,
maybe, using glLineWidth(); help you.
example pseudo code:
size = 5;
Gdx.gl.glLineWidth(size);
mShapeRenderer.begin(....);
..//
mShapeRenderer.end();

CGAL - Boost create undirected graph

I've created a 3D Volume mesh using Cgal's documentation and I was succesfully able to visualize my c3t3 (Complex 3 in triangulation 3) object. The mesh is composed of several connected components the number of which i want to find using boost.
Dealing with c2t3 (cimplex 2 in triangulation 3) in the past i iterated over the vertices of the, make pairs with a vertex_descriptor and then again iterate over the edges finding the vertices and by using add_edge i created the undirected graph and finally return the number of c.c.
Now, the c3t3 object only provides iterators about vertices nad cells (not edges - only can be implicitly found). Can you help me pass the c3t3 object to a graph structure of Boost?
So far i did:
for (Cell_iterator c_it=c3t3.cells_in_complex_begin(); c_it != c3t3.cells_in_complex_end(); ++c_it)
{
Vertex_descriptor vd1 = boost::add_vertex(graph);
Vertex_descriptor vd2 = boost::add_vertex(graph);
Vertex_descriptor vd3 = boost::add_vertex(graph);
Vertex_descriptor vd4 = boost::add_vertex(graph);
C3t3::Vertex_handle v0 = c_it->vertex(0);
C3t3::Vertex_handle v1 = c_it->vertex(1);
C3t3::Vertex_handle v2 = c_it->vertex(2);
C3t3::Vertex_handle v3 = c_it->vertex(3);
vertex_id_map.insert(std::make_pair(v0, vd1));
vertex_id_map.insert(std::make_pair(v1, vd2));
vertex_id_map.insert(std::make_pair(v2, vd3));
vertex_id_map.insert(std::make_pair(v3, vd4));
}
Now i have to create the edges but i don;t know where to find the correct edges correspond to my c3t3 object.. Thank you for your help in advance
CGAL provides "Boost Graph adapters" for many of its classes (at least for triangulations, arrangements and for polyhedral surfaces). See more details in CGAL BGL page.
Essentially all DCEL- or similar structures can be viewed as a combination of two graphs, "primary" for vertex-edge relationships and "dual" for cell-edge relationships; CGAL provides Boost graph adapters for both.
If this out-of-box graphs are not good for you, than you have to answer your central question: how do you iterate over edges of a given cell (i.e. over edges of the "dual" graph) or over edges of a given vertex (i.e. over edges of the "primal" graph).