source: GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.h @ 2608

Revision 2608, 7.0 KB checked in by bittner, 16 years ago (diff)

HR updates

Line 
1// ===================================================================
2// $Id: $
3//
4// ktball.h
5//      CKTB accelerating data structure for ray-shooting
6//
7// Class: CKTB
8//
9// REPLACEMENT_STRING
10//
11// Initial coding by Vlasta Havran
12
13#ifndef __KTBALL_H__
14#define __KTBALL_H__
15
16// GOLEM headers
17#include "configh.h"
18#include "ktbconf.h"
19//#include "ktbi.h"
20#include "ktbai.h"
21#include "ktbtrav.h"
22#include "gzstream.h"
23
24namespace GtpVisibilityPreprocessor {
25
26// forward declarations
27class CKTBAllocMan;
28class RayPacket2x2;
29
30// ----------------------------------------------------------
31// ----------------------------------------------------------
32// ----------------------------------------------------------
33// the CASDS describing CKTB itself, the interface object to
34// the other objects for CKTB tree building up and traversing
35class CKTB
36//  : public CASDS_BB
37{
38protected:
39  // True if the data structure is built up
40  bool builtUp;
41 
42  // the box bounding the whole scene
43  AxisAlignedBox3 bbox;
44 
45  // the number of objects in the case the object list is
46  // not copied to this->objlist
47  int countObjects;
48
49  // precomputed cost for intersection of kd-tree with arbitrary ray
50  float _expectedIntersectionCost;
51 
52  // the class which serves as builder of CKTB tree
53  CKTBAllocManPredecessor *buildClass;
54
55  // the class that provides traversing through CKTB tree
56  CKTBNodeIteratorPredecessor *traversalClass;
57
58  // if we use min boxes enhancement
59  bool makeMinBoxes;
60  // if we want to make boxing with only tight boxes
61  bool makeTightMinBoxes;
62
63  // the size of 2D buffer
64  int size2D;
65  int index2D;
66  // the indices in the buffer
67  int indexRead, indexWrite;
68  // the distance between two rays, where a new buffer of box nodes is written
69  int distSLCTS;
70
71  // Whether we use SLCTS boxes for FindNearest
72  bool useSLCTS;
73 
74  // construct traversal class for CKTB Trees
75  void AllocTraversalClass();
76
77  // provide the parameters for construction of CKTB tree to stream
78  void ProvidePars(ostream &app);
79
80  // builds up CKTB tree without unbounded objects. The 'boxToInclude
81  // is a box that can be optionally given and the kd-tree constructed
82  // must contain such a box. The 'boxToBoundObjects' when given, is
83  // a box to which the objects boxes are bounded. This is used for special
84  // cases such a multilevel kd-trees.
85  void BuildingUp(ObjectContainer& objlist,
86                  const AxisAlignedBox3 *boxToInclude = 0,
87                  const AxisAlignedBox3 *boxToBoundObjects = 0,
88                  bool verbose = true);
89
90  // create the array of pointers to objects addressable by object uniqueID
91  void CreatePointerArray(ObjectContainer &arrobjlist,
92                          int objCounterOffset);
93
94  // checks the boxes of all objects if they are not flat somehow
95  bool CheckBoxes(ObjectContainer& objlist); 
96
97protected:
98  // statistics
99  int numElemCells;
100  int numEmptyElemCells;
101  float emptyVolume;
102  float nonEmptyVolume;
103  int numSolidsInElemCells;
104  int actMaxListLen;
105  int numHierCells;
106  int actMaxDepth;
107  float sumSurfaceAreaLeaves;
108  float sumSurfaceAreaMULcntLeaves;
109  float sumSurfaceAreaInteriorNodes;
110 
111public:
112  // default constructor
113  CKTB();
114
115  // default destructor
116  ~CKTB();
117
118  void BuildUp(const ObjectContainer &objlist);
119
120  // functions that allows correctly traverse recursively allowing to have
121  // the same traversal stack
122  void TraversalReset();
123
124  void StatsReset() {
125    if (traversalClass)
126      traversalClass->DynamicStatsReset();
127  }
128 
129  bool HandleUnboundeds() const { return true;}
130 
131  void GetBBox(AxisAlignedBox3 &box) { box = bbox;}
132   
133  void PrintStats();
134
135  // Delete all the data structures
136  void Remove();
137 
138  void ProvideID(ostream &app);
139 
140  float ExpectecteRayIntersectionCost() const {
141    return _expectedIntersectionCost;
142  }
143 
144  // describe the toplogy of CKTB to a given stream
145  void DescribeTopology(const string &filename,
146                        int objCounterOffset,
147                        int format) { }
148 
149  // Not yet implemented
150  bool RestoreTopology(const string &filename,
151                       long int streamOffset,
152                       const ObjectContainer &objects,
153                       int objCounterOffset,
154                       int format) { return true; }
155 
156  // allocate new build class and return it
157  static CKTBAllocManPredecessor* AllocBuildClass();
158 
159  CKTBNodeIteratorPredecessor* GetTraversalClass() {
160    return traversalClass;
161  }
162 
163  CKTBAllocManPredecessor* GetBuildClass() {
164    return buildClass;
165  }
166  void SetBuildClass(CKTBAllocManPredecessor *build) {
167    buildClass = build;
168  }
169  void DeleteBuildClass();
170 
171  // Here are the functions required from CASDS interface
172  void GatherStats(bool itself = false);
173  void GatherPostStats();
174  void* Locate(const Vector3 &loc);
175 
176  int FindNearestI(const SimpleRay &ray);
177  int FindNearestI(const SimpleRay &ray, const Vector3 &boxMin, const Vector3 &boxMax);
178  void SetOffset(int offset) { traversalClass->SetOffset(offset); }
179  int FindNearestI_16oneDir(SimpleRayContainer &rays, int offset, int copyOffset) {
180    return traversalClass->FindNearestI_16oneDir(rays, offset, copyOffset);
181  }
182  int FindNearestI_16oneDirNoSSE(SimpleRayContainer &rays, int offset) {
183    return traversalClass->FindNearestI_16oneDirNoSSE(rays, offset);
184  }
185  int FindNearestI_16twoDir(SimpleRayContainer &rays) {
186    return traversalClass->FindNearestI_16twoDir(rays);
187  }
188 
189#ifdef __SSE__
190#ifdef _USE_HAVRAN_SSE 
191  // The same operations for packets of rays, if implemented by
192  // a particular ASDS, otherwise it is emulated by decomposition
193  // of a packet to individual rays and traced individually.
194  void FindNearestI(RayPacket2x2 &raypack);
195  void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax);
196#endif
197#else
198  void FindNearestI(RayPacket2x2 &raypack) { }
199  void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax) { }
200#endif // __SSE__
201
202  // Loading and saving
203  void ExportBinLeaf(OUT_STREAM &stream, SKTBNodeT *leaf);
204  SKTBNodeT* ImportBinLeaf(IN_STREAM &stream,
205                           SKTBNodeT **nodeToLink,
206                           const ObjectContainer &objects);
207
208  void ExportBinInterior(OUT_STREAM &stream, SKTBNodeT *interior);
209  SKTBNodeT *ImportBinInterior(IN_STREAM  &stream,
210                               SKTBNodeT **nodeToLink); 
211
212  SKTBNodeT * ImportNextNode(IN_STREAM &stream,
213                             SKTBNodeT **nodeToLink,
214                             const ObjectContainer &objects);
215
216  bool ExportBinTree(const string &filename);
217  bool ImportBinTree(const string &filename, ObjectContainer &objects);
218
219 
220protected: 
221  // The 2D array of pointers to the interior nodes with boxes.
222  SKTBNodeT ***buffer;
223  int sizeBuffer, size1D;
224  void AllocBuffer(int size2D, int size1Dv);
225
226  struct STraversalData
227  { 
228    SKTBNodeT *mNode;
229    int        dir;
230    int        depth;
231    STraversalData() {}
232
233    STraversalData(SKTBNodeT *n): mNode(n)
234    { }
235   
236    STraversalData(SKTBNodeT *n, int ndir, int ndepth):
237      mNode(n), dir(ndir), depth(ndepth) { }
238  };
239};
240
241}
242
243#endif // __KTBALL_H__
Note: See TracBrowser for help on using the repository browser.