#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
//OTHER
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(n) printf("%s\n", n)
//debug
//#define online_judge
#ifndef online_judge
#define debugt(a) cout << (#a) << "=" << a << " ";
#define debugI(a) debugt(a) cout << endl
#define debugII(a, b) debugt(a) debugt(b) cout << endl
#define debugIII(a, b, c) debugt(a) debugt(b) debugt(c) cout << endl
#define debugIV(a, b, c, d) debugt(a) debugt(b) debugt(c) debugt(d) cout << endl
#else
#define debugI(v)
#define debugII(a, b)
#define debugIII(a, b, c)
#define debugIV(a, b, c, d)
#endif
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int MOD = 100000007;
const int MAXN = 1000010;
const double PI = acos(-1.0);
/***********基础*************/
struct Point
{
double x, y, z;
Point(double x=0, double y=0, double z=0):x(x),y(y),z(z) { }
inline void read()
{
scanf("%lf%lf%lf", &x, &y, &z);
}
};
typedef Point Vector;
inline int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
inline bool operator== (Point a, Point b)
{
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0 && dcmp(a.z - b.z) == 0;
}
inline Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y, A.z+B.z);
}
inline Vector operator - (Point A, Point B)
{
return Vector(A.x-B.x, A.y-B.y, A.z-B.z);
}
inline Vector operator * (Vector A, double p)
{
return Vector(A.x*p, A.y*p, A.z*p);
}
inline Vector operator / (Vector A, double p)
{
return Vector(A.x/p, A.y/p, A.z/p);
}
inline double Dot(Vector A, Vector B)
{
return A.x*B.x + A.y*B.y + A.z*B.z;
}
inline double Length(Vector A)
{
return sqrt(Dot(A, A));
}
inline double Angle(Vector A, Vector B)
{
return acos(Dot(A, B) / Length(A) / Length(B));
}
inline Vector Cross(Vector A, Vector B)
{
return Vector(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.x*B.y - A.y*B.x);
}
inline double Area2(Point A, Point B, Point C)
{
return Length(Cross(B-A, C-A));
}
inline double Volume6(Point A, Point B, Point C, Point D)
{
return Dot(D-A, Cross(B-A, C-A));
}
// 四面体的重心
inline Point Centroid(Point A, Point B, Point C, Point D)
{
return (A + B + C + D) / 4.0;
}
/************点线面*************/
// 点p到平面p0-n的距离。n必须为单位向量
inline double DistanceToPlane(Point p, Point p0, Vector n)
{
return fabs(Dot(p-p0, n)); // 如果不取绝对值,得到的是有向距离
}
// 点p在平面p0-n上的投影。n必须为单位向量
inline Point GetPlaneProjection(Point p, Point p0, Vector n)
{
return p-n*Dot(p-p0, n);
}
//直线p1-p2 与平面p0-n的交点
inline Point LinePlaneIntersection(Point p1, Point p2, Point p0, Vector n)
{
Vector v = p2-p1;
double t = (Dot(n, p0-p1) / Dot(n, p2-p1));//分母为0,直线与平面平行或在平面上
return p1 + v*t; //如果是线段 判断t是否在0~1之间
}
// 点P到直线AB的距离
inline double DistanceToLine(Point P, Point A, Point B)
{
Vector v1 = B - A, v2 = P - A;
return Length(Cross(v1, v2)) / Length(v1);
}
//点到线段的距离
double DistanceToSeg(Point p, Point a, Point b)
{
if(a == b) return Length(p-a);
Vector v1 = b-a, v2 = p-a, v3 = p-b;
if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
else return Length(Cross(v1, v2)) / Length(v1);
}
//求异面直线 p1+s*u与p2+t*v的公垂线对应的s 如果平行|重合,返回false
bool LineDistance3D(Point p1, Vector u, Point p2, Vector v, double& s)
{
double b = Dot(u, u) * Dot(v, v) - Dot(u, v) * Dot(u, v);
if(dcmp(b) == 0) return false;
double a = Dot(u, v) * Dot(v, p1-p2) - Dot(v, v) * Dot(u, p1-p2);
s = a/b;
return true;
}
// p1和p2是否在线段a-b的同侧
inline bool SameSide(Point p1, Point p2, Point a, Point b)
{
return dcmp(Dot(Cross(b-a, p1-a), Cross(b-a, p2-a))) >= 0;
}
// 点P在三角形P0, P1, P2中
inline bool PointInTri(Point P, Point P0, Point P1, Point P2)
{
return SameSide(P, P0, P1, P2) && SameSide(P, P1, P0, P2) && SameSide(P, P2, P0, P1);
}
// 三角形P0P1P2是否和线段AB相交
bool TriSegIntersection(const Point& P0, const Point& P1, const Point& P2, const Point& A, const Point& B, Point& P)
{
Vector n = Cross(P1-P0, P2-P0);
if(dcmp(Dot(n, B-A)) == 0) return false; // 线段A-B和平面P0P1P2平行或共面
else // 平面A和直线P1-P2有惟一交点
{
double t = Dot(n, P0-A) / Dot(n, B-A);
if(dcmp(t) < 0 || dcmp(t-1) > 0) return false; // 不在线段AB上
P = A + (B-A)*t; // 交点
return PointInTri(P, P0, P1, P2);
}
}
//空间两三角形是否相交
bool TriTriIntersection(Point* T1, Point* T2)
{
Point P;
for(int i = 0; i < 3; i++)
{
if(TriSegIntersection(T1[0], T1[1], T1[2], T2[i], T2[(i+1)%3], P)) return true;
if(TriSegIntersection(T2[0], T2[1], T2[2], T1[i], T1[(i+1)%3], P)) return true;
}
return false;
}
//空间两直线上最近点对 返回最近距离 点对保存在ans1 ans2中
double SegSegDistance(Point a1, Point b1, Point a2, Point b2, Point& ans1, Point& ans2)
{
Vector v1 = (a1-b1), v2 = (a2-b2);
Vector N = Cross(v1, v2);
Vector ab = (a1-a2);
double ans = Dot(N, ab) / Length(N);
Point p1 = a1, p2 = a2;
Vector d1 = b1-a1, d2 = b2-a2;
double t1, t2;
t1 = Dot((Cross(p2-p1, d2)), Cross(d1, d2));
t2 = Dot((Cross(p2-p1, d1)), Cross(d1, d2));
double dd = Length((Cross(d1, d2)));
t1 /= dd*dd;
t2 /= dd*dd;
ans1 = (a1 + (b1-a1)*t1);
ans2 = (a2 + (b2-a2)*t2);
return fabs(ans);
}
// 判断P是否在三角形A, B, C中,并且到三条边的距离都至少为mindist。保证P, A, B, C共面
bool InsideWithMinDistance(const Point& P, const Point& A, const Point& B, const Point& C, double mindist)
{
if(!PointInTri(P, A, B, C)) return false;
if(DistanceToLine(P, A, B) < mindist) return false;
if(DistanceToLine(P, B, C) < mindist) return false;
if(DistanceToLine(P, C, A) < mindist) return false;
return true;
}
// 判断P是否在凸四边形ABCD(顺时针或逆时针)中,并且到四条边的距离都至少为mindist。保证P, A, B, C, D共面
bool InsideWithMinDistance(const Point& P, const Point& A, const Point& B, const Point& C, const Point& D, double mindist)
{
if(!PointInTri(P, A, B, C)) return false;
if(!PointInTri(P, C, D, A)) return false;
if(DistanceToLine(P, A, B) < mindist) return false;
if(DistanceToLine(P, B, C) < mindist) return false;
if(DistanceToLine(P, C, D) < mindist) return false;
if(DistanceToLine(P, D, A) < mindist) return false;
return true;
}
/*************凸包相关问题*******************/
//加干扰
inline double rand01()
{
return rand() / (double)RAND_MAX;
}
inline double randeps()
{
return (rand01() - 0.5) * eps;
}
inline Point add_noise(const Point& p)
{
return Point(p.x + randeps(), p.y + randeps(), p.z + randeps());
}
struct Face
{
int v[3];
Face(int a, int b, int c)
{
v[0] = a;
v[1] = b;
v[2] = c;
}
inline Vector Normal(const vector<Point>& P) const
{
return Cross(P[v[1]]-P[v[0]], P[v[2]]-P[v[0]]);
}
// f是否能看见P[i]
inline int CanSee(const vector<Point>& P, int i) const
{
return Dot(P[i]-P[v[0]], Normal(P)) > 0;
}
};
// 增量法求三维凸包
// 注意:没有考虑各种特殊情况(如四点共面)。实践中,请在调用前对输入点进行微小扰动
vector<Face> CH3D(const vector<Point>& P)
{
int n = P.size();
vector<vector<int> > vis(n);
for(int i = 0; i < n; i++) vis[i].resize(n);
vector<Face> cur;
cur.push_back(Face(0, 1, 2)); // 由于已经进行扰动,前三个点不共线
cur.push_back(Face(2, 1, 0));
for(int i = 3; i < n; i++)
{
vector<Face> next;
// 计算每条边的“左面”的可见性
for(int j = 0; j < (int)cur.size(); j++)
{
Face& f = cur[j];
int res = f.CanSee(P, i);
if(!res) next.push_back(f);
for(int k = 0; k < 3; k++) vis[f.v[k]][f.v[(k+1)%3]] = res;
}
for(int j = 0; j < (int)cur.size(); j++)
for(int k = 0; k < 3; k++)
{
int a = cur[j].v[k], b = cur[j].v[(k+1)%3];
if(vis[a][b] != vis[b][a] && vis[a][b]) // (a,b)是分界线,左边对P[i]可见
next.push_back(Face(a, b, i));
}
cur = next;
}
return cur;
}
struct ConvexPolyhedron
{
int n;
vector<Point> P, P2;
vector<Face> faces;
bool read()
{
if(scanf("%d", &n) != 1) return false;
P.resize(n);
P2.resize(n);
for(int i = 0; i < n; i++)
{
P[i].read();
P2[i] = add_noise(P[i]);
}
faces = CH3D(P2);
return true;
}
//三维凸包重心
Point centroid()
{
Point C = P[0];
double totv = 0;
Point tot(0,0,0);
for(int i = 0; i < (int)faces.size(); i++)
{
Point p1 = P[faces[i].v[0]], p2 = P[faces[i].v[1]], p3 = P[faces[i].v[2]];
double v = -Volume6(p1, p2, p3, C);
totv += v;
tot = tot + Centroid(p1, p2, p3, C)*v;
}
return tot / totv;
}
//凸包重心到表面最近距离
double mindist(Point C)
{
double ans = 1e30;
for(int i = 0; i < (int)faces.size(); i++)
{
Point p1 = P[faces[i].v[0]], p2 = P[faces[i].v[1]], p3 = P[faces[i].v[2]];
ans = min(ans, fabs(-Volume6(p1, p2, p3, C) / Area2(p1, p2, p3)));
}
return ans;
}
};
int main()
{
//freopen("input.txt", "r", stdin);
return 0;
}
三维计算几何模板整理
最新推荐文章于 2022-02-08 11:10:29 发布