Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
208 jrf 1
#ifndef __BSPTREE_H__
2
#define __BSPTREE_H__
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
 
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
 
319 awk 68
   public:
208 jrf 69
    BSPTree();
70
    ~BSPTree();
71
 
319 awk 72
    void init(std::vector<const Geometry::TriMesh*>& trimesh, 
208 jrf 73
	      int max_objects, int max_level);
319 awk 74
    void init(std::vector<const Geometry::TriMesh*>& _trimesh, 
208 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, 
208 jrf 78
	      std::vector<int> &trilist, 
79
	      int _max_objects, int _max_level);
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:
88
	void delete_node(BSPNode *node);
89
//	bool intersect2(BRender::Ray &ray, const TriAccel &acc, double t_max) const;
90
    void subdivide_node(BSPNode &node, BBox &bbox, 
91
			unsigned int level, 
92
			std::vector<ISectTri*>& objects, 
93
			std::vector<TriAccel*>& tri_objects);
94
    void init();
95
 
290 jrf 96
    bool intersect_node(Ray &ray, const BSPNode &node, 
208 jrf 97
			double t_min, double t_max) const;
98
    void print(BSPNode *node, int depth);
99
    int size(BSPNode *node);
100
    int size();
101
 
349 awk 102
    void make_fast_tree(BSPNode *node);
103
    void push_fast_bsp_node(BSPNode *node, int id);
104
    void intersect_fast_node(Ray &ray, 
208 jrf 105
				      const FastBSPNode *node, 
106
				      double t_min, double t_max) const;
290 jrf 107
    bool intersect(Ray &ray, const ISectTri &isecttri, double t_max) const;
208 jrf 108
  };
109
}
110
#endif
111