How to generate half-edge structure representation for a polygonal mesh? - c++

I would like to generate output to display the numeric data of the Half-Edge Structure that is based from an input of polygonal mesh data (in numeric data form).
The concept to read the polygonal model basically is like this:
For the INPUT, the file is in OFF format and include datas like
(a) First part: the number of vertex, number of face, and number of edge.
(b) Second part: line of each vertex,
(c) Last part: line of each polygonial face.
Example: (based from above)
First part:
4 4 6
Second part:
-1.7 0.0 1.0
1.7 0.0 1.0
0.0 0.0 -2.0
0.0 3.0 0.0
Last part:
3 0 1 2
3 0 3 2
3 3 2 1
3 0 2 3
And, the program must be able to generate following data:
(a) Vertices:
1. vertex index (Total number of vertex input)
2. each x-coordinate, y-coordinate, z-coordinate, and half edge
(b) Half-Edges:
1. half edge index (Total number of half edge to be generated from a.2. above)
2. starting vertex, face, next half edge, and adjacent half edge.
(c) Faces:
1. face index (Total number of face as shown on b.2. starting from 0)
2. half edge.
The concept is like that, no need to display visual graphic but need to use algorithm and generate simple Half-Edge Structure data, then read the OUTPUT file by using Notepad, etc.

This question consists of two parts:
Reading mesh in OFF file format, which includes reading vertex coordinates
-1.7 0.0 1.0
1.7 0.0 1.0
0.0 0.0 -2.0
0.0 3.0 0.0
and triples of vertex indices per each triangle
3 0 1 2
3 0 3 2
3 3 2 1
3 0 2 3
Half-edge data structure construction from these vertex triples. It is the topic of the question Initializing Half-edge data structure from vertices, where you can find both the explanation and links to exemplar implementations.

Related

Unexpected transformationMatrix and offset Matrix with Skeletal Animation

What I use:
Assimp to import .fbx files from blender
OpenGL for rendering
glm lib for handling matrices and vectors
I am trying to make skeletal animation work. I dont read the .fbx file directly into the Program but I convert it to a binary at first. I tried to understand how transformationMatrix and offsetMatrix is supposed to work. This is what I understood: In order to later be able to run an animation, we need to find a way to make a moving Bone affect its vertices but also the bones connected to it. So the idea is to use transformation matrices, which describe the coordinate system of a bone or a node and we multiply along these paths to a bone and then multiply by is offsetMatrix to be back in objectSpace. I think by now I tried every possible combination of multiplying these but I always get something wrong. Then I looked at the values a couple of times and to me it is not obvious at all how this should work. Correct me if I am wrong but my expectation is that when in BindPose and using the offsetMatrices and transformationMatrices from the Assimp import I must result with an identity Matrix, because I want my model in bind pose just as without those Matrices. These are the transformation Matrices from all nodes up to the first bone:
The Root Node is identity
Armature mTransformationMatrix:
100 0 0 0
0 -1.6e-05 100 0
0 -100 -1.629e-05 0
0 0 0 1
Bone mTransformationMatrix:
1 0 0 0
0 1.6e-07 -1 0
0 1 -1.6e-07 0
0 0 0 1
I expect those two to result with something like an identity*100 when multiplied.
mOffsetMatrix of the corresponding Bone:
0.38 0 0 0
0 0 -1 0
0 0.28 0 0
0 0 1.67 1
In my opinion this doesnt help me at all. So either my expectaiton to result with an identity matrix are wrong or the offsetMatrix is.
In case you consider it important what my model looks like:
Edit: I forgot to mention: to read in the aiMatrix4x4 I use
static inline glm::mat4 mat4_cast(const aiMatrix4x4& m) { return glm::transpose(glm::make_mat4(&m.a1)); }
But the transformationMatrices I wrote in this report are directly from the Scene so only the offsetMatrix is transposed. But this doesn't change a thing.
I found the problem. The problem is that I also have to transform from meshSpace back to object space. The offset Matrix is the inverse of the tranformation Matrices multiplied and also multiplied with the inverse of the transformation Matrix to the Mesh which is on a different node.

Two Dimensional Array Representing a Weight Matrix

I'm a bit stumped in how a 2d array(Matrix) such as this
0.0 1.8 9.1 4.0 3.5
1.8 0.0 8.1 5.2 8.6
9.1 8.1 0.0 2.9 8.1
4.0 5.2 2.9 0.0 2.0
3.5 8.6 8.1 2.0 0.0
is supposed to represent a graph. The values represent the weights but then what represents the nodes and edges. I'm trying to brute force find all possible tree's (Which I'm not asking for help on that, just trying to understand how this is supposed to represent this
If you have a (weighted, undirected) graph with 5 vertices---call them v1, v2, v3, v4, v5---the graph can be represented by your matrix.
v1 v2 v3 v4 v5
v1 0.0 1.8 9.1 4.0 3.5
v2 1.8 0.0 8.1 5.2 8.6
v3 9.1 8.1 0.0 2.9 8.1
v4 4.0 5.2 2.9 0.0 2.0
v5 3.5 8.6 8.1 2.0 0.0
The number in, say, (v2, v4), represents an edge connecting v2 and v4 with a weight of 5.2. The zero entry could represent non-edges, or edges with zero weight. Non-weighted graphs are usually represented with a boolean value in each entry, 1 representing an edge, 0 representing no edge. The graph is (well, can be) undirected if the matrix is symmetric.
NB: the picture in your question cannot be represented by the given matrix: the matrix represents a graph with 5 vertices, and the graph represented by the picture has 8 vertices.
A 2D array (=matrix) is a common way to represent a graph. It is also called Adjacency Matrix.
In graph theory, an adjacency matrix is a square
matrix used to represent a finite graph. The elements of the matrix
indicate whether pairs of vertices are adjacent or not in the graph.
A matrix M that is NxN represents a graph with N vertices (nodes).
When M[i][j] = 0 there is no edge between vertices i and j.
When M[i][j] = 1 there exist an edge between vertices i and j.
Sometimes it is easy to have a different number than 1 to represent the weight of this specific edge (same as in your case).

The Concept of Bilinear Interpolation by Accessing 2-d array like 1-d array does

In 2-d array, there are pixels of bmp files. and its size is width(3*65536) * height(3*65536) of which I scaled.
It's like this.
1 2 3 4
5 6 7 8
9 10 11 12
Between 1 and 2, There are 2 holes as I enlarged the original 2-d array. ( multiply 3 )
I use 1-d array-like access method like this.
array[y* width + x]
index
0 1 2 3 4 5 6 7 8 9...
1 2 3 4 5 6 7 8 9 10 11 12
(this array is actually 2-d array and is scaled by multiplying 3)
now I can patch the hole like this solution.
In double for loop, in the condition (j%3==1)
Image[i*width+j] = Image[i*width+(j-1)]*(1-1/3) + Image[i*width+(j+2)]*(1-2/3)
In another condition ( j%3==2 )
Image[i*width+j] = Image[i*width+(j-2)]*(1-2/3) + Image[i*width+(j+1)]*(1-1/3)
This is the way I know I could patch the holes which is so called "Bilinear Interpolation".
I want to be sure about what I know before implementing this logic into my code. Thanks for reading.
Bi linear interpolation requires either 2 linear interpolation passes (horizontal and vertical) per interpolated pixel (well, some of them only require 1), or requires up to 4 source pixels per interpolated pixel.
Between 1 and 2 there are two holes. Between 1 and 5 there are 2 holes. Between 1 and 6 there are 4 holes. Your code, as written, could only patch holes between 1 and 2, not the other holes correctly.
In addition your division is integer division, and does not do what you want.
Generally you are far better off writing a r=interpolate_between(a,b,x,y) function, that interpolates between a and b at step x out of y. Then test and fix. Now scale your image horizontally using it, and check visually you got it right (especially the edges!)
Now try using it to scale vertically only.
Now do both horizontal, then vertical.
Next, write the bilinear version, which you can test again using the linear version three times (will be within rounding error). Then try to bilinear scale the image, checking visually.
Compare with the two-linear scale. It should differ only by rounding error.
At each of these stages you'll have a single "new" operation that can go wrong, with the previous code already validated.
Writing everything at once will lead to complex bug-ridden code.

OpenGL - drawing indices from OBJ using GL_TRIANGLE_STRIP

I was trying to create an OBJ parser which will read OBJ files and render it using GL_TRIANGLE_STRIP. I know OBJ files behaves as TRIANGLES that's why as I read the faces, I rearrange them into STRIPS.
Example I have faces like this
f 5 1 4
f 5 4 8
f 3 7 8
it will be in this order after I rearrange them minus 1 because OBJ files are 1 based indexes.
f 4 0 3
f 3 0 4
f 0 4 3
f 3 4 7
f 4 7 2
f 2 7 6
f 7 6 7
I send this indices to OpenGL and draw them using glDrawElements but the output I get is wrong. Take a look here for the image.
I based the arrangement on the OpenGL documentation on using GL_TRIANGLE_STRIP and I'm stuck on this step as using GL_TRIANGLES works for me. What would cause the incorrect output?
The .obj file contains indexes suitable for rendering in GL_TRIANGLES mode. You render them in GL_TRIANGLES_STRIP mode. You are completely wrong here. Render them in GL_TRIANGLES mode and be happy.
Rendering the same index data in other mode is almost always incorrect. GL_TRIANGLES mode uses three indices per triangle, while GL_TRIANGLE_STRIP uses one index per triangle on average (it is compressed format). So unless you have compressed your index data in approximately three times, you cannot use your indices to draw as GL_TRIANGLE_STRIP.
See wikipedia article for details. Below you can see result of rendering indices {0, 1, 2, 3, 4, 5, 6} in GL_TRIANGLE_STRIP. Obviously, if you render them in GL_TRIANGLES mode, you'll see only 2 triangles instead of 5.

C++ alternative algorithm for solution

I need some help with an algorithm, I have a problem with an program.
I need to make a program where user inputs cordinates for 3 points and coefficient
for linear funciton that crosses the triangle made by those 3 points and i need to compare area of the shapes what is made function crossing that triangle.
I would paste code here but there is things in my native language and i just want to know your alogrithms for this solution, becuase my wokrs only if the points are entered in exact sequence and I cant get handle of that
http://pastebin.com/vNzGuqX4 - code
and for example i use this http://goo.gl/j18Ch0
The code is not finnished, I just noticed if I enter it in different sequence it does not work like when entering points " 1 1 2 5 4 4 0.5 1 5 " works but " 4 4 1 1 2 5 0.5 1 5 " does not
The linear must cross with 2 edges of the triangle at least. So you can find these 2 crossing points first, these 2 points with one of the 3 vertices will make a small triangle. Use this equation to calculate the area of a triangle S = sqrt(l * (l-a) * (l-b) * (l-c)) where l = (a+b+c)/2 and a, b, c are the length of the edge. It should be easy to get the length of an edge given the coordinate of the vertex. One is the area of the small triangle, the other one is the area of the big triangle minus the small one.
If your triangle is ABC, a good approach would be the following:
Find lines that go through points A and B, B and C, and C and A.
Find the intersection of your line with these three lines.
Check which two intersections lie on the triangle sides.
Depending on the intersections calculate the surface of the new small
triangle.