uva 11275 - 3D Triangles(几何)

942 篇文章 2 订阅
99 篇文章 0 订阅

题目链接:uva 11275 - 3D Triangles


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
const double eps = 1e-9;

inline int dcmp (double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }

struct Point3 {
	double x, y, z;

	Point3 (double x = 0, double y = 0, double z = 0): x(x), y(y), z(z) {}
	bool operator < (const Point3& u) const { return dcmp(x-u.x)<0 || (dcmp(x-u.x)==0 && dcmp(y-u.y)<0) || (dcmp(x-u.x)==0 && dcmp(y-u.y)==0 && dcmp(z-u.z) < 0); }
	bool operator > (const Point3& u) const { return u < (*this); }
	bool operator == (const Point3& u) const { return !(u < (*this) || (*this) < u); }
	bool operator != (const Point3& u) const { return !((*this) == u); }
	Point3 operator + (const Point3& u) { return Point3(x+u.x, y+u.y, z+u.z); }
	Point3 operator - (const Point3& u) { return Point3(x-u.x, y-u.y, z-u.z); }
	Point3 operator * (const double u) { return Point3(x*u, y*u, z*u); }
	Point3 operator / (const double u) { return Point3(x/u, y/u, z/u); }

	void read () { scanf("%lf%lf%lf", &x, &y, &z); }
};

typedef Point3 Vector3;

namespace Vectorial {
	double getDot(Vector3 a, Vector3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
	double getLength(Vector3 a) { return sqrt(getDot(a, a)); }
	double getAngle(Vector3 a, Vector3 b) { return acos(getDot(a, b) / getLength(a) / getLength(b)); }
	double getDistanceToPlane (Point3 p, Point3 p0, Vector3& v) { return fabs(getDot(p-p0, v)); }
	Point3 getPlaneProjection (Point3 p, Point3 p0, Vector3 v) { return p - v * getDot(p-p0, v); }
	Vector3 getCross (Vector3 a, Vector3 b) { return Vector3(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x); }
};

namespace Linear {
	using namespace Vectorial;
	double getDistanceToLine(Point3 p, Point3 a, Point3 b) {
		Vector3 v1 = b-a, v2 = p-a;
		return getLength(getCross(v1,v2)) / getLength(v1);
	}
	double getDistanceToSegment(Point3 p, Point3 a, Point3 b) {
		if (a == b) return getLength(p-a);
		Vector3 v1 = b-a, v2 = p-a, v3 = p-b;
		if (dcmp(getDot(v1, v2)) < 0) return getLength(v2);
		else if (dcmp(getDot(v1, v3)) > 0) return getLength(v3);
		else return getLength(getCross(v1, v2)) / getLength(v1);
	}
};

namespace Triangular {
	using namespace Vectorial;
	double getArea (Point3 a, Point3 b, Point3 c) { return getLength(getCross(b-a, c-a)); }
	bool onTriangle (Point3 p, Point3 a, Point3 b, Point3 c) {
		double area1 = getArea(p, a, b);
		double area2 = getArea(p, b, c);
		double area3 = getArea(p, c, a);
		return dcmp(area1 + area2 + area3 - getArea(a, b, c)) == 0;
	}
	bool haveIntersectionTriSeg (Point3 p0, Point3 p1, Point3 p2, Point3 a, Point3 b, Point3& p) {
		Vector3 v = getCross(p1-p0, p2-p0);
		if (dcmp(getDot(v, b-a)) == 0) return false;
		else {
			double t = getDot(v, p0 - a) / getDot(v, b-a);
			if (dcmp(t) < 0 || dcmp(t-1) > 0) return false;
			p = a + (b-a) * t;
			return onTriangle(p, p0, p1, p2);
		}
	}
};


namespace Polygonal {
	using namespace Vectorial;

	struct Face {
		int v[3];
		Face (int a = 0, int b = 0, int c = 0) { v[0] = a, v[1] = b, v[2] = c;}
		Vector3 normal (Point3 *p) const { return getCross(p[v[1]] - p[v[0]], p[v[2]]-p[v[0]]); }
		int cansee (Point3 *p, int i) const {
			return getDot(p[i]-p[v[0]], normal(p)) > 0 ? 1 : 0;
		}
	};

	double getVolume (Point3 a, Point3 b, Point3 c, Point3 d) { return getDot(d-a, getCross(b-a, c-a)) / 6; }

	vector<Face> CH3D (Point3 *p, int n) {
		int vis[3][3];
		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> net;
			for (int j = 0; j < cur.size(); j++) {
				Face& f = cur[j];
				int res = f.cansee(p, i);
				if (!res) net.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 < 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])
						net.push_back(Face(a, b, i));
				}
			}
			cur = net;
		}
		return cur;
	}
};

Point3 T1[3], T2[3];
//bool haveIntersectionTriSeg (Point3 p0, Point3 p1, Point3 p2, Point3 a, Point3 b, Point3& p);

using namespace Triangular;

bool judge(Point3* t1, Point3* t2) {
	Point3 p;
	for (int i = 0; i < 3; i++) {
		if (haveIntersectionTriSeg(t1[0], t1[1], t1[2], t2[i], t2[(i+1)%3], p)) return true;
		if (haveIntersectionTriSeg(t2[0], t2[1], t2[2], t1[i], t1[(i+1)%3], p)) return true;
	}
	return false;
}

int main () {
	int cas;
	scanf("%d", &cas);

	while (cas--) {
		for (int i = 0; i < 3; i++) T1[i].read();
		for (int i = 0; i < 3; i++) T2[i].read();
		printf("%d\n", judge(T1, T2) ? 1 : 0);
	}
	return 0;
}


bool Accel::travel(Node* treeNode, const Ray3f& ray_, Intersection& its, bool shadowRay, uint32_t& hitIdx) const { Ray3f ray(ray_); //与该节点包围盒不相交 cout << "与该节点包围盒求交" << endl; if (!treeNode->bbox.rayIntersect(ray)) { cout << "与该节点包围盒不相交" << endl; return false; } cout << "与该节点包围盒相交" << endl; bool is_hit = false; //是叶子节点 if (treeNode->is_leaf) { cout << "叶子节点" << endl; cout << "叶子节点内三角形数目:" << treeNode->triangles.size() << endl; //与叶子节点内三角形求交 for (size_t i = 0; i < treeNode->triangles.size(); i++) { //与叶子节点内第i个三角形求交 int index = i; cout << "与叶子节点内第" << index << "个三角形求交" << endl; float u, v, t; if (m_mesh->rayIntersect(treeNode->triangles[index], ray, u, v, t) && t < ray.maxt) { //与叶子节点内第i个三角形相交,更新交点 cout << "与叶子节点内第" << i << "个三角形相交,更新交点" << endl; if (shadowRay) return true; ray.maxt = t; its.t = t; its.uv = Point2f(u, v); its.mesh = m_mesh; hitIdx = treeNode->triangles[index]; is_hit = true; } } cout << endl; } //不是叶子节点 else { cout << "非叶子节点" << endl; for (size_t i = 0; i < 8; i++) { //与该节点第i个子节点求交 int index = i; cout << "与该节点第" << index << "个子节点求交" << endl; is_hit = travel(treeNode->child[index], ray, its, shadowRay, hitIdx); } } return is_hit; }
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值