计算几何----判断空间点是否在一个四面体(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 ...
随机推荐
- iOS app性能优化的那些事
iPhone上面的应用一直都是以流畅的操作体验而著称,但是由于之前开发人员把注意力更多的放在开发功能上面,比较少去考虑性能的问题,可能这其中涉及到objective-c,c++跟lua,优化起来相对 ...
- 最小生成树练习3(普里姆算法Prim)
风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...
- java中汉字自动转换成拼音
java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...
- SAP 质检使用非物料基本单位
今天也是奇葩了,物料A基本单位平方米,转化单位卷,销售和采购是按照平方米来采购,但是质检的需要按照平方米来做抽样检验.... 程序开发的: 修改该物料的工序基本单位为卷,再修改检验计划基本单位为卷 程 ...
- 一年成为Emacs高手(像神一样使用编辑器)
作者: 陈斌(redguardtoo) 版本号: 20150424 更新时间: <2015-04-24 Fri> 原创时间: <2012-01-31 Tue> 版权: 本文采用 ...
- Generic泛型
1.问题 未使用泛型时,元素的类型不安全:操作麻烦,可能需要强制转换import java.util.ArrayList;import java.util.List;import org.junit. ...
- JavaScript学习——判断数据类型总结(转)
一.JS中的数据类型 1.数值型(Number):包括整数.浮点数. 2.布尔型(Boolean) 3.字符串型(String) 4.对象(Object) 5.数组(Array) 6.空值(Null) ...
- "LC.exe" exited with code -1 错误
当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可
- C#入门篇6-7:字符串操作 看看字符串的特殊之处 值类型与引用类型的区别
//看看字符串的特殊之处值类型与引用类型的区别 public static void CompareString(string stra, string strb, int i) { #region ...
- ext afteredit
ext afteredit详解 grid.on("afteredit",afterEidt,grid); function(obj) { obj.row;;//修改过的行从0开始 ...