PCL Cloud Normals Non-Deterministic in Simple Test Case - c++
I am trying to figure out if the unorganized surface normal estimation should be deterministic based on a given point cloud; I presumed it should based on this explanation: http://pointclouds.org/documentation/tutorials/normal_estimation.php#normal-estimation
However, when generating a uniform 3x3 surface cuboid (i.e. 26 points in total), and do an 8 nearest neighbor normal estimate over the entire cloud, the 6 points which lie at the center of the 6 cube surfaces do not line up with the co-ordinate axes, and seem rather chaotic. This is surprising, as I understood that this choice of '8' nearest neighbors should constrain the normal estimates for these 6 central surface points to be based only on the other points on the same planar surface. Here is a snippet of the code:.
// compute normals
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
ne.setInputCloud (cloud_ptr);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
ne.setSearchMethod (tree);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
ne.setKSearch(8);
ne.compute (*cloud_normals);
for (int i =0; i<cloud_ptr->size(); i++)
{
cout << "x: " << cloud_ptr->points.at(i).x << " y: " << cloud_ptr->points.at(i).y << " z: " << cloud_ptr->points.at(i).z << endl;
}
for (int i =0; i<cloud_normals->size(); i++)
{
cout << "x: " << cloud_normals->points.at(i).normal_x << " y: " << cloud_normals->points.at(i).normal_y << " z: " << cloud_normals->points.at(i).normal_z << endl;
}
cout << cloud_ptr->size() << endl;
cout << cloud_normals->size() << endl;
And the Output:
x: -1 y: -1 z: 1
x: -1 y: 0 z: 1
x: -1 y: 1 z: 1
x: 0 y: -1 z: 1
x: 0 y: 0 z: 1
x: 0 y: 1 z: 1
x: 1 y: -1 z: 1
x: 1 y: 0 z: 1
x: 1 y: 1 z: 1
x: -1 y: -1 z: -1
x: -1 y: 0 z: -1
x: -1 y: 1 z: -1
x: 0 y: -1 z: -1
x: 0 y: 0 z: -1
x: 0 y: 1 z: -1
x: 1 y: -1 z: -1
x: 1 y: 0 z: -1
x: 1 y: 1 z: -1
x: -1 y: -1 z: 0
x: -1 y: 0 z: 0
x: -1 y: 1 z: 0
x: 1 y: -1 z: 0
x: 1 y: 0 z: 0
x: 1 y: 1 z: 0
x: 0 y: -1 z: 0
x: 0 y: 1 z: 0
x: 0.642542 y: 0.417468 z: -0.642542
x: 0.81252 y: 0.0985752 z: -0.574538
x: 0.642542 y: -0.417468 z: -0.642542
x: -0.0985752 y: 0.574539 z: -0.81252
x: -0.196116 y: 0 z: -0.980581
x: -0.0985752 y: -0.574539 z: -0.81252
x: -0.642542 y: 0.642542 z: -0.417468
x: -0.81252 y: -0.0985752 z: -0.574538
x: -0.642542 y: -0.642542 z: -0.417468
x: 0.642542 y: 0.642542 z: 0.417468
x: 0.81252 y: 0.0985752 z: 0.574538
x: 0.642542 y: -0.642542 z: 0.417468
x: -0.0985752 y: 0.81252 z: 0.574539
x: -0.196116 y: 0 z: 0.980581
x: -0.0985752 y: -0.81252 z: 0.574539
x: -0.642542 y: 0.417468 z: 0.642542
x: -0.81252 y: -0.0985752 z: 0.574538
x: -0.642542 y: -0.417468 z: 0.642542
x: 0.81252 y: 0.574538 z: -0.0985752
x: 1 y: -0 z: -0
x: 0.81252 y: -0.574538 z: 0.0985752
x: -0.81252 y: 0.574538 z: -0.0985752
x: -0.987623 y: 0.153347 z: -0.0329405
x: -0.574539 y: -0.81252 z: 0.0985752
x: 0.153347 y: 0.987623 z: -0.0329405
x: 0.153347 y: -0.987623 z: -0.0329405
26
26
I'm sure I haven't made any mistakes with the cube co-ordinates, which show up perfectly as expected in the cloud viewer.
Any help greatly appreciated!
It is deterministic when the 8 nearest neighbors are deterministic...
In your test case (a 3x3 cube shell, the 8 nearest neighbors of any face center point is actually non deterministic) ie: it first will grab the 4 nearest points vertical and horizontal edge points, but the next 4 points is not so clear, it could grab the 4 corner vertices you want, or it could grab the centers of the 4 connected faces (which are all equidistant).
I tested this in pcl and it is indeed the case. If you increase the number of points on each face to 5, then you indeed will have the perfect central face normals that you are after.
Related
How to move an object along a line given two points?
In my game I'm working on, I have a tower that will shoot at an enemy. I'm trying to get my tower shoot to the enemy when the enemy becomes in range. My fireball shows up, however I'm having problem moving the fireball to the enemy. My latest attempt just makes the ball dance around on the screen. the ball has an x and y position (which resembles where the ball starts. The ball also has a newX and newY position in which is where the enemy is. I was thinking about doing the Pythagorean theorm, but that only gets me the length of the line I want to travel in. Then I was searching online and found out about sin, cos, and atan. I was using atan to get the angle of the slope that I calculated, then plugging the angle in for sin and cos. My Ball::Update function looks like this: int dx = newX - x; int dy = newY - y; int ang = atan(dy, dx); x += cos(ang); y += sin(ang); That code there makes the ball appear to 'dance' on the screen. It doesn't move at all. I tested my code with these lines: x += cos(45); y += sin(45); And the ball moves in a 45 degree angle (heading south east) which is expected. So I'm guessing that my error is when calculating the angle. Any math experts that can help?
This is a small sample to demonstrate linear interpolation for pairs (x, y): #include <iostream> int main() { double x0 = 2.0, y0 = 3.5; double x1 = 7.0, y1 = 2.0; // do linear interpolation in n steps enum { n = 10 }; for (int i = 0; i <= n; ++i) { double t = (double)i / n; double xT = (1.0 - t) * x0 + t * x1; double yT = (1.0 - t) * y0 + t * y1; std::cout << i << ":\tt: " << t << ",\tx: " << xT << ",\ty: " << yT << '\n'; } // your code goes here return 0; } Output: 0: t: 0, x: 2, y: 3.5 1: t: 0.1, x: 2.5, y: 3.35 2: t: 0.2, x: 3, y: 3.2 3: t: 0.3, x: 3.5, y: 3.05 4: t: 0.4, x: 4, y: 2.9 5: t: 0.5, x: 4.5, y: 2.75 6: t: 0.6, x: 5, y: 2.6 7: t: 0.7, x: 5.5, y: 2.45 8: t: 0.8, x: 6, y: 2.3 9: t: 0.9, x: 6.5, y: 2.15 10: t: 1, x: 7, y: 2 Life demo on ideone
c++ after finding the path from an A* search, printing the path continually prints the same Nodes information
I have managed to produce an A* search using c++ (all though it can probably be done much more efficiently) it does it fact go from the start [x][y] to the goal[x][y] and terminates. Th problem i am having is when i want to print this path in terms of which coordiantes were following i try to do this by following pointers to each Nodes parent and keep doing thing untill the pointer == NULL. The problem is when i execute my print method it constantly prints the coordinates of the final state, rather than following the pointer to it's parent untill there is no parent (back at the root Node) Node.h class Node{ private: int xCoord; int yCoord; int value; double fCost; double gCost; double hCost; Node* parent; public: Node(); Node(int x, int y, int value, int cost, Node* parent); void setParent(Node* parent); int getX(); int getY(); int getValue(); double getHCost(); double getFCost(); double getGCost(); Node* getParent(); void setHCost(double hCost); }; struct NodeComparator { bool operator()(Node& first, Node& second) { return (first.getFCost() < second.getFCost()); } }; Node.cpp Node::Node(){ this->xCoord = 0; this->yCoord = 0; this->value = 0; this->parent = NULL; this->fCost = 0; this->gCost = 0; this->hCost = 0.0; } Node::Node(int _x, int _y, int _value, int cost, Node* parent){ this->xCoord = _x; this->yCoord = _y; this->value = _value; this->gCost = cost; this->parent = parent; this->hCost = 0.0; this->fCost = 0; } void Node::setParent(Node* par){ this->parent = par; } int Node::getX(){ return xCoord; } int Node::getY(){ return yCoord; } int Node::getValue(){ return value; } double Node::getGCost(){ return gCost; } double Node::getFCost(){ return gCost + hCost; } double Node::getHCost(){ return hCost; } Node* Node::getParent(){ return parent; } void Node::setHCost(double cost){ this->hCost = cost; } The main.cpp file where the actual search takes place: int main(){ int map[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,2}, {0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0}, {0,0,3,0,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0}, {0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}}; using namespace std; list<Node> openList; vector<Node> closedList; Node end; Node start = initiateStart(map); openList.push_front(start); cout <<"Start index: x " << start.getX() << " y " <<start.getY() << endl; while (!openList.empty()) { Node best = openList.front(); openList.pop_front(); if(!checkInClosedList(closedList, best.getX(), best.getY())){ calcManhattanDistance(best, map); if(best.getValue() == 3){ end = best; cout <<"end index: x " << end.getX() << " y " <<end.getY() << endl; checkPath(&end, map); exit(1); } if(map[best.getX()][best.getY()-1] != 1 && best.getY() - 1 > -1){ if(placeInOpen(openList,best.getX(), best.getY() - 1)){ openList.push_front(generateLeftChild(best, map)); } } //to the right if(map[best.getX()][best.getY()+1] != 1 && best.getY() + 1 < 20){ if(placeInOpen(openList,best.getX(), best.getY() + 1)){ openList.push_front(generateRightChild(best, map)); } } //above if(map[best.getX()-1][best.getY()] != 1 && best.getX() - 1 > -1){ if(placeInOpen(openList,best.getX()-1, best.getY())){ openList.push_front(generateAboveChild(best, map)); } } //below if(map[best.getX()+1][best.getY()] != 1 && best.getX() + 1 < 20){ if(placeInOpen(openList,best.getX()+1, best.getY())){ openList.push_front(generateBelowChild(best, map)); } } closedList.push_back(best); } openList.sort(NodeComparator()); } return 0; } Node initiateStart(int m[20][20]){ Node start; for(int i = 0; i < 20; i++){ for(int j = 0; j < 20; j++){ if(m[i][j] == 2){ start = Node(i, j, m[i][j], 0, NULL); } } } return start; } Node generateLeftChild(Node parent, int m[20][20]){ Node child; child = Node(parent.getX(), parent.getY() - 1, m[parent.getX()][parent.getY() - 1], parent.getGCost() + 1, &parent); calcManhattanDistance(child, m); return child; } Node generateRightChild(Node parent, int m[20][20]){ Node child; child = Node(parent.getX() , parent.getY() + 1, m[parent.getX()][parent.getY() + 1],parent.getGCost() + 1, &parent); calcManhattanDistance(child, m); return child; } Node generateAboveChild(Node parent, int m[20][20]){ Node child; child = Node(parent.getX() - 1, parent.getY(), m[parent.getX() - 1][parent.getY()], parent.getGCost() + 1, &parent); std::cout << "parent for above child X: " << child.getParent()->getX() <<" Y: " << child.getParent()->getY() << std::endl; calcManhattanDistance(child, m); return child; } Node generateBelowChild(Node parent, int m[20][20]){ Node child; child = Node(parent.getX() + 1, parent.getY(), m[parent.getX() + 1][parent.getY()], parent.getGCost() + 1, &parent); calcManhattanDistance(child, m); return child; } void calcManhattanDistance(Node node, int m[20][20]){ int tempX; int tempY; double manhattanDistance; int differenceX; int differenceY; //std::cout << "node x: " << node->getX() << " node y: " << node->getY() << std::endl; for(int i = 0; i < 20; i++){ for(int j = 0; j < 20; j++){ if(m[i][j] == 3){ tempX = i; tempY = j; } } } //sum of term difference, none of these can be negative hense the std::abs differenceX = tempX - node.getX(); differenceY = tempY - node.getY(); manhattanDistance = std::abs(differenceX) + std::abs(differenceY); //std::cout << "Manhattan distance: " << manhattanDistance << std::endl; node.setHCost(manhattanDistance); } bool checkInClosedList(std::vector<Node>& v,int x, int y){ for (std::vector<Node>::iterator iter = v.begin(); iter != v.end(); ++iter) { if(iter->getX() == x && iter->getY() == y){ return true; } } return false; } bool placeInOpen(std::list<Node>& v,int x, int y){ for (std::list<Node>::iterator iter = v.begin(); iter != v.end(); ++iter) { if(iter->getX() == x && iter->getY() == y){ return false; } } return true; } void checkPath(Node *end, int m[20][20]){ int tempX, tempY; Node *temp = end; while(temp != NULL){ tempX = temp->getX(); tempY = temp->getY(); std:: cout << tempX << " " << tempY << std::endl; temp = temp->getParent(); } printMap(m); } void printMap(int m[20][20]){ std::cout << "printy mcprint" << std::endl; for(int i = 0; i< 20; i++){ for(int j = 0; j< 20; j++){ std::cout << m[i][j]; } std::cout<<std::endl; } } Sorry for the large code dump, but i assume you will want to see everything just incase i am setting a parent wrong somewhere! I added a print statement to test how i add the parents, you can see this in the generateAboveChild function, the program produce this output: Start index: x 0 y 19 parent for above child X: 1 Y: 19 parent for above child X: 2 Y: 19 parent for above child X: 3 Y: 19 parent for above child X: 2 Y: 18 parent for above child X: 1 Y: 17 parent for above child X: 4 Y: 19 parent for above child X: 5 Y: 19 parent for above child X: 4 Y: 18 parent for above child X: 3 Y: 17 parent for above child X: 2 Y: 16 parent for above child X: 1 Y: 15 parent for above child X: 1 Y: 14 parent for above child X: 6 Y: 19 parent for above child X: 7 Y: 19 parent for above child X: 6 Y: 18 parent for above child X: 5 Y: 17 parent for above child X: 4 Y: 16 parent for above child X: 3 Y: 15 parent for above child X: 2 Y: 14 parent for above child X: 3 Y: 14 parent for above child X: 8 Y: 19 parent for above child X: 9 Y: 19 parent for above child X: 8 Y: 18 parent for above child X: 7 Y: 17 parent for above child X: 5 Y: 15 parent for above child X: 4 Y: 14 parent for above child X: 4 Y: 13 parent for above child X: 6 Y: 15 parent for above child X: 8 Y: 17 parent for above child X: 10 Y: 19 parent for above child X: 11 Y: 19 parent for above child X: 10 Y: 18 parent for above child X: 9 Y: 17 parent for above child X: 7 Y: 15 parent for above child X: 6 Y: 14 parent for above child X: 5 Y: 13 parent for above child X: 4 Y: 11 parent for above child X: 8 Y: 15 parent for above child X: 10 Y: 17 parent for above child X: 12 Y: 19 parent for above child X: 13 Y: 19 parent for above child X: 12 Y: 18 parent for above child X: 11 Y: 17 parent for above child X: 9 Y: 15 parent for above child X: 8 Y: 14 parent for above child X: 7 Y: 13 parent for above child X: 6 Y: 12 parent for above child X: 5 Y: 11 parent for above child X: 4 Y: 9 parent for above child X: 3 Y: 10 parent for above child X: 14 Y: 19 parent for above child X: 15 Y: 19 parent for above child X: 14 Y: 18 parent for above child X: 13 Y: 17 parent for above child X: 12 Y: 16 parent for above child X: 10 Y: 14 parent for above child X: 9 Y: 13 parent for above child X: 8 Y: 12 parent for above child X: 7 Y: 11 parent for above child X: 6 Y: 10 parent for above child X: 2 Y: 10 parent for above child X: 5 Y: 9 parent for above child X: 3 Y: 8 parent for above child X: 1 Y: 10 parent for above child X: 7 Y: 10 parent for above child X: 16 Y: 19 parent for above child X: 17 Y: 19 parent for above child X: 16 Y: 18 parent for above child X: 15 Y: 17 parent for above child X: 14 Y: 16 parent for above child X: 13 Y: 15 parent for above child X: 12 Y: 14 parent for above child X: 11 Y: 13 parent for above child X: 10 Y: 12 parent for above child X: 8 Y: 10 parent for above child X: 2 Y: 7 parent for above child X: 1 Y: 8 parent for above child X: 18 Y: 19 parent for above child X: 19 Y: 19 parent for above child X: 18 Y: 18 parent for above child X: 17 Y: 17 parent for above child X: 16 Y: 16 parent for above child X: 15 Y: 15 parent for above child X: 14 Y: 14 parent for above child X: 13 Y: 13 parent for above child X: 12 Y: 12 parent for above child X: 11 Y: 11 parent for above child X: 2 Y: 5 parent for above child X: 1 Y: 6 parent for above child X: 15 Y: 14 parent for above child X: 19 Y: 17 parent for above child X: 18 Y: 16 parent for above child X: 17 Y: 15 parent for above child X: 16 Y: 14 parent for above child X: 14 Y: 12 parent for above child X: 13 Y: 11 parent for above child X: 12 Y: 10 parent for above child X: 3 Y: 4 parent for above child X: 2 Y: 3 parent for above child X: 1 Y: 4 parent for above child X: 13 Y: 10 parent for above child X: 15 Y: 12 parent for above child X: 17 Y: 14 parent for above child X: 19 Y: 15 parent for above child X: 18 Y: 14 parent for above child X: 16 Y: 12 parent for above child X: 15 Y: 11 parent for above child X: 14 Y: 10 parent for above child X: 4 Y: 4 parent for above child X: 5 Y: 4 parent for above child X: 4 Y: 3 parent for above child X: 3 Y: 2 parent for above child X: 2 Y: 1 parent for above child X: 1 Y: 2 parent for above child X: 15 Y: 10 parent for above child X: 17 Y: 12 parent for above child X: 19 Y: 14 parent for above child X: 18 Y: 12 parent for above child X: 17 Y: 11 parent for above child X: 16 Y: 10 parent for above child X: 6 Y: 4 parent for above child X: 7 Y: 4 parent for above child X: 6 Y: 3 parent for above child X: 5 Y: 2 parent for above child X: 4 Y: 1 parent for above child X: 3 Y: 0 parent for above child X: 1 Y: 0 parent for above child X: 19 Y: 12 parent for above child X: 19 Y: 11 parent for above child X: 18 Y: 10 parent for above child X: 17 Y: 9 parent for above child X: 4 Y: 0 parent for above child X: 8 Y: 4 parent for above child X: 9 Y: 4 parent for above child X: 8 Y: 3 parent for above child X: 7 Y: 2 parent for above child X: 6 Y: 1 parent for above child X: 5 Y: 0 parent for above child X: 19 Y: 9 parent for above child X: 18 Y: 8 parent for above child X: 17 Y: 7 parent for above child X: 6 Y: 0 parent for above child X: 10 Y: 4 parent for above child X: 11 Y: 4 parent for above child X: 10 Y: 3 end index: x 9 y 2 But, when i uncomment the line which calls checkPath(&end, map);(this is near the top of main.cpp under the if conditon if(best.getValue() == 3) the i get this output: ( i have removed the cout print for the "parent for above child") Start index: x 0 y 19 9 2 9 2 9 2 9 2 9 2 9 2 9 2 and these 9 2 prints continue forever, i assume the problem is in the printPath function ive tried debugging it but i cant see what i am doing wrong. Cheers, Chris.
I changed my implementation of how Node are handled and stored Node* rather than Node Objects in my list and vector as well as using the new keyword on creation. This resolved my problem my A* now works.
c++ sort object vector [duplicate]
This question already has answers here: Sorting a vector of custom objects (14 answers) Closed 9 years ago. I've a question regarding sorting of object in a vector my object consists of x,y and civ. so I would like to know how do I sort the value of the civ in an descending order but at the same time retaining the value of x and y attached to it.. original X: 4 Y: 5 CIV: 10 X: 3 Y: 2 CIV: 30 X: 3 Y: 3 CIV: 05 sorted X: 3 Y: 2 CIV: 30 X: 4 Y: 5 CIV: 10 X: 3 Y: 3 CIV: 05 missionplan.cpp for(int j=0; j < 5; j++) { for(int i = j; i < topfive.size(); i++) { if(topfive[i].getcivIndex() < topfive[i].getcivIndex) { topfive[i].swap(topfive[i]); } } pointtwoD = topfive.at(i);//display all data starting from the first index pointtwoD.displayPointdata(); }
You have a misktake in your code. if(topfive[i].getcivIndex() < topfive[i].getcivIndex) //The second i should be j { topfive[i].swap(topfive[i]); // which should be topfive[j].swap(topfive[i]) }
How can I plot (x,y,z) coordinates dynamically
My application obtains x,y,z coordinates like this: x: -0.020941 y: -0.241276 z: 0.956 -------------- x: 0.0782352 y: -0.159108 z: 0.923 -------------- x: 0.0665857 y: -0.140757 z: 0.885 -------------- x: 0.0485952 y: -0.0859762 z: 0.785 -------------- x: 0.04494 y: -0.0477933 z: 0.749 -------------- x: -0.183467 y: 0.0505905 z: 0.64 -------------- x: -0.0519514 y: -0.0137343 z: 0.627 -------------- x: -0.0630648 y: -0.0206495 z: 0.586 -------------- x: -0.0774924 y: -0.0189667 z: 0.569 -------------- x: 0.0100971 y: -0.0100971 z: 0.558 -------------- x: 0.00456857 y: -0.0126905 z: 0.533 -------------- x: 0.000491429 y: -0.00835429 z: 0.516 -------------- x: -0.0227838 y: -0.01018 z: 0.509 -------------- x: -0.0222133 y: -0.00589333 z: 0.476 -------------- x: -0.10161 y: -0.00850476 z: 0.47 -------------- x: -0.0775429 y: 0.0162095 z: 0.46 -------------- x: -0.0897705 y: 0.0219057 z: 0.451 -------------- What I'm doing is every 3 seconds I'm taking the smallest z value from this stream and plotting it's position within a 2d array (using it's x and y position) my question is how do I plot this result as I don't know what the maximum or minimum score could be and they're all double type, not int for the index. Is there a 'nice' way of plotting this? I'd also like to be able to manipulate this data based on it's array index. e.g. find where data is clustering, etc.
It seems that your issue is a result of having your heart set on using an array. Using a vector would allow you to add grid points dynamically as well as averaging (and performing other calculations) whenever you wanted. That being said, if you really want to use an array then like you have noticed you cannot use doubles and/or negative values as indices. You could work out a system by adding an offset and multiplying your x,y values to get rounded integers and a center not at (0,0) but maybe at (2,2) or something instead but this would be really unwieldy and confusing.
Trouble with iterating through std::set (c++)
I am having problems with sets all day. Some I can solve, others not. This one is bugging me since the morning, and I have run out of patience. Please help, mighty stackoverflow! So, I have a set which contains a custom object of mine, its called "vect" and is based on eigen::matrix. That means, I get values from vects with the [] operator. set<vect*> *tvps=getTheSet(); for (set<vect*>::iterator iter = tvps->begin(); iter != tvps->end(); ++iter) { vect v= **iter; // Don't really know why two asterisks, // but my compiler would complain int x=v[0]; int y=v[1]; doStuffWith( v[0],v[1]); } Now, this will compile and run and everything. But the values I get from the iterator are 30% trash: x: 110 y: 90 x: 230 y: 130 x: 250 y: 100 x: 230 y: 130 x: 110 y: 290 x: 140 y: 260 x: 180 y: 280 x: 150 y: 210 x: -2147483648 y: 0 x: 180 y: 280 x: 170 y: 230 x: 240 y: 270 x: -2147483648 y: 0 x: -429917536 y: 0 x: 0 y: -2147483648 I checked at the point where the set was put together which values where put in. Only the ones between 10 and 300.. as intended. How comes I find others in it? Did I screw up with the iterator?
There are a few possibilities as to why this is happening to you: Are you reusing the set. If yes, do you clear() it between reuses ? You are storing pointers to vectors in the set, are you changing the vectors inside the set ? Perhaps you want to store the pointer to the vector because otherwise the compiler gives you an error due to the fact that it doesn't know how to compare two vectors. Why don't you try to implement the comparison function and store vectors directly ? that way your work would be a lot easier and much less error prone. Also the problem you have now is that the elements in the set don't seem to be ordered.. in fact they are ordered by the pointer value, and not by X and Y (and I suppose that's what you want)
You have a set of pointers to vectors. In **iter first you dereference iterator, then pointer laying inside. Keeping pointers can also be causing your problem: if you freed or forgot to initialize some of them, result would be rubbish.