Code_TYMPAN  4.4.0
Industrial site acoustic simulation
KdtreeAccelerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #ifndef KDTREEACCELERATOR_H
17 #define KDTREEACCELERATOR_H
18 
19 #include "Geometry/BBox.h"
20 #include <vector>
21 #include "Geometry/Shape.h"
22 #include "Accelerator.h"
23 
28 typedef struct kdNode
29 {
30  bool isLeaf()
31  {
32  return ((int)(flag & 3)) == 3;
33  }
34  bool isNode()
35  {
36  return ((int)(flag & 3)) < 3;
37  }
38  unsigned int getNbPrimitives()
39  {
40  return nbPrims >> 2;
41  }
42  unsigned int AboveChild()
43  {
44  return secondChild >> 2;
45  }
46  unsigned int* getPrims()
47  {
48  return prims;
49  }
50  int getAxe()
51  {
52  return (flag & 3);
53  }
54  float getAxeValue()
55  {
56  return split;
57  }
58  unsigned int getFirstIndex()
59  {
60  return firstIndex;
61  }
62  void createLeaf(unsigned int _nbPrims, unsigned int _firstIndex,
63  unsigned int* _prims);
64  void createNode(int axis, float _split, unsigned int nextChild);
65 private:
66  union
67  {
68  int flag;
69  unsigned int
71  unsigned int nbPrims;
72  };
73 
74  union
75  {
76  float split;
77  unsigned int firstIndex;
78  unsigned int* prims;
79  };
81 
82 typedef struct infoPrim
83 {
84  unsigned int indexPrim;
87 
88 /* Uncommented cause not used
89 struct CompareToMid
90 {
91  CompareToMid(int d, float m) { dim = d; mid = m; }
92  int dim;
93  float mid;
94  bool operator()(InfoPrim a) const
95  {
96  return a.box.centroid[dim] < mid;
97  }
98 }; */
99 
101 {
105  TaBRecBoundEdge(float tt, int pn, bool starting)
106  {
107  t = tt;
108  primNum = pn;
109  type = starting ? START : END;
110  }
111  bool operator<(const TaBRecBoundEdge& e) const
112  {
113  if (t == e.t)
114  {
115  return (int)type < (int)e.type;
116  }
117  else
118  {
119  return t < e.t;
120  }
121  }
122  float t;
123  int primNum;
124  enum
125  {
127  END
128  } type;
129 };
131 struct KdToDo
132 {
134  float tmin, tmax;
135 };
136 
141 struct KdStack
142 {
144  float tmin;
145  float tmax;
146  unsigned int pos;
147 };
148 
154 {
155 
156 public:
158  KdtreeAccelerator(std::vector<Shape*>* _initialMesh = NULL, BBox _globalBox = BBox());
160  virtual ~KdtreeAccelerator();
161 
162  virtual bool build();
163 
164  virtual decimal traverse(Ray* r, std::list<Intersection>& result) const;
165 
167  void setMaxProfondeur(int _maxProfondeur)
168  {
169  maxProfondeur = _maxProfondeur;
170  }
173  {
174  return maxProfondeur;
175  }
176 
178  void setMaxPrimPerLeaf(int _maxPrimPerLeaf)
179  {
180  maxPrimPerLeaf = _maxPrimPerLeaf;
181  }
184  {
185  return maxPrimPerLeaf;
186  }
188  std::vector<BBox>& getBBox()
189  {
190  return tableBox;
191  }
193  void print();
194 
195  bool alreadyFail;
196  unsigned int nbFail;
197  Ray r;
198  bool trace;
199 
200 protected:
202  void generateMidKdTree(int currentProfondeur, BBox& localBox, unsigned int nbPrims, unsigned int* prims);
204  void generateSAHKdTree(int currentProfondeur, BBox& localBox, TaBRecBoundEdge* edges[3],
205  unsigned int nbPrims, unsigned int* prims);
206  std::vector<Shape*>* initialMesh;
207  // BBox globalBox;
208 
209  std::vector<InfoPrim> tablePrimitive;
210  std::vector<BBox> tableBox;
211  std::vector<KDNode> tableNode;
212 
215 
217 
218  float isectCost;
219  float emptyBonus;
221 };
222 
223 #endif
struct infoPrim InfoPrim
struct kdNode KDNode
Base class for accelerators.
Definition: Accelerator.h:27
Definition of a bounding box which is aligned along the axis (BBox AABB).
Definition: BBox.h:32
K-d tree Accelerator (based on space splitting)
int realMaxProfondeur
Real max depth.
std::vector< BBox > & getBBox()
Get the vector of bounding boxes.
std::vector< BBox > tableBox
Bounding boxes list of the tree.
void setMaxPrimPerLeaf(int _maxPrimPerLeaf)
Set maximal primitives per leaf.
int getMaxPrimPerLeaf()
Get maximal primitives per leaf.
float emptyBonus
Parameter for best splitting.
virtual decimal traverse(Ray *r, std::list< Intersection > &result) const
Run this accelerator.
void generateSAHKdTree(int currentProfondeur, BBox &localBox, TaBRecBoundEdge *edges[3], unsigned int nbPrims, unsigned int *prims)
Generate the tree with SAH (Surface Area Heuristic) method.
Ray r
Unused attribute.
int maxPrimPerLeaf
Maximal primitives per leaf.
virtual ~KdtreeAccelerator()
Destructor.
float isectCost
Parameter for best splitting.
KdtreeAccelerator(std::vector< Shape * > *_initialMesh=NULL, BBox _globalBox=BBox())
Constructor.
int getMaxProfondeur()
Get maximal depth.
void setMaxProfondeur(int _maxProfondeur)
Set maximal depth.
std::vector< Shape * > * initialMesh
Pointer to the mesh.
float traversalCost
Parameter for best splitting.
int maxProfondeur
Maximal depth.
bool alreadyFail
Unused attribute.
virtual bool build()
Build this accelerator.
void generateMidKdTree(int currentProfondeur, BBox &localBox, unsigned int nbPrims, unsigned int *prims)
Generate the tree by middle subdivision.
void print()
Print the tree (not implemented yet)
unsigned int nbFail
Unused attribute.
std::vector< InfoPrim > tablePrimitive
List of primitives and their bounding box.
bool trace
Unused attribute.
std::vector< KDNode > tableNode
: Describes a ray by a pair of unsigned int. The first one gives the source number (in the range 0-40...
Definition: Ray.h:38
float decimal
Definition: mathlib.h:45
Stack structure for the k-d tree. Optimized storage for the nodes.
float tmax
Left time from the node.
KDNode node
Next node to browser if fail happens.
float tmin
Entry time inside the node.
unsigned int pos
Position in the stack.
KdToDo struct.
KDNode * node
bool operator<(const TaBRecBoundEdge &e) const
TaBRecBoundEdge(float tt, int pn, bool starting)
Constructor.
TaBRecBoundEdge()
Default constructor.
enum TaBRecBoundEdge::@6 type
unsigned int indexPrim
Node structure (optimized to be stored on 2 bytes)
unsigned int AboveChild()
Return second child.
int flag
0 : node, x axis, 1 : node, y axis, 2 : node, z axis, 3 : leaf. 2 bits used
unsigned int * prims
Leaf : array containing primitives indexes.
int getAxe()
Return the axis number of the separator plane (node)
unsigned int nbPrims
Leaf : number of primitives. Use 30 bits integer.
unsigned int secondChild
Node : the first child is just after current node, second one is further.
void createLeaf(unsigned int _nbPrims, unsigned int _firstIndex, unsigned int *_prims)
Leaf initialization.
bool isNode()
Return true if the node is a node.
unsigned int firstIndex
Leaf : index of the first primitive in the list.
float getAxeValue()
Return the axis value of the separator plane (node)
bool isLeaf()
Return true if the node is a leaf.
void createNode(int axis, float _split, unsigned int nextChild)
Node initialization.
unsigned int getFirstIndex()
Return the index of the first node primitive (all)
float split
Node : separator axis value.
unsigned int * getPrims()
Get the primitives.
unsigned int getNbPrimitives()
Return primitives number.