MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
bvh.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  David Schroeder, 2010-ish, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  Dan Keefe, 2018, University of Minnesota
12  ...
13  */
14 
15 #ifndef SRC_BVH_H_
16 #define SRC_BVH_H_
17 
18 #include "aabb.h"
19 #include "point3.h"
20 
21 
22 namespace mingfx {
23 
24 // forward declarations
25 class Mesh;
26 class Ray;
27 
28 
40 class BVH {
41 public:
43  BVH();
44 
45  virtual ~BVH();
46 
57  void CreateFromMesh(const Mesh &mesh);
58 
59 
63  void CreateFromListOfBoxes(const std::vector<AABB> &boxes);
64 
65 
73  std::vector<int> IntersectAndReturnUserData(const Ray &r) const;
74 
75 
76 private:
77 
78  // Simple internal data structure for storing each node of the BVH tree.
79  class Node {
80  public:
81  Node() : child1(NULL), child2(NULL) {}
82 
83  // Links to children
84  Node *child1;
85  Node *child2;
86 
87  // Contains all geometry below this node.
88  AABB box;
89  };
90 
91 
92  // for now, the copy constructor is private so no copies are allowed.
93  // eventually, this would be good to implement and then it can be made public.
94  BVH(const BVH &other);
95 
96  void BuildHierarchyRecursive(Node *node, std::vector<AABB> boxes);
97  void IntersectRecursive(const Ray &r, Node *node, std::vector<int> *data_list) const;
98  void FreeNodeRecursive(Node* node);
99 
100  Node* root_;
101 };
102 
103 
104 } // end namespace
105 
106 #endif
A 3D axis-aligned bounding box defined by two corners (min and max).
Definition: aabb.h:31
A Bounding Volume Hierarchy (BVH) data structure that can be used to accelerate ray-object intersecti...
Definition: bvh.h:40
void CreateFromMesh(const Mesh &mesh)
Creates a bounding volume hierarchy where each leaf node contains a single triangle from the mesh.
void CreateFromListOfBoxes(const std::vector< AABB > &boxes)
Creates a BVH where each leaf node contains one of the boxes passed in to the function.
BVH()
Initializes the class with an empty hierarchy.
virtual ~BVH()
std::vector< int > IntersectAndReturnUserData(const Ray &r) const
Traverse the BVH to find leaf nodes whose AABBs are intersected by the ray.
A triangle mesh data structure that can be rendered with a ShaderProgram like DefaultShader.
Definition: mesh.h:127
Stores the mathematical object of a ray that begins at an origin (a 3D point) and points in a directi...
Definition: ray.h:54
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21