实现 Ray-Bounding Volume 求交与 BVH 查找

在本次编程练习中,你需要实现以下函数:

• IntersectP(const Ray& ray, const Vector3f& invDir, const std::array<int, 3="">& dirIsNeg) in the Bounds3.hpp: 这个函数的 作用是判断包围盒 BoundingBox 与光线是否相交,你需要按照课程介绍的算 法实现求交过程。

• getIntersection(BVHBuildNode* node, const Ray ray)in BVH.cpp: 建 立 BVH 之后,我们可以用它加速求交过程。该过程递归进行,你将在其中调 用你实现的 Bounds3::IntersectP.

IntersectP(const Ray& ray, const Vector3f& invDir, const std::array<int, 3="">& dirIsNeg)

inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir,
const std::array<int, 3>& dirIsNeg) const
{
// invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z),
// use this because Multiply is faster that Division
// dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),
// int(y>0),int(z>0)], use this to simplify your logic
// TODO test if ray bound intersects Vector3f t_enter_xzy = (pMin - ray.origin) * invDir;
Vector3f t_exit_xzy = (pMax - ray.origin) * invDir;
// 如果光线在某一个轴上的分量是负数,则对应轴上的面越大越先进入
if(!dirIsNeg[0]) std::swap(t_enter_xzy.x, t_exit_xzy.x);
if(!dirIsNeg[1]) std::swap(t_enter_xzy.y, t_exit_xzy.y);
if(!dirIsNeg[2]) std::swap(t_enter_xzy.z, t_exit_xzy.z); float tenter = std::max(std::max(t_enter_xzy.x, t_enter_xzy.y), t_enter_xzy.z);
float texit = std::min(std::min(t_exit_xzy.x, t_exit_xzy.y), t_exit_xzy.z); return tenter < texit && texit >= 0;
}

getIntersection(BVHBuildNode* node, const Ray ray)

Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
{
Intersection res;
// TODO Traverse the BVH to find intersection // 如果光线与改包围盒无交点,直接返回false的res
if(!node->bounds.IntersectP(ray, ray.direction_inv, std::array<int, 3>
{ray.direction.x > 0, ray.direction.y > 0, ray.direction.z > 0})){
return res;
}
// 如果当前节点为叶子节点,遍历node中的每一个物体使之于光线求交
if(node->object){
return node->object->getIntersection(ray);
} // 如果当前节点的盒子与光线相交则分别递归到左右
Intersection l = getIntersection(node->left, ray);
Intersection r = getIntersection(node->right, ray); //如果有两个交点,返回较近的那个交点
return l.distance <= r.distance ? l : r;
}

效果图

随机推荐

  1. QGroupBox自定义样式(仿DotNetBar)

    QSS如下: QGroupBox { border:1px solid gray; border-radius:5px; margin-top:3ex; font-size:10pt; color:# ...

  2. CentOS7 压缩及打包的常用命令

    gzip gzip 文件名 压缩文件 gzip -d 文件名 解压文件 gunzip 文件名 解压文件 gzip -1 #压缩级别 最高到9 默认是6级别 gzip -f # 强制覆盖同名压缩包 gz ...

  3. Linux 内核相关命令

    Shell 命令: ipcs # 查看共享内存 dmesg # 显示内核消息 sudo dmesg -c # 清空内核消息 sudo mknod /dev/rwbuf c 60 0 sudo insm ...

  4. git 修改提交作者及提交日期

    进入交互式 rebase 模式 git rebase -i <commit> 你要修改哪次提交的日期,就 rebase 到该提交的上一次提交. git 提示你新的分支要包含哪些提交,默认已 ...

  5. 记录荒废了三年的四年.net开发的第三次面试,苏州斯莱克

    现在就业的确崩了 这次面试的时间是8月28号.距离上一次面试已经过去了一个月了,距离开始找工作已经过去了2个月.没多少找工作经验的我也体会到了什么叫就业崩了. 看了一线码农的采访计划后,我也把苏州列为 ...

  6. pc 移动端 双端切换-路由判断

    该封装主要以分类形式,实现对路由的简易区分.便于项目管理. 创建好项目,勾选路由插件,会自动生成 router文件夹与index.ts . index.ts 初始内容 创建项目 自动生成的router ...

  7. Redis实战之session共享

    当线上集群时候,会出现session共享问题. 虽然Tomcat提供了session copy的功能,但是缺点比较明显: 1:当Tomcat多的时候,session需要大量同步到多台集群上,占用内网宽 ...

  8. Java多线程并发编程中并发容器第二篇之List的并发类讲解

    Java多线程并发编程中并发容器第二篇之List的并发类讲解 概述 本文我们将详细讲解list对应的并发容器以及用代码来测试ArrayList.vector以及CopyOnWriteArrayList ...

  9. PyTorch从入门到放弃之张量模块

    目录 张量的数据类型 torch.rand()函数 torch.randn()函数 torch.normal()函数 torch.linspace()函数 torch.manual_seed()函数 ...

  10. 关于 MemoryStream 的多次写入,模拟post请求,接收方 .net core web api无法读取到流数据,报 System.IO.IOException: Unexpected end of Stream, the content may have already be en read by another component.

    主要是业务不熟害死人.记录一下.真想抽自己几个嘴巴子 先说结论,知道这个结论的下面文字都是废话: 一.new MemoryStream 的时候,如果需要分次写入,只能是像下面这样子 MemoryStr ...