CGAL read_OFF discards face depending on vertex order - c++

When reading an off-file with cgal it appears that the vertex order of a face decides whether or not it is read in by read_OFF. But the off-file definition does not say anything about the vertex order of a face.
I am reading in self generated off-files using the read_OFF method of cgal:
using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_3 = typename Kernel::Point_3;
...
CGAL::Surface_mesh<Point_3> test_mash;
CGAL::IO::read_OFF(file_name, test_mash);
std::cout << "Number of vertices: " << test_mash.vertices().size()
<< ", Number of faces: " << test_mash.faces().size() << std::endl;
two_faces_read.off:
OFF
4 2 0
1 1 1
2 -2 2
3 3 -3
-4 4 4
3 0 1 2
3 0 3 1
one_face_read.off:
OFF
4 2 0
1 1 1
2 -2 2
3 3 -3
-4 4 4
3 0 1 2
3 0 1 3
Reading two_faces_read.off works as expected, printing:
Number of vertices: 4, Number of faces: 2.
But when i read one_face_read.off i get Number of vertices: 4, Number of faces: 1. The only difference between these two files is the last line, the vertex order of the second face is different. After trying all possible combinations it seems that with 031, 103, 310 2 faces are read in, while with 013, 130, 301 only 1 face is read in.
The off-file specification referenced by cgal does not mention any rules concerning the vertex order of a face.
Why does this happen and how can i ensure that all faces are read in?

one_face_read.off does not define a valid surface mesh has the orientation of the two faces are not compatible. You can use the following function to read points and faces and call CGAL::Polygon_mesh_processing::is_polygon_soup_a_polygon_mesh() to check if the input is a valid surface mesh. The function CGAL::Polygon_mesh_processing::orient_polygon_soup() can be used to fix orientations. CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh() can be used to create the mesh.

Related

How does OpenGL actually read indices?

I trying to figure out how to draw a flat terrain with triangle_strips, and while I was writing the loop for creating indices array I thought how does OpenGL know to parse the indices because so far I have seen everybody when creating indices write them like that:
0 3 1 4 2 5 0xFFFF 3 6 4 7 5 8
(here being 0xFFFF primitive restart that marks the end of the strip)
and so technically as I understand this should be parsed with offset +1 for every triangle...so the first triangle would use first three indices (0 3 1) and the next with offset +1 (3 1 4)...next (1 4 2) and so on. Here's the picture in case I didn't explain well:
But other way that I have seen is creating indices for every triangle separately .... so:
0 3 1 3 1 4 1 4 2 4 2 5
So my question is how to specify the layout...does OpenGL do this automatically as I set draw call to GL_TRIANGLE_STRIP or else?
There are three kinds of triangle primitives:
GL_TRIANGLES: Vertices 0, 1, and 2 form a triangle. Vertices 3, 4, and 5 form a triangle. And so on.
GL_TRIANGLE_STRIP: Every group of 3 adjacent vertices forms a triangle. A vertex stream of n length will generate n-2 triangles.
GL_TRIANGLE_FAN: The first vertex is always held fixed. From there on, every group of 2 adjacent vertices form a triangle with the first. So with a vertex stream, you get a list of triangles like so: (0, 1, 2) (0, 2, 3), (0, 3, 4), etc. A vertex stream of n length will generate n-2 triangles.
Your first scenario describes a GL_TRIANGLE_STRIP, while your second scenario describes GL_TRIANGLES.

How do I wrap a sprite around a cube without GL_REPEAT? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using OpenGL 3.2+. I created a cube VBO of 8 verticies (each vertex consists of 8 floats: x,y,z,r,g,b,s,t), an EBO to create the faces for that cube, and a VAO to tie it all together.
I have a sprite atlas consisting of hundreds of 16x16 sprites. I'm trying to get 1 sprite from that atlas to appear on all sides of the cube (any orientation is fine).
By tweaking the "s,t" parameters, I can get 4 of the 6 faces to work, but I can't seem to get the last 2 faces to show anything other than random garbage.
Do I need to modify my shader to perform some kind of custom UV wrapping on the atlas?
By tweaking the "s,t" parameters, I can get 4 of the 6 faces to work, but I can't seem to get the last 2 faces to show anything other than random garbage.
At least you have to use separate vertices and attributes, for 2 edges of the cube. This means you need at least 8+2*2=12 different sets of vertex attributes.
x y z u v
0 : -1 1 1 0 0
1 : 1 1 1 1 0
2 : -1 -1 1 0 1
3 : 1 -1 1 1 1
4 : -1 -1 -1 0 0
5 : 1 -1 -1 1 0
6 : -1 1 -1 0 1
7 : 1 1 -1 1 1
8 : -1 1 1 1 1
9 : -1 1 -1 1 0
10 : 1 1 1 0 1
11 : 1 1 -1 0 0
Note, the vertex attribute sets with the indices 0, 2, 4 and 6 have an identically u-coordinate of 0. And the vertex attribute sets with the indices 1, 3, 5 and 7 have an identically u-coordinate of 1.
If you want to wrap a texture to a quad you have to vary the u and the v coordinates. Because of that you have to add the separated vertex attribute sets 8, 9, 10 and 11.
Note, if you want to add normal vectors which are perpendicular to the side planes of the cube, the you need 24 vertex attributes sets. 4 for each side of the cube.

Remove Duplicates in Array and update refering indices. (Remove Duplicate Vertices and Faces)

i need to find a fast way to remove duplicate vertices and faces of a mesh.
The vertices and faces are stored in a Buffers, because I'm working with large data sets I use boost::shared_array<float> vertexBuffer and boost::shared_array<float> faceBuffer for the Buffers.
Next I create a KD-Search-Tree and check for each point if it has a neighbour with dist(point, neighbour)<tollerance. If a neighbour is found i map the neigbour index to the point index in a unordered_map:
oldToNewID[neighbour_id]=point_id
Now i would like to move all vertices that are stored as value in the map to the end of the vertexbuffer. The problem here is that I need to also change the ID of the Face - Points.
For example:
VertexBuffer:
1.3 4.5 3.7|1.8 4.5 3.7|1.3 4.5 3.7|4.3 8.5 2.7|4.9 4.5 2.7
-----------|-----------|-----------|-----------|-----------
Vertex 0 Vertex 1 Vertex 2 Vertex 3 Vertex 4
FaceBuffer:
0 1 3 | 1 2 3 | 4 0 1 | 2 3 4
------------------------------------------------
Face 0 | Face 1 | Face 2 | Face 3
Now Vertex 0 and Vertex 2 are duplicate. So now i somehow have to change the VertexBuffer and FaceBuffer efficiantly so that i will result in:
Updated VertexBuffer:
1.3 4.5 3.7|1.8 4.5 3.7|4.3 8.5 2.7|4.9 4.5 2.7|1.3 4.5 3.7
-----------|-----------|-----------|-----------|-----------
Vertex 0 Vertex 1 Vertex 3 Vertex 4 Vertex 2
NewID 0 NewID 1 NewID 2 NewID 3
Updated FaceBuffer:
0 1 2 | 1 0 2 | 3 0 1 | 0 2 3
------------------------------------------------
Face 0 | Face 1 | Face 2 | Face 3
The duplicates should be moved to the back of the buffer and the faces that where referencing to Vertex 2 needed to be changed. Also each face refering to Vertex 3 and Vertex 4 must be updated because they changed their position.
Any Ideas how to realise that?
Thanks alot!

Meshing options to generate number of the sides of and element (tetgen-triangle)

I wrote a finite element code in fortran 90.
This code is really fast, except the meshing process.
I used triangle and tetgen for meshing in 2D and 3D, respectively, so this process is fast, of course.
For example, for the unit square [0,1]x[0,1] in 2D I have a file with the coordinates of its nodes (for example, a mesh with 5 nodes):
1 0.0 0.0 # coordinates of node 1
2 1.0 0.0 # coordinates of node 2
3 1.0 1.0 # coordinates of node 3
4 0.0 1.0 # coordinates of node 4
5 0.5 0.5 # coordinates of node 5
called coordinate.dat, which have 4 elements (triangles) with nodes called element.dat
1 1 5 4 # vertices of triangle 1
2 1 2 5 # vertices of triangle 2
3 2 3 5 # vertices of triangle 3
4 5 2 4 # vertices of triangle 4
I also have a file where each row i is the number of its initial an final node, called edge.dat:
1 1 2 # initial and final node of edge 1
2 2 3 # initial and final node of edge 2
3 3 4 # initial and final node of edge 3
4 4 1 # initial and final node of edge 4
5 1 5 # initial and final node of edge 5
6 5 2 # initial and final node of edge 6
7 2 5 # initial and final node of edge 7
8 5 4 # initial and final node of edge 8
With this files, I need to generate the following information:
(1) Given an element (triangle or tetrahedron), I need to know the number of its sides (edges and faces, respectively). For example, I need to generate the following structure or file, called struct1.dat:
1 5 8 4 # triangle 1 has the edges number 5, 8 and 4
2 1 6 5 # triangle 2 has the edges number 1, 6 and 5
3 6 2 7 # triangle 2 has the edges number 6, 2 and 7
4 7 3 8 # triangle 4 has the edges number 7, 3 and 8
(2) Furthermore, given a side (edge or face) I need to know the element numbers of the 2 elements (or only one if the side is on the boundary) shared by that side. For example, I need to generate the following structure (or file) called struct2.dat:
1 2 0 # edge number 1 is only on element 2
2 3 0 # edge number 2 is only on element 3
3 4 0 # edge number 3 is only on element 4
4 1 0 # edge number 4 is only on element 1
5 1 2 # edge number 5 is sharing by elements 1 and 2
6 3 2 # edge number 6 is sharing by elements 3 and 2
7 4 3 # edge number 7 is sharing by elements 4 and 3
8 1 4 # edge number 8 is sharing by elements 1 and 4
For both of these structures, struct1.dat and struct2.dat, my code is very slow because I used a brute force approach with a lot of loops..
I am looking for an algorithm (a paper, or better: a subroutine in fortran available for download) optimized for this? I want to continue using triangle and tetgen, but I am willing to listen to other options.

About matrix of edge in graph using c++

I confuse how to define the program that will find the matrix of edge in a graph.
The problem is if one inputs the value of adjacency matrix that give information about connection of vertices in a graph, example : there are 3 vertices, then V1 connected to V2 but not with V3, then V2 connected to V3, it gives :
0 1 0
1 0 1
0 1 0
now, with that information, I want to make the program which find the connection of edge to edge, example there are 3 edges : 1-2 edge, 2-3 edge, and its output is :
0 1
1 0
I know to make the first output "Adjacency matrix", but the second.
Thanks in advance.
An adjacency matrix is used to represent a graph which means you have both vertices and edges in it. So if the input is :
0 1 1
1 0 1
1 1 0
It means your graph is complete. If you read it with putting numbers on your matrix:
X 1 2 3
1 0 1 1
2 1 0 1
3 1 1 0
You can see that:
V1 connects to V2 and V3
V2 connects to V1 and V3
V3 connects to V1 and V2
By reading row n you see which vertice Vn connects to and by reading column n you can see which vertices are connected to Vn.
In your example, your graph is non-oriented because your matrix is diagonal.
If your aim is to find what are the edges of your graph, you already have the information in the adjacency matrix. If you want to see what vertices are "double connected" you need to find if for vertices Vi and Vj the M[i][j] and the M[j][i] members of your matrix both have the value 1 (It is always the case in non-oriented graph). A possible algorithm for that is just a double loop on your matrix representation (generally two vectors/lists/arrays).