First time Stack Overflow questioner here and C++ newbie, so here goes:
I'm trying to build a C++ program which needs to handle geometry. I have been trying to get boost::geometry to work for me, but I am having the following issue. My points need to maintain an ID value or other identifying tag (I need to link them to properties stored in other objects). I can successfully register this point using BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET and carry out boost::geometry operations, however the moment I carry out any operations with it boost::geometry seems to create new copies of my point without the id value.
Is there something I'm missing about using boost::geometry with custom points that would make it possible to do what I'm trying to do, or do I have to re-think my approach and find some other way to do what I'm trying to do?
The following code shows an example point class (int id is the identifier) and also a sample of code which compiles and runs (with appropriate #include and namespace declarations) however it keeps removing my point IDs:
point class:
class My_Point
{
public:
My_Point(const My_Point &p);
My_Point(double x = 0.0, double y = 0.0, int new_id = 0);
const double Get_X() const;
const double Get_Y() const;
void Set_X(double new_x);
void Set_Y(double new_y);
const int Get_ID() const;
void Set_ID(int new_id);
private:
int id;
double x;
double y;
}
test code:
void TestPolygon()
{
vector < My_Point > p;
p.push_back(My_Point(0.0, 0.0, 0));
p.push_back(My_Point(1.0, 0.0, 1));
p.push_back(My_Point(1.0, 1.0, 2));
p.push_back(My_Point(0.0, 1.0, 3));
p.push_back(My_Point(0.0, 0.0, 4));
cout << "Initial points are:\n";
for (int i = 0, n = p.size(); i < n; i++)
{
cout << point_to_string(p.at(i)) << "\n";
}
detect_enter();
polygon<My_Point> poly;
append(poly, p);
//this code gives each point with an incorrect id of 0
cout << "Polygon points are:\n";
for (int i = 0, n = poly.outer().size(); i < n; i++)
{
cout << point_to_string(poly.outer().at(i)) << "\n";
}
detect_enter();
strategy::transform::rotate_transformer<degree, double, 2, 2> rotate(45.0);
for (int i = 0, n = poly.outer().size(); i < n; i++)
{
transform(poly.outer().at(i), poly.outer().at(i), rotate);
}
vector<My_Point> p2;
p2 = poly.outer();
//this code gives an incorrect id of 0.
cout << "Final points are:\n";
for (int i = 0, n = p2.size(); i < n; i++)
{
cout << point_to_string(p2.at(i)) << "\n";
}
detect_enter();
//this code gives the correct id values as expected.
cout << "Original points were:\n";
for (int i = 0, n = p.size(); i < n; i++)
{
cout << point_to_string(p.at(i)) << "\n";
}
}
Aucun commentaire:
Enregistrer un commentaire