The spica renderer
bvh.h
1 #ifdef _MSC_VER
2 #pragma once
3 #endif
4 
5 #ifndef _SPICA_BBVH_ACCEL_H_
6 #define _SPICA_BBVH_ACCEL_H_
7 
8 #include <memory>
9 
10 #include "core/accelerator.h"
11 #include "core/renderparams.h"
12 
13 namespace spica {
14 
16  int primIdx;
17  Point3d centroid;
18  Bounds3d bounds;
19  BVHPrimitiveInfo() {}
20  BVHPrimitiveInfo(int pid, const Bounds3d& b)
21  : primIdx(pid)
22  , centroid()
23  , bounds(b) {
24  centroid = (b.posMax() + b.posMin()) * 0.5;
25  }
26 };
27 
28 struct BVHNode {
29  Bounds3d bounds;
30  BVHNode* left;
31  BVHNode* right;
32  int splitAxis;
33  int primIdx;
34 
35  void initLeaf(const Bounds3d& b, int pid) {
36  this->bounds = b;
37  this->splitAxis = 0;
38  this->primIdx = pid;
39  }
40 
41  void initFork(const Bounds3d& b, BVHNode* l, BVHNode* r, int axis) {
42  this->bounds = b;
43  this->left = l;
44  this->right = r;
45  this->splitAxis = axis;
46  this->primIdx = -1;
47  }
48 
49  bool isLeaf() const {
50  return primIdx >= 0;
51  }
52 };
53 
57 class SPICA_EXPORTS BVHAccel : public Accelerator {
58 public:
59  explicit BVHAccel(const std::vector<std::shared_ptr<Primitive>> &prims,
60  bool useSIMD = false);
61  BVHAccel(const std::vector<std::shared_ptr<Primitive>> &prims,
62  RenderParams &params);
63  virtual ~BVHAccel();
64 
65  Bounds3d worldBound() const override;
66  void construct() override;
67  virtual bool intersect(Ray& ray, SurfaceInteraction* isect) const override;
68  virtual bool intersect(Ray& ray) const override;
69  std::vector<Triangle> triangulate() const override;
70 
71 private:
72  // Private internal classes
73  union Children;
74  struct BucketInfo;
75  struct SIMDBVHNode;
76  struct ComparePoint;
77  struct CompareToBucket;
78 
79  // Private methods
80  bool intersectBVH(Ray &ray, SurfaceInteraction *isect) const;
81  bool intersectBVH(Ray &ray) const;
82  bool intersectQBVH(Ray &ray, SurfaceInteraction *isect) const;
83  bool intersectQBVH(Ray &ray) const;
84 
85  BVHNode* constructRec(std::vector<BVHPrimitiveInfo>& buildData,
86  int start, int end);
87  void release();
88  void collapse2QBVH(BVHNode* node);
89 
90  // Private fields
91  BVHNode* root_;
92  std::vector<std::unique_ptr<BVHNode>> nodes_;
93  std::vector<SIMDBVHNode*> simdNodes_;
94  bool useSIMD_;
95 };
96 
97 SPICA_EXPORT_ACCEL_PLUGIN(BVHAccel, "Standard bounding volume hierarchy");
98 
99 } // namespace spica
100 
101 #endif // _SPICA_BBVH_ACCEL_
Definition: bvh.cc:103
Definition: bvh.h:15
Definition: interaction.h:68
Definition: bvh.cc:52
Ray class.
Definition: ray.h:24
Definition: bvh.cc:94
Interface for intersection test accelerators.
Definition: accelerator.h:23
Definition: renderparams.h:27
Binary BVH accelerator class.
Definition: bvh.h:57
Definition: bvh.cc:61
Definition: bvh.h:28