Subversion Repositories gelsvn

Rev

Rev 428 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
443 jab 1
#ifndef __GEOMETRY_BSPTREE_H__
2
#define __GEOMETRY_BSPTREE_H__
208 jrf 3
 
4
#include <vector>
5
 
6
#include "CGLA/Mat4x4f.h"
7
#include "Geometry/TriMesh.h"
8
 
9
#include "BBox.h"
10
 
290 jrf 11
namespace Geometry 
208 jrf 12
{
13
  struct BSPNode;
14
 
15
  // Initialization structure
16
  struct BSPNode 
17
  {
18
    unsigned char axis_leaf; // 00 = axis 0, 01 = axis 1, 10 = axis 2, 11 = leaf
19
    double plane;
20
    BSPNode *left, *right;
21
    int id;
22
    int count;
23
    unsigned int ref;
24
  };
25
 
26
  // Faster structure
27
  struct BSPLeaf {
28
    // bits 0..30 : offset to first son
29
    // bit 31 (sign) flag whether node is a leaf
30
    unsigned int flagAndOffset;
31
    unsigned int count;
32
  };
33
 
34
  struct BSPInner {
35
    unsigned int flagAndOffset;
36
    // bits 0..1 : splitting dimension
37
    // bits 2..30: offset bits
38
    // bit 31 (sign) flag whether node is a leaf
39
    float splitCoordinate;
40
  };
41
 
42
  union FastBSPNode
43
  {
44
    BSPLeaf leaf;
45
    BSPInner inner;
46
  };
47
 
48
  class BSPTree 
49
  {
50
    bool b_is_build;
319 awk 51
 
428 jrf 52
	  static int node_calls;
208 jrf 53
    static int tri_calls;
54
    BSPNode* root;
55
    BBox bbox;
319 awk 56
    std::vector<const Geometry::TriMesh*> trimesh;
208 jrf 57
    std::vector<CGLA::Mat4x4f> transforms;
58
 
59
    std::vector<ISectTri> isecttris;
60
    std::vector<TriAccel> triaccel;
61
    std::vector<ISectTri*> all_objects;
62
    std::vector<TriAccel*> all_triaccel;
63
    std::vector<FastBSPNode> fast_tree;
64
 
65
    unsigned int max_objects;
66
    unsigned int max_level;
67
 
428 jrf 68
  public:
208 jrf 69
    BSPTree();
70
    ~BSPTree();
71
 
319 awk 72
    void init(std::vector<const Geometry::TriMesh*>& trimesh, 
428 jrf 73
              int max_objects, int max_level);
319 awk 74
    void init(std::vector<const Geometry::TriMesh*>& _trimesh, 
428 jrf 75
              std::vector<CGLA::Mat4x4f>& _transforms, 
76
              int _max_objects, int _max_level);
319 awk 77
    void init(const Geometry::TriMesh* mesh, CGLA::Mat4x4f transform, 
428 jrf 78
              std::vector<int> &trilist, 
79
              int _max_objects, int _max_level);
208 jrf 80
 
81
    void build();
82
    bool is_build();
319 awk 83
    void clear();
208 jrf 84
 
319 awk 85
    bool intersect(Ray &ray) const;
86
 
87
  private:
428 jrf 88
    void delete_node(BSPNode *node);
319 awk 89
    void subdivide_node(BSPNode &node, BBox &bbox, 
428 jrf 90
                        unsigned int level, 
91
                        std::vector<ISectTri*>& objects, 
92
                        std::vector<TriAccel*>& tri_objects);
319 awk 93
    void init();
94
 
290 jrf 95
    bool intersect_node(Ray &ray, const BSPNode &node, 
428 jrf 96
                        double t_min, double t_max) const;
208 jrf 97
    void print(BSPNode *node, int depth);
98
    int size(BSPNode *node);
99
    int size();
100
 
349 awk 101
    void make_fast_tree(BSPNode *node);
102
    void push_fast_bsp_node(BSPNode *node, int id);
103
    void intersect_fast_node(Ray &ray, 
428 jrf 104
                             const FastBSPNode *node, 
105
                             double t_min, double t_max) const;
290 jrf 106
    bool intersect(Ray &ray, const ISectTri &isecttri, double t_max) const;
208 jrf 107
  };
108
}
109
#endif
110