Blender V2.61 - r43446

bvh_build.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_BUILD_H__
00019 #define __BVH_BUILD_H__
00020 
00021 #include <float.h>
00022 
00023 #include "bvh.h"
00024 
00025 #include "util_boundbox.h"
00026 #include "util_vector.h"
00027 
00028 CCL_NAMESPACE_BEGIN
00029 
00030 class BVHParams;
00031 class Mesh;
00032 class Object;
00033 class Progress;
00034 
00035 /* BVH Builder */
00036 
00037 class BVHBuild
00038 {
00039 public:
00040     struct Reference
00041     {
00042         int prim_index;
00043         int prim_object;
00044         BoundBox bounds;
00045 
00046         Reference()
00047         {
00048         }
00049     };
00050 
00051     struct NodeSpec
00052     {
00053         int num;
00054         BoundBox bounds;
00055 
00056         NodeSpec()
00057         {
00058             num = 0;
00059         }
00060     };
00061 
00062     BVHBuild(
00063         const vector<Object*>& objects,
00064         vector<int>& prim_index,
00065         vector<int>& prim_object,
00066         const BVHParams& params,
00067         Progress& progress);
00068     ~BVHBuild();
00069 
00070     BVHNode *run();
00071 
00072 protected:
00073     /* adding references */
00074     void add_reference_mesh(NodeSpec& root, Mesh *mesh, int i);
00075     void add_reference_object(NodeSpec& root, Object *ob, int i);
00076     void add_references(NodeSpec& root);
00077 
00078     /* building */
00079     BVHNode *build_node(const NodeSpec& spec, int level, float progress_start, float progress_end);
00080     BVHNode *create_leaf_node(const NodeSpec& spec);
00081     BVHNode *create_object_leaf_nodes(const Reference *ref, int num);
00082 
00083     void progress_update(float progress_start, float progress_end);
00084 
00085     /* object splits */
00086     struct ObjectSplit
00087     {
00088         float sah;
00089         int dim;
00090         int num_left;
00091         BoundBox left_bounds;
00092         BoundBox right_bounds;
00093 
00094         ObjectSplit()
00095         : sah(FLT_MAX), dim(0), num_left(0)
00096         {
00097         }
00098     };
00099 
00100     ObjectSplit find_object_split(const NodeSpec& spec, float nodeSAH);
00101     void do_object_split(NodeSpec& left, NodeSpec& right, const NodeSpec& spec, const ObjectSplit& split);
00102 
00103     /* spatial splits */
00104     struct SpatialSplit
00105     {
00106         float sah;
00107         int dim;
00108         float pos;
00109 
00110         SpatialSplit()
00111         : sah(FLT_MAX), dim(0), pos(0.0f)
00112         {
00113         }
00114     };
00115 
00116     struct SpatialBin
00117     {
00118         BoundBox bounds;
00119         int enter;
00120         int exit;
00121     };
00122 
00123     SpatialSplit find_spatial_split(const NodeSpec& spec, float nodeSAH);
00124     void do_spatial_split(NodeSpec& left, NodeSpec& right, const NodeSpec& spec, const SpatialSplit& split);
00125     void split_reference(Reference& left, Reference& right, const Reference& ref, int dim, float pos);
00126 
00127     /* objects and primitive references */
00128     vector<Object*> objects;
00129     vector<Reference> references;
00130 
00131     /* output primitive indexes and objects */
00132     vector<int>& prim_index;
00133     vector<int>& prim_object;
00134 
00135     /* build parameters */
00136     BVHParams params;
00137 
00138     /* progress reporting */
00139     Progress& progress;
00140     double progress_start_time;
00141     int progress_num_duplicates;
00142 
00143     /* spatial splitting */
00144     float spatial_min_overlap;
00145     vector<BoundBox> spatial_right_bounds;
00146     SpatialBin spatial_bins[3][BVHParams::NUM_SPATIAL_BINS];
00147 };
00148 
00149 CCL_NAMESPACE_END
00150 
00151 #endif /* __BVH_BUILD_H__ */
00152