Code Monkey home page Code Monkey logo

raytracing_bvh_optimize's Introduction

RayTracing_BVH_Optimize

🔑思路来源

我在学习Ray Tracing: The Next Week的时候,在Bounding Volume Hierarchies中发现作者建树时采用的策略是:

  1. randomly choose an axis
  2. sort the primitives (using std::sort)
  3. put half in each subtree

我感觉这个随机算法不太优秀,于是我就想到了之前学KD-Tree时学过的一些建树策略:轮转建树方差建树。因此我对原本的代码做了一些修改。

除此之外,我还优化掉了 std::sort 函数,因为我们只需要找到中间元素的位置(用来划分子树),而不需要对全部元素排序。其中 std::sort 的复杂度为 $O(n\log n)$。我采用了类似快排的**,用分治来进行这一步的操作,代码中对应的就是 std::nth_element() 函数,且它的复杂度为 $O(n)$

🛠️如何使用

  1. [bvh.h]bvh_node 类中增加两个成员函数 :
// Rotational tree building
shared_ptr<bvh_node> bvh_node_optimize1 (
    const std::vector<shared_ptr<hittable>>& src_objects,
    size_t start, size_t end, double time0, double time1, int dep
);
// variance tree building
shared_ptr<bvh_node> bvh_node_optimize2 (
    const std::vector<shared_ptr<hittable>>& src_objects,
    size_t start, size_t end, double time0, double time1
);

​ 其中具体的实现在文件 Rotational_tree_buildingVariance_tree_building 中。

  1. 增加 bvh_node 的无参构造函数:
bvh_node() : left(nullptr), right(nullptr) {}
  1. 修改 bvh_node 的含参构造函数:
bvh_node::bvh_node(
    const std::vector<shared_ptr<hittable>>& src_objects,
    size_t start, size_t end, double time0, double time1
) {
    auto res = bvh_node_optimize1(src_objects, start, end, time0, time1, 0);
    // auto res = bvh_node_optimize2(src_objects, start, end, time0, time1);
    left  = res->left;
    right = res->right;
    box = res->box;
}

⏲️优化效果

OS: Ubuntu 22.04.1 LTS on Windows 10 x86_64;

随机化种子:srand(1024);

hittable_list random_scene(); 为基准程序进行比较,结果如下:

建树方式 运行时间(s)
随机建树(base) 312
轮转建树 219
方差建树 209

参考:Oi-Wiki-Kd-Tree.

raytracing_bvh_optimize's People

Contributors

moyan1082 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.