Blender V2.61 - r43446

bvh_node.h

Go to the documentation of this file.
00001 /*
00002  * Adapted from code copyright 2009-2010 NVIDIA Corporation
00003  * Modifications Copyright 2011, Blender Foundation.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef __BVH_NODE_H__
00019 #define __BVH_NODE_H__
00020 
00021 #include "util_boundbox.h"
00022 #include "util_debug.h"
00023 #include "util_types.h"
00024 
00025 CCL_NAMESPACE_BEGIN
00026 
00027 enum BVH_STAT
00028 {
00029     BVH_STAT_NODE_COUNT,
00030     BVH_STAT_INNER_COUNT,
00031     BVH_STAT_LEAF_COUNT,
00032     BVH_STAT_TRIANGLE_COUNT,
00033     BVH_STAT_CHILDNODE_COUNT
00034 };
00035 
00036 class BVHParams;
00037 
00038 class BVHNode
00039 {
00040 public:
00041     BVHNode()
00042     {
00043     }
00044 
00045     virtual ~BVHNode() {}
00046     virtual bool is_leaf() const = 0;
00047     virtual int num_children() const = 0;
00048     virtual BVHNode *get_child(int i) const = 0;
00049     virtual int num_triangles() const { return 0; }
00050     virtual void print(int depth = 0) const = 0;
00051 
00052     float getArea() const { return m_bounds.area(); }
00053 
00054     BoundBox m_bounds;
00055     uint m_visibility;
00056 
00057     // Subtree functions
00058     int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const;
00059     float computeSubtreeSAHCost(const BVHParams& p, float probability = 1.0f) const;    
00060     void deleteSubtree();
00061 };
00062 
00063 class InnerNode : public BVHNode
00064 {
00065 public:
00066     InnerNode(const BoundBox& bounds, BVHNode* child0, BVHNode* child1)
00067     {
00068         m_bounds = bounds;
00069         m_visibility = child0->m_visibility|child1->m_visibility;
00070         children[0] = child0;
00071         children[1] = child1;
00072     }
00073 
00074     bool is_leaf() const { return false; }
00075     int num_children() const { return 2; }
00076     BVHNode *get_child(int i) const{ assert(i>=0 && i<2); return children[i]; }
00077     void print(int depth) const;
00078 
00079     BVHNode *children[2];
00080 };
00081 
00082 class LeafNode : public BVHNode
00083 {
00084 public:
00085     LeafNode(const BoundBox& bounds, uint visibility, int lo, int hi) 
00086     {
00087         m_bounds = bounds;
00088         m_visibility = visibility;
00089         m_lo = lo;
00090         m_hi = hi;
00091     }
00092 
00093     LeafNode(const LeafNode& s)
00094     : BVHNode()
00095     {
00096         *this = s;
00097     }
00098 
00099     bool is_leaf() const { return true; }
00100     int num_children() const { return 0; }
00101     BVHNode *get_child(int) const { return NULL; }
00102     int num_triangles() const { return m_hi - m_lo; }
00103     void print(int depth) const;
00104 
00105     int m_lo;
00106     int m_hi;
00107 };
00108 
00109 CCL_NAMESPACE_END
00110 
00111 #endif /* __BVH_NODE_H__ */
00112