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)内部的更多相关文章

  1. HDU 1756 Cupid's Arrow 计算几何 判断一个点是否在多边形内

    LINK:Cupid's Arrow 前置函数 atan2 返回一个向量的幅角.范围为[Pi,-Pi) 值得注意的是 返回的是 相对于x轴正半轴的辐角. 而判断一个点是否在一个多边形内 通常有三种方法 ...

  2. POJ-2318 TOYS 计算几何 判断点在线段的位置

    题目链接:https://cn.vjudge.net/problem/POJ-2318 题意 在一个矩形内,给出n-1条线段,把矩形分成n快四边形 问某些点在那个四边形内 思路 二分+判断点与位置关系 ...

  3. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  4. 计算几何--判断两条线段相交--poj 2653

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8862   Accepted: 3262 De ...

  5. 判断一个点是否在多边形内部,射线法思路,C#实现

    感谢原作者,原理请看原作者的文章 http://www.html-js.com/article/1517 C#实现 public string rayCasting(PointF p, PointF[ ...

  6. FZU 2148 moon game (计算几何判断凸包)

    Moon Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. 判断一个点在多边形的内部C++

    /* 原理: 将测试点的Y坐标与多边形的每一个点进行比较, ** 会得到测试点所在的行与多边形边的所有交点. ** 如果测试点的两边点的个数都是奇数个, ** 则该测试点在多边形内,否则在多边形外. ...

  8. HDU - 4458 计算几何判断点是否在多边形内

    思路:将飞机看成不动的,然后枚举时间看点是否在多边形内部. #include<bits/stdc++.h> #define LL long long #define fi first #d ...

  9. [算法]Python判断一个点是否在多边形内部

    题目: 代码: # -*- coding:utf-8 -*- def rayCasting(p, poly): px = p['x'] py = p['y'] flag = False i = 0 l ...

随机推荐

  1. 【Java基础之Object类(一)】Java中Object类中的所有方法(toString、equals、hashCode、clone、finalize、wait和notify等)详解(转载)

    java中的hashcode.equals和toString方法都是基类Object的方法. 首先说说toString方法,简单的总结了下API说明就是:返回该对象的字符串表示,信息应该是简明但易于读 ...

  2. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. 集成 Union Pay - 银联支付

    作者感言 前面已经把WeChat SDK的支付, AliPay SDK搞得七七八八了, 接下来就是银联支付的Union Pay SDK.最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系我, ...

  4. 聚类算法:K-means 算法(k均值算法)

    k-means算法:      第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设 ...

  5. matlab可变参数

    Varargin Nargin if nargin == 2 a1 = varargin{1}; a2 = varargin{2};

  6. C#拉姆达(=>)表达式

    前言: 之前小猪曾经分享过自己对C#委托的一点理解 其实在使用委托的过程中我们会大量的使用拉姆达(=>)表达式 介绍: "Lambda表达式"是一个匿名函数,是一种高效的类似 ...

  7. 使用URL访问网络资源

    URL(Uniform  Resource  Locator)对象代表统一资源定位器,它是指向互联网“资源”的指针.资源可以是简单的文件或目录,也可以是对更复杂的对象的引用,例如对数据库或搜索引擎的查 ...

  8. if语句使用

    package yuan; public class Yuan { public static void main(String[] args) { int a = 1; int b = 8; int ...

  9. 两天以来对plsqldev操作的记忆

    frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...

  10. bzoj 2428: [HAOI2006]均分数据

    #include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #inclu ...