您的位置:首页 > 其它

hdu 4266 三维凸包(增量法)

2012-08-25 17:40 387 查看

The Worm in the Apple

Time Limit: 50000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 7 Accepted Submission(s): 5

[align=left]Problem Description[/align]
Willy the Worm was living happily in an apple – until some vile human picked the apple, and started to eat it! Now, Willy must escape! Given a description of the apple (defined as a convex shape in 3D space), and a list of possible positions in the apple for Willy (defined as 3D points), determine the minimum distance Willy must travel to get to the surface of the apple from each point.

[align=left]Input[/align]
There will be several test cases in the input. Each test case will begin with a line with a single integer n (4≤n≤1,000), which tells the number of points describing the apple. On the next n lines will be three integers x, y and z (-10,000≤x,y,z≤10,000), where each point (x,y,z) is either on the surface of, or within, the apple. The apple is the convex hull of these points. No four points will be coplanar. Following the description of the apple, there will be a line with a single integer q (1≤q≤100,000), which is the number of queries – that is, the number of points where Willy might be inside the apple. Each of the following q lines will contain three integers x, y and z (-10,000≤x,y,z≤10,000), representing a point (x,y,z) where Willy might be. All of Willy’s points are guaranteed to be inside the apple. The input will end with a line with a single 0.

[align=left]Output[/align]
For each query, output a single floating point number, indicating the minimum distance Willy must travel to exit the apple. Output this number with exactly 4 decimal places of accuracy, using standard 5 up / 4 down rounding (e.g. 2.12344 rounds to 2.1234, 2.12345 rounds to 2.1235). Output each number on its own line, with no spaces, and do not print any blank lines between answers.

[align=left]Sample Input[/align]

6
0 0 0
100 0 0
0 100 0
0 0 100
20 20 20
30 20 10
4
1 1 1
30 30 35
7 8 9
90 2 2
0

[align=left]Sample Output[/align]

1.0000 2.8868 7.0000 2.0000
题意:求三维凸包中的点到凸包的最短距离
思路:利用增量算法求出三维凸包的每个面,再用点到面的距离,暴力找出最小的距离。
三维凸包的增量法:

初始时需要一个四面体。可以先找两个不同点P1,P2,寻找和它们不共线的第三个点P3,再找不共面的第四个点P4,。如果找不到,则调用二维凸包算法。

接下来计算剩下点的随机排列。每次加一个点,有两种情况:

情况1:新点在当前凸包内部,只需简单地忽略该点,如图1所示。

情况2:新点在当前凸包外部,需要计算并的凸包,在这种情况下,首先需要计算原凸包相对于Pr的水平面,即Pr可以看到的封闭区域,如图2所示。

View Code

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include <iostream>
using namespace std;
#define PR 1e-9
#define N 1100
struct TPoint
{
double x,y,z;
TPoint(){}
TPoint(double _x,double _y,double _z):x(_x),y(_y),z(_z){}
TPoint operator-(const TPoint p) {return TPoint(x-p.x,y-p.y,z-p.z);}
TPoint operator*(const TPoint p) {return TPoint(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);}//叉积
double operator^(const TPoint p) {return x*p.x+y*p.y+z*p.z;}//点积
};
TPoint dd;
struct fac//
{
int a,b,c;//凸包一个面上的三个点的编号
bool ok;//该面是否是最终凸包中的面
};
TPoint xmult(TPoint u,TPoint v)
{
return TPoint(u.y*v.z-v.y*u.z,u.z*v.x-u.x*v.z,u.x*v.y-u.y*v.x);
}
double dmult(TPoint u,TPoint v)
{
return u.x*v.x+u.y*v.y+u.z*v.z;
}
TPoint subt(TPoint u,TPoint v)
{
return TPoint(u.x-v.x,u.y-v.y,u.z-v.z);
}
double vlen(TPoint u)
{
return sqrt(u.x*u.x+u.y*u.y+u.z*u.z);
}
TPoint pvec(TPoint a,TPoint b,TPoint c)
{
return xmult(subt(a,b),subt(b,c));
}
double Dis(TPoint a,TPoint b,TPoint c,TPoint d)
{
return fabs(dmult(pvec(a,b,c),subt(d,a)))/vlen(pvec(a,b,c));
}
struct T3dhull
{
int n;//初始点数
TPoint ply
;//初始点
int trianglecnt;//凸包上三角形数
fac tri
;//凸包三角形
int vis

;//点i到点j是属于哪个面
double dist(TPoint a){return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);}//两点长度
double area(TPoint a,TPoint b,TPoint c){return dist((b-a)*(c-a));}//三角形面积*2
double volume(TPoint a,TPoint b,TPoint c,TPoint d){return (b-a)*(c-a)^(d-a);}//四面体有向体积*6
double ptoplane(TPoint &p,fac &f)//正:点在面同向
{
TPoint m=ply[f.b]-ply[f.a],n=ply[f.c]-ply[f.a],t=p-ply[f.a];
return (m*n)^t;
}
void deal(int p,int a,int b)
{
int f=vis[a][b];//与当前面(cnt)共边(ab)的那个面
fac add;
if(tri[f].ok)
{
if((ptoplane(ply[p],tri[f]))>PR) dfs(p,f);//如果p点能看到该面f,则继续深度探索f的3条边,以便更新新的凸包面
else//否则因为p点只看到cnt面,看不到f面,则p点和a、b点组成一个三角形。
{
add.a=b,add.b=a,add.c=p,add.ok=1;
vis[p][b]=vis[a][p]=vis[b][a]=trianglecnt;
tri[trianglecnt++]=add;
}
}
}
void dfs(int p,int cnt)//维护凸包,如果点p在凸包外更新凸包
{
tri[cnt].ok=0;//当前面需要删除,因为它在更大的凸包里面
//下面把边反过来(先b,后a),以便在deal()中判断与当前面(cnt)共边(ab)的那个面。即判断与当头面(cnt)相邻的3个面(它们与当前面的共边是反向的,如下图中(1)的法线朝外(即逆时针)的面130和312,它们共边13,但一个方向是13,另一个方向是31)

deal(p,tri[cnt].b,tri[cnt].a);
deal(p,tri[cnt].c,tri[cnt].b);
deal(p,tri[cnt].a,tri[cnt].c);
}
bool same(int s,int e)//判断两个面是否为同一面
{
TPoint a=ply[tri[s].a],b=ply[tri[s].b],c=ply[tri[s].c];
return fabs(volume(a,b,c,ply[tri[e].a]))<PR
&&fabs(volume(a,b,c,ply[tri[e].b]))<PR
&&fabs(volume(a,b,c,ply[tri[e].c]))<PR;
}
void construct()//构建凸包
{
int i,j;
trianglecnt=0;
if(n<4) return ;
bool tmp=true;
for(i=1;i<n;i++)//前两点不共点
{
if((dist(ply[0]-ply[i]))>PR)
{
swap(ply[1],ply[i]); tmp=false; break;
}
}
if(tmp) return;
tmp=true;
for(i=2;i<n;i++)//前三点不共线
{
if((dist((ply[0]-ply[1])*(ply[1]-ply[i])))>PR)
{
swap(ply[2],ply[i]); tmp=false; break;
}
}
if(tmp) return ;
tmp=true;

for(i=3;i<n;i++)//前四点不共面
{
if(fabs((ply[0]-ply[1])*(ply[1]-ply[2])^(ply[0]-ply[i]))>PR)
{
swap(ply[3],ply[i]); tmp=false; break;
}
}
if(tmp) return ;
fac add;
for(i=0;i<4;i++)//构建初始四面体(4个点为ply[0],ply[1],ply[2],ply[3])
{
add.a=(i+1)%4,add.b=(i+2)%4,add.c=(i+3)%4,add.ok=1;
if((ptoplane(ply[i],add))>0) swap(add.b,add.c);//保证逆时针,即法向量朝外,这样新点才可看到。
vis[add.a][add.b]=vis[add.b][add.c]=vis[add.c][add.a]=trianglecnt;//逆向的有向边保存
tri[trianglecnt++]=add;
}
for(i=4;i<n;i++)//构建更新凸包
{
for(j=0;j<trianglecnt;j++)//对每个点判断是否在当前3维凸包内或外(i表示当前点,j表示当前面)
{
if(tri[j].ok&&(ptoplane(ply[i],tri[j]))>PR)//对当前凸包面进行判断,看是否点能否看到这个面
{
dfs(i,j); break;//点能看到当前面,更新凸包的面(递归,可能不止更新一个面)。当前点更新完成后break跳出循环
}
}
}
int cnt=trianglecnt;//这些面中有一些tri[i].ok=0,它们属于开始建立但后来因为在更大凸包内故需删除的,所以下面几行代码的作用是只保存最外层的凸包
trianglecnt=0;
for(i=0;i<cnt;i++)
{
if(tri[i].ok)
tri[trianglecnt++]=tri[i];
}
}
double res()
{
double _min=1e300;
for(int i=0;i<trianglecnt;i++)
{
double now=Dis(ply[tri[i].a],ply[tri[i].b],ply[tri[i].c],dd);
if(_min>now) _min=now;
}
return _min;
}
}hull;
int main()
{
double now,_min;
while(scanf("%d",&hull.n)!=EOF)
{
if(hull.n==0) break;
int i,j,q;

for(i=0;i<hull.n;i++)
scanf("%lf%lf%lf",&hull.ply[i].x,&hull.ply[i].y,&hull.ply[i].z);
hull.construct();
scanf("%d",&q);
for(j=0;j<q;j++)
{
scanf("%lf%lf%lf",&dd.x,&dd.y,&dd.z);
printf("%.4lf\n",hull.res());
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: