insert object to set c++ - c++

I have this following fucntion to handle points (among a set of points) whose type is Start_Vertex:
void handleStartVertex(Vertex vi)
{
cout << "start Vertex begins ######################################################################################" << endl;
cout << "Handling start " << vi << vertexType[vi.type] << endl;
HalfEdge *ei = vi.incidentEdge;
std :: vector<HalfEdge > :: iterator it,itprev;
cout << "Origin of the incident Edge " << *(ei->origin) << endl;
//INSERT EI IN TOW AND SET HELPER AS VI
ei->setHelper(&vi);
tow.push_back(*ei);
cout << "Content of Tow in START_VERTEX" << endl;
for(it = tow.begin();it != tow.end();it++)
cout << "origin of edge " << *((*it).origin) << " Helper " << *((*it).helper) << endl;
cout << "start Vertex stops ######################################################################################\n\n" << endl;
}
where tow is :
vector<HalfEdge > tow;
The problem is when I am setting the helper of an Edge and pushing it into vector tow,for all edges previously present in tow, the helper changes to that of the edge being presently pushed. I am not understanding why is this happening? Any work around is appreciated. Here are some results which can make the question more clear.
start Vertex begins ###########################################################################
Handling start (2,9) START_VERTEX
Origin of the incident Edge (2,9)
Content of Tow in START_VERTEX
origin of edge (6,12)
Helper (2,9)
origin of edge (9,11)
Helper (2,9)
origin of edge (2,9)
Helper (2,9)
start Vertex stops ############################################################################
The correct result I should be getting is:
origin of edge (6,12) Helper (6,12)
origin of edge (9,11) Helper (9,11)
origin of edge (2,9) Helper (2,9)

The Vertex vi is destroyed at the return of the function. You are setting the helper to the memory occupied by Vertex vi, and this memory is occupied by Vertex (2, 9) when you do the last print. Pass a pointer to the Vertex vi as input to the function:
void handleStartVertex(Vertex* vi)
{
...
}

Related

CGAL Slicer : How to perform a mesh cut?

I'd like to perform the following action using CGAL :
Intersect a surface mesh; the cut mesh; with another surface mesh; the cutted mesh; to divide the cutted mesh in 2 different meshes.
To do so I use the CGAL Mesh Slicer which gives me a set of polylines. Unfortunately, the slicer doesn't provide the information of which face of the cutted mesh each polyline belongs to. Moreover it effectively perfom the slicer action.
How can I retrieve this information ?
I need this information to perform the subdivision of the cutted mesh along the polylines, then the division in 2 separate meshes.
In my context the cut mesh is a surface mesh like this :
Here is the cut mesh + the cutted mesh :
In my code, I generate a Plane from each faces of the cut mesh and perform a slice operation with the cutted mesh.
Here is my code :
//Import off files and instantiate meshes
const char* filename1 = "test.off";
const char* filename2 = "cutMesh2.off";
std::ifstream input(filename1);
Mesh cuttedMesh, cutMesh;
if (!input || !(input >> cuttedMesh))
{
std::cerr << "First mesh is not a valid off file." << std::endl;
return 0;
}
input.close();
input.open(filename2);
if (!input || !(input >> cutMesh))
{
std::cerr << "Second mesh is not a valid off file." << std::endl;
return 0;
}
input.close();
// AABB Slicer constructor from the cutted mesh
AABB_tree tree(edges(cuttedMesh).first, edges(cuttedMesh).second, cuttedMesh);
tree.accelerate_distance_queries();
CGAL::Polygon_mesh_slicer<Mesh, K> slicer_aabb(cuttedMesh, tree);
std::cout << cutMesh.num_vertices()<< std::endl;
// For each face of the cut mesh
BOOST_FOREACH(face_descriptor f, faces(cutMesh))
{
std::cout << "Face " << f << std::endl;
Point points [3];
int i = 0;
//for each point of the current face
BOOST_FOREACH(vertex_descriptor v, CGAL::vertices_around_face(cutMesh.halfedge(f), cutMesh))
{
points[i]= cutMesh.point(v);
++i;
}
Polylines polylines;
// Perform the slice between the current face of the cut mesh and the cutted mesh
slicer_aabb(K::Plane_3(points[0],points[1],points[2]), std::back_inserter(polylines));
std::cout << "the slicer intersects " << polylines.size() << " polylines" << std::endl;
//for each polyline computed by this face of the cutmesh
BOOST_FOREACH(Polyline_type polyline,polylines)
{
std::cout << "Polyline : " << polyline.size() << " points"<< std::endl;
BOOST_FOREACH(Point point, polyline)
{
std::cout << "Point : " << point << std::endl;
}
}
std::cout << std::endl;
polylines.clear();
}
Thanks for your help.
The answer to the post has been proposed by sloriot :
Try using the non-documented clip() function located in Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing‌​/internal/clip.h

"Vector Subscript Out of Range" using std::vector as the value in an std::map

I have the following code, which is a simplified version of what I'm trying to do.
std::vector<GLuint> testVector = { 6, 7, 8 };
std::map<std::pair<GLfloat, GLfloat>, std::vector<GLuint>> testMap;
testMap.insert(std::make_pair(std::make_pair(1.0f,1.0f), testVector));
std::vector <GLuint> retrievalVector =
testMap.find(std::make_pair(1.0f, 1.0f))->second;
std::cout << "Retrieval Vector: " << retrievalVector[0] << "\t"
<< retrievalVector[1] << "\t"
<< retrievalVector[2] << std::endl;
retrievalVector.push_back(9);
std::cout << "Retrieval Vector: " << retrievalVector[0] << "\t"
<< retrievalVector[1] << "\t"
<< retrievalVector[2] << "\t"
<< retrievalVector[3] << std::endl;
testMap.insert(std::make_pair(std::make_pair(1.0f, 1.0f), retrievalVector));
retrievalVector = testMap.find(std::make_pair(1.0f, 1.0f))->second;
std::cout << "Retrieval Vector: " << retrievalVector[0] << "\t"
<< retrievalVector[1] << "\t"
<< retrievalVector[2] << "\t"
<< retrievalVector[3] << std::endl;
Basically, I insert a vector of integers into a map using coordinates as a key. I retrieve the same vector and find that the contents were stored successfully. I then add another integer to the vector and reinsert it in the same place. Retrieving it once more and attempting to print the vector's contents (the last cout) leads to an out of range access.
Can someone who is more knowledgeable explain why this occurs?
The problem is that your second insert fails, as you already have a value at that position. If you want to modify the value, you have to do something like testMap[std::make_pair(1.0f, 1.0f)] = retrievalVector;.
The behaviour is exactly as expected. According to the documentation:
Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
You already have a std::make_pair(1.0f, 1.0f) in there, so your return value should be:
Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.
If you check testMap.insert(std::make_pair(std::make_pair(1.0f, 1.0f), retrievalVector)).second it should be false, so there is still a three-element array there, not the four-element one you hoped had been added, therefore the program crashes on trying to access retrievalVector[3].
By the way, C++17 has an insert_or_assign() function that will do what you hoped insert() would do.

Calculating bindPose matrix (Skeletal Animation)

Ive succesfully parsed the iqe (Inter Quake Exporter) format and now im stuck at displaying it in bindpose.
All vertices have a weird transformation, where root bone(which covers the full mesh for orientation) is not the only bone with influence for that vertex. You can see it at the arm / should / neck area of the mesh. This mesh has 3 bones. One root bone overing the whole mesh and two arm bones.
You can see how the mesh should look like in the background (exported as obj)
For better understanding, i have the following system:
1. I load all vertex data into one big vbo
(vertices, uvs, normals, tangent, bitangent, boneIndicies(4) (index of joint list) and boneWeights(4))
2. I add all joints to a joint list and create a tree system (simple linked list with position, rotation and parent pointer)
3. i have a seperate list called boneMatrices where i store.. well my bone matrices. currently every frame, later i will precalculate the matrices for each animation frame.
I try to calculate the bone matrix the following way:
for (int i = 0; i < this->jointList.size(); i++)
{
pixel::CJoint *joint = this->jointList.at(i);
std::cout << "Joint ------- " << joint->name << " -------- values: \n";
std::cout << "Translation: " << joint->position.x << " " << joint->position.y << " " << joint->position.z << "\n";
std::cout << "Quaternion: " << joint->rotation.x << " " << joint->rotation.y << " " << joint->rotation.z << " " << joint->rotation.w << "\n";
pixel::matrix4 rotation = pixel::CMatrix::fromQuaternion(joint->rotation);
pixel::matrix4 offset = pixel::CMatrix::translateMatrix(joint->position);
pixel::matrix4 baseMatrix = rotation * offset; // translation * rotation
joint->bindPose = baseMatrix;
joint->invBindPose = pixel::CMatrix::inverseMatrix(baseMatrix);
if (joint->parent != NULL)
{
std::cout << "Joint: " << joint->name << " is child of " << joint->parent->name << " \n";
joint->bindPose = joint->bindPose * joint->parent->invBindPose;
joint->invBindPose = pixel::CMatrix::inverseMatrix(joint->bindPose);
}
std::cout << "\n";
}
I store the transposed (else the mesh is upside down) of joint->invBindPose in the boneMatrices and send it to the shader:
boneMatrix is a std::vector of matrix4
this->material.setParameter("boneMatrix", this->boneMatrices.at(0), this->boneMatrices.size());
The root bone bind calculation has to be right (at least i think) because the head is at the right place and the eyes too (which dont have a bone influence currently)

Why value of object's attribute returns zero after adding its pointer to a map in c++

So I have this:
//sons is an attribute of the object node that is a vector<Node*> that is initialized before
map<string,Node*> nodes;
string node_id = "node";
string son_id = "son";
Node *node = new Node(node_id, matrix, son_id, Prim);
cout << "Before " << node << endl;
cout << "Value of sons before map: " << node->sons[0] << endl;
nodes[node_id] = node;
cout << "After: " << nodes.find(node_id)->second << endl;
cout << "Value of sons after map: " << nodes.find(node_id)->second->sons[0];
I'm getting this output (with varying memory positions from execution to execution):
Before: 0x9dfdda8
Value of sons before map: 0xbff1a774 // consistant with memory position with created obj
After: 0x9dfdda8
Value of sons after map: 0
Why is this happening and how can I fix it?! I've been searching for the solution and trying to figure this out for 4 hours now...
cout << "After: " << nodes.find(root_id)->second << endl;
cout << "Value of sons after map: " << nos.find(root_id)->second->sons[0];
Why is the second line refering to nos and the first one nodes? Is it just a typo?
If those are really different objects, that might explain why you see inconsisten results
You are adding node to the map with the key node_id and then looking it up later with root_id and not even checking if it found one, so you're probably getting some undefined behaviour by accessing end(nodes). You need to access the map with the same key to get the same object.
Also you appear to have some confusion with your variables, using no and nos when you are apparently needing to access either node or the map's variable.

Boost:: Dijkstra Shortest Path, how to get vertice index from path iterator?

Before you start reading, to help you understand my issue, I am telling that I have copied the code from this link: Dijkstra Shortest Path with VertexList = ListS in boost graph
So.. I am rewriting my program code to use boost, but now when 99% is ready I am stuck with my GPS (for a game).
I have a list of nodes, which I added in a way which fortunately was easy to convert to the boost method. The thing I needed to do was just create a vertice variable like this:
Vertex Vx[MAX_NODES];
I copied the typedefs from the link I have given.
The way I add vertices is this:
stringstream s;
s << i;
Vx[i] = add_vertex(s.str(),dgraph);
Where "i" equals an integer number. (eg int i = 9)
And eges are also easy to add. Now, I have my own structured array called "xNode". and eg :
xNode[i] holds all the information for X Y Z positions (xNode[i].X xNode[i].Y etc) of the nodes.
Now when using the code snippet from the link I have done this:
// Write shortest path
std::cout << "Shortest path from " << startid << " to " << endid << ":" << std::endl;
float totalDistance = 0;
for(PathType::reverse_iterator pathIterator = path.rbegin(); pathIterator != path.rend(); ++pathIterator)
{
std::cout << source(*pathIterator, dgraph) << " -> " << target(*pathIterator, dgraph)
<< " = " << get( boost::edge_weight, dgraph, *pathIterator ) << std::endl;
}
And this is where I am stuck, as "source(*pathIterator, dgraph)" and "target(*pathIterator, dgraph)
" Get addresses, but I need the vertice indexes to access xNode[i], i is the NodeID (or well the vertice ID | Vx[i]).
How can I do that?
EDIT:
I tried to do:
for(PathType::reverse_iterator pathIterator = path.rbegin(); pathIterator != path.rend(); ++pathIterator)
{
for(int i = 0; i < MAX_NODES; ++i)
{
if(source(*pathIterator, dgraph) == *((Vertex*)Vx[i]))
{
cout << " " << i << " " << endl;
break;
}
}
}
but this just crashes..
With the typedefs from that question, you can use get(boost::vertex_index, dgraph, v) to get the index of v. You can also cache the property map using:
IndexMap vi = get(boost::vertex_index, dgraph);
then use get(vi, v) to get the index for v.