GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/Geometry/BSPTree.h
00001 #ifndef __GEOMETRY_BSPTREE_H__
00002 #define __GEOMETRY_BSPTREE_H__
00003 
00004 #include <vector>
00005 
00006 #include "CGLA/Mat4x4f.h"
00007 #include "Geometry/TriMesh.h"
00008 
00009 #include "BBox.h"
00010 
00011 namespace Geometry 
00012 {
00013   struct BSPNode;
00014 
00015   // Initialization structure
00016   struct BSPNode 
00017   {
00018     unsigned char axis_leaf; // 00 = axis 0, 01 = axis 1, 10 = axis 2, 11 = leaf
00019     double plane;
00020     BSPNode *left, *right;
00021     size_t id;
00022     int count;
00023     unsigned int ref;
00024   };
00025 
00026   // Faster structure
00027   struct BSPLeaf {
00028     // bits 0..30 : offset to first son
00029     // bit 31 (sign) flag whether node is a leaf
00030     unsigned int flagAndOffset;
00031     unsigned int count;
00032   };
00033 
00034   struct BSPInner {
00035     unsigned int flagAndOffset;
00036     // bits 0..1 : splitting dimension
00037     // bits 2..30: offset bits
00038     // bit 31 (sign) flag whether node is a leaf
00039     float splitCoordinate;
00040   };
00041 
00042   union FastBSPNode
00043   {
00044     BSPLeaf leaf;
00045     BSPInner inner;
00046   };
00047 
00048   class BSPTree 
00049   {
00050     bool b_is_build;
00051 
00052           static int node_calls;
00053     static int tri_calls;
00054     BSPNode* root;
00055     BBox bbox;
00056     std::vector<const Geometry::TriMesh*> trimesh;
00057     std::vector<CGLA::Mat4x4f> transforms;
00058     
00059     std::vector<ISectTri> isecttris;
00060     std::vector<TriAccel> triaccel;
00061     std::vector<ISectTri*> all_objects;
00062     std::vector<TriAccel*> all_triaccel;
00063     std::vector<FastBSPNode> fast_tree;
00064     
00065     unsigned int max_objects;
00066     unsigned int max_level;
00067         
00068   public:
00069     BSPTree();
00070     ~BSPTree();
00071 
00072     void init(std::vector<const Geometry::TriMesh*>& trimesh, 
00073               int max_objects, int max_level);
00074     void init(std::vector<const Geometry::TriMesh*>& _trimesh, 
00075               std::vector<CGLA::Mat4x4f>& _transforms, 
00076               int _max_objects, int _max_level);
00077     void init(const Geometry::TriMesh* mesh, CGLA::Mat4x4f transform, 
00078               std::vector<int> &trilist, 
00079               int _max_objects, int _max_level);
00080 
00081     void build();
00082     bool is_build();
00083     void clear();
00084 
00085     bool intersect(Ray &ray) const;
00086         
00087   private:
00088     void delete_node(BSPNode *node);
00089     void subdivide_node(BSPNode &node, BBox &bbox, 
00090                         unsigned int level, 
00091                         std::vector<ISectTri*>& objects, 
00092                         std::vector<TriAccel*>& tri_objects);
00093     void init();
00094 
00095     bool intersect_node(Ray &ray, const BSPNode &node, 
00096                         double t_min, double t_max) const;
00097     void print(BSPNode *node, int depth);
00098     int size(BSPNode *node);
00099     int size();
00100 
00101     void make_fast_tree(BSPNode *node);
00102     void push_fast_bsp_node(BSPNode *node, int id);
00103     void intersect_fast_node(Ray &ray, 
00104                              const FastBSPNode *node, 
00105                              double t_min, double t_max) const;
00106     bool intersect(Ray &ray, const ISectTri &isecttri, double t_max) const;
00107   };
00108 }
00109 #endif
00110 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations