//主要执行时间集中在Sort上面(5/6),考虑基数排序
//TODO:把线段信息和团编号从点的结构里分离出来,在GetGroup中单独建立数组,
//线段信息只在GetGroup函数中有用,结束后可以delete,节省空间。
struct Point {
//3D
int x;
int y;
int z;
//所在线段长度
int xlinelen;
int ylinelen;
int zlinelen;
//团编号
int group;
//所在线段起始位置
Point ** xline;
Point ** yline;
Point ** zline;
};
typedef deque <Point **> LineStack;
const int N = 1000000; //点个数
//生成随机点,测试用
void GenRandPoint (Point * pPoint, int n)
{
clock_t x = clock();
srand (x);
for (int i = 0; i < n; i++){
pPoint[i].x = rand()%100;
pPoint[i].y = rand()%100;
pPoint[i].z = rand()%100;
}
}
//比较两点的x值,用于sort
inline int CompareXPoint(Point * pPoint1, Point * pPoint2 )
{
return ((pPoint1->x) < (pPoint2->x));
}
inline int CompareYPoint(Point * pPoint1, Point * pPoint2 )
{
return ((pPoint1->y) < (pPoint2->y));
}
inline int CompareZPoint(Point * pPoint1, Point * pPoint2 )
{
return ((pPoint1->z) < (pPoint2->z));
}
//输出PointX中所有X轴上相邻的点组成的线段(包括单个点)
void SortX(Point ** PointX, int n)
{
sort(PointX, &(PointX[n]), CompareYPoint); //先按Y排序
Point ** begin = PointX, ** end = PointX + 1;
while (begin != &(PointX[n])){
int currenty = (*begin)->y;
while ((end != &(PointX[n])) && ((*end)->y == currenty)){
end++;
}
sort(begin, end, CompareZPoint); //Y相同的点按Z排序
begin = end;
end++;
}
begin = PointX, end = PointX + 1;
while (begin != &(PointX[n])){
int currenty = (*begin)->y;
int currentz = (*begin)->z;
while ((end != &(PointX[n])) && ((*end)->z == currentz) && ((*end)->y == currenty)){
end++;
}
sort(begin, end, CompareXPoint); //YZ都相同的点按X排序
begin = end;
end++;
}
}
void SortY(Point ** PointY, int n)
{
//memcpy (PointY, PointX, sizeof (Point *) * n);
sort(PointY, &(PointY[n]), CompareZPoint);
Point ** begin = PointY, ** end = PointY + 1;
while (begin != &(PointY[n])){
int currentz = (*begin)->z;
while ((end != &(PointY[n])) && ((*end)->z == currentz)){
end++;
}
sort(begin, end, CompareXPoint);
begin = end;
end++;
}
begin = PointY, end = PointY + 1;
while (begin != &(PointY[n])){
int currentz = (*begin)->z;
int currentx = (*begin)->x;
while ((end != &(PointY[n])) && ((*end)->x == currentx) && ((*end)->z == currentz)){
end++;
}
sort(begin, end, CompareYPoint);
begin = end;
end++;
}
}
void SortZ(Point ** PointZ, int n)
{
//memcpy (PointZ, PointY, sizeof (Point *) * n);
sort(PointZ, &(PointZ[n]), CompareXPoint);
Point ** begin = PointZ, ** end = PointZ + 1;
while (begin != &(PointZ[n])){
int currentx = (*begin)->x;
while ((end != &(PointZ[n])) && ((*end)->x == currentx)){
end++;
}
sort(begin, end, CompareYPoint);
begin = end;
end++;
}
begin = PointZ, end = PointZ + 1;
while (begin != &(PointZ[n])){
int currentx = (*begin)->x;
int currenty = (*begin)->y;
while ((end != &(PointZ[n])) && ((*end)->y == currenty) && ((*end)->x == currentx)){
end++;
}
sort(begin, end, CompareZPoint);
begin = end;
end++;
}
}
/*void SortX(Point ** PointX, int n)
{
stable_sort(PointX, &(PointX[n]), CompareXPoint);
stable_sort(PointX, &(PointX[n]), CompareYPoint);
stable_sort(PointX, &(PointX[n]), CompareZPoint);
}