Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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