如何在右侧直角棱镜(3D矩形)上找到最近点 [英] How to find the closest point on a right rectangular prism ( 3d rectangle )

查看:214
本文介绍了如何在右侧直角棱镜(3D矩形)上找到最近点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个正确的直角棱镜,其角落标记如下。



,并且在3D空间中有一个随机点 q ,我如何找到立方体中最接近的点 q

解决方案

假设在C#中存在Vector类型的库,并将点产品定义为

  double Dot(Vector3 a,Vector3 b)=> a.X * b.X + a.Y * b.Y + a.Z * b.Z; 

和LengthSquared定义为

  double LengthSquared()=>点(这一点,这一点); 

将该点投影到超矩形的每个独立轴上,以查找
标量参数投影。然后使标量参数饱和到面部的
限制。然后对这些组件进行求和以得到答案


  public Vector3 ClosestPointTo 
(Vector3 q,Vector3原点,Vector3 v100,Vector3 v010,Vector3 v001)
{
var px = v100;
var py = v010;
var pz = v001;

var vx =(px - origin);
var vy =(py - origin);
var vz =(pz - origin);

var tx = Vector3.Dot(q - origin,vx)/ vx.LengthSquared();
var ty = Vector3.Dot(q - origin,vy)/ vy.LengthSquared();
var tz = Vector3.Dot(q - origin,vz)/ vz.LengthSquared();

tx = tx< 0? 0:tx> 1? 1:tx;
ty = ty< 0? 0:ty> 1? 1:ty;
tz = tz< 0? 0:tz> 1? 1:tz;

var p = tx * vx + ty * vy + tz * vz + origin;

return p;
}


If I have a right rectangular prism with corners labeled as below.

and I have a random point q in 3D space how do I find the closest point on the cube to q

解决方案

Assuming the presence of a library of Vector types in C# with dot product defined as

double Dot(Vector3 a, Vector3 b) => a.X * b.X + a.Y*b.Y + a.Z*b.Z;

and LengthSquared defined as

double LengthSquared ()=> Dot(this,this);

Project the point onto each independant axis of the hyper rectangle to find the scalar parameters of the projection. Then saturate the scalar parameters at the limit of the faces. Then sum the components to get the answer

public Vector3 ClosestPointTo
    (Vector3 q, Vector3 origin, Vector3 v100, Vector3 v010, Vector3 v001)
{
    var px = v100;
    var py = v010;
    var pz = v001;

    var vx = (px - origin);
    var vy = (py - origin);
    var vz = (pz - origin);

    var tx = Vector3.Dot( q - origin, vx ) / vx.LengthSquared();
    var ty = Vector3.Dot( q - origin, vy ) / vy.LengthSquared();
    var tz = Vector3.Dot( q - origin, vz ) / vz.LengthSquared();

    tx = tx < 0 ? 0 : tx > 1 ? 1 : tx;
    ty = ty < 0 ? 0 : ty > 1 ? 1 : ty;
    tz = tz < 0 ? 0 : tz > 1 ? 1 : tz;

    var p = tx * vx + ty * vy + tz * vz + origin;

    return p;
}

这篇关于如何在右侧直角棱镜(3D矩形)上找到最近点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆