计算几何----判断空间点是否在一个四面体(tetrahedron)内部
DESCRIPTION:
判断空间点 P(x, y, z)是否在一个四面体的内部?
Let the tetrahedron have vertices
V1 = (x1, y1, z1)
V2 = (x2, y2, z2)
V3 = (x3, y3, z3)
V4 = (x4, y4, z4)
and your test point be
P = (x, y, z).
Then the point P is in the tetrahedron if following fivedeterminants all have the same sign.
|x1 y1 z1 1|
D0 = |x2 y2 z2 1|
|x3 y3 z3 1|
|x4 y4 z4 1| |x y z 1|
D1 = |x2 y2 z2 1|
|x3 y3 z3 1|
|x4 y4 z4 1| |x1 y1 z1 1|
D2 = |x y z 1|
|x3 y3 z3 1|
|x4 y4 z4 1| |x1 y1 z1 1|
D3 = |x2 y2 z2 1|
|x y z 1|
|x4 y4 z4 1| |x1 y1 z1 1|
D4 = |x2 y2 z2 1|
|x3 y3 z3 1|
|x y z 1|
简单地对上面的算法进行分析:
其实上述算法的核心思想是 四面体的体积 = 4个小四面体的之和(判断点 与 四面体的四个面各自组成的 小四面体)
但是注意: 一个四面体的体积可有上述的行列式计算, 但是行列式的值可能是负的,只有保证点的顺序是左手法则是才能保证是正的。
// copyright @ L.J.SHOU Dec.18, 2013
// test whether a point is in a tet
#include "include/cmatrix"
#include "pt.h"
#include <cassert>
#include <vector>
#include <iostream>
using namespace std;
typedef techsoft::matrix<double> Matrix;//class for matrix
typedef cpt<double> CPt; //class for points
enum SpaceRelation{ IN, OUT, ONSURFACE}; /*
* tell whether a point is in a tetrahedran or not
* return IN, OUT, ONSURFACE
*/
SpaceRelation TestPointInTet(vector<CPt>& tet, CPt& point)
{
assert(tet.size() == 4);
Matrix mat[5];
for(int i=0; i<5; ++i)
mat[i].resize(4,4);
double det[5]; for(int i=0; i<4; ++i)
{
mat[0](i,0) = tet[i].x;
mat[0](i,1) = tet[i].y;
mat[0](i,2) = tet[i].z;
mat[0](i,3) = 1;
} if(mat[0].det() < 0)
{
swap(tet[0].x, tet[1].x);
swap(tet[0].y, tet[1].y);
swap(tet[0].z, tet[1].z); for(int i=0; i<4; ++i)
{
mat[0](i,0) = tet[i].x;
mat[0](i,1) = tet[i].y;
mat[0](i,2) = tet[i].z;
mat[0](i,3) = 1;
}
} mat[1](0,0) = point.x;
mat[1](0,1) = point.y;
mat[1](0,2) = point.z;
mat[1](0,3) = 1;
for(int i=0; i<4; ++i)
{
if(i == 0) continue;
mat[1](i,0) = tet[i].x;
mat[1](i,1) = tet[i].y;
mat[1](i,2) = tet[i].z;
mat[1](i,3) = 1;
} mat[2](1,0) = point.x;
mat[2](1,1) = point.y;
mat[2](1,2) = point.z;
mat[2](1,3) = 1;
for(int i=0; i<4; ++i)
{
if(i == 1) continue;
mat[2](i,0) = tet[i].x;
mat[2](i,1) = tet[i].y;
mat[2](i,2) = tet[i].z;
mat[2](i,3) = 1;
} mat[3](2,0) = point.x;
mat[3](2,1) = point.y;
mat[3](2,2) = point.z;
mat[3](2,3) = 1;
for(int i=0; i<4; ++i)
{
if(i == 2) continue;
mat[3](i,0) = tet[i].x;
mat[3](i,1) = tet[i].y;
mat[3](i,2) = tet[i].z;
mat[3](i,3) = 1;
} mat[4](3,0) = point.x;
mat[4](3,1) = point.y;
mat[4](3,2) = point.z;
mat[4](3,3) = 1;
for(int i=0; i<4; ++i)
{
if(i == 3) continue;
mat[4](i,0) = tet[i].x;
mat[4](i,1) = tet[i].y;
mat[4](i,2) = tet[i].z;
mat[4](i,3) = 1;
} double volume = 0;
for(int i=0; i<5; ++i)
{
det[i] = mat[i].det();
//cout << det[i] << endl;
} for(int i=1; i<=4; ++i)
volume += fabs(det[i]); if(fabs(det[0]-volume) < 1e-15)
{
for(int i=1; i<=4; ++i)
{
if(fabs(det[i]) < 1e-15)
return ONSURFACE;
}
return IN;
}
else
return OUT;
}
计算几何----判断空间点是否在一个四面体(tetrahedron)内部的更多相关文章
- HDU 1756 Cupid's Arrow 计算几何 判断一个点是否在多边形内
LINK:Cupid's Arrow 前置函数 atan2 返回一个向量的幅角.范围为[Pi,-Pi) 值得注意的是 返回的是 相对于x轴正半轴的辐角. 而判断一个点是否在一个多边形内 通常有三种方法 ...
- POJ-2318 TOYS 计算几何 判断点在线段的位置
题目链接:https://cn.vjudge.net/problem/POJ-2318 题意 在一个矩形内,给出n-1条线段,把矩形分成n快四边形 问某些点在那个四边形内 思路 二分+判断点与位置关系 ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- 计算几何--判断两条线段相交--poj 2653
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8862 Accepted: 3262 De ...
- 判断一个点是否在多边形内部,射线法思路,C#实现
感谢原作者,原理请看原作者的文章 http://www.html-js.com/article/1517 C#实现 public string rayCasting(PointF p, PointF[ ...
- FZU 2148 moon game (计算几何判断凸包)
Moon Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- 判断一个点在多边形的内部C++
/* 原理: 将测试点的Y坐标与多边形的每一个点进行比较, ** 会得到测试点所在的行与多边形边的所有交点. ** 如果测试点的两边点的个数都是奇数个, ** 则该测试点在多边形内,否则在多边形外. ...
- HDU - 4458 计算几何判断点是否在多边形内
思路:将飞机看成不动的,然后枚举时间看点是否在多边形内部. #include<bits/stdc++.h> #define LL long long #define fi first #d ...
- [算法]Python判断一个点是否在多边形内部
题目: 代码: # -*- coding:utf-8 -*- def rayCasting(p, poly): px = p['x'] py = p['y'] flag = False i = 0 l ...
随机推荐
- [Java] Java 获取数据库所有表基本信息和表中的所有列基本信息代码
废话不多说.上代码 import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import ...
- Longest Consecutive Sequence [LeetCode]
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- hdu5876 Sparse Graph(补图最短路 bfs)
题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...
- 转 SQL Union和SQL Union All两者用法区别效率以及与order by 和 group by配合问题
SQL Union和SQL Union All两者用法区别效率以及与order by 和 group by配合问题 SQL Union和SQL Union All用法 SQL UNION 操作符 UN ...
- HTML第二部分 CSS样式表
CSS(cascading style sheets,层叠样式表),作用是美化HTML网页. /*注释*/ 注释语法 2.1 样式表的基本概念 2.1.1样式表的分类 1.内联样式表 和HTML联 ...
- Objective-C:Foundation框架-常用类-NSArray
NSArray是用来存储对象的有序列表(NSSet是没有顺序的),它是不可变的.NSArray不能存储C语言中的基本数据类型,如int\float\enum\struct等,也不能存储nil.其用法如 ...
- Akumuli时间序列数据库——列存储,LSM,MVCC
Features Column-oriented time-series database. Log-structured append-only B+tree with multiversion c ...
- MongoDB replicaSet
MongoDB 的replication机制除了最普通的Master/Slave模式之外,更强大的就是其支持自动故障转移的Replica Sets模式了.相对于其问题多多的auto-sharding机 ...
- 2016年5月19日php,mysql配置
1.php配置 1. 配置disable_functiondisable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshe ...
- drbd
1.DRBD安装 1.1.安装依赖包: [java] view plaincopy yum -y install gcc kernel-devel kernel-headers flex 下载安装dr ...