C
cata.1972
Hi,
I have structure representing a 2 dimesional point:
struct K2dCoords
{
K2dCoords(void):m_x(0), m_y(0){};
K2dCoords(double x, double y):m_x(x), m_y(y){};
K2dCoords(const K2dCoords& rhs): m_x(rhs.m_x), m_y(rhs.m_y){};
double getX() const {return m_x;}
double getY() const {return m_y;}
double m_x;
double m_y;
};
I want to store pointers to instances of this in a STL set.
So I define the sorting criterion, such that two points are considered
equal
if the distance between them is negligeble (less than a tolerance):
struct eq
{
bool operator() (const K2dCoords* p1, const K2dCoords* p2) const
{
const double tol = 0.25 / 1E3;
return sqrt((p1->getX() - p2->getX()) * (p1->getX() - p2->getX
()) + (p1->getY() - p2->getY()) * (p1->getY() - p2->getY())) > tol;
}
};
The problem is that after I've inserted some point in my set, I cannot
find them:
// some initialisation data
const double inc = 0.25;
const double deckLength = 1.0;
const double deckWidth = 0.48;
double x = 0;
double y = 0;
const double PI = 3.1415926;
std::set<K2dCoords*, eq> stlMap;
// create some points and add them to my set
while (x <= deckLength)
{
while (y <= deckWidth)
{
const double xSp = x * cos(30.0 * PI / 180.0) - y * sin
(30.0 * PI / 180.0);
const double ySp = x * sin(30.0 * PI / 180.0) + y * cos
(30.0 * PI / 180.0);
stlMap.insert(new K2dCoords(xSp, ySp));
y += inc;
}
x += inc;
y = 0;
}
// check if I've got them; the test point are created in the same
way as they were created for insertion
x = 0;
y = 0;
while (x <= deckLength)
{
while (y <= deckWidth)
{
const double xSp = x * cos(30.0 * PI / 180.0) - y * sin
(30.0 * PI / 180.0);
const double ySp = x * sin(30.0 * PI / 180.0) + y * cos
(30.0 * PI / 180.0);
std::set<K2dCoords*, eq>::iterator it = stlMap.find(new
K2dCoords(xSp, ySp));
if (it == stlMap.end())
{
ASSERT(FALSE); // suerly this should not assert, but
it does (when x=0.25 and y=0)!!!
}
y += inc;
}
x += inc;
y = 0;
}
What am I doing wrong? Thanks.
I have structure representing a 2 dimesional point:
struct K2dCoords
{
K2dCoords(void):m_x(0), m_y(0){};
K2dCoords(double x, double y):m_x(x), m_y(y){};
K2dCoords(const K2dCoords& rhs): m_x(rhs.m_x), m_y(rhs.m_y){};
double getX() const {return m_x;}
double getY() const {return m_y;}
double m_x;
double m_y;
};
I want to store pointers to instances of this in a STL set.
So I define the sorting criterion, such that two points are considered
equal
if the distance between them is negligeble (less than a tolerance):
struct eq
{
bool operator() (const K2dCoords* p1, const K2dCoords* p2) const
{
const double tol = 0.25 / 1E3;
return sqrt((p1->getX() - p2->getX()) * (p1->getX() - p2->getX
()) + (p1->getY() - p2->getY()) * (p1->getY() - p2->getY())) > tol;
}
};
The problem is that after I've inserted some point in my set, I cannot
find them:
// some initialisation data
const double inc = 0.25;
const double deckLength = 1.0;
const double deckWidth = 0.48;
double x = 0;
double y = 0;
const double PI = 3.1415926;
std::set<K2dCoords*, eq> stlMap;
// create some points and add them to my set
while (x <= deckLength)
{
while (y <= deckWidth)
{
const double xSp = x * cos(30.0 * PI / 180.0) - y * sin
(30.0 * PI / 180.0);
const double ySp = x * sin(30.0 * PI / 180.0) + y * cos
(30.0 * PI / 180.0);
stlMap.insert(new K2dCoords(xSp, ySp));
y += inc;
}
x += inc;
y = 0;
}
// check if I've got them; the test point are created in the same
way as they were created for insertion
x = 0;
y = 0;
while (x <= deckLength)
{
while (y <= deckWidth)
{
const double xSp = x * cos(30.0 * PI / 180.0) - y * sin
(30.0 * PI / 180.0);
const double ySp = x * sin(30.0 * PI / 180.0) + y * cos
(30.0 * PI / 180.0);
std::set<K2dCoords*, eq>::iterator it = stlMap.find(new
K2dCoords(xSp, ySp));
if (it == stlMap.end())
{
ASSERT(FALSE); // suerly this should not assert, but
it does (when x=0.25 and y=0)!!!
}
y += inc;
}
x += inc;
y = 0;
}
What am I doing wrong? Thanks.