64,577
社区成员
发帖
与我相关
我的任务
分享
具体代码如下:
int main()
{
struct _ComparePt
{
bool operator()(const ZzGePoint3d& _Left, const ZzGePoint3d& _Right) const
{
if (FloatComp(_Left.x,_Right.x, 0.001) != 0)
return (_Left.x < _Right.x);
if (FloatComp(_Left.y,_Right.y, 0.001) != 0)
return (_Left.y < _Right.y);
return (FloatComp(_Left.z,_Right.z,0.001)==-1);
}
};
vector<ZzGePoint3d> ptArr;
set<ZzGePoint3d, _ComparePt> ptSet(ptArr.begin(), ptArr.end());
vector<int> indexArr;
indexArr.resize(ptArr.size());
for (int i = 0; i < ptArr.size(); ++i)
{
set<ZzGePoint3d, _ComparePt>::iterator it = ptSet.find(ptArr[i]);
//这里得到的这个it可能等于ptSet.end() 应该是_ComparePt写的有问题
indexArr[i] = std::distance(ptSet.begin(), it);
}
return 0;
}
bool operator () (ZzGePoint3d const& a, ZzGePoint3d const& b) const
{
double const e = 0.001;
if (a.x<b.x-e) { return 1; }
if (a.x>b.x+e) { return 0; }
if (a.y<b.y-e) { return 1; }
if (a.y>b.y+e) { return 0; }
if (a.z<b.z-e) { return 1; }
if (a.z>b.z+e) { return 0; }
return 0;
}
int FloatComp(double a,double b,double dPre)
{
double c = a - b;
if (c > dPre)
return 1;
if (c < -dPre)
return -1;
return 0;
}
int FloatCompBySort(double a,double b,double dPre)
{
double c = a - b;
if (c > dPre)
return 1;
if (c < -dPre)
return -1;
if (c == 0.0)
{
return 0;
}
long n1 = 0.5+a/dPre;
long n2 = 0.5+b/dPre;
if (n1>n2)
{
return 1;
}
if (n1<n2)
return -1;
}
return 0;
}